Code
Diff
  • av=abs
  • 1
    function av(num) {
    
    2
      return Math.abs(num);
    
    3
    }
    
    1+
    av=abs
    

Bad practice to override exisitng module function.

Code
Diff
  • factorial_=lambda x:reduce(lambda x,y:x*y,range(1,x+1))
  • 1
    def factorial(n): 
    
    2
        if n == 0: 
    
    3
            return 1 
    
    4
        else: 
    
    5
            recurse = factorial(n-1) 
    
    6
            result = n * recurse 
    
    7
            return result
    
    8
    9
    # thats how i learned it from "think python"
    
    1+
    factorial_=lambda x:reduce(lambda x,y:x*y,range(1,x+1))
    
Code
Diff
  • digital_root=lambda n:digital_root(sum(int(i) for i in str(n)))if n>9 else n
  • 1
    def digital_root(n):
    
    2
        if n>9:
    
    3
            return digital_root(sum(int(i) for i in str(n)))
    
    4
        return n
    
    1+
    digital_root=lambda n:digital_root(sum(int(i) for i in str(n)))if n>9 else n
    
Code
Diff
  • def abbrev(s):
        return s[0] + str(len(s[1:-1])) + s[-1]
  • 1
    def abbrev(str):
    
    2
        return str[0] + str(len(str[1::-1])) + str[-1]
    
    1+
    def abbrev(s):
    
    2+
        return s[0] + str(len(s[1:-1])) + s[-1]
    
Strings
Arrays

Used a lambda function instead.

Input should not be used as a variable/parameter name as it coincides with a method name.

Code
Diff
  • special_fission=lambda s:list(filter(lambda x:x.isalpha(),list(s)))
  • 1
    def Special_Fission(input):
    
    2
        return list(filter(lambda letter: letter.isalpha(), input))
    
    1+
    special_fission=lambda s:list(filter(lambda x:x.isalpha(),list(s)))
    
Code
Diff
  • remove=lambda s:''.join([c for c in s if c.islower()])
  • 1
    CAPITAL_LETTERS = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
    
    2
                       'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z')
    
    3
    NUMBERS = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
    
    4
    SPACE = (' ')
    
    5
    6
    def remove(ಠ‿ಠ):
    
    7
        for category in (CAPITAL_LETTERS, NUMBERS, SPACE):
    
    8
            for item in category:
    
    9
                while item in ಠ‿ಠ:
    
    10
                    ಠ‿ಠ = remove(ಠ‿ಠ.replace(item, ''))
    
    11
        return ಠ‿ಠ
    
    1+
    remove=lambda s:''.join([c for c in s if c.islower()])
    
Fundamentals
Code
Diff
  • e=lambda n:n//2*2
  • 1
    e=lambda n:n-(n&1)
    
    1+
    e=lambda n:n//2*2
    
Code
Diff
  • package kata
    func Multiple3And5(n int) bool {
        return n%15==0
    }
  • 11
    package kata
    
    2
    3
    func Multiple3And5(number int) bool {
    
    4
        return number%15 == 0
    
    2+
    func Multiple3And5(n int) bool {
    
    3+
        return n%15==0
    
    55
    }
    

prod function is added as of 3.8, but it does not work on an empty list.

Code
Diff
  • from math import prod
    def prod_(l):
        return prod(l) if len(l)>0 else 0
  • 1
    from functools import reduce
    
    2
    from operator import __mul__
    
    3
    def prod(numbers):
    
    4
        return reduce(__mul__, numbers) if numbers else 0
    
    1+
    from math import prod
    
    2+
    def prod_(l):
    
    3+
        return prod(l) if len(l)>0 else 0
    
Code
Diff
  • flip_the_number=lambda n:n[::-1]
  • 1
    def flip_the_number(num):
    
    2
        return num[::-1]
    
    1+
    flip_the_number=lambda n:n[::-1]
    

I'm only using true and operands this time

Code
Diff
  • def returnhundred(s):
        return (True + True + True + True + True + True + True + True + True + True) ** (True + True)
        
  • 1
    def returnhundred(word):return 100
    
    1+
    def returnhundred(s):
    
    2+
        return (True + True + True + True + True + True + True + True + True + True) ** (True + True)
    
    22
        
    
Performance
Puzzles
Games
Fundamentals

Sum a 2D array/list, but try to do both the following:

  • play code golf
  • write performant code - push the amount of huge 1000000 * 1000000 lists multiplied further
f=lambda x:sum(sum(y)for y in x)
Algorithms
Fundamentals

6 chars saved

Code
Diff
  • validateKey=(k,s=k.split('-'))=>k.length+s[0].length==14&&!isNaN(s[0])&&!isNaN(s[1])&&!'333-444-555-666-777-888-999'.includes(s[0])&&s[1]%7==0&&!'089'.includes(k.slice(-1))
  • 1
    const validateKey=(k,s=k.split('-'))=>k.length+s[0].length==14&&!isNaN(s[0])&&!isNaN(s[1])&&!'333-444-555-666-777-888-999'.includes(s[0])&&s[1]%7==0&&!'089'.includes(k.slice(-1))
    
    1+
    validateKey=(k,s=k.split('-'))=>k.length+s[0].length==14&&!isNaN(s[0])&&!isNaN(s[1])&&!'333-444-555-666-777-888-999'.includes(s[0])&&s[1]%7==0&&!'089'.includes(k.slice(-1))
    
Mathematics
Algorithms
Numbers

Find the distance of

x**2 - ((x-1)**2)

given parameter x, without calculating the exponents itself

Challenges -

  • Create a formula that works for negatives.

  • Create a formula that works for all powers.

def power_distance(x):
    return x * 2 - 1
Performance

Changed conversion to set to fromkeys, which has sped up the code by ~2-3 seconds.

Code
Diff
  • def divisors(n):
        res=[]
        for i in range(1,int(n*0.5)+1):
            if i in res: break
            if n % i == 0: 
                res.append(i)
                res.append(int(n / i))
        
        return sorted(dict.fromkeys(res))
  • 11
    def divisors(n):
    
    22
        res=[]
    
    33
        for i in range(1,int(n*0.5)+1):
    
    44
            if i in res: break
    
    55
            if n % i == 0: 
    
    66
                res.append(i)
    
    77
                res.append(int(n / i))
    
    88
        
    
    9
        return sorted(list(set(res)))
    
    9+
        return sorted(dict.fromkeys(res))
    
Loading more items...