Skip to content
4 changes: 2 additions & 2 deletions 1_beginner/chapter5/practice/alternating.py
Original file line number Diff line number Diff line change
@@ -1,12 +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

"""

# Write code here.
Expand Down
24 changes: 14 additions & 10 deletions 2_intermediate/chapter10/practice/img_avg.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
""" Here is the challenge problem for 2d loops>
Images are often represented as 3d arrays,
"""
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 r, g, and b value.
and each pixel has an RGB (red, green, blue) value
which determines 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
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 surroun 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 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.
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]
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]
"""

from PIL import Image
Expand Down
11 changes: 11 additions & 0 deletions 2_intermediate/chapter10/practice/odd_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Odd Sum
# Given a 2D list, find the sum of all elements at odd indexes for all
# the lists at odd indexes. Print this sum times the sum of all
# first element of all the 1D lists in the 2D list.
#
# Ex:[[1,2,3,6],[2,41,2,1]]should have print 42 after the program runs.
#
# Write the code below.

two_d_list = [[1, 2, 3, 5, 2], [2, 3, 1, 4], [2, 3, 1, 2, 21], [21, 3, 1, 41]]
# two_d_list should print 51 after the program runs.
9 changes: 9 additions & 0 deletions 2_intermediate/chapter10/practice/smooth_max.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Given a 2D list, let's call a element "smooth" if index of the
# element in its 1D list plus the element is even. For example,
# given the 2D list [[0,4][2,6]], the 1st element of each of the
# 1D list is considered "smooth" because 0 + 0 is 0 and 0 + 2 is 2
# (both are even numbers). Find the maximum "smooth" element and
# print it. Using the example [[0,4][2,6]] again, the maximum
# "smooth" element is 2 because 2 is bigger than 0.

two_d_list = [[425, 214, 412, 123], [312, 214, 123, 343]]
33 changes: 20 additions & 13 deletions 2_intermediate/chapter10/solutions/img_avg.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
""" Here is the challenge problem for 2d loops>
Images are often represented as 3d arrays,
"""
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 r, g, and b value.
and each pixel has an RGB (red, green, blue) value
which determines 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
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 surroun 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 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.
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]
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]
"""

from PIL import Image
Expand All @@ -37,9 +41,12 @@

# 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"""
"""
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]
Expand Down
22 changes: 22 additions & 0 deletions 2_intermediate/chapter10/solutions/odd_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Odd Sum
# Given a 2D list, find the sum of all elements at odd indexes for all
# the lists at odd indexes. Print this sum times the sum of all
# first element of all the 1D lists in the 2D list.
#
# Ex:[[1,2,3,6],[2,41,2,1]]should have print 42 after the program runs.
#
# Write the code below.

two_d_list = [[1, 2, 3, 5, 2], [2, 3, 1, 4], [2, 3, 1, 2, 21], [21, 3, 1, 41]]
# two_d_list should print 51 after the program runs.

odd_sum = 0
for outer_idx in range(1, len(two_d_list), 2):
for inner_idx in range(1, len(two_d_list[outer_idx]), 2):
odd_sum += two_d_list[outer_idx][inner_idx]

first_sum = 0
for inner_list in range(len(two_d_list)):
first_sum += two_d_list[inner_list][0]

print(odd_sum * first_sum)
20 changes: 20 additions & 0 deletions 2_intermediate/chapter10/solutions/smooth_max.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Given a 2D list, let's call a element "smooth" if index of the
# element in its 1D list plus the element is even. For example,
# given the 2D list [[0,4][2,6]], the 1st element of each of the
# 1D list is considered "smooth" because 0 + 0 is 0 and 0 + 2 is 2
# (both are even numbers). Find the maximum "smooth" element and
# print it. Using the example [[0,4][2,6]] again, the maximum
# "smooth" element is 2 because 2 is bigger than 0.

two_d_list = [[425, 214, 412, 123], [312, 214, 123, 343]]
curr_max = None

for outer_idx in range(len(two_d_list)):
for inner_idx in range(len(two_d_list[outer_idx])):
curr_elem = two_d_list[outer_idx][inner_idx]
to_check = curr_elem + inner_idx
if to_check % 2 == 0:
if curr_max is None or curr_elem > curr_max:
curr_max = curr_elem

print(curr_max)