Skip to content

Commit 80aaf41

Browse files
authored
Merge branch 'master' into neha-add-exercises
2 parents e748656 + 74b81ec commit 80aaf41

File tree

7 files changed

+98
-25
lines changed

7 files changed

+98
-25
lines changed

1_beginner/chapter5/practice/alternating.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
"""
1+
"""
2+
Alternating
23
34
Ask the user for an integer. The print the numbers from 1 to that number,
45
but alternating in sign. For example, if the input was 5, what would be printed
56
is 1, -1, 2, -2, 3, -3, 4, -4, 5. (Note, DO NOT include the last negative
67
number).
78
89
Do this with a for loop
9-
1010
"""
1111

1212
# Write code here.

2_intermediate/chapter10/practice/img_avg.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
1-
""" Here is the challenge problem for 2d loops>
2-
Images are often represented as 3d arrays,
1+
"""
2+
Image Average
3+
4+
Here is the challenge problem for 2D loops:
5+
Images are often represented as 3D arrays,
36
where the rows and columns are the pixels in the image,
4-
and each pixel has an r, g, and b value.
7+
and each pixel has an RGB (red, green, blue) value
8+
which determines the color of the pixel.
59
610
The interesting thing is that we can iterate over images.
711
The challenge is, given an image, create a program that
812
will return a different image where each pixel is the average
913
of the pixels surrounding it in the original image.
1014
11-
The neighbors of an image are all the pixels that surroun it,
12-
1 on each side, and 4 on the diagonals, for 8 in total.
13-
Each pixel doesn't necessarily have 8 neighbors, though (think about why)
15+
The neighbors of an image are all the pixels that surround it,
16+
1 on each side, and 4 on the diagonals, for 8 in total. Each
17+
pixel doesn't necessarily have 8 neighbors, though (think about why).
1418
1519
The code to grab an image from the internet and make it
1620
into an array is given to you. The code also displays the new image
1721
you create in the end.
1822
19-
NOTE: The image is 3 dimensional because each pixel has rgb values.
23+
NOTE: The image is 3 dimensional because each pixel has RGB values.
2024
To find the average value of all of a pixels neighbors, you must
2125
change the average of the red value to the red value, blue to blue, etc.
22-
For example, if the neighbors of a pixel with value [1,2,3]
23-
were [20,30,40] and [10,120,30], the new pixel that would replace the original one would be
24-
[15,75,35]
26+
For example, if the neighbors of a pixel with value [1, 2, 3]
27+
were [20, 30, 40] and [10, 120, 30], the new pixel that would replace the
28+
original one would be [15, 75, 35]
2529
"""
2630

2731
from PIL import Image
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Odd Sum
2+
# Given a 2D list, find the sum of all elements at odd indexes for all
3+
# the lists at odd indexes. Print this sum times the sum of all
4+
# first element of all the 1D lists in the 2D list.
5+
#
6+
# Ex:[[1,2,3,6],[2,41,2,1]]should have print 42 after the program runs.
7+
#
8+
# Write the code below.
9+
10+
two_d_list = [[1, 2, 3, 5, 2], [2, 3, 1, 4], [2, 3, 1, 2, 21], [21, 3, 1, 41]]
11+
# two_d_list should print 51 after the program runs.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Given a 2D list, let's call a element "smooth" if index of the
2+
# element in its 1D list plus the element is even. For example,
3+
# given the 2D list [[0,4][2,6]], the 1st element of each of the
4+
# 1D list is considered "smooth" because 0 + 0 is 0 and 0 + 2 is 2
5+
# (both are even numbers). Find the maximum "smooth" element and
6+
# print it. Using the example [[0,4][2,6]] again, the maximum
7+
# "smooth" element is 2 because 2 is bigger than 0.
8+
9+
two_d_list = [[425, 214, 412, 123], [312, 214, 123, 343]]

