From 88d9f0233097f8b7f82510f965ff3d9fc73f87a7 Mon Sep 17 00:00:00 2001 From: Srikar Eranky Date: Sat, 25 Jul 2020 13:01:58 -0700 Subject: [PATCH 1/7] Add Python problem and solution for Ch.11 --- .../chapter11/practice/remove_duplicates.py | 13 ++++++++ .../chapter11/solutions/remove_duplicates.py | 31 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 2_intermediate/chapter11/practice/remove_duplicates.py create mode 100644 2_intermediate/chapter11/solutions/remove_duplicates.py diff --git a/2_intermediate/chapter11/practice/remove_duplicates.py b/2_intermediate/chapter11/practice/remove_duplicates.py new file mode 100644 index 00000000..c985da8a --- /dev/null +++ b/2_intermediate/chapter11/practice/remove_duplicates.py @@ -0,0 +1,13 @@ +# Write a function called remove_duplicates +# The sole parameter of the function should be a list +# The function should look through a list, +# Find all duplicate elements, and remove them +# Sort the resulting list +# Hint: To sort a list, use sorted(list) +# Another hint: Use list.count(element) +# To count the number of times that element appears + +# Example: array = [1,1,2,5,4,6,12,3,4,6] +# Result should print [1,2,3,4,5,6,12] + +# Write code here diff --git a/2_intermediate/chapter11/solutions/remove_duplicates.py b/2_intermediate/chapter11/solutions/remove_duplicates.py new file mode 100644 index 00000000..9e6958a3 --- /dev/null +++ b/2_intermediate/chapter11/solutions/remove_duplicates.py @@ -0,0 +1,31 @@ +# Write a function called remove_duplicates +# The sole parameter of the function should be a list +# The function should look through a list, +# Find all duplicate elements, and remove them +# Sort the resulting list +# Hint: To sort a list, use sorted(list) +# Another hint: Use list.count(element) +# To count the number of times that element appears + +# Example: array = [1,1,2,5,4,6,12,3,4,6] +# Result should print [1,2,3,4,5,6,12] + +# Write code here + +list1 = [1, 1, 2, 5, 4, 6, 12, 3, 4, 6] # Define your list + + +def remove_duplicate(array): + for i in array: + # Checks if element appears multiple times + for j in range(i, len(array)-1): + # Counts the number of times an element appears. + if array.count(array[i]) == 1: + break # If it appears once, break out of the loop + else: + # If it appears more than once, remove it + array.remove(array[i]) + return sorted(array) # Sorts the list + + +print(remove_duplicate(list1)) # Call the function From beccfdf68ce264aa25005818a42c4170fe28408f Mon Sep 17 00:00:00 2001 From: Lint Action Date: Sat, 25 Jul 2020 20:14:41 +0000 Subject: [PATCH 2/7] Fix code style issues with Black --- 2_intermediate/chapter11/solutions/remove_duplicates.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2_intermediate/chapter11/solutions/remove_duplicates.py b/2_intermediate/chapter11/solutions/remove_duplicates.py index 9e6958a3..363a54bd 100644 --- a/2_intermediate/chapter11/solutions/remove_duplicates.py +++ b/2_intermediate/chapter11/solutions/remove_duplicates.py @@ -18,7 +18,7 @@ def remove_duplicate(array): for i in array: # Checks if element appears multiple times - for j in range(i, len(array)-1): + for j in range(i, len(array) - 1): # Counts the number of times an element appears. if array.count(array[i]) == 1: break # If it appears once, break out of the loop From 19cde759bc3c4fa7597116655d68611d17248150 Mon Sep 17 00:00:00 2001 From: Srikar Eranky <59518247+srikar-eranky@users.noreply.github.com> Date: Sun, 26 Jul 2020 17:18:08 -0700 Subject: [PATCH 3/7] Update 2_intermediate/chapter11/practice/remove_duplicates.py Co-authored-by: abhatia1205 <32373316+abhatia1205@users.noreply.github.com> --- 2_intermediate/chapter11/practice/remove_duplicates.py | 1 + 1 file changed, 1 insertion(+) diff --git a/2_intermediate/chapter11/practice/remove_duplicates.py b/2_intermediate/chapter11/practice/remove_duplicates.py index c985da8a..26ca6991 100644 --- a/2_intermediate/chapter11/practice/remove_duplicates.py +++ b/2_intermediate/chapter11/practice/remove_duplicates.py @@ -3,6 +3,7 @@ # The function should look through a list, # Find all duplicate elements, and remove them # Sort the resulting list +# YOU MAY NOT USE THE set() function IN PYTHON. Use this problem to practice list iteration! # Hint: To sort a list, use sorted(list) # Another hint: Use list.count(element) # To count the number of times that element appears From ff38d5280c547f677e98202d748e2cc58f65ade4 Mon Sep 17 00:00:00 2001 From: Lint Action Date: Mon, 27 Jul 2020 00:19:19 +0000 Subject: [PATCH 4/7] Fix code style issues with Black --- 1_beginner/chapter3/solutions/temperature.py | 2 +- .../chapter5/practice/add_all_the_way.py | 4 +- 1_beginner/chapter5/practice/alternating.py | 4 +- 1_beginner/chapter5/practice/fibonnaci.py | 8 +- 1_beginner/chapter5/practice/prime.py | 4 +- 1_beginner/chapter5/practice/series.py | 2 +- 2_intermediate/chapter10/practice/img_avg.py | 4 +- 2_intermediate/chapter10/solutions/img_avg.py | 82 +++++++++---------- 8 files changed, 53 insertions(+), 57 deletions(-) diff --git a/1_beginner/chapter3/solutions/temperature.py b/1_beginner/chapter3/solutions/temperature.py index 88387da6..4601f780 100644 --- a/1_beginner/chapter3/solutions/temperature.py +++ b/1_beginner/chapter3/solutions/temperature.py @@ -1,4 +1,4 @@ - # Temperature +# Temperature # Write a program that converts Celsius to Fahrenheit. # It should ask the user for the temperature in # degrees Celsius and then print the temperature in degrees Fahrenheit. diff --git a/1_beginner/chapter5/practice/add_all_the_way.py b/1_beginner/chapter5/practice/add_all_the_way.py index 06b936cc..b91d4817 100644 --- a/1_beginner/chapter5/practice/add_all_the_way.py +++ b/1_beginner/chapter5/practice/add_all_the_way.py @@ -7,6 +7,4 @@ # write code here - - -#Try using the other loop and do the same p[roblem again \ No newline at end of file +# Try using the other loop and do the same p[roblem again diff --git a/1_beginner/chapter5/practice/alternating.py b/1_beginner/chapter5/practice/alternating.py index 1cbabb74..9ae5eb98 100644 --- a/1_beginner/chapter5/practice/alternating.py +++ b/1_beginner/chapter5/practice/alternating.py @@ -9,9 +9,9 @@ """ -#Write code here. +# Write code here. number = int(input("Enter Number Here: ")) -#Now try it with a while loop \ No newline at end of file +# Now try it with a while loop diff --git a/1_beginner/chapter5/practice/fibonnaci.py b/1_beginner/chapter5/practice/fibonnaci.py index 737d3c53..1aa27bde 100644 --- a/1_beginner/chapter5/practice/fibonnaci.py +++ b/1_beginner/chapter5/practice/fibonnaci.py @@ -1,4 +1,4 @@ -''' CHALLENGE PROBLEM!! NOT FOR THE FAINT OF HEART! +""" CHALLENGE PROBLEM!! NOT FOR THE FAINT OF HEART! The Fibonacci numbers, discovered by Leonardo di Fibonacci, is a sequence of numbers that often shows up in mathematics and, @@ -12,9 +12,9 @@ The challenge is to use a for loop (not recursion, if you know what that is), to find the 100th Fibonnaci number. -''' +""" -#write code here +# write code here -#Can you do it with a while loop? \ No newline at end of file +# Can you do it with a while loop? diff --git a/1_beginner/chapter5/practice/prime.py b/1_beginner/chapter5/practice/prime.py index fd776fd5..e631641e 100644 --- a/1_beginner/chapter5/practice/prime.py +++ b/1_beginner/chapter5/practice/prime.py @@ -9,7 +9,7 @@ """ -#write code here +# write code here numer = int(input("Enter number here: ")) @@ -25,4 +25,4 @@ """ -#write code here \ No newline at end of file +# write code here diff --git a/1_beginner/chapter5/practice/series.py b/1_beginner/chapter5/practice/series.py index 6628525f..c8ea06e8 100644 --- a/1_beginner/chapter5/practice/series.py +++ b/1_beginner/chapter5/practice/series.py @@ -13,4 +13,4 @@ Also, try adding up 1 -1/3 + 1/5 - 1/7 + 1/9 - 1/11 ... 10 million times. then multiply this result by 4. What number is this close to? -""" \ No newline at end of file +""" diff --git a/2_intermediate/chapter10/practice/img_avg.py b/2_intermediate/chapter10/practice/img_avg.py index 240e66df..34d7c040 100644 --- a/2_intermediate/chapter10/practice/img_avg.py +++ b/2_intermediate/chapter10/practice/img_avg.py @@ -37,10 +37,10 @@ plt.imshow(img) plt.show() -#write code to create newimg here +# write code to create newimg here plt.imshow(newimg) plt.show() plt.imshow(transpose) -plt.show() \ No newline at end of file +plt.show() diff --git a/2_intermediate/chapter10/solutions/img_avg.py b/2_intermediate/chapter10/solutions/img_avg.py index 10ecfb3e..2961ce16 100644 --- a/2_intermediate/chapter10/solutions/img_avg.py +++ b/2_intermediate/chapter10/solutions/img_avg.py @@ -35,50 +35,48 @@ transpose = numpy.transpose(img) -#write code to create newimg here +# write code to create newimg here def solution1(): - """Iterating over the image here. i is a variable from 0 to the width of the image. + """Iterating over the image here. i is a variable from 0 to the width of the image. j is a variable that ranges from 0 to the height of the image. i is associated with values""" - for i in range(len(img)): - for j in range(len(img[0])): - x_n = [0] - y_n = [0] - - if(i == 0): - x_n.append(1) - elif(i == len(img)-1): - x_n.append(-1) - else: - x_n.append(1) - x_n.append(-1) - - if(j == 0): - y_n.append(1) - elif(j == len(img[0])-1): - y_n.append(-1) - else: - y_n.append(1) - y_n.append(-1) - - r_avg = -1*img[i][j][0] - g_avg = -1*img[i][j][1] - b_avg = -1*img[i][j][2] - c = -1 - - for x in x_n: - for y in y_n: - r_avg += img[i+x][j+y][0] - g_avg += img[i+x][j+y][1] - b_avg += img[i+x][j+y][2] - c+=1 - r_avg = r_avg/c - g_avg = g_avg/c - b_avg = b_avg/c - - newimg[i][j] = [r_avg, g_avg, b_avg] - - + for i in range(len(img)): + for j in range(len(img[0])): + x_n = [0] + y_n = [0] + + if i == 0: + x_n.append(1) + elif i == len(img) - 1: + x_n.append(-1) + else: + x_n.append(1) + x_n.append(-1) + + if j == 0: + y_n.append(1) + elif j == len(img[0]) - 1: + y_n.append(-1) + else: + y_n.append(1) + y_n.append(-1) + + r_avg = -1 * img[i][j][0] + g_avg = -1 * img[i][j][1] + b_avg = -1 * img[i][j][2] + c = -1 + + for x in x_n: + for y in y_n: + r_avg += img[i + x][j + y][0] + g_avg += img[i + x][j + y][1] + b_avg += img[i + x][j + y][2] + c += 1 + r_avg = r_avg / c + g_avg = g_avg / c + b_avg = b_avg / c + + newimg[i][j] = [r_avg, g_avg, b_avg] solution1() @@ -87,4 +85,4 @@ def solution1(): plt.show() plt.imshow(transpose) -plt.show() \ No newline at end of file +plt.show() From 506bf896b8b5f425e801a3bf83c8004ecdb24145 Mon Sep 17 00:00:00 2001 From: Srikar Eranky <59518247+srikar-eranky@users.noreply.github.com> Date: Sun, 26 Jul 2020 18:50:20 -0700 Subject: [PATCH 5/7] Update 2_intermediate/chapter11/solutions/remove_duplicates.py Co-authored-by: abhatia1205 <32373316+abhatia1205@users.noreply.github.com> --- 2_intermediate/chapter11/solutions/remove_duplicates.py | 1 + 1 file changed, 1 insertion(+) diff --git a/2_intermediate/chapter11/solutions/remove_duplicates.py b/2_intermediate/chapter11/solutions/remove_duplicates.py index 363a54bd..7d40b1a8 100644 --- a/2_intermediate/chapter11/solutions/remove_duplicates.py +++ b/2_intermediate/chapter11/solutions/remove_duplicates.py @@ -3,6 +3,7 @@ # The function should look through a list, # Find all duplicate elements, and remove them # Sort the resulting list +# YOU MAY NOT USE THE set() function IN PYTHON. Use this problem to practice list iteration! # Hint: To sort a list, use sorted(list) # Another hint: Use list.count(element) # To count the number of times that element appears From 4d26c4a87029e9a77a64bbf4314a01cd0ce207d8 Mon Sep 17 00:00:00 2001 From: Srikar Eranky <59518247+srikar-eranky@users.noreply.github.com> Date: Sun, 26 Jul 2020 21:09:49 -0700 Subject: [PATCH 6/7] Update remove_duplicates.py Redid the solution using dictionaries, didn't realize that they were covered in Ch.9. If you plan on teaching Ch.11 before Ch.9, highly recommend that you don't use this problem. --- .../chapter11/solutions/remove_duplicates.py | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/2_intermediate/chapter11/solutions/remove_duplicates.py b/2_intermediate/chapter11/solutions/remove_duplicates.py index 7d40b1a8..3e382b27 100644 --- a/2_intermediate/chapter11/solutions/remove_duplicates.py +++ b/2_intermediate/chapter11/solutions/remove_duplicates.py @@ -3,10 +3,11 @@ # The function should look through a list, # Find all duplicate elements, and remove them # Sort the resulting list -# YOU MAY NOT USE THE set() function IN PYTHON. Use this problem to practice list iteration! +# YOU MAY NOT USE THE set() function IN PYTHON. # Hint: To sort a list, use sorted(list) -# Another hint: Use list.count(element) -# To count the number of times that element appears +# Another hint: Use dict.fromkeys(list) +# To take the elements from a list, +# and convert them to keys in a dictionary # Example: array = [1,1,2,5,4,6,12,3,4,6] # Result should print [1,2,3,4,5,6,12] @@ -16,17 +17,16 @@ list1 = [1, 1, 2, 5, 4, 6, 12, 3, 4, 6] # Define your list -def remove_duplicate(array): - for i in array: - # Checks if element appears multiple times - for j in range(i, len(array) - 1): - # Counts the number of times an element appears. - if array.count(array[i]) == 1: - break # If it appears once, break out of the loop - else: - # If it appears more than once, remove it - array.remove(array[i]) - return sorted(array) # Sorts the list +# Define your Function +def remove_duplicates(array): + my_list = list(dict.fromkeys(array)) + # Converts the list into a dictionary. + # Fromkeys(array) turns each item into a key + # There cannot be multiple keys, + # So all the duplicate keys are removed + # Convert the keys back into a list + return sorted(my_list) + # Returns the sorted list of keys that are not duplicate. -print(remove_duplicate(list1)) # Call the function +print(remove_duplicates(list1)) # Call the function From 6cc6537a3593d40bbfc177d8c1b07ec64d1b9b40 Mon Sep 17 00:00:00 2001 From: Srikar Eranky <59518247+srikar-eranky@users.noreply.github.com> Date: Sun, 26 Jul 2020 21:15:26 -0700 Subject: [PATCH 7/7] Update remove_duplicates.py --- 2_intermediate/chapter11/practice/remove_duplicates.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/2_intermediate/chapter11/practice/remove_duplicates.py b/2_intermediate/chapter11/practice/remove_duplicates.py index 26ca6991..54a7f5a3 100644 --- a/2_intermediate/chapter11/practice/remove_duplicates.py +++ b/2_intermediate/chapter11/practice/remove_duplicates.py @@ -3,10 +3,11 @@ # The function should look through a list, # Find all duplicate elements, and remove them # Sort the resulting list -# YOU MAY NOT USE THE set() function IN PYTHON. Use this problem to practice list iteration! +# YOU MAY NOT USE THE set() function IN PYTHON. # Hint: To sort a list, use sorted(list) -# Another hint: Use list.count(element) -# To count the number of times that element appears +# Another hint: Use dict.removekeys(list) +# To take the elements from a list, +# and convert them to keys in a dictionary # Example: array = [1,1,2,5,4,6,12,3,4,6] # Result should print [1,2,3,4,5,6,12]