From 20e09c6b691288a78c5433b966c29ffeede0b043 Mon Sep 17 00:00:00 2001 From: Citrus716 <60357364+Citrus716@users.noreply.github.com> Date: Thu, 6 Aug 2020 17:48:18 -0400 Subject: [PATCH 01/31] Changed img_avg.py template Added an example and other changes to improve understanding of the problem. --- 2_intermediate/chapter10/practice/img_avg.py | 29 ++++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/2_intermediate/chapter10/practice/img_avg.py b/2_intermediate/chapter10/practice/img_avg.py index dc9de796..73a5fc1d 100644 --- a/2_intermediate/chapter10/practice/img_avg.py +++ b/2_intermediate/chapter10/practice/img_avg.py @@ -12,27 +12,36 @@ will return a different image where each pixel is the average of the pixels surrounding it in the original image. -The neighbors of an image are all the pixels that surround it, -1 on each side, and 4 on the diagonals, for 8 in total. Each -pixel doesn't necessarily have 8 neighbors, though (think about why). - -The code to grab an image from the internet and make it -into an array is given to you. The code also displays the new image -you create in the end. - NOTE: The image is 3 dimensional because each pixel has RGB values. To find the average value of all of a pixels neighbors, you must change the average of the red value to the red value, blue to blue, etc. For example, if the neighbors of a pixel with value [1, 2, 3] were [20, 30, 40] and [10, 120, 30], the new pixel that would replace the original one would be [15, 75, 35] + +EXAMPLE: A image with,let's say 9 pixels may look like: +[[[31,41,42], [51,1,101], [24,141,33]], + [[50,21,28], [31,49,201], [90,54,33]], + [[12,81,3], [22,8,91], [101,141,132]]] + +HINT: Don't forget that a pixel may have varying amount of neighboring +pixels. A pixel at the edge, for example, has 3 neighboring pixels while +a pixel at the center of the image has 8 neighboring pixels. + +GIVEN: The code to grab an image from the internet and make it +into an array is given to you. The code also displays the new image +you create in the end. These weren't taught in the main curriculum, so +it isn't expected for students to fully understand what the given code +does. """ +#All the libraries imported from PIL import Image import requests import numpy import matplotlib.pyplot as plt +#Code that grabs the image from the internet and makes it into an array url = "https://images.dog.ceo/breeds/waterdog-spanish/20180723_185544.jpg" img = numpy.array(Image.open(requests.get(url, stream=True).raw)).tolist() newimg = img @@ -41,8 +50,10 @@ plt.imshow(img) plt.show() -# write code to create newimg here +#Write code to create newimg here + +#Code that displays the new image at the end. plt.imshow(newimg) plt.show() From 7eec75c1ca03da6fe3ee6c10d2e1586777076d45 Mon Sep 17 00:00:00 2001 From: Citrus716 <60357364+Citrus716@users.noreply.github.com> Date: Thu, 6 Aug 2020 17:51:22 -0400 Subject: [PATCH 02/31] Update img_avg.py solution Added an example and other changes to improve understanding of the problem. Didn't change the actual solution other than removing the use of functions. --- 2_intermediate/chapter10/solutions/img_avg.py | 124 ++++++++++-------- 1 file changed, 67 insertions(+), 57 deletions(-) diff --git a/2_intermediate/chapter10/solutions/img_avg.py b/2_intermediate/chapter10/solutions/img_avg.py index 815ab8ea..2c9bdefb 100644 --- a/2_intermediate/chapter10/solutions/img_avg.py +++ b/2_intermediate/chapter10/solutions/img_avg.py @@ -12,82 +12,92 @@ will return a different image where each pixel is the average of the pixels surrounding it in the original image. -The neighbors of an image are all the pixels that surround it, -1 on each side, and 4 on the diagonals, for 8 in total. Each -pixel doesn't necessarily have 8 neighbors, though (think about why). - -The code to grab an image from the internet and make it -into an array is given to you. The code also displays the new image -you create in the end. - NOTE: The image is 3 dimensional because each pixel has RGB values. To find the average value of all of a pixels neighbors, you must change the average of the red value to the red value, blue to blue, etc. For example, if the neighbors of a pixel with value [1, 2, 3] were [20, 30, 40] and [10, 120, 30], the new pixel that would replace the original one would be [15, 75, 35] + +EXAMPLE: A image with,let's say 9 pixels may look like: +[[[31,41,42], [51,1,101], [24,141,33]], + [[50,21,28], [31,49,201], [90,54,33]], + [[12,81,3], [22,8,91], [101,141,132]]] + +HINT: Don't forget that a pixel may have varying amount of neighboring +pixels. A pixel at the edge, for example, has 3 neighboring pixels while +a pixel at the center of the image has 8 neighboring pixels. + +GIVEN: The code to grab an image from the internet and make it +into an array is given to you. The code also displays the new image +you create in the end. These weren't taught in the main curriculum, so +it isn't expected for students to fully understand what the given code +does. """ +#All the libraries imported from PIL import Image import requests import numpy import matplotlib.pyplot as plt +#Code that grabs the image from the internet and makes it into an array url = "https://images.dog.ceo/breeds/waterdog-spanish/20180723_185544.jpg" -img = numpy.array(Image.open(requests.get(url, stream=True).raw)) +img = numpy.array(Image.open(requests.get(url, stream=True).raw)).tolist() newimg = img transpose = numpy.transpose(img) +plt.imshow(img) +plt.show() -# 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. - 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] - - -solution1() +#Write code to create newimg here +""" +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] + + +#Code that displays the new image at the end. plt.imshow(newimg) plt.show() From 365dbd3a752d17e9724c19753db1ba97e540fb73 Mon Sep 17 00:00:00 2001 From: Citrus716 <60357364+Citrus716@users.noreply.github.com> Date: Thu, 6 Aug 2020 21:01:25 -0400 Subject: [PATCH 03/31] Made the characters per line less in alternating Made the characters per line less in practice/alternating --- 1_beginner/chapter5/practice/alternating.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/1_beginner/chapter5/practice/alternating.py b/1_beginner/chapter5/practice/alternating.py index a57c1dde..984edfbc 100644 --- a/1_beginner/chapter5/practice/alternating.py +++ b/1_beginner/chapter5/practice/alternating.py @@ -1,10 +1,12 @@ """ Alternating +Ask the user for an integer. The print the numbers from 1 +to that number, but alternating in sign. For example, if the input +was 5, what would be printed is 1, -1, 2, -2, 3, -3, 4, -4, 5. +(Note, DO NOT include the last negative number). +Do this with a for loop +""" -Ask the user for an integer. The print the numbers from 1 to that number, -but alternating in sign. For example, if the input was 5, what would be printed -is 1, -1, 2, -2, 3, -3, 4, -4, 5. (Note, DO NOT include the last negative -number). Do this with a for loop """ From 2d2872baacd20f10a77d29798f312718a4d1a17f Mon Sep 17 00:00:00 2001 From: Citrus716 <60357364+Citrus716@users.noreply.github.com> Date: Thu, 6 Aug 2020 21:02:16 -0400 Subject: [PATCH 04/31] Added my solution to alternating.py. Added my solution to alternating.py. I didn't make this problem. Good problem though. --- 1_beginner/chapter5/solutions/alternating.py | 30 ++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 1_beginner/chapter5/solutions/alternating.py diff --git a/1_beginner/chapter5/solutions/alternating.py b/1_beginner/chapter5/solutions/alternating.py new file mode 100644 index 00000000..0f1d9bdd --- /dev/null +++ b/1_beginner/chapter5/solutions/alternating.py @@ -0,0 +1,30 @@ +""" +Alternating +Ask the user for an integer. The print the numbers from 1 +to that number, but alternating in sign. For example, if the input +was 5, what would be printed is 1, -1, 2, -2, 3, -3, 4, -4, 5. +(Note, DO NOT include the last negative number). +Do this with a for loop +""" + +# Write code here. + +number = int(input("Enter Number Here: ")) +for num in range(1,number+1): + if num == number: + print(num) + else: + print(num) + print(-1*num) + + + +# Now try it with a while loop +number = int(input("Enter Number Here: ")) +current_num = 1 +while(current_num < number): + print(current_num) + print(-1 *current_num) + current_num+= 1 +print(current_num) + From ca5a514aa1aac5ca8abebea09975dc10df5e2a12 Mon Sep 17 00:00:00 2001 From: Lint Action Date: Fri, 7 Aug 2020 01:04:05 +0000 Subject: [PATCH 05/31] Fix code style issues with Black --- 1_beginner/chapter5/solutions/alternating.py | 12 +++++------- 2_intermediate/chapter10/practice/img_avg.py | 8 ++++---- 2_intermediate/chapter10/solutions/img_avg.py | 10 +++++----- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/1_beginner/chapter5/solutions/alternating.py b/1_beginner/chapter5/solutions/alternating.py index 0f1d9bdd..907f2fc4 100644 --- a/1_beginner/chapter5/solutions/alternating.py +++ b/1_beginner/chapter5/solutions/alternating.py @@ -10,21 +10,19 @@ # Write code here. number = int(input("Enter Number Here: ")) -for num in range(1,number+1): +for num in range(1, number + 1): if num == number: print(num) else: print(num) - print(-1*num) - + print(-1 * num) # Now try it with a while loop number = int(input("Enter Number Here: ")) current_num = 1 -while(current_num < number): +while current_num < number: print(current_num) - print(-1 *current_num) - current_num+= 1 + print(-1 * current_num) + current_num += 1 print(current_num) - diff --git a/2_intermediate/chapter10/practice/img_avg.py b/2_intermediate/chapter10/practice/img_avg.py index 73a5fc1d..fe829c5c 100644 --- a/2_intermediate/chapter10/practice/img_avg.py +++ b/2_intermediate/chapter10/practice/img_avg.py @@ -35,13 +35,13 @@ does. """ -#All the libraries imported +# All the libraries imported from PIL import Image import requests import numpy import matplotlib.pyplot as plt -#Code that grabs the image from the internet and makes it into an array +# Code that grabs the image from the internet and makes it into an array url = "https://images.dog.ceo/breeds/waterdog-spanish/20180723_185544.jpg" img = numpy.array(Image.open(requests.get(url, stream=True).raw)).tolist() newimg = img @@ -50,10 +50,10 @@ plt.imshow(img) plt.show() -#Write code to create newimg here +# Write code to create newimg here -#Code that displays the new image at the end. +# Code that displays the new image at the end. plt.imshow(newimg) plt.show() diff --git a/2_intermediate/chapter10/solutions/img_avg.py b/2_intermediate/chapter10/solutions/img_avg.py index 2c9bdefb..5a4798da 100644 --- a/2_intermediate/chapter10/solutions/img_avg.py +++ b/2_intermediate/chapter10/solutions/img_avg.py @@ -35,13 +35,13 @@ does. """ -#All the libraries imported +# All the libraries imported from PIL import Image import requests import numpy import matplotlib.pyplot as plt -#Code that grabs the image from the internet and makes it into an array +# Code that grabs the image from the internet and makes it into an array url = "https://images.dog.ceo/breeds/waterdog-spanish/20180723_185544.jpg" img = numpy.array(Image.open(requests.get(url, stream=True).raw)).tolist() newimg = img @@ -51,7 +51,7 @@ plt.show() -#Write code to create newimg here +# Write code to create newimg here """ Iterating over the image here. i is a variable from 0 to the width of the image. @@ -95,9 +95,9 @@ b_avg = b_avg / c newimg[i][j] = [r_avg, g_avg, b_avg] - -#Code that displays the new image at the end. + +# Code that displays the new image at the end. plt.imshow(newimg) plt.show() From 992b468e68ba2bcb0601f63be525dab8ccd43ea1 Mon Sep 17 00:00:00 2001 From: Rebecca Dang Date: Fri, 7 Aug 2020 11:00:42 -0700 Subject: [PATCH 06/31] Fix style alternating.py --- 1_beginner/chapter5/practice/alternating.py | 11 ++--------- 1_beginner/chapter5/solutions/alternating.py | 15 +++++++-------- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/1_beginner/chapter5/practice/alternating.py b/1_beginner/chapter5/practice/alternating.py index 984edfbc..7c59aa5a 100644 --- a/1_beginner/chapter5/practice/alternating.py +++ b/1_beginner/chapter5/practice/alternating.py @@ -4,16 +4,9 @@ to that number, but alternating in sign. For example, if the input was 5, what would be printed is 1, -1, 2, -2, 3, -3, 4, -4, 5. (Note, DO NOT include the last negative number). -Do this with a for loop +Do this with a for loop and then with a while loop. """ - -Do this with a for loop -""" - -# Write code here. - -number = int(input("Enter Number Here: ")) - +# Write code here # Now try it with a while loop diff --git a/1_beginner/chapter5/solutions/alternating.py b/1_beginner/chapter5/solutions/alternating.py index 907f2fc4..b4b9ccc8 100644 --- a/1_beginner/chapter5/solutions/alternating.py +++ b/1_beginner/chapter5/solutions/alternating.py @@ -4,25 +4,24 @@ to that number, but alternating in sign. For example, if the input was 5, what would be printed is 1, -1, 2, -2, 3, -3, 4, -4, 5. (Note, DO NOT include the last negative number). -Do this with a for loop +Do this with a for loop and then with a while loop. """ -# Write code here. - -number = int(input("Enter Number Here: ")) +# for loop solution +number = int(input("Enter number here: ")) for num in range(1, number + 1): if num == number: print(num) else: print(num) - print(-1 * num) + print(-num) -# Now try it with a while loop -number = int(input("Enter Number Here: ")) +# while loop solution +number = int(input("Enter number here: ")) current_num = 1 while current_num < number: print(current_num) - print(-1 * current_num) + print(-current_num) current_num += 1 print(current_num) From 3ae03146fd8ebe34e436ca08b90364987215bf31 Mon Sep 17 00:00:00 2001 From: Rebecca Dang Date: Fri, 7 Aug 2020 13:18:10 -0700 Subject: [PATCH 07/31] Fix style and clarify instructions for img_avg.py --- 2_intermediate/chapter10/practice/img_avg.py | 58 +++++++++------- 2_intermediate/chapter10/solutions/img_avg.py | 69 +++++++++++-------- 2 files changed, 75 insertions(+), 52 deletions(-) diff --git a/2_intermediate/chapter10/practice/img_avg.py b/2_intermediate/chapter10/practice/img_avg.py index fe829c5c..f8d576dc 100644 --- a/2_intermediate/chapter10/practice/img_avg.py +++ b/2_intermediate/chapter10/practice/img_avg.py @@ -1,59 +1,71 @@ """ Image Average -Here is the challenge problem for 2D loops: -Images are often represented as 3D arrays, -where the rows and columns are the pixels in the image, -and each pixel has an RGB (red, green, blue) value -which determines the color of the pixel. +Here is the challenge problem for nested loops: +Images are often represented as 3D lists. +The outer list is the entire image. +The 1st level inner list is a row of pixels. +The 2nd level inner list is the RGB values for that pixel. +RGB (red, green, blue) values determine the color of the pixel. The interesting thing is that we can iterate over images. -The challenge is, given an image, create a program that +The challenge is: given an image, create a program that will return a different image where each pixel is the average of the pixels surrounding it in the original image. -NOTE: The image is 3 dimensional because each pixel has RGB values. To find the average value of all of a pixels neighbors, you must -change the average of the red value to the red value, blue to blue, etc. +calculate the average of the red values, blue values, and green values. For example, if the neighbors of a pixel with value [1, 2, 3] were [20, 30, 40] and [10, 120, 30], the new pixel that would replace the -original one would be [15, 75, 35] +original one would be [15, 75, 35] (since the average of 20 and 10 is 15, +the average of 30 and 120 is 75, and the average of 40 and 30 is 35). -EXAMPLE: A image with,let's say 9 pixels may look like: -[[[31,41,42], [51,1,101], [24,141,33]], - [[50,21,28], [31,49,201], [90,54,33]], - [[12,81,3], [22,8,91], [101,141,132]]] +EXAMPLE: An image with 9 pixels may look like: +[ + [ + [31, 41, 42], [51, 1, 101], [24, 141, 33] + ], + + [ + [50, 21, 28], [31, 49, 201], [90, 54, 33] + ], + + [ + [12, 81, 3], [22, 8, 91], [101, 141, 132] + ] +] HINT: Don't forget that a pixel may have varying amount of neighboring pixels. A pixel at the edge, for example, has 3 neighboring pixels while -a pixel at the center of the image has 8 neighboring pixels. +a pixel at the center of the image has 8 neighboring pixels (one on each +of its 4 sides, and then one at each of its 4 corners). GIVEN: The code to grab an image from the internet and make it into an array is given to you. The code also displays the new image -you create in the end. These weren't taught in the main curriculum, so -it isn't expected for students to fully understand what the given code -does. +you create in the end. These weren't taught in the main curriculum, so +it isn't expected for students to fully understand what the given code +does. """ -# All the libraries imported +# Import libraries needed to run the program from PIL import Image import requests import numpy import matplotlib.pyplot as plt # Code that grabs the image from the internet and makes it into an array -url = "https://images.dog.ceo/breeds/waterdog-spanish/20180723_185544.jpg" -img = numpy.array(Image.open(requests.get(url, stream=True).raw)).tolist() -newimg = img +IMAGE_URL = "https://images.dog.ceo/breeds/waterdog-spanish/20180723_185544.jpg" +img = numpy.array(Image.open(requests.get(IMAGE_URL, stream=True).raw)).tolist() +newimg = img # the newimg starts as a copy of the original image transpose = numpy.transpose(img) +# Code that displays the original image plt.imshow(img) plt.show() # Write code to create newimg here - -# Code that displays the new image at the end. +# Code that displays the new image at the end plt.imshow(newimg) plt.show() diff --git a/2_intermediate/chapter10/solutions/img_avg.py b/2_intermediate/chapter10/solutions/img_avg.py index 5a4798da..53f18955 100644 --- a/2_intermediate/chapter10/solutions/img_avg.py +++ b/2_intermediate/chapter10/solutions/img_avg.py @@ -1,60 +1,71 @@ """ Image Average -Here is the challenge problem for 2D loops: -Images are often represented as 3D arrays, -where the rows and columns are the pixels in the image, -and each pixel has an RGB (red, green, blue) value -which determines the color of the pixel. +Here is the challenge problem for nested loops: +Images are often represented as 3D lists. +The outer list is the entire image. +The 1st level inner list is a row of pixels. +The 2nd level inner list is the RGB values for that pixel. +RGB (red, green, blue) values determine the color of the pixel. The interesting thing is that we can iterate over images. -The challenge is, given an image, create a program that +The challenge is: given an image, create a program that will return a different image where each pixel is the average of the pixels surrounding it in the original image. -NOTE: The image is 3 dimensional because each pixel has RGB values. To find the average value of all of a pixels neighbors, you must -change the average of the red value to the red value, blue to blue, etc. +calculate the average of the red values, blue values, and green values. For example, if the neighbors of a pixel with value [1, 2, 3] were [20, 30, 40] and [10, 120, 30], the new pixel that would replace the -original one would be [15, 75, 35] +original one would be [15, 75, 35] (since the average of 20 and 10 is 15, +the average of 30 and 120 is 75, and the average of 40 and 30 is 35). -EXAMPLE: A image with,let's say 9 pixels may look like: -[[[31,41,42], [51,1,101], [24,141,33]], - [[50,21,28], [31,49,201], [90,54,33]], - [[12,81,3], [22,8,91], [101,141,132]]] +EXAMPLE: An image with 9 pixels may look like: +[ + [ + [31, 41, 42], [51, 1, 101], [24, 141, 33] + ], + + [ + [50, 21, 28], [31, 49, 201], [90, 54, 33] + ], + + [ + [12, 81, 3], [22, 8, 91], [101, 141, 132] + ] +] HINT: Don't forget that a pixel may have varying amount of neighboring pixels. A pixel at the edge, for example, has 3 neighboring pixels while -a pixel at the center of the image has 8 neighboring pixels. +a pixel at the center of the image has 8 neighboring pixels (one on each +of its 4 sides, and then one at each of its 4 corners). GIVEN: The code to grab an image from the internet and make it into an array is given to you. The code also displays the new image -you create in the end. These weren't taught in the main curriculum, so -it isn't expected for students to fully understand what the given code -does. +you create in the end. These weren't taught in the main curriculum, so +it isn't expected for students to fully understand what the given code +does. """ -# All the libraries imported +# Import libraries needed to run the program from PIL import Image import requests import numpy import matplotlib.pyplot as plt # Code that grabs the image from the internet and makes it into an array -url = "https://images.dog.ceo/breeds/waterdog-spanish/20180723_185544.jpg" -img = numpy.array(Image.open(requests.get(url, stream=True).raw)).tolist() -newimg = img +IMAGE_URL = "https://images.dog.ceo/breeds/waterdog-spanish/20180723_185544.jpg" +img = numpy.array(Image.open(requests.get(IMAGE_URL, stream=True).raw)).tolist() +newimg = img # the newimg starts as a copy of the original image transpose = numpy.transpose(img) +# Code that displays the original image plt.imshow(img) plt.show() - -# Write code to create newimg here """ -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 """ @@ -79,9 +90,9 @@ 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] + r_avg = -img[i][j][0] + g_avg = -img[i][j][1] + b_avg = -img[i][j][2] c = -1 for x in x_n: @@ -97,7 +108,7 @@ newimg[i][j] = [r_avg, g_avg, b_avg] -# Code that displays the new image at the end. +# Code that displays the new image at the end plt.imshow(newimg) plt.show() From 5a5daa6671f546e3e1f055b39f22aa274a9a269c Mon Sep 17 00:00:00 2001 From: Lint Action Date: Fri, 7 Aug 2020 20:20:02 +0000 Subject: [PATCH 08/31] Fix code style issues with Black --- 2_intermediate/chapter10/practice/img_avg.py | 8 ++++++-- 2_intermediate/chapter10/solutions/img_avg.py | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/2_intermediate/chapter10/practice/img_avg.py b/2_intermediate/chapter10/practice/img_avg.py index f8d576dc..b65777f2 100644 --- a/2_intermediate/chapter10/practice/img_avg.py +++ b/2_intermediate/chapter10/practice/img_avg.py @@ -54,8 +54,12 @@ import matplotlib.pyplot as plt # Code that grabs the image from the internet and makes it into an array -IMAGE_URL = "https://images.dog.ceo/breeds/waterdog-spanish/20180723_185544.jpg" -img = numpy.array(Image.open(requests.get(IMAGE_URL, stream=True).raw)).tolist() +IMAGE_URL = ( + "https://images.dog.ceo/breeds/waterdog-spanish/20180723_185544.jpg" +) +img = numpy.array( + Image.open(requests.get(IMAGE_URL, stream=True).raw) +).tolist() newimg = img # the newimg starts as a copy of the original image transpose = numpy.transpose(img) diff --git a/2_intermediate/chapter10/solutions/img_avg.py b/2_intermediate/chapter10/solutions/img_avg.py index 53f18955..8839d863 100644 --- a/2_intermediate/chapter10/solutions/img_avg.py +++ b/2_intermediate/chapter10/solutions/img_avg.py @@ -54,8 +54,12 @@ import matplotlib.pyplot as plt # Code that grabs the image from the internet and makes it into an array -IMAGE_URL = "https://images.dog.ceo/breeds/waterdog-spanish/20180723_185544.jpg" -img = numpy.array(Image.open(requests.get(IMAGE_URL, stream=True).raw)).tolist() +IMAGE_URL = ( + "https://images.dog.ceo/breeds/waterdog-spanish/20180723_185544.jpg" +) +img = numpy.array( + Image.open(requests.get(IMAGE_URL, stream=True).raw) +).tolist() newimg = img # the newimg starts as a copy of the original image transpose = numpy.transpose(img) From ef8914b2652b66ec4308833388b1bbe1314db078 Mon Sep 17 00:00:00 2001 From: Rebecca Dang Date: Fri, 7 Aug 2020 13:36:54 -0700 Subject: [PATCH 09/31] Add push events to workflows --- .github/workflows/python-format.yml | 2 +- .github/workflows/python-lint.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python-format.yml b/.github/workflows/python-format.yml index 71331888..b074980b 100644 --- a/.github/workflows/python-format.yml +++ b/.github/workflows/python-format.yml @@ -3,7 +3,7 @@ # Autofixes problems if possible (it's a black formatter) name: Python (Lint Action) -on: pull_request +on: [pull_request, push] jobs: format-lint-python: diff --git a/.github/workflows/python-lint.yml b/.github/workflows/python-lint.yml index 308b56a8..049d2c33 100644 --- a/.github/workflows/python-lint.yml +++ b/.github/workflows/python-lint.yml @@ -2,7 +2,7 @@ # Submits code reviews based on flake8 output name: Python (Lintly) -on: pull_request +on: [pull_request, push] jobs: lint-python: From 4f8b1f45fbc0f882f644663665581727d220db9a Mon Sep 17 00:00:00 2001 From: Rebecca Dang Date: Fri, 7 Aug 2020 13:44:05 -0700 Subject: [PATCH 10/31] Ignore flake8 rule E203 (black is correct) Adding the comment noqa: [error code] ignores only that line for flake8. --- 1_beginner/chapter6/solutions/names.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1_beginner/chapter6/solutions/names.py b/1_beginner/chapter6/solutions/names.py index ef98419a..d77c971b 100644 --- a/1_beginner/chapter6/solutions/names.py +++ b/1_beginner/chapter6/solutions/names.py @@ -14,7 +14,7 @@ # Some other possible answers: # group = people[0, 5, 2] or people[0, len(people), 2] -group = people[0 : len(people) : 2] +group = people[0 : len(people) : 2] # noqa: E203 print(people) print(group) From bfe10d8d0dc3b1956c0b83e64b6d6eef53827d7b Mon Sep 17 00:00:00 2001 From: Rebecca Dang Date: Fri, 7 Aug 2020 13:46:17 -0700 Subject: [PATCH 11/31] Remove on push event Lintly only supports PRs --- .github/workflows/python-lint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/python-lint.yml b/.github/workflows/python-lint.yml index 049d2c33..308b56a8 100644 --- a/.github/workflows/python-lint.yml +++ b/.github/workflows/python-lint.yml @@ -2,7 +2,7 @@ # Submits code reviews based on flake8 output name: Python (Lintly) -on: [pull_request, push] +on: pull_request jobs: lint-python: From c10fa84ddcd73b054ebe555e05995567a2627557 Mon Sep 17 00:00:00 2001 From: Citrus716 <60357364+Citrus716@users.noreply.github.com> Date: Tue, 11 Aug 2020 15:29:13 -0400 Subject: [PATCH 12/31] Added how to install libraries I read that PIL is dead and hasn't been updated since 2009. To get "PIL" you must pip install pillow because that's the new name. However, when you import, the code is import PIL in order to allow for backward compatibility. Anyways, I made the instructions to install the libraries. --- 2_intermediate/chapter10/practice/img_avg.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/2_intermediate/chapter10/practice/img_avg.py b/2_intermediate/chapter10/practice/img_avg.py index b65777f2..d6e40bbd 100644 --- a/2_intermediate/chapter10/practice/img_avg.py +++ b/2_intermediate/chapter10/practice/img_avg.py @@ -48,6 +48,22 @@ """ # Import libraries needed to run the program +# Before importing the libraries, you must have them installed. +# Follow the following instructions to get all the libraries installed: +# -1. We first have to make sure pip is there. To check, run pip --version +# in the terminal. If a version appeared, then pip is there. If no +# version appears, update your Python to the latest version of 2 or 3. +# -2. In terminal, run pip install pillow. Wait for Successfully installed +# (something) to pop on the terminal. +# -3. In terminal, run pip install requests. Wait for Successfully installed +# (something) to pop on the terminal. +# -4. In terminal, run pip install numpy. Wait for Successfully installed +# (something) to pop on the terminal. +# -5. In terminal, run pip install matplotlib. Wait for Successfully +# installed (something) to pop on the terminal. +# -6. If all 2-5 all were successful, now you all the packages needed for +# this problem. + from PIL import Image import requests import numpy From 56415f6bb5691db057f6c003a7c1a436a9cc8ce0 Mon Sep 17 00:00:00 2001 From: Lint Action Date: Tue, 11 Aug 2020 19:29:35 +0000 Subject: [PATCH 13/31] Fix code style issues with Black --- 2_intermediate/chapter10/practice/img_avg.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/2_intermediate/chapter10/practice/img_avg.py b/2_intermediate/chapter10/practice/img_avg.py index d6e40bbd..d6c40b26 100644 --- a/2_intermediate/chapter10/practice/img_avg.py +++ b/2_intermediate/chapter10/practice/img_avg.py @@ -51,7 +51,7 @@ # Before importing the libraries, you must have them installed. # Follow the following instructions to get all the libraries installed: # -1. We first have to make sure pip is there. To check, run pip --version -# in the terminal. If a version appeared, then pip is there. If no +# in the terminal. If a version appeared, then pip is there. If no # version appears, update your Python to the latest version of 2 or 3. # -2. In terminal, run pip install pillow. Wait for Successfully installed # (something) to pop on the terminal. @@ -59,9 +59,9 @@ # (something) to pop on the terminal. # -4. In terminal, run pip install numpy. Wait for Successfully installed # (something) to pop on the terminal. -# -5. In terminal, run pip install matplotlib. Wait for Successfully +# -5. In terminal, run pip install matplotlib. Wait for Successfully # installed (something) to pop on the terminal. -# -6. If all 2-5 all were successful, now you all the packages needed for +# -6. If all 2-5 all were successful, now you all the packages needed for # this problem. from PIL import Image From 62a2930ec8b8abed19e6190798a1abc9f0e9c25c Mon Sep 17 00:00:00 2001 From: Citrus716 <60357364+Citrus716@users.noreply.github.com> Date: Tue, 11 Aug 2020 15:36:59 -0400 Subject: [PATCH 14/31] Added instruction to installing libraries in solution Copy pasted from my instructions I added in practice. --- 2_intermediate/chapter10/solutions/img_avg.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/2_intermediate/chapter10/solutions/img_avg.py b/2_intermediate/chapter10/solutions/img_avg.py index 8839d863..df38eaf8 100644 --- a/2_intermediate/chapter10/solutions/img_avg.py +++ b/2_intermediate/chapter10/solutions/img_avg.py @@ -48,6 +48,22 @@ """ # Import libraries needed to run the program +# Before importing the libraries, you must have them installed. +# Follow the following instructions to get all the libraries installed: +# -1. We first have to make sure pip is there. To check, run pip --version +# in the terminal. If a version appeared, then pip is there. If no +# version appears, update your Python to the latest version of 2 or 3. +# -2. In terminal, run pip install pillow. Wait for Successfully installed +# (something) to pop on the terminal. +# -3. In terminal, run pip install requests. Wait for Successfully installed +# (something) to pop on the terminal. +# -4. In terminal, run pip install numpy. Wait for Successfully installed +# (something) to pop on the terminal. +# -5. In terminal, run pip install matplotlib. Wait for Successfully +# installed (something) to pop on the terminal. +# -6. If all 2-5 all were successful, now you all the packages needed for +# this problem. + from PIL import Image import requests import numpy From eabe3ac4f06502c170e0174a7c358d537a7514f7 Mon Sep 17 00:00:00 2001 From: Citrus716 <60357364+Citrus716@users.noreply.github.com> Date: Sat, 15 Aug 2020 14:55:41 -0400 Subject: [PATCH 15/31] Swapped i and j descriptions and added more. Swapped i and j descriptions and added more. The i represents top to bottom(height) while the j represents left to right(width). --- 2_intermediate/chapter10/solutions/img_avg.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/2_intermediate/chapter10/solutions/img_avg.py b/2_intermediate/chapter10/solutions/img_avg.py index df38eaf8..6948ac14 100644 --- a/2_intermediate/chapter10/solutions/img_avg.py +++ b/2_intermediate/chapter10/solutions/img_avg.py @@ -84,10 +84,14 @@ plt.show() """ -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 +The double for loop gets all combination of indexes necessary + to access all the pixels inside the list. +i is the index determining which inner + list to get. Using the Example as the visual, this + index goes from top to bottom. +j is the index determining which list inside + inner list to get. Using the Example as the visual, this + index goes from left to right. """ for i in range(len(img)): for j in range(len(img[0])): From 1826aecdbbb3851850c08102d8f05bd01b6c67f7 Mon Sep 17 00:00:00 2001 From: Lint Action Date: Sat, 14 Nov 2020 18:24:53 +0000 Subject: [PATCH 16/31] Fix code style issues with Black --- 3_advanced/chapter14/practice/update_score.py | 8 ++++---- 3_advanced/chapter14/solutions/update_score.py | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/3_advanced/chapter14/practice/update_score.py b/3_advanced/chapter14/practice/update_score.py index 73447bae..4b666dde 100644 --- a/3_advanced/chapter14/practice/update_score.py +++ b/3_advanced/chapter14/practice/update_score.py @@ -1,15 +1,15 @@ # Problem name: update_score -# A hacker named Dan wishes to hack into a competition where +# A hacker named Dan wishes to hack into a competition where # they judge participants in three categories on a scale of 10. # Dan wants his friend Bob to win. -# Bob will only win if he has all 10s while the +# Bob will only win if he has all 10s while the # other competitors, Jo and Stan, don’t. # Judges will store the scoring within a tuple ([...], [...], [...]). # The scores before Dan hacked are given. -# Bob will be located as the first person the judges scored and will +# Bob will be located as the first person the judges scored and will # have the lowest points out of any participant. - + # Create a program to help Dan help Bob win. # Also, print Bob’s score at the end. # Use tuple unpacking to solve this problem. diff --git a/3_advanced/chapter14/solutions/update_score.py b/3_advanced/chapter14/solutions/update_score.py index f0e29743..76e6ade5 100644 --- a/3_advanced/chapter14/solutions/update_score.py +++ b/3_advanced/chapter14/solutions/update_score.py @@ -1,13 +1,13 @@ # Problem name: update_score -# A hacker named Dan wishes to hack into a competition where +# A hacker named Dan wishes to hack into a competition where # they judge participants in three categories on a scale of 10. # Dan wants his friend Bob to win. -# Bob will only win if he has all 10s while the +# Bob will only win if he has all 10s while the # other competitors, Jo and Stan, don’t. # Judges will store the scoring within a tuple ([...], [...], [...]). # The scores before Dan hacked are given. -# Bob will be located as the first person the judges scored and will +# Bob will be located as the first person the judges scored and will # have the lowest points out of any participant. # Create a program to help Dan help Bob win. From 0d7e11ca16633c9c9b85e373c1d0a56aabaf9911 Mon Sep 17 00:00:00 2001 From: Lint Action Date: Sat, 21 Nov 2020 21:36:44 +0000 Subject: [PATCH 17/31] Fix code style issues with Black --- .../chapter16/practice/ch16_practice1.py | 6 ++---- .../chapter16/practice/ch16_practice2.py | 6 +++--- .../chapter16/solutions/ch16_practice1.py | 15 +++++++-------- .../chapter16/solutions/ch16_practice2.py | 19 +++++++++---------- 4 files changed, 21 insertions(+), 25 deletions(-) diff --git a/3_advanced/chapter16/practice/ch16_practice1.py b/3_advanced/chapter16/practice/ch16_practice1.py index 4001e575..4a4988f4 100644 --- a/3_advanced/chapter16/practice/ch16_practice1.py +++ b/3_advanced/chapter16/practice/ch16_practice1.py @@ -6,11 +6,9 @@ At the end, put the total running time of code. """ -#ex_list = [?,?,?...] #Input,O(1) -num_even = 0 #O(1) +# ex_list = [?,?,?...] #Input,O(1) +num_even = 0 # O(1) for num in ex_list: if num % 2 == 0: num_even += 1 print(num_even) - - diff --git a/3_advanced/chapter16/practice/ch16_practice2.py b/3_advanced/chapter16/practice/ch16_practice2.py index 10b0a949..871d2465 100644 --- a/3_advanced/chapter16/practice/ch16_practice2.py +++ b/3_advanced/chapter16/practice/ch16_practice2.py @@ -6,9 +6,9 @@ At the end, put the total running time of code. """ -#ex_list = [?,?,?,...]#Input,O(1) -for i in range(2):#O(1) - ex_list.insert(0,1) +# ex_list = [?,?,?,...]#Input,O(1) +for i in range(2): # O(1) + ex_list.insert(0, 1) ex_list.append(1) for number in ex_list: for number in ex_list: diff --git a/3_advanced/chapter16/solutions/ch16_practice1.py b/3_advanced/chapter16/solutions/ch16_practice1.py index 39bfd25a..c2f0fb4f 100644 --- a/3_advanced/chapter16/solutions/ch16_practice1.py +++ b/3_advanced/chapter16/solutions/ch16_practice1.py @@ -6,11 +6,10 @@ At the end, put the total running time of code. """ -#ex_list = [?,?,?...] #Input,O(1) -num_even = 0 #O(1) -for num in ex_list: #O(n) - if num % 2 == 0: #O(1) - num_even += 1 #O(1) -print(num_even) #O(1) -#Total running time = O(n) - +# ex_list = [?,?,?...] #Input,O(1) +num_even = 0 # O(1) +for num in ex_list: # O(n) + if num % 2 == 0: # O(1) + num_even += 1 # O(1) +print(num_even) # O(1) +# Total running time = O(n) diff --git a/3_advanced/chapter16/solutions/ch16_practice2.py b/3_advanced/chapter16/solutions/ch16_practice2.py index 1cc59fe9..dd44b541 100644 --- a/3_advanced/chapter16/solutions/ch16_practice2.py +++ b/3_advanced/chapter16/solutions/ch16_practice2.py @@ -6,13 +6,12 @@ At the end, put the total running time of code. """ -#ex_list = [?,?,?,...]#Input,O(1) -for i in range(2):#O(1) - ex_list.insert(0,1)#O(n) - ex_list.append(1)#O(1) -for number in ex_list:#O(1) - for number in ex_list:#O(1) - break#O(1) - break#O(1) -#Total running time = O(n) - +# ex_list = [?,?,?,...]#Input,O(1) +for i in range(2): # O(1) + ex_list.insert(0, 1) # O(n) + ex_list.append(1) # O(1) +for number in ex_list: # O(1) + for number in ex_list: # O(1) + break # O(1) + break # O(1) +# Total running time = O(n) From 5bc41347560d5d4b08369bc23245cfee228fbe3d Mon Sep 17 00:00:00 2001 From: Ben <71541167+BenVN123@users.noreply.github.com> Date: Thu, 27 May 2021 19:36:01 -0700 Subject: [PATCH 18/31] Delete this file because it no longer exists Because this file no longer exists in the Python repo, it is throwing an error for the pull request. --- .../chapter16/solutions/ch16_practice2.py | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 3_advanced/chapter16/solutions/ch16_practice2.py diff --git a/3_advanced/chapter16/solutions/ch16_practice2.py b/3_advanced/chapter16/solutions/ch16_practice2.py deleted file mode 100644 index dd44b541..00000000 --- a/3_advanced/chapter16/solutions/ch16_practice2.py +++ /dev/null @@ -1,17 +0,0 @@ -""" -The following code is not meant to be run because -there's no input. Instead, analyze it's running time -in terms of Big-O. The first two lines are already -analyzed for you. Do the same for all the other lines. -At the end, put the total running time of code. -""" - -# ex_list = [?,?,?,...]#Input,O(1) -for i in range(2): # O(1) - ex_list.insert(0, 1) # O(n) - ex_list.append(1) # O(1) -for number in ex_list: # O(1) - for number in ex_list: # O(1) - break # O(1) - break # O(1) -# Total running time = O(n) From 11b478a1b4fcfe08e6108f8c211dcaf5c0280f2c Mon Sep 17 00:00:00 2001 From: Ben <71541167+BenVN123@users.noreply.github.com> Date: Thu, 27 May 2021 19:41:07 -0700 Subject: [PATCH 19/31] Delete this because the file no longer exists --- 3_advanced/chapter16/solutions/ch16_practice1.py | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 3_advanced/chapter16/solutions/ch16_practice1.py diff --git a/3_advanced/chapter16/solutions/ch16_practice1.py b/3_advanced/chapter16/solutions/ch16_practice1.py deleted file mode 100644 index c2f0fb4f..00000000 --- a/3_advanced/chapter16/solutions/ch16_practice1.py +++ /dev/null @@ -1,15 +0,0 @@ -""" -The following code is not meant to be run because -there's no input. Instead, analyze it's running time -in terms of Big-O. The first two lines are already -analyzed for you. Do the same for all the other lines. -At the end, put the total running time of code. -""" - -# ex_list = [?,?,?...] #Input,O(1) -num_even = 0 # O(1) -for num in ex_list: # O(n) - if num % 2 == 0: # O(1) - num_even += 1 # O(1) -print(num_even) # O(1) -# Total running time = O(n) From 2ce575b36330b92212b2f900e5e2bd63d600d360 Mon Sep 17 00:00:00 2001 From: Ben <71541167+BenVN123@users.noreply.github.com> Date: Thu, 27 May 2021 19:43:53 -0700 Subject: [PATCH 20/31] Delete this because the file no longer exists --- 3_advanced/chapter16/practice/ch16_practice2.py | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 3_advanced/chapter16/practice/ch16_practice2.py diff --git a/3_advanced/chapter16/practice/ch16_practice2.py b/3_advanced/chapter16/practice/ch16_practice2.py deleted file mode 100644 index 871d2465..00000000 --- a/3_advanced/chapter16/practice/ch16_practice2.py +++ /dev/null @@ -1,16 +0,0 @@ -""" -The following code is not meant to be run because -there's no input. Instead, analyze it's running time -in terms of Big-O. The first two lines are already -analyzed for you. Do the same for all the other lines. -At the end, put the total running time of code. -""" - -# ex_list = [?,?,?,...]#Input,O(1) -for i in range(2): # O(1) - ex_list.insert(0, 1) - ex_list.append(1) -for number in ex_list: - for number in ex_list: - break - break From 9694652551da0ccc610df6090c71ef5799b98367 Mon Sep 17 00:00:00 2001 From: Ben <71541167+BenVN123@users.noreply.github.com> Date: Thu, 27 May 2021 19:45:19 -0700 Subject: [PATCH 21/31] Delete this because the file no longer exists **Changes** - Because this file no longer exists in the Python repo, it is throwing an error for the pull request. - This file is currently in chapter 15, not 16 --- 3_advanced/chapter16/practice/ch16_practice1.py | 14 -------------- 1 file changed, 14 deletions(-) delete mode 100644 3_advanced/chapter16/practice/ch16_practice1.py diff --git a/3_advanced/chapter16/practice/ch16_practice1.py b/3_advanced/chapter16/practice/ch16_practice1.py deleted file mode 100644 index 4a4988f4..00000000 --- a/3_advanced/chapter16/practice/ch16_practice1.py +++ /dev/null @@ -1,14 +0,0 @@ -""" -The following code is not meant to be run because -there's no input. Instead, analyze it's running time -in terms of Big-O. The first two lines are already -analyzed for you. Do the same for all the other lines. -At the end, put the total running time of code. -""" - -# ex_list = [?,?,?...] #Input,O(1) -num_even = 0 # O(1) -for num in ex_list: - if num % 2 == 0: - num_even += 1 -print(num_even) From 2901452d6acecda93c6d20736426ab62701a53d5 Mon Sep 17 00:00:00 2001 From: Ben <71541167+BenVN123@users.noreply.github.com> Date: Thu, 27 May 2021 21:12:36 -0700 Subject: [PATCH 22/31] Fix styling error --- 2_intermediate/chapter10/solutions/img_avg.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/2_intermediate/chapter10/solutions/img_avg.py b/2_intermediate/chapter10/solutions/img_avg.py index 6948ac14..b0e8737e 100644 --- a/2_intermediate/chapter10/solutions/img_avg.py +++ b/2_intermediate/chapter10/solutions/img_avg.py @@ -89,8 +89,8 @@ i is the index determining which inner list to get. Using the Example as the visual, this index goes from top to bottom. -j is the index determining which list inside - inner list to get. Using the Example as the visual, this +j is the index determining which list inside + inner list to get. Using the Example as the visual, this index goes from left to right. """ for i in range(len(img)): From a3c604a4b0a38f35ed0d26f6e18c977fd708ed99 Mon Sep 17 00:00:00 2001 From: chrehall68 Date: Mon, 9 Aug 2021 10:25:05 -0700 Subject: [PATCH 23/31] Fix it so that it works --- 2_intermediate/chapter10/practice/img_avg.py | 40 ++--- 2_intermediate/chapter10/solutions/img_avg.py | 158 ++++++++++-------- 2 files changed, 101 insertions(+), 97 deletions(-) diff --git a/2_intermediate/chapter10/practice/img_avg.py b/2_intermediate/chapter10/practice/img_avg.py index d6c40b26..f68f2c51 100644 --- a/2_intermediate/chapter10/practice/img_avg.py +++ b/2_intermediate/chapter10/practice/img_avg.py @@ -39,30 +39,18 @@ pixels. A pixel at the edge, for example, has 3 neighboring pixels while a pixel at the center of the image has 8 neighboring pixels (one on each of its 4 sides, and then one at each of its 4 corners). - -GIVEN: The code to grab an image from the internet and make it -into an array is given to you. The code also displays the new image -you create in the end. These weren't taught in the main curriculum, so -it isn't expected for students to fully understand what the given code -does. """ # Import libraries needed to run the program # Before importing the libraries, you must have them installed. -# Follow the following instructions to get all the libraries installed: -# -1. We first have to make sure pip is there. To check, run pip --version -# in the terminal. If a version appeared, then pip is there. If no -# version appears, update your Python to the latest version of 2 or 3. -# -2. In terminal, run pip install pillow. Wait for Successfully installed -# (something) to pop on the terminal. -# -3. In terminal, run pip install requests. Wait for Successfully installed -# (something) to pop on the terminal. -# -4. In terminal, run pip install numpy. Wait for Successfully installed -# (something) to pop on the terminal. -# -5. In terminal, run pip install matplotlib. Wait for Successfully -# installed (something) to pop on the terminal. -# -6. If all 2-5 all were successful, now you all the packages needed for -# this problem. +# This problem requires the following libraries: +# pillow, requests, numpy, and matplotlib +# If you don't already have them installed, open your command prompt or terminal +# and please do +# this: pip install -U (library) (any other libraries, each separated by a space) +# ex: pip install -U numpy matplotlib requests pillow +# Note: on some windows machines, you may need to +# do: py -m pip install -U (library) (any other libraries, each separated by a space) from PIL import Image import requests @@ -76,18 +64,20 @@ img = numpy.array( Image.open(requests.get(IMAGE_URL, stream=True).raw) ).tolist() -newimg = img # the newimg starts as a copy of the original image -transpose = numpy.transpose(img) + +# create newimg as an empty list so that we'll know if something went wrong +# ie. if we try to display it and the function didn't run, we'd get an +# invalid shape error +newimg = [[[] for column in row] for row in img] # Code that displays the original image +print("now displaying the original image") plt.imshow(img) plt.show() # Write code to create newimg here # Code that displays the new image at the end +print("now displaying the new image") plt.imshow(newimg) plt.show() - -plt.imshow(transpose) -plt.show() diff --git a/2_intermediate/chapter10/solutions/img_avg.py b/2_intermediate/chapter10/solutions/img_avg.py index b0e8737e..9bed93b1 100644 --- a/2_intermediate/chapter10/solutions/img_avg.py +++ b/2_intermediate/chapter10/solutions/img_avg.py @@ -39,30 +39,19 @@ pixels. A pixel at the edge, for example, has 3 neighboring pixels while a pixel at the center of the image has 8 neighboring pixels (one on each of its 4 sides, and then one at each of its 4 corners). - -GIVEN: The code to grab an image from the internet and make it -into an array is given to you. The code also displays the new image -you create in the end. These weren't taught in the main curriculum, so -it isn't expected for students to fully understand what the given code -does. """ # Import libraries needed to run the program # Before importing the libraries, you must have them installed. -# Follow the following instructions to get all the libraries installed: -# -1. We first have to make sure pip is there. To check, run pip --version -# in the terminal. If a version appeared, then pip is there. If no -# version appears, update your Python to the latest version of 2 or 3. -# -2. In terminal, run pip install pillow. Wait for Successfully installed -# (something) to pop on the terminal. -# -3. In terminal, run pip install requests. Wait for Successfully installed -# (something) to pop on the terminal. -# -4. In terminal, run pip install numpy. Wait for Successfully installed -# (something) to pop on the terminal. -# -5. In terminal, run pip install matplotlib. Wait for Successfully -# installed (something) to pop on the terminal. -# -6. If all 2-5 all were successful, now you all the packages needed for -# this problem. +# This problem requires the following libraries: +# pillow, requests, numpy, and matplotlib +# If you don't already have them installed, open your command prompt or terminal +# and please do +# this: pip install -U (library) (any other libraries, each separated by a space) +# ex: pip install -U numpy matplotlib requests pillow +# Note: on some windows machines, you may need to +# do: py -m pip install -U (library) (any other libraries, each separated by a space) + from PIL import Image import requests @@ -76,65 +65,90 @@ img = numpy.array( Image.open(requests.get(IMAGE_URL, stream=True).raw) ).tolist() -newimg = img # the newimg starts as a copy of the original image -transpose = numpy.transpose(img) + +# create newimg as an empty list so that we'll know if something went wrong +# ie. if we try to display it and the function didn't run, we'd get an +# invalid shape error +newimg = [[[] for column in row] for row in img] # Code that displays the original image +print("now displaying the original image") plt.imshow(img) plt.show() -""" -The double for loop gets all combination of indexes necessary - to access all the pixels inside the list. -i is the index determining which inner - list to get. Using the Example as the visual, this - index goes from top to bottom. -j is the index determining which list inside - inner list to get. Using the Example as the visual, this - index goes from left to right. -""" -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 = -img[i][j][0] - g_avg = -img[i][j][1] - b_avg = -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] +def distort(original_image, new_image): + """ + Modifies new_image so that each pixel in new_image + will be the average of the surrounding + DISTORTION_RADIUS pixels. + DISTORTION_RADIUS can be changed for more/less distortion. + Arguments: + original_image (list or tuple) - the reference image. + new_image (list) - the image to modify. + """ + DISTORTION_RADIUS = 1 + + for row in range(len(original_image)): + for column in range(len(original_image[0])): + x_relative_indexes = [] + y_relative_indexes = [0] + + # handle y relative indexes + # +1 to DISTORTION_RADIUS because stop is exclusive + for relative_y in range(-DISTORTION_RADIUS, DISTORTION_RADIUS + 1): + if ( + row + relative_y < 0 + or row + relative_y > len(original_image) - 1 + ): + # ignore relative indexes that are out of range of the original image + continue + # if it isn't out of range, it's valid and should be appended + y_relative_indexes.append(relative_y) + + # handle x relative indexes + # +1 to DISTORTION_RADIUS because stop is exclusive + for relative_x in range(-DISTORTION_RADIUS, DISTORTION_RADIUS + 1): + if ( + column + relative_x < 0 + or column + relative_x > len(original_image[0]) - 1 + ): + # ignore relative indexes that are out of range of the original image + continue + # if it isn't out of range, it's valid and should be appended + x_relative_indexes.append(relative_x) + + # at this point, x_relative_indexes and y_relative_indexes are + # complete, so now we use them. + r_total = g_total = b_total = counter = 0 # initialize variables + for x in x_relative_indexes: + for y in y_relative_indexes: + # since images are 'rgb': + # red is the first val + r_total += original_image[row + y][column + x][0] + + # green is the second val + g_total += original_image[row + y][column + x][1] + + # blue is third val + b_total += original_image[row + y][column + x][2] + + counter += 1 + + # round because images don't deal w/ floats, only integers + r_avg = round(r_total / counter) + g_avg = round(g_total / counter) + b_avg = round(b_total / counter) + + # update the pixel in newimg to match the average of its + # surrounding pixels + new_image[row][column] = [r_avg, g_avg, b_avg] + + +print("now modifying file. Depending on your pc, this may take a while.") +distort(img, newimg) # Code that displays the new image at the end +print("now displaying the new image") plt.imshow(newimg) plt.show() - -plt.imshow(transpose) -plt.show() From e65f5097f62037bbd59f9ab6e5ac8e10ed6ee865 Mon Sep 17 00:00:00 2001 From: Lint Action Date: Mon, 9 Aug 2021 17:25:38 +0000 Subject: [PATCH 24/31] Fix code style issues with Black --- 2_intermediate/chapter10/practice/img_avg.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/2_intermediate/chapter10/practice/img_avg.py b/2_intermediate/chapter10/practice/img_avg.py index f68f2c51..2b71c3b3 100644 --- a/2_intermediate/chapter10/practice/img_avg.py +++ b/2_intermediate/chapter10/practice/img_avg.py @@ -46,10 +46,10 @@ # This problem requires the following libraries: # pillow, requests, numpy, and matplotlib # If you don't already have them installed, open your command prompt or terminal -# and please do +# and please do # this: pip install -U (library) (any other libraries, each separated by a space) # ex: pip install -U numpy matplotlib requests pillow -# Note: on some windows machines, you may need to +# Note: on some windows machines, you may need to # do: py -m pip install -U (library) (any other libraries, each separated by a space) from PIL import Image @@ -66,7 +66,7 @@ ).tolist() # create newimg as an empty list so that we'll know if something went wrong -# ie. if we try to display it and the function didn't run, we'd get an +# ie. if we try to display it and the function didn't run, we'd get an # invalid shape error newimg = [[[] for column in row] for row in img] From 81c198b15115e8d1003daf37f5c343f8a7983f4b Mon Sep 17 00:00:00 2001 From: chrehall68 Date: Mon, 9 Aug 2021 10:29:33 -0700 Subject: [PATCH 25/31] Fix comment so that it's w/i line length limit --- 2_intermediate/chapter10/solutions/img_avg.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/2_intermediate/chapter10/solutions/img_avg.py b/2_intermediate/chapter10/solutions/img_avg.py index 9bed93b1..35a93598 100644 --- a/2_intermediate/chapter10/solutions/img_avg.py +++ b/2_intermediate/chapter10/solutions/img_avg.py @@ -101,7 +101,8 @@ def distort(original_image, new_image): row + relative_y < 0 or row + relative_y > len(original_image) - 1 ): - # ignore relative indexes that are out of range of the original image + # ignore relative indexes that are out of range of the + # original image continue # if it isn't out of range, it's valid and should be appended y_relative_indexes.append(relative_y) @@ -113,7 +114,8 @@ def distort(original_image, new_image): column + relative_x < 0 or column + relative_x > len(original_image[0]) - 1 ): - # ignore relative indexes that are out of range of the original image + # ignore relative indexes that are out of range of the + # original image continue # if it isn't out of range, it's valid and should be appended x_relative_indexes.append(relative_x) From 233c2d5d13db876a0bbca591555aff49ad2c3df7 Mon Sep 17 00:00:00 2001 From: chrehall68 Date: Mon, 9 Aug 2021 10:40:58 -0700 Subject: [PATCH 26/31] Add comments, fix small bug The bug was that the averages were slightly off because I forgot to set y_relative_indexes to an empty list (which I now did, which is why it's now fixed) --- 2_intermediate/chapter10/solutions/img_avg.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/2_intermediate/chapter10/solutions/img_avg.py b/2_intermediate/chapter10/solutions/img_avg.py index 35a93598..66077ed6 100644 --- a/2_intermediate/chapter10/solutions/img_avg.py +++ b/2_intermediate/chapter10/solutions/img_avg.py @@ -87,12 +87,18 @@ def distort(original_image, new_image): original_image (list or tuple) - the reference image. new_image (list) - the image to modify. """ - DISTORTION_RADIUS = 1 + DISTORTION_RADIUS = 1 # this should be a positive integer + # Note that each increase of DISTORTION_RADIUS increases + # run time amazingly. Slower PC's should stick to values like + # 1 or 2 for DISTORTION_RADIUS for row in range(len(original_image)): for column in range(len(original_image[0])): + # we set these to empty lists because the for loops + # will iterate through all valid relative indexes + # (including 0) and append them to these lists. x_relative_indexes = [] - y_relative_indexes = [0] + y_relative_indexes = [] # handle y relative indexes # +1 to DISTORTION_RADIUS because stop is exclusive From 5ef9329b52aca8b9b681b6d38d41bca387124dc3 Mon Sep 17 00:00:00 2001 From: chrehall68 Date: Mon, 9 Aug 2021 10:44:05 -0700 Subject: [PATCH 27/31] Update workflows to match what it is in main branch --- .github/workflows/python-format.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python-format.yml b/.github/workflows/python-format.yml index 827badd1..0eac2193 100644 --- a/.github/workflows/python-format.yml +++ b/.github/workflows/python-format.yml @@ -3,7 +3,7 @@ # Autofixes problems if possible (it's a black formatter) name: Python (Lint Action) -on: [pull_request, push] +on: pull_request jobs: format-lint-python: @@ -30,4 +30,4 @@ jobs: black_args: "--line-length 79 --exclude='1_beginner/chapter1/examples/error.py'" # same max line length as flake8 flake8_args: "--max-line-length=88 --ignore=E203,W503 --exclude=1_beginner/chapter1/examples/error.py" # prevent conflicts with black auto_fix: true # auto commit style fixes - + \ No newline at end of file From ca6b173f1ef6c5feda673b487a6a4b6e2832121b Mon Sep 17 00:00:00 2001 From: chrehall68 Date: Mon, 9 Aug 2021 10:46:22 -0700 Subject: [PATCH 28/31] Update names.py to match what it is in main branch --- 1_beginner/chapter6/solutions/names.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1_beginner/chapter6/solutions/names.py b/1_beginner/chapter6/solutions/names.py index d77c971b..ef98419a 100644 --- a/1_beginner/chapter6/solutions/names.py +++ b/1_beginner/chapter6/solutions/names.py @@ -14,7 +14,7 @@ # Some other possible answers: # group = people[0, 5, 2] or people[0, len(people), 2] -group = people[0 : len(people) : 2] # noqa: E203 +group = people[0 : len(people) : 2] print(people) print(group) From 88c72e7b6e0bcd24adb7fe8fe795a42b7551e5c9 Mon Sep 17 00:00:00 2001 From: Ben Nguyen <71541167+BenVN123@users.noreply.github.com> Date: Mon, 9 Aug 2021 11:26:42 -0700 Subject: [PATCH 29/31] Fix comment --- 1_beginner/chapter5/practice/alternating.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1_beginner/chapter5/practice/alternating.py b/1_beginner/chapter5/practice/alternating.py index 7c59aa5a..d0610795 100644 --- a/1_beginner/chapter5/practice/alternating.py +++ b/1_beginner/chapter5/practice/alternating.py @@ -1,6 +1,6 @@ """ Alternating -Ask the user for an integer. The print the numbers from 1 +Ask the user for an integer. Then print the numbers from 1 to that number, but alternating in sign. For example, if the input was 5, what would be printed is 1, -1, 2, -2, 3, -3, 4, -4, 5. (Note, DO NOT include the last negative number). From a91d7f68ec31186d5a264e51cde952d601307d09 Mon Sep 17 00:00:00 2001 From: Ben Nguyen <71541167+BenVN123@users.noreply.github.com> Date: Mon, 9 Aug 2021 11:29:16 -0700 Subject: [PATCH 30/31] Edit comment --- 1_beginner/chapter5/solutions/alternating.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1_beginner/chapter5/solutions/alternating.py b/1_beginner/chapter5/solutions/alternating.py index b4b9ccc8..de8f197d 100644 --- a/1_beginner/chapter5/solutions/alternating.py +++ b/1_beginner/chapter5/solutions/alternating.py @@ -1,6 +1,6 @@ """ Alternating -Ask the user for an integer. The print the numbers from 1 +Ask the user for a positive integer. Then print the numbers from 1 to that number, but alternating in sign. For example, if the input was 5, what would be printed is 1, -1, 2, -2, 3, -3, 4, -4, 5. (Note, DO NOT include the last negative number). From f4c6905c3f53ea6b47e1005cc1fdd96b2c8f5441 Mon Sep 17 00:00:00 2001 From: Ben Nguyen <71541167+BenVN123@users.noreply.github.com> Date: Mon, 9 Aug 2021 11:29:31 -0700 Subject: [PATCH 31/31] Update alternating.py --- 1_beginner/chapter5/practice/alternating.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1_beginner/chapter5/practice/alternating.py b/1_beginner/chapter5/practice/alternating.py index d0610795..0fe44abc 100644 --- a/1_beginner/chapter5/practice/alternating.py +++ b/1_beginner/chapter5/practice/alternating.py @@ -1,6 +1,6 @@ """ Alternating -Ask the user for an integer. Then print the numbers from 1 +Ask the user for a positive integer. Then print the numbers from 1 to that number, but alternating in sign. For example, if the input was 5, what would be printed is 1, -1, 2, -2, 3, -3, 4, -4, 5. (Note, DO NOT include the last negative number).