From 1dcfd757e8b716a2306f7ebb112d8e66c3edaac3 Mon Sep 17 00:00:00 2001 From: Clement Desmidt Date: Wed, 8 Jan 2020 17:32:09 +0100 Subject: [PATCH] :sparkles: Finishing 11 new elements --- Incinerator/rotate-hole.py | 26 +++ Incinerator/the-lancers.py | 161 +++++++++++++++++++ Mine/caesar-cipher-encryptor.py | 30 ++++ Mine/morse-encoder.py | 28 ++++ Mine/skew-symmetric-matrix.py | 28 ++++ Polygon/create-intervals-iterator-version.py | 34 ++++ Polygon/flatten-a-list-iterator-version.py | 25 +++ Polygon/most-wanted-letter-2.py | 27 ++++ PyconTW/matrix-transpose.py | 17 ++ PyconTW/simple_hashlib.py | 13 ++ PyconTW/weekend-counter.py | 22 +++ 11 files changed, 411 insertions(+) create mode 100644 Incinerator/rotate-hole.py create mode 100644 Incinerator/the-lancers.py create mode 100644 Mine/caesar-cipher-encryptor.py create mode 100644 Mine/morse-encoder.py create mode 100644 Mine/skew-symmetric-matrix.py create mode 100644 Polygon/create-intervals-iterator-version.py create mode 100644 Polygon/flatten-a-list-iterator-version.py create mode 100644 Polygon/most-wanted-letter-2.py create mode 100644 PyconTW/matrix-transpose.py create mode 100644 PyconTW/simple_hashlib.py create mode 100644 PyconTW/weekend-counter.py diff --git a/Incinerator/rotate-hole.py b/Incinerator/rotate-hole.py new file mode 100644 index 0000000..a85668e --- /dev/null +++ b/Incinerator/rotate-hole.py @@ -0,0 +1,26 @@ +def rotate(state, pipe_numbers): + count = [] + for i in range(len(state)): + last = state.pop() + state.insert(0, last) + is_good = True + for j in pipe_numbers: + if state[j] != 1: + is_good = False + break + if is_good: + if i+1 >= len(state): + count.append(0) + else: + count.append(i+1) + count.sort() + print(count) + return count + + +if __name__ == '__main__': + # These "asserts" using only for self-checking and not necessary for auto-testing + assert rotate([1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1], [0, 1]) == [1, 8], "Example" + assert rotate([1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1], [0, 1, 2]) == [], "Mission impossible" + assert rotate([1, 0, 0, 0, 1, 1, 0, 1], [0, 4, 5]) == [0], "Don't touch it" + assert rotate([1, 0, 0, 0, 1, 1, 0, 1], [5, 4, 5]) == [0, 5], "Two cannonballs in the same pipe" diff --git a/Incinerator/the-lancers.py b/Incinerator/the-lancers.py new file mode 100644 index 0000000..c52b722 --- /dev/null +++ b/Incinerator/the-lancers.py @@ -0,0 +1,161 @@ +# Taken from mission The Vampires +# Taken from mission The Defenders +# Taken from mission Army Battles +# Taken from mission The Warriors + + +class Warrior: + def __init__(self): + self.health = 50 + self.attack = 5 + self.is_alive = True + self.defense = 0 + self.vampirism = 0 + + def hit(self, other_unit) -> bool: + hit_points = self.attack - other_unit.defense + hit_points = hit_points if hit_points > 0 else 0 + other_unit.health -= hit_points + other_unit.is_alive = other_unit.health > 0 + + self.health += (hit_points * self.vampirism) + return other_unit.is_alive + + +class Knight(Warrior): + def __init__(self): + super().__init__() + self.attack = 7 + + +class Defender(Warrior): + def __init__(self): + super().__init__() + self.health = 60 + self.attack = 3 + self.defense = 2 + + +class Vampire(Warrior): + def __init__(self): + super().__init__() + self.health = 40 + self.attack = 4 + self.vampirism = 0.5 + + +class Rookie(Warrior): + def __init__(self, *args, **kwargs): + super().__init__() + self.health = 50 + self.attack = 1 + + +class Lancer(Warrior): + def __init__(self): + super().__init__() + self.attack = 6 + + +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 + + +class Army: + def __init__(self): + self.units = [] + + def add_units(self, unit_type, quantity): + for i in range(quantity): + self.units.append(unit_type()) + + +class Battle: + @staticmethod + def fight(army_1: Army, army_2: Army) -> bool: + while len(army_1.units) > 0 and len(army_2.units) > 0: + current_unit_1 = army_1.units[0] + current_unit_2 = army_2.units[0] + while current_unit_1.is_alive and current_unit_2.is_alive: + current_unit_1_attack = current_unit_1.hit(current_unit_2) + if isinstance(current_unit_1, Lancer) and len(army_2.units) > 1: + army_2.units[1].health -= current_unit_1.attack * 0.5 + if not current_unit_1_attack: + army_2.units.pop(0) + break + current_unit_2_attack = current_unit_2.hit(current_unit_1) + if isinstance(current_unit_2, Lancer) and len(army_1.units) > 1: + army_1.units[1].health -= current_unit_2.attack * 0.5 + if not current_unit_2_attack: + army_1.units.pop(0) + break + return len(army_1.units) > 0 + + +if __name__ == '__main__': + # These "asserts" using only for self-checking and not necessary for auto-testing + + # fight tests + chuck = Warrior() + bruce = Warrior() + carl = Knight() + dave = Warrior() + mark = Warrior() + bob = Defender() + mike = Knight() + rog = Warrior() + lancelot = Defender() + eric = Vampire() + adam = Vampire() + richard = Defender() + ogre = Warrior() + freelancer = Lancer() + vampire = Vampire() + + 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 + assert fight(bob, mike) == False + assert fight(lancelot, rog) == True + assert fight(eric, richard) == False + assert fight(ogre, adam) == True + assert fight(freelancer, vampire) == True + assert freelancer.is_alive == True + + # battle tests + my_army = Army() + my_army.add_units(Defender, 2) + my_army.add_units(Vampire, 2) + my_army.add_units(Lancer, 4) + my_army.add_units(Warrior, 1) + + enemy_army = Army() + enemy_army.add_units(Warrior, 2) + enemy_army.add_units(Lancer, 2) + enemy_army.add_units(Defender, 2) + enemy_army.add_units(Vampire, 3) + + army_3 = Army() + army_3.add_units(Warrior, 1) + army_3.add_units(Lancer, 1) + army_3.add_units(Defender, 2) + + army_4 = Army() + army_4.add_units(Vampire, 3) + army_4.add_units(Warrior, 1) + army_4.add_units(Lancer, 2) + + battle = Battle() + + assert battle.fight(my_army, enemy_army) == True + assert battle.fight(army_3, army_4) == False + print("Coding complete? Let's try tests!") diff --git a/Mine/caesar-cipher-encryptor.py b/Mine/caesar-cipher-encryptor.py new file mode 100644 index 0000000..52f08d1 --- /dev/null +++ b/Mine/caesar-cipher-encryptor.py @@ -0,0 +1,30 @@ +import string + + +def to_encrypt(text, delta): + alphabet = list(string.ascii_lowercase) + new_text = [] + for letter in text: + if letter.isalpha(): + letter_index = alphabet.index(letter) + delta + if letter_index > len(alphabet): + letter_index = letter_index - len(alphabet) + if letter_index < 0: + letter_index = len(alphabet) + letter_index + new_text.append(alphabet[letter_index]) + else: + new_text.append(letter) + return "".join(new_text) + + +if __name__ == '__main__': + print("Example:") + print(to_encrypt('abc', 10)) + + # These "asserts" using only for self-checking and not necessary for auto-testing + assert to_encrypt("a b c", 3) == "d e f" + assert to_encrypt("a b c", -3) == "x y z" + assert to_encrypt("simple text", 16) == "iycfbu junj" + assert to_encrypt("important text", 10) == "swzybdkxd dohd" + assert to_encrypt("state secret", -13) == "fgngr frperg" + print("Coding complete? Click 'Check' to earn cool rewards!") diff --git a/Mine/morse-encoder.py b/Mine/morse-encoder.py new file mode 100644 index 0000000..7cea35c --- /dev/null +++ b/Mine/morse-encoder.py @@ -0,0 +1,28 @@ +MORSE = {'a': '.-', 'b': '-...', 'c': '-.-.', + 'd': '-..', 'e': '.', 'f': '..-.', + 'g': '--.', 'h': '....', 'i': '..', + 'j': '.---', 'k': '-.-', 'l': '.-..', + 'm': '--', 'n': '-.', 'o': '---', + 'p': '.--.', 'q': '--.-', 'r': '.-.', + 's': '...', 't': '-', 'u': '..-', + 'v': '...-', 'w': '.--', 'x': '-..-', + 'y': '-.--', 'z': '--..', '0': '-----', + '1': '.----', '2': '..---', '3': '...--', + '4': '....-', '5': '.....', '6': '-....', + '7': '--...', '8': '---..', '9': '----.' + } + + +def morse_encoder(text): + return " ".join([MORSE[letter] if letter in MORSE.keys() else " " for letter in text.lower()]) + + +if __name__ == '__main__': + print("Example:") + print(morse_encoder('some text')) + '... --- -- . - . -..- -' + # These "asserts" using only for self-checking and not necessary for auto-testing + assert morse_encoder("some text") == "... --- -- . - . -..- -" + assert morse_encoder("2018") == "..--- ----- .---- ---.." + assert morse_encoder("It was a good day") == ".. - .-- .- ... .- --. --- --- -.. -.. .- -.--" + print("Coding complete? Click 'Check' to earn cool rewards!") diff --git a/Mine/skew-symmetric-matrix.py b/Mine/skew-symmetric-matrix.py new file mode 100644 index 0000000..bdbbbae --- /dev/null +++ b/Mine/skew-symmetric-matrix.py @@ -0,0 +1,28 @@ +def checkio(matr): + clone = zip(*matr) + modifiedClone = [] + for row in clone: + modifiedClone.append(list(map(mult, row))) + + for i, row in enumerate(matr): + for j, cell in enumerate(matr[i]): + if cell != modifiedClone[i][j]: + return False + return True + + +def mult(e): + return -1 * e + + +if __name__ == '__main__': + assert checkio([[0, 1, 2], + [-1, 0, 1], + [-2, -1, 0]]) == True, 'First' + assert checkio([[0, 1, 2], + [-1, 1, 1], + [-2, -1, 0]]) == False, 'Second' + assert checkio([[0, 1, 2], + [-1, 0, 1], + [-3, -1, 0]]) == False, 'Third' + print('All ok') diff --git a/Polygon/create-intervals-iterator-version.py b/Polygon/create-intervals-iterator-version.py new file mode 100644 index 0000000..9165c07 --- /dev/null +++ b/Polygon/create-intervals-iterator-version.py @@ -0,0 +1,34 @@ +def create_intervals(data): + """ + Create a list of intervals out of set of ints. + """ + results = [] + data = sorted(data) + start = None + current_number = None + for number in data: + if start is None: + start = number + current_number = start + continue + if number != start and number != (current_number + 1): + results.append((start, current_number)) + start = number + current_number = number + if start is not None: + results.append((start, current_number)) + return iter(results) + + +if __name__ == '__main__': + # These "asserts" using only for self-checking and not necessary for auto-testing + res = create_intervals(iter(sorted(list({1, 2, 3, 4, 5, 7, 8, 12})))) + assert hasattr(res, '__iter__'), "your function should return the iterator object" + assert hasattr(res, '__next__'), "your function should return the iterator object" + + assert list(create_intervals(iter(sorted(list({1, 2, 3, 4, 5, 7, 8, 12}))))) == [ + (1, 5), (7, 8), (12, 12)], "First" + assert list(create_intervals(iter(sorted(list({1, 2, 3, 6, 7, 8, 4, 5}))))) == [ + (1, 8)], "Second" + assert list(create_intervals(iter([]))) == [], "Empty" + print('Almost done! The only thing left to do is to Check it!') diff --git a/Polygon/flatten-a-list-iterator-version.py b/Polygon/flatten-a-list-iterator-version.py new file mode 100644 index 0000000..a97c465 --- /dev/null +++ b/Polygon/flatten-a-list-iterator-version.py @@ -0,0 +1,25 @@ +def flat_list(array): + return iter(flat_iter(array)) + + +def flat_iter(array): + new_array = [] + for i in array: + if hasattr(i, '__next__'): + new_array += flat_iter(i) + else: + new_array.append(i) + return new_array + + +if __name__ == '__main__': + res = flat_list([1, 2, 3]) + assert hasattr(res, '__iter__'), "your function should return the iterator object" + assert hasattr(res, '__next__'), "your function should return the iterator object" + + assert list(flat_list(iter([1, 2, 3]))) == [1, 2, 3], "First" + assert list(flat_list(iter([1, iter([2, 2, 2]), 4]))) == [1, 2, 2, 2, 4], "Second" + assert list(flat_list(iter([iter([2]), iter([4, iter([5, 6, iter([6]), 6, 6, 6]), 7])]))) == [2, 4, 5, 6, 6, 6, 6, + 6, 7], "Third" + assert list(flat_list(iter([-1, iter([1, iter([-2]), 1]), -1]))) == [-1, 1, -2, 1, -1], "Four" + print('Done! Check it') diff --git a/Polygon/most-wanted-letter-2.py b/Polygon/most-wanted-letter-2.py new file mode 100644 index 0000000..6d586c6 --- /dev/null +++ b/Polygon/most-wanted-letter-2.py @@ -0,0 +1,27 @@ +def most_wanted(text: str) -> str: + text = text.lower() + letters = [] + max = 0 + for letter in text: + if letter.isalpha(): + if letter not in letters: + count = text.count(letter) + if max == count: + letters.append(letter) + if count > max: + letters = [letter] + max = count + return letters + + +if __name__ == '__main__': + # These "asserts" using only for self-checking and not necessary for auto-testing + assert sorted(most_wanted("Hello World!")) == ["l"], "Hello test" + assert sorted(most_wanted("How do you do?")) == ["o"], "O is most wanted" + assert sorted(most_wanted("One")) == ["e", "n", "o"], "All letter only once." + assert sorted(most_wanted("Oops!")) == ["o"], "Don't forget about lower case." + assert sorted(most_wanted("AAaooo!!!!")) == ["a", "o"], "Only letters." + assert sorted(most_wanted("abe")) == ["a", "b", "e"], "The First." + print("Start the long test") + assert sorted(most_wanted("a" * 9000 + "b" * 1000)) == ["a"], "Long." + print("The local tests are done.") diff --git a/PyconTW/matrix-transpose.py b/PyconTW/matrix-transpose.py new file mode 100644 index 0000000..844bea9 --- /dev/null +++ b/PyconTW/matrix-transpose.py @@ -0,0 +1,17 @@ +def checkio(matr): + """return a transposed matrix""" + return map(list, zip(*matr)) + + +if __name__ == '__main__': + assert checkio([[1, 2], + [1, 2]]) == [[1, 1], + [2, 2]], 'First' + assert checkio([[1, 0, 3, 4, 0], + [2, 0, 4, 5, 6], + [3, 4, 9, 0, 6]]) == [[1, 2, 3], + [0, 0, 4], + [3, 4, 9], + [4, 5, 0], + [0, 6, 6]], 'Second' + print('All ok') diff --git a/PyconTW/simple_hashlib.py b/PyconTW/simple_hashlib.py new file mode 100644 index 0000000..28ecdf8 --- /dev/null +++ b/PyconTW/simple_hashlib.py @@ -0,0 +1,13 @@ +import hashlib + + +def checkio(hashed_string, algorithm): + m = hashlib.new(algorithm) + m.update(hashed_string.encode('utf-8')) + return m.hexdigest() + + +if __name__ == '__main__': + # These "asserts" using only for self-checking and not necessary for auto-testing + assert checkio('welcome', 'md5') == '40be4e59b9a2a2b5dffb918c0e86b3d7' + assert checkio('happy spam', 'sha224') == '6e9dc3e01d57f1598c2b40ce59fc3527e698c77b15d0840ae96a8b5e' diff --git a/PyconTW/weekend-counter.py b/PyconTW/weekend-counter.py new file mode 100644 index 0000000..b2c75da --- /dev/null +++ b/PyconTW/weekend-counter.py @@ -0,0 +1,22 @@ +from datetime import date, timedelta + + +def checkio(from_date: date, to_date: date) -> int: + """ + Count the days of rest + """ + plus_a_day = timedelta(days=1) + count = 0 + while from_date != to_date: + if from_date.weekday() == 5 or from_date.weekday() == 6: + count += 1 + from_date += plus_a_day + count += 1 if to_date.weekday() == 5 or to_date.weekday() == 6 else 0 + return count + + +# These "asserts" using only for self-checking and not necessary for auto-testing +if __name__ == '__main__': + assert checkio(date(2013, 9, 18), date(2013, 9, 23)) == 2, "1st example" + assert checkio(date(2013, 1, 1), date(2013, 2, 1)) == 8, "2nd example" + assert checkio(date(2013, 2, 2), date(2013, 2, 3)) == 2, "3rd example"