2_intermediate/chapter10/solutions/img_avg.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
1-
""" Here is the challenge problem for 2d loops>
2-
Images are often represented as 3d arrays,
1+
"""
2+
Image Average
3+
4+
Here is the challenge problem for 2D loops:
5+
Images are often represented as 3D arrays,
36
where the rows and columns are the pixels in the image,
4-
and each pixel has an r, g, and b value.
7+
and each pixel has an RGB (red, green, blue) value
8+
which determines the color of the pixel.
59
610
The interesting thing is that we can iterate over images.
711
The challenge is, given an image, create a program that
812
will return a different image where each pixel is the average
913
of the pixels surrounding it in the original image.
1014
11-
The neighbors of an image are all the pixels that surroun it,
12-
1 on each side, and 4 on the diagonals, for 8 in total.
13-
Each pixel doesn't necessarily have 8 neighbors, though (think about why)
15+
The neighbors of an image are all the pixels that surround it,
16+
1 on each side, and 4 on the diagonals, for 8 in total. Each
17+
pixel doesn't necessarily have 8 neighbors, though (think about why).
1418
1519
The code to grab an image from the internet and make it
1620
into an array is given to you. The code also displays the new image
1721
you create in the end.
1822
19-
NOTE: The image is 3 dimensional because each pixel has rgb values.
23+
NOTE: The image is 3 dimensional because each pixel has RGB values.
2024
To find the average value of all of a pixels neighbors, you must
2125
change the average of the red value to the red value, blue to blue, etc.
22-
For example, if the neighbors of a pixel with value [1,2,3]
23-
were [20,30,40] and [10,120,30], the new pixel that would replace the original one would be
24-
[15,75,35]
26+
For example, if the neighbors of a pixel with value [1, 2, 3]
27+
were [20, 30, 40] and [10, 120, 30], the new pixel that would replace the
28+
original one would be [15, 75, 35]
2529
"""
2630

2731
from PIL import Image
@@ -37,9 +41,12 @@
3741

3842
# write code to create newimg here
3943
def solution1():
40-
"""Iterating over the image here. i is a variable from 0 to the width of the image.
41-
j is a variable that ranges from 0 to the height of the image. i is associated with
42-
values"""
44+
"""
45+
Iterating over the image here. i is a variable from
46+
0 to the width of the image.
47+
j is a variable that ranges from 0 to the height of the image.
48+
i is associated with values
49+
"""
4350
for i in range(len(img)):
4451
for j in range(len(img[0])):
4552
x_n = [0]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Odd Sum
2+
# Given a 2D list, find the sum of all elements at odd indexes for all
3+
# the lists at odd indexes. Print this sum times the sum of all
4+
# first element of all the 1D lists in the 2D list.
5+
#
6+
# Ex:[[1,2,3,6],[2,41,2,1]]should have print 42 after the program runs.
7+
#
8+
# Write the code below.
9+
10+
two_d_list = [[1, 2, 3, 5, 2], [2, 3, 1, 4], [2, 3, 1, 2, 21], [21, 3, 1, 41]]
11+
# two_d_list should print 51 after the program runs.
12+
13+
odd_sum = 0
14+
for outer_idx in range(1, len(two_d_list), 2):
15+
for inner_idx in range(1, len(two_d_list[outer_idx]), 2):
16+
odd_sum += two_d_list[outer_idx][inner_idx]
17+
18+
first_sum = 0
19+
for inner_list in range(len(two_d_list)):
20+
first_sum += two_d_list[inner_list][0]
21+
22+
print(odd_sum * first_sum)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Given a 2D list, let's call a element "smooth" if index of the
2+
# element in its 1D list plus the element is even. For example,
3+
# given the 2D list [[0,4][2,6]], the 1st element of each of the
4+
# 1D list is considered "smooth" because 0 + 0 is 0 and 0 + 2 is 2
5+
# (both are even numbers). Find the maximum "smooth" element and
6+
# print it. Using the example [[0,4][2,6]] again, the maximum
7+
# "smooth" element is 2 because 2 is bigger than 0.
8+
9+
two_d_list = [[425, 214, 412, 123], [312, 214, 123, 343]]
10+
curr_max = None
11+
12+
for outer_idx in range(len(two_d_list)):
13+
for inner_idx in range(len(two_d_list[outer_idx])):
14+
curr_elem = two_d_list[outer_idx][inner_idx]
15+
to_check = curr_elem + inner_idx
16+
if to_check % 2 == 0:
17+
if curr_max is None or curr_elem > curr_max:
18+
curr_max = curr_elem
19+
20+
print(curr_max)

0 commit comments

Comments
 (0)