# 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() functiondisemvowel = 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
import codewars_test as test # TODO Write tests import solution # or from solution import example def refsol(): print('I am refsol') x = 3 return x @test.it('test') def _(): solution.solution() test.pass_()
- import codewars_test as test
- # TODO Write tests
- import solution # or from solution import example
- def refsol():
- print('I am refsol')
- x = 3
- return x
- @test.it('test')
- def _():
- solution.solution()
- test.pass_()
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)
from solution import Student @test.describe("Student") def test_group(): s1 = Student('niccolò', 'machiavelli', [100, 100, 100, 100]) s2 = Student('hannibal', 'lector', [100, 100, 99, 98]) s3 = Student('julius', 'caesar', [93, 89, 86, 91]) s4 = Student('jack', 'sparrow', [71, 81, 78, 75]) s5 = Student('forest', 'gump', [65, 75, 70, 69]) s6 = Student('jarjar', 'binks', [10, 3, 1, 0]) @test.it("First Name") def first_name_test(): samples = [(s1, 'niccolò'), (s2, 'hannibal'), (s3, 'julius'), (s4, 'jack'), (s5, 'forest'), (s6, 'jarjar')] for sample in samples: test.assert_equals(sample[0].first_name, sample[1]) @test.it("Last Name") def first_name_test(): samples = [(s1, 'machiavelli'), (s2, 'lector'), (s3, 'caesar'), (s4, 'sparrow'), (s5, 'gump'), (s6, 'binks')] for sample in samples: test.assert_equals(sample[0].last_name, sample[1]) @test.it("Full Name") def first_name_test(): samples = [(s1, 'niccolò machiavelli'), (s2, 'hannibal lector'), (s3, 'julius caesar'), (s4, 'jack sparrow'), (s5, 'forest gump'), (s6, 'jarjar binks')] for sample in samples: test.assert_equals(sample[0].full_name, sample[1]) @test.it("Email Address") def first_name_test(): samples = [(s1, 'niccolòm@codewars.com'), (s2, 'hanniball@codewars.com'), (s3, 'juliusc@codewars.com'), (s4, 'jacks@codewars.com'), (s5, 'forestg@codewars.com'), (s6, 'jarjarb@codewars.com')] for sample in samples: test.assert_equals(sample[0].email, sample[1]) @test.it("Student Grades") def first_name_test(): samples = [(s1, 'A'), (s2, 'A'), (s3, 'B'), (s4, 'C'), (s5, 'D'), (s6, 'F')] for sample in samples: test.assert_equals(sample[0].assess(), sample[1]) @test.it("Random Grades") def first_name_test(): number_of_grades = len(s1.grades) for i in range(10): s1.add_random_grade() test.assert_equals(number_of_grades := number_of_grades + 1, len(s1.grades))
- from solution import Student
- @test.describe("Student")
- def test_group():
- s1 = Student('niccolò', 'machiavelli', [100, 100, 100, 100])
- s2 = Student('hannibal', 'lector', [100, 100, 99, 98])
- s3 = Student('julius', 'caesar', [93, 89, 86, 91])
- s4 = Student('jack', 'sparrow', [71, 81, 78, 75])
- s5 = Student('forest', 'gump', [65, 75, 70, 69])
- s6 = Student('jarjar', 'binks', [10, 3, 1, 0])
- @test.it("First Name")
- def first_name_test():
- samples = [(s1, 'niccolò'), (s2, 'hannibal'), (s3, 'julius'), (s4, 'jack'), (s5, 'forest'), (s6, 'jarjar')]
- for sample in samples:
- test.assert_equals(sample[0].first_name, sample[1])
- @test.it("Last Name")
- def first_name_test():
- samples = [(s1, 'machiavelli'), (s2, 'lector'), (s3, 'caesar'), (s4, 'sparrow'), (s5, 'gump'), (s6, 'binks')]
- for sample in samples:
- test.assert_equals(sample[0].last_name, sample[1])
- @test.it("Full Name")
- def first_name_test():
- samples = [(s1, 'niccolò machiavelli'), (s2, 'hannibal lector'), (s3, 'julius caesar'), (s4, 'jack sparrow'), (s5, 'forest gump'), (s6, 'jarjar binks')]
- for sample in samples:
- test.assert_equals(sample[0].full_name, sample[1])
- @test.it("Email Address")
- def first_name_test():
- samples = [(s1, 'niccolòm@codewars.com'), (s2, 'hanniball@codewars.com'), (s3, 'juliusc@codewars.com'), (s4, 'jacks@codewars.com'), (s5, 'forestg@codewars.com'), (s6, 'jarjarb@codewars.com')]
- for sample in samples:
- test.assert_equals(sample[0].email, sample[1])
- @test.it("Student Grades")
- def first_name_test():
- samples = [(s1, 'A'), (s2, 'A'), (s3, 'B'), (s4, 'C'), (s5, 'D'), (s6, 'F')]
- for sample in samples:
test.assert_equals(sample[0].assess(), sample[1])- test.assert_equals(sample[0].assess(), sample[1])
- @test.it("Random Grades")
- def first_name_test():
- number_of_grades = len(s1.grades)
- for i in range(10):
- s1.add_random_grade()
- test.assert_equals(number_of_grades := number_of_grades + 1, len(s1.grades))
theletterh=lambda x:"".join("H" for _ in range(x))
"H".__mul__- theletterh=lambda x:"".join("H" for _ in range(x))
import codewars_test as test import solution # H # H @test.describe("The H") def test_group(): @test.it("HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH\nH \nHHHHH") def test_case(): test.assert_equals(theletterh(3), 'HHH') test.assert_equals(theletterh(0), '') test.assert_equals(theletterh(1), 'H') test.assert_equals(theletterh(13), 'HHHHHHHHHHHHH')
- import codewars_test as test
- import solution # H
- # H
- @test.describe("The H")
- def test_group():
- @test.it("HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH\nH \nHHHHH")
- def test_case():
- test.assert_equals(theletterh(3), 'HHH')
- test.assert_equals(theletterh(0), '')
- test.assert_equals(theletterh(1), 'H')
- test.assert_equals(theletterh(13), 'HHHHHHHHHHHHH')
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))
import codewars_test as test # TODO Write tests import solution # or from solution import example # test.assert_equals(actual, expected, [optional] message) @test.describe("Example") def test_group(): @test.it("Tests") def test_case(): test.assert_equals(remove_numbers("1234567890__A1234567890"), "__A") test.assert_equals(remove_numbers("Hello world!"), "Hello world!") test.assert_equals(remove_numbers("2 times 2 is 4"), " times is ") test.assert_equals(remove_numbers("this is the year 2021"), "this is the year ")
- import codewars_test as test
- # TODO Write tests
- import solution # or from solution import example
- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Example")
- def test_group():
- @test.it("Tests")
- def test_case():
- test.assert_equals(remove_numbers("1234567890__A1234567890"), "__A")
- test.assert_equals(remove_numbers("Hello world!"), "Hello world!")
- test.assert_equals(remove_numbers("2 times 2 is 4"), " times is ")
- test.assert_equals(remove_numbers("this is the year 2021"), "this is the year ")