Earn extra honor and gain new allies!
Honor is earned for each new codewarrior who joins.
Learn more
Sorting
Algorithms
Code
Diff
  • def sort_by_salary(a):
        return tuple(n for n,s in sorted((w for w in a if w[1].isnumeric()),key=lambda _:int(_[1]),reverse=True))[:5]
  • 1
    def sort_by_salary(workers):
    
    2
        return tuple(x[0] for x in sorted([x for x in workers if x[1]!='-'], key=lambda x: int(x[1]), reverse=True)[:5])
    
    1+
    def sort_by_salary(a):
    
    2+
        return tuple(n for n,s in sorted((w for w in a if w[1].isnumeric()),key=lambda _:int(_[1]),reverse=True))[:5]
    

c'mon

Code
Diff
  • find_max=max
  • 1
    def find_max(arr):
    
    2
        arr.sort()
    
    3
        return (arr[int(len(arr)-1)])
    
    1+
    find_max=max
    

:=

Code
Diff
  • calculate_2048=lambda s:(2**(x:=s*s+1),(2**(x+1))-4)
  • 1
    def calculate_2048(size):
    
    2
        highest = 2 ** (size**2 + 1)
    
    3
        sumtotal = (2 ** (size**2 + 2)) - 4
    
    4
        return highest, sumtotal
    
    1+
    calculate_2048=lambda s:(2**(x:=s*s+1),(2**(x+1))-4)
    
Code
Diff
  • from operator import add, sub as minum, mul as multiply, truediv as divide
    from functools import reduce
    run=lambda f,*a:reduce(lambda t,n:f(t,n),a)
  • 1
    from itertools import accumulate
    
    2
    import operator
    
    3
    4
    add = operator.add
    
    5
    minum = operator.sub
    
    6
    multiply = operator.mul
    
    7
    divide = operator.truediv
    
    8
        
    
    9
    def run(func,*args):
    
    10
        return list(accumulate(list(args), func))[-1]
    
    1+
    from operator import add, sub as minum, mul as multiply, truediv as divide
    
    2+
    from functools import reduce
    
    3+
    run=lambda f,*a:reduce(lambda t,n:f(t,n),a)
    

having mixed return types is not a good practice

Code
Diff
  • def anagram_check(words):
        a, b = words.split()
        i = sorted(a) == sorted(b)
        if i:
            i += a[0] == b[0] and a[-1] == b[-1]
        return ('No Anagram', 'Anagram', 'Super Anagram')[i]
  • 11
    def anagram_check(words):
    
    2
        f, s = words.split()
    
    3
        if sorted(f) == sorted(s):
    
    4
            if f[0] == s[0] and f[-1] == s[-1]:
    
    5
                return'Super Anagram'
    
    6
            return 'Anagram'
    
    7
        return False
    
    2+
        a, b = words.split()
    
    3+
        i = sorted(a) == sorted(b)
    
    4+
        if i:
    
    5+
            i += a[0] == b[0] and a[-1] == b[-1]
    
    6+
        return ('No Anagram', 'Anagram', 'Super Anagram')[i]
    
Code
Diff
  • returnhundred=(_=!false+(!+[]+[]+![]).length)=>_*_
  • 1
    function returnhundred() {
    
    2
      return returnhundred.name.charCodeAt(9);
    
    3
    }
    
    1+
    returnhundred=(_=!false+(!+[]+[]+![]).length)=>_*_
    
Code
Diff
  • def items(a, b=None):
        b = b if b else []
        for x in a:
            if isinstance(x, str):
                b.append(x)
            else:
                try:    b = items(iter(x), b)
                except: b.append(x)
        return b
    
    unpack_list = lambda a: ' '.join(str(_) for _ in items(a))
  • 1
    def isIterable(l):
    
    2
        if isinstance(l, str):
    
    3
            return False
    
    4
        try:
    
    5
            l[0]
    
    6
            return True
    
    7
        except:
    
    8
            return False
    
    9
        
    
    10
    def unpackList(l):
    
    11
        output = []
    
    12
        stack = l
    
    13
    14
        while stack != []:
    
    15
            val = stack.pop()
    
    16
            if isIterable(val):
    
    17
                stack += list(val)
    
    1+
    def items(a, b=None):
    
    2+
        b = b if b else []
    
    3+
        for x in a:
    
    4+
            if isinstance(x, str):
    
    5+
                b.append(x)
    
    1818
            else:
    
    19
                output.append(str(val))
    
    7+
                try:    b = items(iter(x), b)
    
    8+
                except: b.append(x)
    
    9+
        return b
    
    2020
    21
        return ' '.join(output[::-1])
    
    11+
    unpack_list = lambda a: ' '.join(str(_) for _ in items(a))
    
Code
Diff
  • int returnhundred()
    {
      return stoi(std::to_string('H' & 'U' & 'N' & 'D' & 'R' & 'E' & 'D'), 0, 16);
    }
  • 11
    int returnhundred()
    
    22
    {
    
    3
      return 'h' ^ 'u' ^ 'n' ^ 'd' ^ 'r' ^ 'e' ^ 'd';
    
    3+
      return stoi(std::to_string('H' & 'U' & 'N' & 'D' & 'R' & 'E' & 'D'), 0, 16);
    
    44
    }
    
Code
Diff
  • class Kata
    {
      
      private static string LANGUAGE = "C#";
      
      public static void Main()
      {
        System.Console.WriteLine($"Hello, {LANGUAGE}!");
      }
      
    }
  • 11
    class Kata
    
    22
    {
    
    3+
      
    
    4+
      private static string LANGUAGE = "C#";
    
    5+
      
    
    33
      public static void Main()
    
    44
      {
    
    5
        string language = "C#";
    
    6
        System.Console.WriteLine($"Hello, {language}!");
    
    8+
        System.Console.WriteLine($"Hello, {LANGUAGE}!");
    
    77
      }
    
    10+
      
    
    88
    }
    
