Variables
Basic Language Features
Fundamentals
Conditional Statements
Control Flow
Loops
Arrays
Code
Diff
  • def is_prime(num):
        if not num&1 and num != 2:
            return False
        for i in range(3, int(num ** 0.5) + 1, 2):
            if not num % i:
                return False
        return True
    
    def get_primes(max_num):
        return [i for i in range(2, max_num) if is_prime(i)]
  • 1
    import math
    
    2
    33
    def is_prime(num):
    
    4
        if(not (num&1) and num != 2):
    
    2+
        if not num&1 and num != 2:
    
    55
            return False
    
    6
        for i in range(3, int(math.sqrt(num)) + 1, 2):
    
    7
            if (num % i) == 0:
    
    4+
        for i in range(3, int(num ** 0.5) + 1, 2):
    
    5+
            if not num % i:
    
    88
                return False
    
    99
        return True
    
    1010
    1111
    def get_primes(max_num):
    
    1212
        return [i for i in range(2, max_num) if is_prime(i)]
    
Code
Diff
  • import random
    
    def rubegoldberg():
        find = "codewars"
        total = 1 - len(find)
    
        for x, y in enumerate(find):
            if x % 2:
                total -= ord(y) - 97
            else:
                total += ord(y) - 97        
    
        for n in random.sample([i for i in range(10)], 10):
            if n:
                a = 1 / n
            else:
                return total
  • 1
    import string
    
    22
    import random
    
    33
    44
    def rubegoldberg():
    
    5
        alpha = string.ascii_lowercase
    
    6
        find, total = "codewars", 1
    
    4+
        find = "codewars"
    
    5+
        total = 1 - len(find)
    
    6+
    77
        for x, y in enumerate(find):
    
    8
            if x % 2 == 0:
    
    9
                total += alpha.index(y)
    
    8+
            if x % 2:
    
    9+
                total -= ord(y) - 97
    
    1010
            else:
    
    11
                total -= alpha.index(y)
    
    12
        total -= len(find)
    
    13
        nums = []
    
    14
        for i in range(0, 10):
    
    15
            nums.append(i)
    
    16
        random.shuffle(nums)
    
    17
        try:
    
    18
            for n in nums:
    
    11+
                total += ord(y) - 97        
    
    12+
    13+
        for n in random.sample([i for i in range(10)], 10):
    
    14+
            if n:
    
    1919
                a = 1 / n
    
    20
        except ZeroDivisionError:
    
    21
            return total
    
    16+
            else:
    
    17+
                return total
    
Code
Diff
  • decr = lambda a: sum(int(h, 16) for h in a.split(':'))
  • 1
    decr = lambda a: sum([int(h,16) for h in a.split(':')])
    
    1+
    decr = lambda a: sum(int(h, 16) for h in a.split(':'))
    
Fundamentals
Numbers
Mathematics
Algorithms
Code
Diff
  • def temperature_convert(temperature):
        value, a, b = temperature
    
        if a == b:
            return value
        
        converter = {'ck': value + 273.15,
                     'cr': (value * 1.8) + 491.67,
                     'cf': (value * 1.8) + 32,
                     'rc': (value - 491.67) * (5/9),
                     'rk': value * (5/9),
                     'rf': value - 459.67,
                     'kc': value - 273.15,
                     'kr': value * 1.8,
                     'kf': ((value - 273.15) * 1.8) + 32,
                     'fc': (value - 32) * (5/9),
                     'fk': ((value - 32) * (5/9)) + 273.15,
                     'fr': value + 459.67}
        
        return int(converter[str(a + b)])
  • 11
    def temperature_convert(temperature):
    
    2+
        value, a, b = temperature
    
    3+
    4+
        if a == b:
    
    5+
            return value
    
    22
        
    
    3
        if temperature[1] == temperature[2]:
    
    4
            return temperature[0]
    
    7+
        converter = {'ck': value + 273.15,
    
    8+
                     'cr': (value * 1.8) + 491.67,
    
    9+
                     'cf': (value * 1.8) + 32,
    
    10+
                     'rc': (value - 491.67) * (5/9),
    
    11+
                     'rk': value * (5/9),
    
    12+
                     'rf': value - 459.67,
    
    13+
                     'kc': value - 273.15,
    
    14+
                     'kr': value * 1.8,
    
    15+
                     'kf': ((value - 273.15) * 1.8) + 32,
    
    16+
                     'fc': (value - 32) * (5/9),
    
    17+
                     'fk': ((value - 32) * (5/9)) + 273.15,
    
    18+
                     'fr': value + 459.67}
    
    55
        
    
    6
        converter = {'ck': '{} + 273.15',
    
    7
                     'cr': '({} * 1.8) + 491.67',
    
    8
                     'cf': '({} * 1.8) + 32',
    
    9
                     'rc': '({} - 491.67) * (5/9)',
    
    10
                     'rk': '{} * (5/9)',
    
    11
                     'rf': '{} - 459.67',
    
    12
                     'kc': '{} - 273.15',
    
    13
                     'kr': '{} * 1.8',
    
    14
                     'kf': '(({} - 273.15) * 1.8) + 32',
    
    15
                     'fc': '({} - 32) * (5/9)',
    
    16
                     'fk': '(({} - 32) * (5/9)) + 273.15',
    
    17
                     'fr': '{} + 459.67'}
    
    18
        
    
    19
        value = temperature[0]
    
    20
        formula = converter[str(temperature[1] + temperature[2])]
    
    21
        
    
    22
        return int(eval(formula.format(value)))
    
    20+
        return int(converter[str(a + b)])
    
Variables
Basic Language Features
Fundamentals
Conditional Statements
Control Flow
Loops
Arrays
Code
Diff
  • def is_prime(num):
        for i in range(2, num):
            if (num % i) == 0:
                return False
        return True
    
    def get_primes(max_num):
        return [i for i in range(2, max_num) if is_prime(i)]
  • 11
    def is_prime(num):
    
    2
        for i in range(2,num):
    
    2+
        for i in range(2, num):
    
    33
            if (num % i) == 0:
    
    44
                return False
    
    55
        return True
    
    66
    77
    def get_primes(max_num):
    
    8
        list_primes = []
    
    9
    10
        for num1 in range(2,max_num):
    
    11
            if is_prime(num1):
    
    12
                list_primes.append(num1)
    
    13
        return list_primes
    
    8+
        return [i for i in range(2, max_num) if is_prime(i)]
    
Code
Diff
  • pin_validator = lambda pin : len(str(pin)) == 5
  • 1
    def pin_validator(pin):
    
    2
        return len(str(pin)) == 5 and str(pin).isdigit() 
    
    1+
    pin_validator = lambda pin : len(str(pin)) == 5