Add some easy one

This commit is contained in:
Clement Desmidt 2019-12-18 21:06:22 +01:00
parent 95b2aa6c2f
commit 1ac80708fc
6 changed files with 127 additions and 0 deletions

View File

@ -0,0 +1,19 @@
def checkio(number: int) -> int:
number = str(number)
x = 1
for y in number:
if y != '0':
x *= int(y)
return x
if __name__ == '__main__':
print('Example:')
print(checkio(123405))
# These "asserts" using only for self-checking and not necessary for auto-testing
assert checkio(123405) == 120
assert checkio(999) == 729
assert checkio(1000) == 1
assert checkio(1111) == 1
print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")

16
Elementary/easy-unpack.py Normal file
View File

@ -0,0 +1,16 @@
def easy_unpack(elements: tuple) -> tuple:
"""
returns a tuple with 3 elements - first, third and second to the last
"""
return elements[0], elements[2], elements[-2]
if __name__ == '__main__':
print('Examples:')
print(easy_unpack((1, 2, 3, 4, 5, 6, 7, 9)))
# These "asserts" using only for self-checking and not necessary for auto-testing
assert easy_unpack((1, 2, 3, 4, 5, 6, 7, 9)) == (1, 3, 7)
assert easy_unpack((1, 1, 1, 1)) == (1, 1, 1)
assert easy_unpack((6, 3, 7)) == (6, 7, 3)
print('Done! Go Check!')

18
Elementary/index-power.py Normal file
View File

@ -0,0 +1,18 @@
def index_power(array: list, n: int) -> int:
"""
Find Nth power of the element with index N.
"""
return (array[n] ** n) if n < len(array) else -1
if __name__ == '__main__':
print('Example:')
print(index_power([1, 2, 3, 4], 2))
# These "asserts" using only for self-checking and not necessary for auto-testing
assert index_power([1, 2, 3, 4], 2) == 9, "Square"
assert index_power([1, 3, 10, 100], 3) == 1000000, "Cube"
assert index_power([0, 1], 0) == 1, "Zero power"
assert index_power([1, 2], 3) == -1, "IndexError"
assert index_power([1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 9) == 1, "IndexError"
print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")

View File

@ -0,0 +1,12 @@
def mult_two(a, b):
return a * b
if __name__ == '__main__':
print("Example:")
print(mult_two(3, 2))
# These "asserts" are used for self-checking and not for an auto-testing
assert mult_two(3, 2) == 6
assert mult_two(1, 0) == 0
print("Coding complete? Click 'Check' to earn cool rewards!")

45
Home/the-warriors.py Normal file
View File

@ -0,0 +1,45 @@
class Warrior:
def __init__(self):
self.health = 50
self.attack = 5
self.is_alive = True
def hit(self, other_unit) -> bool:
other_unit.health -= self.attack
other_unit.is_alive = other_unit.health > 0
return other_unit.is_alive
class Knight(Warrior):
def __init__(self):
super().__init__()
self.attack = 7
def fight(unit_1, unit_2):
while unit_1.is_alive and unit_2.is_alive:
if not unit_1.hit(unit_2):
return True
if not unit_2.hit(unit_1):
return False
if __name__ == '__main__':
# These "asserts" using only for self-checking and not necessary for auto-testing
chuck = Warrior()
bruce = Warrior()
carl = Knight()
dave = Warrior()
mark = Warrior()
assert fight(chuck, bruce) == True
assert fight(dave, carl) == False
assert chuck.is_alive == True
assert bruce.is_alive == False
assert carl.is_alive == True
assert dave.is_alive == False
assert fight(carl, mark) == False
assert carl.is_alive == False
print("Coding complete? Let's try tests!")

View File

@ -0,0 +1,17 @@
def verify_anagrams(first_word, second_word):
first_word = first_word.replace(" ", "").lower()
second_word = list(second_word.replace(" ", "").lower())
for letter in first_word:
if letter in second_word:
second_word.remove(letter)
else:
return False
return len(second_word) == 0
if __name__ == '__main__':
# These "asserts" using only for self-checking and not necessary for auto-testing
assert isinstance(verify_anagrams("a", 'z'), bool), "Boolean!"
assert verify_anagrams("Programming", "Gram Ring Mop") == True, "Gram of code"
assert verify_anagrams("Hello", "Ole Oh") == False, "Hello! Ole Oh!"
assert verify_anagrams("Kyoto", "Tokyo") == True, "The global warming crisis of 3002"