diff --git a/Blizzard/capital-city.py b/Blizzard/capital-city.py new file mode 100644 index 0000000..b3ae13a --- /dev/null +++ b/Blizzard/capital-city.py @@ -0,0 +1,44 @@ +class Capital: + class __Singleton: + def __init__(self, city_name): + self.val = None + self.city_name = city_name + + def __str__(self): + return repr(self) + self.val + + def name(self): + return self.city_name + + instance = None + + def __new__(cls, city_name): # _new_ est toujours une méthode de classe + if not Capital.instance: + Capital.instance = Capital.__Singleton(city_name) + return Capital.instance + + def __getattr__(self, attr): + return getattr(self.instance, attr) + + def __setattr__(self, attr, val): + return setattr(self.instance, attr, val) + + @staticmethod + def name(): + return Capital.instance.name() + + +if __name__ == '__main__': + # These "asserts" using only for self-checking and not necessary for auto-testing + + ukraine_capital_1 = Capital("Kyiv") + ukraine_capital_2 = Capital("London") + ukraine_capital_3 = Capital("Marocco") + + assert ukraine_capital_2.name() == "Kyiv" + assert ukraine_capital_3.name() == "Kyiv" + + assert ukraine_capital_2 is ukraine_capital_1 + assert ukraine_capital_3 is ukraine_capital_1 + + print("Coding complete? Let's try tests!") diff --git a/Blizzard/gcd.py b/Blizzard/gcd.py new file mode 100644 index 0000000..28acdf3 --- /dev/null +++ b/Blizzard/gcd.py @@ -0,0 +1,31 @@ +def divisorGenerator(n, maximum): + divisors = [1] + for i in range(1, min(n+1, maximum+1)): + if n % i == 0 and i not in divisors: + divisors.append(i) + return divisors + + +def greatest_common_divisor(*numbers): + divisors = [] + numbers = list(numbers) + numbers.sort() + maximum = min(numbers[0], round(numbers[-1:][0] / 2)) + number_of_numbers = len(numbers) + for number in numbers: + divisors += divisorGenerator(number, maximum) + divisors.sort() + divisors.reverse() + for divisor in divisors: + if divisors.count(divisor) == number_of_numbers: + return divisor + + +if __name__ == '__main__': + # These "asserts" using only for self-checking and not necessary for auto-testing + assert greatest_common_divisor(6, 4) == 2, "Simple" + assert greatest_common_divisor(2, 4, 8) == 2, "Three arguments" + assert greatest_common_divisor(2, 3, 5, 7, 11) == 1, "Prime numbers" + assert greatest_common_divisor(3, 9, 3, 9) == 3, "Repeating arguments" + assert greatest_common_divisor(1, 1) == 1, "1" + assert greatest_common_divisor(4294967296, 2) == 2, "Edge2" diff --git a/Blizzard/the-highest-building.py b/Blizzard/the-highest-building.py new file mode 100644 index 0000000..05d850a --- /dev/null +++ b/Blizzard/the-highest-building.py @@ -0,0 +1,45 @@ +def highest_building(buildings): + for height, building in enumerate(buildings): + if building.count(1) > 0: + return [building.index(1)+1, len(buildings) - height] + return [0, 0] + + +if __name__ == '__main__': + print("Example:") + print(highest_building([ + [0, 0, 1, 0], + [1, 0, 1, 0], + [1, 1, 1, 0], + [1, 1, 1, 1] + ])) + + # These "asserts" using only for self-checking and not necessary for auto-testing + assert highest_building([ + [0, 0, 1, 0], + [1, 0, 1, 0], + [1, 1, 1, 0], + [1, 1, 1, 1] + ]) == [3, 4], "Common" + assert highest_building([ + [0, 0, 0, 0], + [0, 0, 0, 0], + [0, 0, 0, 0], + [0, 0, 0, 1] + ]) == [4, 1], "Cabin in the wood" + assert highest_building([ + [1, 0, 0, 0, 0], + [1, 1, 0, 0, 0], + [1, 1, 1, 0, 0], + [1, 1, 1, 1, 0], + [1, 1, 1, 1, 1] + ]) == [1, 5], "Triangle" + assert highest_building([ + [0, 0, 0, 1, 0, 0, 0], + [0, 0, 1, 1, 1, 0, 0], + [0, 1, 1, 1, 1, 1, 0], + [1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1, 1] + ]) == [4, 6], "Pyramid" + print("Coding complete? Click 'Check' to earn cool rewards!") diff --git a/Blizzard/when-is-friday.py b/Blizzard/when-is-friday.py new file mode 100644 index 0000000..7799f51 --- /dev/null +++ b/Blizzard/when-is-friday.py @@ -0,0 +1,25 @@ +from datetime import date + + +def friday(day): + day, month, year = list(map(int, day.split("."))) + the_date = date(year, month, day) + friday_count = 4 + the_date_weekday = the_date.weekday() + if the_date_weekday == 4: + return 0 + elif the_date_weekday < friday_count: + return friday_count - the_date_weekday + else: + return 7 - the_date_weekday + friday_count + + +if __name__ == '__main__': + print("Example:") + print(friday('23.04.2018')) + + # These "asserts" using only for self-checking and not necessary for auto-testing + assert friday('23.04.2018') == 4 + assert friday('01.01.1999') == 0 + assert friday("11.11.1111") == 6 + print("Coding complete? Click 'Check' to earn cool rewards!") diff --git a/Home/pawn-brotherhood.py b/Home/pawn-brotherhood.py new file mode 100644 index 0000000..ddb6253 --- /dev/null +++ b/Home/pawn-brotherhood.py @@ -0,0 +1,21 @@ +def safe_pawns(pawns: set) -> int: + cols = "abcdefgh" + safe_count = 0 + for pawn in pawns: + row = int(pawn[1]) + if row == 1: + continue + col_index = cols.index(pawn[0]) + protector_1 = cols[col_index - 1] + str(row - 1) if col_index - 1 >= 0 else None + protector_2 = cols[col_index + 1] + str(row - 1) if col_index + 1 < len(pawns) and row > 1 else None + if protector_1 in pawns or protector_2 in pawns: + safe_count += 1 + return safe_count + + +if __name__ == '__main__': + # These "asserts" using only for self-checking and not necessary for auto-testing + assert safe_pawns({"b4", "d4", "f4", "c3", "e3", "g5", "d2"}) == 6 + assert safe_pawns({"b4", "c4", "d4", "e4", "f4", "g4", "e5"}) == 1 + assert safe_pawns({"a1", "b2", "c3", "d4", "e5", "f6", "g7", "h8"}) == 8 + print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")