Ad
Code
Diff
  • # let's compare :P
    
    from time import time
    from random import choice
    
    letters = [chr(i) for i in range(ord('a'), ord('z') + 1)] + [chr(i) for i in range(ord('A'), ord('Z') + 1)]
    
    function_one = lambda s:  ''.join(filter(lambda x: x.lower() not in "aeiou", s))
    function_two = lambda x : ''.join(l * (l.lower() not in 'aeiou') for l in x)
    
    for function, name in ((function_one, 'filter '), (function_two, 'boolean')):
        
        before = time()
        
        # run 100k tests
        for _ in range(80_000):
            length = 70
            word = "".join(choice(letters) for _ in range(length))
            function(word)
        
        after = time()
        print(f'Function with {name} took {round(after - before, 5)} seconds')
        
    # not really a difference, I guess? :D
    
    disemvowel = lambda x : ''.join(l * (l.lower() not in 'aeiou') for l in x) # let this here, so that tests won't fail
    • # Avoids use of filter() function
    • disemvowel = lambda x : ''.join(l * (l.lower() not in 'aeiou') for l in x)
    • # let's compare :P
    • from time import time
    • from random import choice
    • letters = [chr(i) for i in range(ord('a'), ord('z') + 1)] + [chr(i) for i in range(ord('A'), ord('Z') + 1)]
    • function_one = lambda s: ''.join(filter(lambda x: x.lower() not in "aeiou", s))
    • function_two = lambda x : ''.join(l * (l.lower() not in 'aeiou') for l in x)
    • for function, name in ((function_one, 'filter '), (function_two, 'boolean')):
    • before = time()
    • # run 100k tests
    • for _ in range(80_000):
    • length = 70
    • word = "".join(choice(letters) for _ in range(length))
    • function(word)
    • after = time()
    • print(f'Function with {name} took {round(after - before, 5)} seconds')
    • # not really a difference, I guess? :D
    • disemvowel = lambda x : ''.join(l * (l.lower() not in 'aeiou') for l in x) # let this here, so that tests won't fail
Code
Diff
  • disemvowel=lambda s: ''.join(filter(lambda x: x.lower() not in "aeiou", s))
    • disemvowel=lambda s:''.join(i for i in s if i.lower() not in "aieou")
    • disemvowel=lambda s: ''.join(filter(lambda x: x.lower() not in "aeiou", s))
Code
Diff
  • def solution():
        __import__('__main__').refsol()
    
    • def solution():
    • import __main__
    • __main__.refsol()
    • __import__('__main__').refsol()
Code
Diff
  • from random import choice
    
    class Student:
        
        def __init__(self, first_name, last_name, grades=[]):
            self.first_name = first_name
            self.last_name = last_name
            self.grades = grades
    
        @property
        def full_name(self):
            return f'{self.first_name} {self.last_name}'
    
        @property
        def email(self):
            return f'{self.first_name}{self.last_name[0]}@codewars.com'
    
        @property
        def grade_average(self):
            return sum(self.grades) / len(self.grades)
        
        @property
        def basic_grades(self):
            return (('A', 90), ('B', 80), ('C', 70), ('D', 65))
    
        def assess(self):
            avg = self.grade_average
            for grade in self.basic_grades:
                if avg >= grade[1]:
                    return grade[0]
            return 'F'
                
        def add_random_grade(self):
            random_grade = choice(range(1, 11))
            self.grades.append(random_grade)
            
    • from random import choice
    • class Student:
    • def __init__(self, first_name, last_name, grades=[]):
    • self.first_name = first_name
    • self.last_name = last_name
    • self.grades = grades
    • @property
    • def full_name(self):
    • return f'{self.first_name} {self.last_name}'
    • @property
    • def email(self):
    • return f'{self.first_name}{self.last_name[0]}@codewars.com'
    • @property
    • def grade_average(self):
    • return sum(self.grades) / len(self.grades)
    • @property
    • def basic_grades(self):
    • return (('A', 90), ('B', 80), ('C', 70), ('D', 65))
    • def assess(self):
    • avg = self.grade_average
    • for grade in self.basic_grades:
    • if avg >= grade[1]:
    • return grade[0]
    • return 'F'
    • def add_random_grade(self):
    • random_grade = choice(range(1, 11))
    • self.grades.append(random_grade)
Code
Diff
  • interface Kumite {
      static boolean hasThree(int x) {
        return x % 10 == 3 ? true : x < 10 ? false: hasThree(x/10);
      }
    }
    • interface Kumite {
    • static boolean hasThree(int x) {
    • return (x + "").contains("3") ;
    • return x % 10 == 3 ? true : x < 10 ? false: hasThree(x/10);
    • }
    • }
Code
Diff
  • theletterh=lambda x:"".join("H" for _ in range(x))
    • "H".__mul__
    • theletterh=lambda x:"".join("H" for _ in range(x))
Code
Diff
  • find_first_sub_string=lambda text,sub:[None, index:=text.find(sub)][index>-1]
    • find_first_sub_string = lambda text, sub: next(filter(lambda x: x>-1, [text.find(sub)]), None)
    • find_first_sub_string=lambda text,sub:[None, index:=text.find(sub)][index>-1]
Code
Diff
  • def find_first_sub_string(text: str, 
                              sub: str) -> int:
        
        return [None, index:=text.find(sub)][index>-1]
    • def find_first_sub_string(text: str,
    • sub: str):
    • return index if ~(index:= text.find(sub)) else None
    • sub: str) -> int:
    • return [None, index:=text.find(sub)][index>-1]
Code
Diff
  • def convert_binary_decimal(number):
        return int(str(number), 2)
    • def convert_binary_decimal(number):
    • return sum(int(val)*2**power for power, val in enumerate(str(number)[::-1]))
    • return int(str(number), 2)
Code
Diff
  • const revstr = str => [...str].reverse().join``
    • const revstr = str => [...str].reverse().join('')
    • const revstr = str => [...str].reverse().join``
Code
Diff
  • digits = "0123456789".__contains__
    
    remove_numbers=lambda x:''.join(map(lambda x: x if not digits(x) else "", x))
    • remove_numbers=lambda string:''.join(i for i in string if not i.isnumeric())
    • digits = "0123456789".__contains__
    • remove_numbers=lambda x:''.join(map(lambda x: x if not digits(x) else "", x))
Code
Diff
  • const revstr = str => str.split('').reverse().join``
    • const revstr = str => str.split('').reverse().join('')
    • const revstr = str => str.split('').reverse().join``
Code
Diff
  • add = lambda *x: sum(x)
    • def add(a, b):
    • return a + b
    • add = lambda *x: sum(x)
Code
Diff
  • def add(a, b):
        return (a**2-b**2)//(a-b) if a!=b else a*2
    • def add(a, b):
    • return (a**2-b**2)//(a-b)
    • return (a**2-b**2)//(a-b) if a!=b else a*2
Loading more items...