diff --git a/Elementary/digits-multiplication.py b/Elementary/digits-multiplication.py new file mode 100644 index 0000000..b243564 --- /dev/null +++ b/Elementary/digits-multiplication.py @@ -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!") diff --git a/Elementary/easy-unpack.py b/Elementary/easy-unpack.py new file mode 100644 index 0000000..5cbfa82 --- /dev/null +++ b/Elementary/easy-unpack.py @@ -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!') diff --git a/Elementary/index-power.py b/Elementary/index-power.py new file mode 100644 index 0000000..e4ac468 --- /dev/null +++ b/Elementary/index-power.py @@ -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!") diff --git a/Elementary/multiply-intro.py b/Elementary/multiply-intro.py new file mode 100644 index 0000000..18e07e0 --- /dev/null +++ b/Elementary/multiply-intro.py @@ -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!") diff --git a/Home/the-warriors.py b/Home/the-warriors.py new file mode 100644 index 0000000..3d893a5 --- /dev/null +++ b/Home/the-warriors.py @@ -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!") diff --git a/ScientificExpedition/verify-anagrams.py b/ScientificExpedition/verify-anagrams.py new file mode 100644 index 0000000..1606370 --- /dev/null +++ b/ScientificExpedition/verify-anagrams.py @@ -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"