Earn extra honor and gain new allies!
Honor is earned for each new codewarrior who joins.
Learn more
Code
Diff
  • t = str.maketrans("ACGTacgt", "TGCATGCA")
    
    reverse_compliment = lambda dna: "".join(chr(t[ord(x)]) for x in reversed(dna))
  • 1
    reverse_compliment = lambda dna: dna[::-1].translate(str.maketrans("ACGTacgt", "TGCATGCA"))
    
    1+
    t = str.maketrans("ACGTacgt", "TGCATGCA")
    
    2+
    3+
    reverse_compliment = lambda dna: "".join(chr(t[ord(x)]) for x in reversed(dna))
    
Variables
Basic Language Features
Fundamentals
Conditional Statements
Control Flow
Loops
Arrays
Code
Diff
  • def get_primes(n):
        a = [1] * n
        a[0] = a[1] = 0
        for i in range(2, n):
            if a[i]:
                x, y = divmod(n - i**2, i)
                a[i**2:n:i] = [0] * (x + bool(y))
        return [i for i, x in enumerate(a) if x]
  • 1
    from math import sqrt
    
    2
    3
    def is_prime(num):
    
    4
        for newnum in range(2, int(sqrt(num)) + 1):
    
    5
            if num % newnum == 0:
    
    6
                return False
    
    7
        return True
    
    8
    9
    def get_primes(num):
    
    10
        return [n for n in range(2, num + 1) if is_prime(n)]
    
    11
      
    
    1+
    def get_primes(n):
    
    2+
        a = [1] * n
    
    3+
        a[0] = a[1] = 0
    
    4+
        for i in range(2, n):
    
    5+
            if a[i]:
    
    6+
                x, y = divmod(n - i**2, i)
    
    7+
                a[i**2:n:i] = [0] * (x + bool(y))
    
    8+
        return [i for i, x in enumerate(a) if x]
    
