Dmitry Telenkov 1 tahun lalu
induk
melakukan
b1c6a78b43
2 mengubah file dengan 107 tambahan dan 3 penghapusan
  1. 73 3
      courses/python_for_begginers/func.py
  2. 34 0
      courses/python_for_begginers/pytwo.py

+ 73 - 3
courses/python_for_begginers/func.py

@@ -666,12 +666,82 @@ nes = {'Germany': {'berlin': 7},
           'USA': {'washington': 1, 'New York': 4}}
 
 
-print(flatten_dict({'Q': {'w': {'E': {'r': {'T': {'y': 123}}}}}}))
+# print(flatten_dict({'Q': {'w': {'E': {'r': {'T': {'y': 123}}}}}}))
 
 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+# Сортировка слиянием
 
 def merge_two_list(a, b):
-	pass
+	c = []
+	i = j = 0
+	while i < len(a) and j < len(b):
+		if a[i] < b[j]:
+			c.append(a[i])
+			i += 1
+		else:
+			c.append(b[j])
+			j += 1
+	if i < len(a):
+		c += a[i:]
+	elif j < len(b):
+		c += b[j:]
+	return c
+
 
 def merge_sort(s):
-	pass
+	if len(s) == 1:
+		return s
+	middle = len(s)//2
+	left = merge_sort(s[:middle])
+	right = merge_sort(s[middle:])
+	return merge_two_list(left, right)
+
+# print(merge_sort([7, 5, 2 ,3, 9, 8, 6]))
+
+#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+# Быстра сортировка
+
+def quick_sort(s):
+	if len(s) <= 1:
+		return s
+	
+	elem = s[0]
+	left = list(filter(lambda x: x < elem, s))
+	center = [i for i in s if i == elem]
+	right = list(filter(lambda x: x > elem, s))
+
+	return quick_sort(left) + center + quick_sort(right)
+
+# print(quick_sort([7, 5, 2 ,3, 9, 8, 6]))
+
+
+#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+# per = lambda a, b, c : a + b + c
+# t = lambda x: 'positive' if x > 0 else 'negative'
+
+# print(per(1, 2, 3))
+
+#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+# Сортировка по ключам
+
+# a = [321, 32, 54, 3 ,56, 45, 23423, 423, 5435, 234]
+# a.sort(key=lambda x: x//10%10)
+
+
+#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+# def linear(k, b):
+# 	return lambda x: x*k+b
+
+# graf1 = linear(2, 5)
+# graf2 = linear(5, 3)
+# print(graf1(3))
+# print(graf2(9))
+
+
+# starts_with = lambda s: True if s[0] == 'W' else False
+# print(starts_with("World"))
+
+# average = lambda *args: sum(*args)/len(*args) 
+# print(average((1, 3, 6, 3)))

+ 34 - 0
courses/python_for_begginers/pytwo.py

@@ -0,0 +1,34 @@
+
+class MyClass(object):
+	
+	def __init__(self):
+		self.string = "test string"
+
+	@staticmethod
+	def print_string():
+		print "hi"
+
+
+def foo():
+	return new_my_class()
+
+
+def fooo(*args, **kwargs):
+	return "fooo"
+
+
+
+new_my_class = MyClass
+new_my_class.print_string()
+
+foo().print_string()
+
+MyClass = fooo
+
+new_foo = MyClass
+print(new_foo)
+
+# new_my_class = MyClass()
+# new_my_class.print_string()
+
+