Dmitry Telenkov 1 年之前
父節點
當前提交
869b4706fb
共有 2 個文件被更改,包括 108 次插入2 次删除
  1. 18 0
      courses/python_for_begginers/dec.py
  2. 90 2
      courses/python_for_begginers/func.py

+ 18 - 0
courses/python_for_begginers/dec.py

@@ -0,0 +1,18 @@
+def my_decorator(function_to_decorate):
+	def the_wrapper_around_the_original_function():
+		print("Этот код работает до вызова функции")
+		function_to_decorate()
+		print("Этот код работает после вызова функции")
+	return the_wrapper_around_the_original_function
+
+
+def stand_alone_function():
+	print("Эта функция не изменяется")
+
+
+@my_decorator
+def another_stand_alone_function():
+	print("Оставль меня в покое")
+
+
+another_stand_alone_function()

+ 90 - 2
courses/python_for_begginers/func.py

@@ -297,5 +297,93 @@ def caesar_cipher(message: str, shift: int):
 			my_str += i
 	return my_str
 
-assert caesar_cipher('leave out all the rest', -1) == 'kdzud nts zkk sgd qdrs'
-assert caesar_cipher('one more light', 3) == 'rqh pruh oljkw'
+# assert caesar_cipher('leave out all the rest', -1) == 'kdzud nts zkk sgd qdrs'
+# assert caesar_cipher('one more light', 3) == 'rqh pruh oljkw'
+
+#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~		
+
+MIN_DRIVING_AGE = 18
+
+def allowed_driving(name: str, age: int) -> None:
+    global MIN_DRIVING_AGE
+    if (age >= MIN_DRIVING_AGE):
+        print(name, "может водить")
+    else:
+        print(name, "еще рано садиться за руль")
+
+# allowed_driving('tim', 17)
+
+#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~		
+
+def get_word_indices(strings: list) -> dict:
+	l = []
+	d = {}
+	for i in strings:
+		l.append(i.lower().split())
+
+	for i in range(len(l)):
+		for j in l[i]:
+			if j not in d.keys():
+				d.setdefault(j, [i])
+			else:
+				short_list = d.get(j)
+				short_list.append(i)
+				d.update({j: short_list})
+	return d
+		
+
+# assert get_word_indices(['Look at my horse', 'my horse', 'is amazing']) == {'look': [0], 'at': [0], 'my': [0, 1], 'horse': [0, 1], 'is': [2], 'amazing': [2]}
+	
+def foo(a: int, b: int) -> int:
+	return a + b
+
+# foo(b=2, 3)
+
+#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~		
+
+def replace(text: str, old: str, new: str = ''):
+	s = ''
+	for i in text:
+		if i == old:
+			s += new
+		else:
+			s += i
+	return s
+
+# assert replace('Нет', 'т') == 'Не'
+# assert replace('Bella Ciao', 'a') == 'Bell Cio'
+# assert replace('nobody; i myself farewell analysis', 'l', 'z') == 'nobody; i mysezf farewezz anazysis'
+# assert replace('commend me to my kind lord meaning', 'M', 'w') == 'commend me to my kind lord meaning'
+
+#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~		
+
+def make_header(header: str, h_size : int = 1):
+	return "<h" + str(h_size) + ">" + header + "</h" + str(h_size) + ">"
+
+# print(make_header("hello", 6))
+	
+#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~		
+
+def append_to_list(value, my_list = None):
+	if my_list is None:
+		my_list = []
+	my_list.append(value)
+	print(my_list, id(my_list))
+
+# append_to_list(77)
+
+#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~		
+
+def create_matrix(size : int = 3, up_fill : int = 0, down_fill : int = 0):
+	l = [[i if i == j else 0 for j in range(1, size + 1) ] for i in range(1, size + 1)]
+	
+	for i in range(0, size):
+		for j in range(0, size):
+			if j > i:
+				l[i][j] = up_fill
+			elif j < i:
+				l[i][j] = down_fill
+
+	return l
+
+print(create_matrix(size=4, up_fill=7, down_fill=9))