Performance
Code
Diff
  • divisors=lambda n,r=[]:r.clear()or[d-n//d and r.append(n//d)or d for d in range(1,int(n**.5+1))if n%d<1]+r[::-1]
  • 1
    divisors=lambda n,r=[]:r.clear()or[d for d in range(1,int(n**.5+1))if n%d<1 and(d-n//d and r.append(n//d)or 1)]+r[::-1]
    
    1+
    divisors=lambda n,r=[]:r.clear()or[d-n//d and r.append(n//d)or d for d in range(1,int(n**.5+1))if n%d<1]+r[::-1]
    
-- code which does not return anything
Code
Diff
  • int nbDaysInMonth(int n, bool m) {
      return n == 2 ? 28 + m : 30 + (n + (n > 7) & 1);
    }
  • 1
    int nbDaysInMonth(int month, bool leap) {
    
    2
        int days[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
    
    3
        return days[month - 1] + (month == 2 && leap);
    
    1+
    int nbDaysInMonth(int n, bool m) {
    
    2+
      return n == 2 ? 28 + m : 30 + (n + (n > 7) & 1);
    
    44
    }
    
Algorithms
Arrays
Mathematics
Numbers
Statistics
Data
Code
Diff
  • function mad(a) {
      if (!a.length) return null;
      let n = a.reduce((x, y) => x + y, 0) / a.length;
      return a.reduce((x, y) => x + Math.abs(y - n), 0) / a.length;
    }
  • 1
    function mad(array) {
    
    2
    if(array.length === 0){return null}
    
    3
    if(array.length === 1){return 0}
    
    4
    5
    let average = (array.reduce((acc, cV) => acc + cV))/array.length
    
    6
    let array2 = array.map((item,index,arr)=> Math.abs(item-average))
    
    7
    return (array2.reduce((acc, cV) => acc + cV))/array2.length;
    
    1+
    function mad(a) {
    
    2+
      if (!a.length) return null;
    
    3+
      let n = a.reduce((x, y) => x + y, 0) / a.length;
    
    4+
      return a.reduce((x, y) => x + Math.abs(y - n), 0) / a.length;
    
    88
    }
    
Code
Diff
  • #include <functional>
    #include <string>
    
    std::string calculator(int op, int x, int y) {
      static std::array<std::function<int(int, int)>, 5> ops{
        std::plus<int>(),
        std::minus<int>(),
        std::multiplies<int>(),
        std::divides<int>(),
        std::modulus<int>(),
      };
      if (op < 1 || op > 5 || (!y && (op == 4 || op == 5))) return "Invalid Input!";
      return std::to_string(ops[op-1](x, y));
    }
  • 1
    #include <iostream>
    
    2
    using namespace std;
    
    1+
    #include <functional>
    
    2+
    #include <string>
    
    33
    4
    string die() {
    
    5
      return "Invalid Input!";
    
    6
    }
    
    7
    8
    string Calculator(int choice, int x, int y) {
    
    9
        string answer;
    
    10
        if (choice == 1 || choice == 2 || choice == 3 || choice == 4 || choice == 5){
    
    11
            switch (choice) {
    
    12
            case 1:        
    
    13
                   answer = std::to_string(x + y);
    
    14
                   break;
    
    15
            case 2:        
    
    16
                   answer = std::to_string(x - y);
    
    17
                   break; 
    
    18
            case 3:        
    
    19
                   answer = std::to_string(x * y);
    
    20
                   break;
    
    21
            case 4:        
    
    22
                   answer = std::to_string(x / y);
    
    23
                   break;
    
    24
            case 5:        
    
    25
                   answer = std::to_string(x % y);
    
    26
                   break;
    
    27
            }
    
    28
        }
    
    29
        else { return die(); }
    
    30
    return answer;
    
    4+
    std::string calculator(int op, int x, int y) {
    
    5+
      static std::array<std::function<int(int, int)>, 5> ops{
    
    6+
        std::plus<int>(),
    
    7+
        std::minus<int>(),
    
    8+
        std::multiplies<int>(),
    
    9+
        std::divides<int>(),
    
    10+
        std::modulus<int>(),
    
    11+
      };
    
    12+
      if (op < 1 || op > 5 || (!y && (op == 4 || op == 5))) return "Invalid Input!";
    
    13+
      return std::to_string(ops[op-1](x, y));
    
    3131
    }
    
Code
Diff
  • def convert(s):
        return "".join(x.lower() if i & 1 else x.upper() for i, x in enumerate(filter(str.isalpha, s)))
  • 1
    import string
    
    2
    3
    def HeLlOwOrLddddd(strng):
    
    4
        strng = [x for x in strng if x in string.ascii_letters]
    
    5
        return "".join([strng[x].upper() if x % 2 == 0 else strng[x].lower() for x in range(len(strng))])
    
    1+
    def convert(s):
    
    2+
        return "".join(x.lower() if i & 1 else x.upper() for i, x in enumerate(filter(str.isalpha, s)))
    
Code
Diff
  • public class HeLlOwOrLddddd {
      public static String convert(String s) {
        StringBuilder sb = new StringBuilder();
        boolean f = false;
        for (char x : s.toCharArray())
          if (Character.isLetter(x))
            sb.append((f = !f) ? Character.toUpperCase(x) : Character.toLowerCase(x));
        return sb.toString();
      }
    }
  • 11
    public class HeLlOwOrLddddd {
    
    2
      public static String convert(String input) {
    
    3
          String salida = "";
    
    4
          boolean mayus = true;
    
    5
          for (int i=0;i<input.length();i++){
    
    6
            if (Character.isLetter(input.charAt(i))){
    
    7
                if (mayus){
    
    8
                    salida+=Character.toUpperCase(input.charAt(i));
    
    9
                    mayus=false;
    
    10
                }else{
    
    11
                    salida+=Character.toLowerCase(input.charAt(i));
    
    12
                    mayus=true;
    
    13
                }
    
    14
            }        
    
    15
        }
    
    16
          return salida;
    
    17
    2+
      public static String convert(String s) {
    
    3+
        StringBuilder sb = new StringBuilder();
    
    4+
        boolean f = false;
    
    5+
        for (char x : s.toCharArray())
    
    6+
          if (Character.isLetter(x))
    
    7+
            sb.append((f = !f) ? Character.toUpperCase(x) : Character.toLowerCase(x));
    
    8+
        return sb.toString();
    
    1818
      }
    
    1919
    }
    
Code
Diff
  • def generate_order(n):
        a, m = [*range(n)], (n + 1) // 2
        return [y for x in zip(a[:m][::-1], a[m:]) for y in x] + ([0] if n & 1 else [])
  • 1
    def generateOrder(n):
    
    2
        if n == 0:
    
    3
            return []
    
    4
            
    
    5
        r = []
    
    6
        
    
    7
        if n % 2 == 0 :
    
    8
            r = generateOrder(n-1)
    
    9
            r.append(n-1)
    
    10
        else:
    
    11
            middle = round((n / 2) - 0.01)
    
    12
            r = [middle]
    
    13
            for i in range(middle):
    
    14
                r.append(middle + i + 1)
    
    15
                r.append(middle - i - 1)
    
    16
        
    
    17
        return r
    
    1+
    def generate_order(n):
    
    2+
        a, m = [*range(n)], (n + 1) // 2
    
    3+
        return [y for x in zip(a[:m][::-1], a[m:]) for y in x] + ([0] if n & 1 else [])
    
Code
Diff
  • larger_than_5=(5).__lt__
  • 1
    larger_than_5=lambda a:a>5
    
    1+
    larger_than_5=(5).__lt__
    
Fundamentals
Code
Diff
  • from itertools import accumulate
    
    def invert(a):
        return [*accumulate((x - y for x, y in zip([a[0] * 2] + a, a)), lambda x, y: (x + y) % 12)]
  • 1
    def invert(tone_row):
    
    2
        inversion = [tone_row[0]]
    
    3
        for i in range(0, 11):
    
    4
            inversion.append((inversion[i] + tone_row[i] - tone_row[i + 1]) % 12)
    
    5
        return inversion
    
    1+
    from itertools import accumulate
    
    2+
    3+
    def invert(a):
    
    4+
        return [*accumulate((x - y for x, y in zip([a[0] * 2] + a, a)), lambda x, y: (x + y) % 12)]
    
Code
Diff
  • def calculator(a, o, b):
        return eval(f"{a}{o}{b}")
  • 1
    def calculator(a, operator, b):
    
    2
        return eval(str(a) + operator + str(b))
    
    1+
    def calculator(a, o, b):
    
    2+
        return eval(f"{a}{o}{b}")
    
Code
Diff
  • divisible_by_3 = lambda x: x == x // 3 * 3
  • 1
    divisibility_by_3 = lambda x: int(x / 3) == x / 3
    
    1+
    divisible_by_3 = lambda x: x == x // 3 * 3
    
Code
Diff
  • from math import pi
    from operator import mul
    
    circle_area, circle_circumference = [(lambda op: (lambda r: pi * op(r, 2)))(f) for f in [pow, mul]]
  • 1
    PI = 3.141592653589793
    
    2
    circle_area = lambda r: round(PI * r ** 2, 2)
    
    3
    circle_cir  = lambda r: round(PI * r  * 2, 2)
    
    1+
    from math import pi
    
    2+
    from operator import mul
    
    44
    5
    # or
    
    6
    7
    circle_area = lambda r: round(__import__('math').pi * r ** 2, 2)
    
    8
    circle_cir  = lambda r: round(__import__('math').pi * r  * 2, 2)
    
    4+
    circle_area, circle_circumference = [(lambda op: (lambda r: pi * op(r, 2)))(f) for f in [pow, mul]]
    
Loading more items...