From e1449b2ac3e2bef840d9147aef754deeb0c390c8 Mon Sep 17 00:00:00 2001 From: surister Date: Thu, 28 Nov 2019 10:33:07 +0100 Subject: [PATCH 1/5] add python 'empty return' answer --- README.md | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8634b1c..c935f2b 100644 --- a/README.md +++ b/README.md @@ -2404,7 +2404,70 @@ def reverse_string(string):
What empty return returns?
-
+ + +Short answer is: It returns a None object. + +We could go a bit deeper and explain the difference between + +``` +def a (): + return + +>>> None + +``` + +And + +``` +def a (): + pass + +>>> None +``` +Or we could be asked this as a following question, since they both give the same result. + +We could use the dis module to see what's going on: + +``` + 2 0 LOAD_CONST 0 (", line 2>) + 2 LOAD_CONST 1 ('a') + 4 MAKE_FUNCTION 0 + 6 STORE_NAME 0 (a) + + 5 8 LOAD_CONST 2 (", line 5>) + 10 LOAD_CONST 3 ('b') + 12 MAKE_FUNCTION 0 + 14 STORE_NAME 1 (b) + 16 LOAD_CONST 4 (None) + 18 RETURN_VALUE + +Disassembly of ", line 2>: + 3 0 LOAD_CONST 0 (None) + 2 RETURN_VALUE + +Disassembly of ", line 5>: + 6 0 LOAD_CONST 0 (None) + 2 RETURN_VALUE +``` + +An empty return is exactly the same as return None and functions without any return +will always return None regardless of the operations, therefore + + + + +``` +def sum(a, b): + global C + c = a + b + +>>> None + +``` + + ##### Time Complexity From 31ae1ce3796eca852899135ee15066663ec28a19 Mon Sep 17 00:00:00 2001 From: surister Date: Thu, 28 Nov 2019 10:48:26 +0100 Subject: [PATCH 2/5] styling --- README.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/README.md b/README.md index c935f2b..ebea961 100644 --- a/README.md +++ b/README.md @@ -2415,7 +2415,6 @@ def a (): return >>> None - ``` And @@ -2452,19 +2451,16 @@ Disassembly of ", line 5>: 2 RETURN_VALUE ``` -An empty return is exactly the same as return None and functions without any return +An empty return is exactly the same as return None and functions without any explicit return will always return None regardless of the operations, therefore - - ``` def sum(a, b): global C c = a + b >>> None - ``` From 9ffbda01d2e90662c9b9676ca9ad29b6c554ba28 Mon Sep 17 00:00:00 2001 From: surister Date: Thu, 28 Nov 2019 11:01:35 +0100 Subject: [PATCH 3/5] Add new python 'unique elementes in list' answer --- README.md | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ebea961..570905f 100644 --- a/README.md +++ b/README.md @@ -2250,12 +2250,57 @@ def return_sum():
How do you swap values between two variables?
+``` x, y = y, x +``` +
-How to check if all the elements in a given lists are unique? so [1, 2, 3] is unique but [1, 1, 2, 3] is not unique but we 1 twice
-
+How to check if all the elements in a given lists are unique? so [1, 2, 3] is unique but [1, 1, 2, 3] is not unique because 1 exists twice
+ + +There are many ways of solving this problem: +# Note: :list and -> bool are just python typings, they are not needed for the correct execution of the algorithm. + +Taking advantage of sets and len: + +``` +def is_unique(l:list) -> bool: + return len(set(l)) == len(l) +``` + +This one is can be seen used in other programming languages. + +``` +def is_unique2(l:list) -> bool: + seen = [] + + for i in l: + if i in seen: + return False + seen.append(i) + return True +``` + +Here we just count and make sure every element is just repeated once. + +``` +def is_unique3(l:list) -> bool: + for i in l: + if l.count(i) > 1: + return False + return True +``` + +This one might look more convulated but hey, one liners. + +``` +def is_unique4(l:list) -> bool: + return all(map(lambda x: l.count(x) < 2, l)) +``` + +
What _ is used for in Python?
@@ -2457,7 +2502,7 @@ will always return None regardless of the operations, therefore ``` def sum(a, b): - global C + global c c = a + b >>> None From 4135b09412de123223add5282b02c26876a34a15 Mon Sep 17 00:00:00 2001 From: surister Date: Thu, 28 Nov 2019 11:03:38 +0100 Subject: [PATCH 4/5] styling --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 570905f..a6e2fa2 100644 --- a/README.md +++ b/README.md @@ -2261,7 +2261,7 @@ x, y = y, x There are many ways of solving this problem: -# Note: :list and -> bool are just python typings, they are not needed for the correct execution of the algorithm. +# Note: :list and -> bool are just python typings, they are not needed for the correct execution of the algorithm. Taking advantage of sets and len: From e63da0db6238673bc74e5e4cf04b97a807cf2a6f Mon Sep 17 00:00:00 2001 From: surister Date: Thu, 28 Nov 2019 11:04:10 +0100 Subject: [PATCH 5/5] styling --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a6e2fa2..4f4d1e4 100644 --- a/README.md +++ b/README.md @@ -2260,7 +2260,7 @@ x, y = y, x How to check if all the elements in a given lists are unique? so [1, 2, 3] is unique but [1, 1, 2, 3] is not unique because 1 exists twice
-There are many ways of solving this problem: +There are many ways of solving this problem:
# Note: :list and -> bool are just python typings, they are not needed for the correct execution of the algorithm. Taking advantage of sets and len: