From 9e8291a54e0bc55db5cff03a1469588c264d5ced Mon Sep 17 00:00:00 2001 From: Pavel Marychev Date: Sun, 1 Aug 2021 00:38:49 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D1=88=D1=91=D0=BB=20=D0=B3?= =?UTF-8?q?=D0=BB=D0=B0=D0=B2=D1=83=208?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ch8/average_age.py | 32 ++++++++++++++++++++++++++++++++ ch8/fibonacci.py | 29 +++++++++++++++++++++++++++++ ch8/koch.py | 32 ++++++++++++++++++++++++++++++++ ch8/palindrome.py | 14 ++++++++++++++ ch8/sum.py | 39 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 146 insertions(+) create mode 100644 ch8/average_age.py create mode 100644 ch8/fibonacci.py create mode 100644 ch8/koch.py create mode 100644 ch8/palindrome.py create mode 100644 ch8/sum.py diff --git a/ch8/average_age.py b/ch8/average_age.py new file mode 100644 index 0000000..17968c6 --- /dev/null +++ b/ch8/average_age.py @@ -0,0 +1,32 @@ +def average_age(name): + average_age = 0 + i = 0 + friends = users[name]['Друзья'] + for friend in friends: + average_age += users[friend]['Возраст'] + i += 1 + average_age = average_age / i + return average_age + + +def anti_soc(users): + max = 1000 + for name in users: + user = users[name] + friends = user['Друзья'] + if len(friends) < max: + most_anti_soc = name + max = len(friends) + return most_anti_soc + +users = {} +users['Ким'] = {'Эл. почта': 'kim@oreilly.com', 'Пол': 'ж', 'Возраст': 27, 'Друзья': ['Джон', 'Джош']} +users['Джон'] = {'Эл. почта': 'john@oreilly.com', 'Пол': 'м', 'Возраст': 24, 'Друзья': ['Ким', 'Джош']} +users['Джош'] = {'Эл. почта': 'josh@oreilly.com', 'Пол': 'м', 'Возраст': 32, 'Друзья': ['Ким']} + + +print(average_age('Ким')) +print(average_age('Джон')) +print(average_age('Джош')) + +print(anti_soc(users)) diff --git a/ch8/fibonacci.py b/ch8/fibonacci.py new file mode 100644 index 0000000..99bb7b3 --- /dev/null +++ b/ch8/fibonacci.py @@ -0,0 +1,29 @@ +import time + +cache = {} +def fibonacci(n): + ''' + :param n: - число + :return: - булево + + Время выполнения будет очень - очень долгим + ''' + global cache + if n in cache: + return cache[n] + if n == 0: + return 0 + elif n == 1: + return 1 + else: + result = fibonacci(n - 1) + fibonacci(n - 2) + cache[n] = result + return result + + +for i in range(20, 55, 5): + start = time.time() + result = fibonacci(i) + end = time.time() + duration = end - start + print(i, result, duration) diff --git a/ch8/koch.py b/ch8/koch.py new file mode 100644 index 0000000..b848dcd --- /dev/null +++ b/ch8/koch.py @@ -0,0 +1,32 @@ +import turtle + +def setup(pencil): + pencil.color('blue') + pencil.penup() + pencil.goto(-200, 100) + pencil.pendown() + + +def koch(pencil, size, order): + if order == 0: + pencil.forward(size) + else: + for angle in [60, -120, 60, 0]: + koch(pencil, size/3, order-1) + pencil.left(angle) + + +def main(): + pencil = turtle.Turtle() + setup(pencil) + + order = 4 + size = 400 + for i in range(0, 3): + koch(pencil, size, order) + pencil.right(120) + +if __name__ == '__main__': + main() + turtle.tracer(100) + turtle.mainloop() \ No newline at end of file diff --git a/ch8/palindrome.py b/ch8/palindrome.py new file mode 100644 index 0000000..1aef72a --- /dev/null +++ b/ch8/palindrome.py @@ -0,0 +1,14 @@ +def is_palindrome(word): + if len(word) <= 0: + return True + else: + if word[0] == word[-1]: + return is_palindrome(word[1:-1]) + else: + return False + + +words = ['tacocat', 'radar', 'yak', 'radar', 'kayjak'] + +for word in words: + print(is_palindrome(word)) diff --git a/ch8/sum.py b/ch8/sum.py new file mode 100644 index 0000000..fd5d1d8 --- /dev/null +++ b/ch8/sum.py @@ -0,0 +1,39 @@ +marbles = [10, 13, 39, 14, 41, 9, 3, 100] + + +def recursive_compute_sum(list): + if len(list) == 0: + return 0 + else: + first = list[0] + rest = list[1:] + sum = first + recursive_compute_sum(rest) + return sum + + +sum = recursive_compute_sum(marbles) +print('Сумма равна', sum) + +letter = 'radars' + +def polindrom(str): + # с помощию цикла + # list = [] + # ob_list = [] + # for i in str: + # list.append(i) + # for i in str[::-1]: + # ob_list.append(i) + # if list == ob_list: + # return 'полиндром' + # else: + # return 'не полиндром' + # без цикла, просто через срез + if str == str[::-1]: + return 'полиндром' + else: + return 'не полиндром' + +status = polindrom(letter) +print('Слово', letter, status) +