Code
Diff
  • def how_many(total, diff):
        volley = (total - diff) // 2
        basket = volley + diff
        return volley, basket
  • 1
    def how_many(sum,difference):
    
    2
        return [(sum-difference)/2,(sum+difference)/2]
    
    1+
    def how_many(total, diff):
    
    2+
        volley = (total - diff) // 2
    
    3+
        basket = volley + diff
    
    4+
        return volley, basket
    
Code
Diff
  • function returnhundred() {
      return returnhundred.name.charCodeAt(9);
    }
  • 11
    function returnhundred() {
    
    2
    return 10 ** 2;
    
    2+
      return returnhundred.name.charCodeAt(9);
    
    33
    }
    
Arrays

A gaming company has just built a slots game with 1 line.
Each line is made up of an array object displaying what landed in those slots.

In the code they pass this line into the function isWinner()
This will return an object which supplies a boolean to say if they have won and how many matches in the row there was.

The matches must start from the first entry and accumulate, a winner is only true when there is over 2 or more matches.

Code
Diff
  • function isWinner(lineInfo){
      let matches = 0, numbers = {}, others = {};
      for(let item of lineInfo){
        if(typeof(item) == 'number'){
          if(item in numbers) {
            matches += numbers[item] == 1 ? 2 : 1;
            numbers[item] += 1;
          }
          else
            numbers[item] = 1;
        }
        else{
          if(item in others) {
            matches += others[item] == 1 ? 2 : 1;
            others[item] += 1;
          }
          else
            others[item] = 1;
        }
      }
      return [matches != 0, matches];
    }
  • 11
    function isWinner(lineInfo){
    
    2
      let matchCount = 0;
    
    3
      
    
    4
      //turn it into numbers so it's easier to deal with
    
    5
      lineInfo = lineInfo.map(x => parseInt(x));
    
    6
      
    
    7
      //for each number in the array, step through the rest of the array while
    
    8
      //the numbers match.
    
    9
      for(let i = 0; i<lineInfo.length; i++) {
    
    10
        const number = lineInfo[i];
    
    11
        let currentMatchCount = 1;
    
    12
        let index = i;
    
    13
        
    
    14
        //loop through the remaining items in the array while they match the current number.
    
    15
        //If they don't match, or we reached the end of the array we can bail early.
    
    16
        while(lineInfo[index+1] !== undefined && lineInfo[index+1] === number) {
    
    17
          currentMatchCount++;
    
    18
          index++;
    
    2+
      let matches = 0, numbers = {}, others = {};
    
    3+
      for(let item of lineInfo){
    
    4+
        if(typeof(item) == 'number'){
    
    5+
          if(item in numbers) {
    
    6+
            matches += numbers[item] == 1 ? 2 : 1;
    
    7+
            numbers[item] += 1;
    
    8+
          }
    
    9+
          else
    
    10+
            numbers[item] = 1;
    
    1919
        }
    
    20
        
    
    21
        //we found more matches for this number, lineInfo[i], so update matchCount,
    
    22
        //with the new count, and then fast forward the iterator
    
    23
        //by the number of matches found (slight performance bonus)
    
    24
        if(currentMatchCount > matchCount) {
    
    25
          matchCount = currentMatchCount;
    
    26
          i = i+currentMatchCount-1;
    
    12+
        else{
    
    13+
          if(item in others) {
    
    14+
            matches += others[item] == 1 ? 2 : 1;
    
    15+
            others[item] += 1;
    
    16+
          }
    
    17+
          else
    
    18+
            others[item] = 1;
    
    2727
        }
    
    2828
      }
    
    29
      
    
    30
      return [matchCount > 1, matchCount];
    
    21+
      return [matches != 0, matches];
    
    3131
    }
    
Mathematics
Algorithms
Numbers
Code
Diff
  • def average(g)
      g.size < 1 ? nil : g.reduce(:+).to_f / g.size
    end 
  • 1
    def average(grades)
    
    2
      grades.length < 1 ? nil : grades.reduce(:+).to_f / grades.length
    
    1+
    def average(g)
    
    2+
      g.size < 1 ? nil : g.reduce(:+).to_f / g.size
    
    33
    end 
    
Lists
Data Structures
Fundamentals
Arrays
Code
Diff
  • D = {1:'yes', 2:'no'}
    l = [1, 2, 3, 4, 5]
    a = [D.get(v, 'idle') for v in l]
    print(a)
  • 1+
    D = {1:'yes', 2:'no'}
    
    11
    l = [1, 2, 3, 4, 5]
    
    2
    a = ['idle' if v not in (1, 2) else 'yneos'[v-1::2] for v in l]
    
    3+
    a = [D.get(v, 'idle') for v in l]
    
    33
    print(a)
    
Lists
Data Structures
Fundamentals
Arrays
Code
Diff
  • l = [1, 2, 3, 4, 5]
    a = ['idle' if v not in (1, 2) else 'yneos'[v-1::2] for v in l]
    print(a)
  • 11
    l = [1, 2, 3, 4, 5]
    
    2
    a = ['yes' if v == 1 else 'no' if v == 2 else 'idle' for v in l]
    
    2+
    a = ['idle' if v not in (1, 2) else 'yneos'[v-1::2] for v in l]
    
    33
    print(a)
    
Loading more items...