Skip to content

Commit 84f9185

Browse files
committed
Merge branch 'pr'
2 parents 71fc97c + 3c80817 commit 84f9185

File tree

9 files changed

+224
-2
lines changed

9 files changed

+224
-2
lines changed

1_beginner/chapter3/solutions/temperature.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Temperature
1+
# Temperature
22
# Write a program that converts Celsius to Fahrenheit.
33
# It should ask the user for the temperature in
44
# degrees Celsius and then print the temperature in degrees Fahrenheit.

1_beginner/chapter5/practice/add_all_the_way.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@
55
# You can use a for or while loop.
66

77
# write code here
8+
9+
10+
11+
12+
#Try using the other loop and do the same p[roblem again
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
3+
Ask the user for an integer. The print the numbers from 1 to that number,
4+
but alternating in sign. For example, if the input was 5, what would be printed
5+
is 1, -1, 2, -2, 3, -3, 4, -4, 5. (Note, DO NOT include the last negative
6+
number).
7+
8+
Do this with a for loop
9+
10+
"""
11+
12+
#Write code here.
13+
14+
number = int(input("Enter Number Here: "))
15+
16+
17+
#Now try it with a while loop
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Even
22
# Print every even number greater than 10
3-
# and less than 101
3+
# and less than 101 (10 ia included)
44

55
# write code here
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
''' CHALLENGE PROBLEM!! NOT FOR THE FAINT OF HEART!
2+
3+
The Fibonacci numbers, discovered by Leonardo di Fibonacci,
4+
is a sequence of numbers that often shows up in mathematics and,
5+
interestingly, nature. The sequence goes as such:
6+
7+
1,1,2,3,5,8,13,21,34,55,...
8+
9+
where the sequence starts with 1 and 1, and then each number is the sum of the
10+
previous 2. For example, 8 comes after 5 because 5+3 = 8, and 55 comes after 34
11+
because 34+21 = 55.
12+
13+
The challenge is to use a for loop (not recursion, if you know what that is),
14+
to find the 100th Fibonnaci number.
15+
'''
16+
17+
#write code here
18+
19+
20+
#Can you do it with a while loop?
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
3+
Write a program to check if a number is prime or not. A prime
4+
number is one that is not divisible by any number other than
5+
1 and itself. For example, 11 is prime because it is not divisible
6+
by 2,3,4,5,...10 (i.e. 11/10, for example, is not an integer).
7+
8+
Write a for loop to check if a number is prime or not.
9+
10+
"""
11+
12+
#write code here
13+
14+
numer = int(input("Enter number here: "))
15+
16+
"""
17+
Given a number less than or equal to 10 billion, see if you can check if it is
18+
prime in UNDER 2 SECONDS. The code you wrote above probably wont do that, so
19+
you will have to figure out a clever solution.
20+
21+
NOTE: THE ABOVE IS A CHALLENGE PROBLEM, AND IS EXTRREEMMLY HARD TO DO.
22+
I COULDN'T DO IT OPTIMALY UNTIL I LEARNED HOW TO. If you can't
23+
figure out the solution, don't feel discouraged, its seriously a really
24+
hard problem (I have seen this asked to college students :O)
25+
26+
"""
27+
28+
#write code here
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
3+
Here are some interesting results from mathematics that we can
4+
model with computer science.
5+
6+
In class we added up 1 + 1/2 + 1/4 + 1/8 ...
7+
See if you can add up 1 + 1/3 + 1/9 + 1/27 + 1/81 ... in a similar fashion.
8+
What is the answer? What if, instead of 3, we use 5 (1 + 1/5 + 1/25 + 1/125...)
9+
7? Do you see a patter? (Note: make sure not to end up with an infinite loop).
10+
11+
12+
13+
Also, try adding up 1 -1/3 + 1/5 - 1/7 + 1/9 - 1/11 ... 10 million times. then
14+
multiply this result by 4. What number is this close to?
15+
16+
"""
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
""" Here is the challenge problem for 2d loops>
2+
Images are often represented as 3d arrays,
3+
where the rows and columns are the pixels in the image,
4+
and each pixel has an r, g, and b value.
5+
6+
The interesting thing is that we can iterate over images.
7+
The challenge is, given an image, create a program that
8+
will return a different image where each pixel is the average
9+
of the pixels surrounding it in the original image.
10+
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)
14+
15+
The code to grab an image from the internet and make it
16+
into an array is given to you. The code also displays the new image
17+
you create in the end.
18+
19+
NOTE: The image is 3 dimensional because each pixel has rgb values.
20+
To find the average value of all of a pixels neighbors, you must
21+
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]
25+
"""
26+
27+
from PIL import Image
28+
import requests
29+
import numpy
30+
import matplotlib.pyplot as plt
31+
32+
url = "https://images.dog.ceo/breeds/waterdog-spanish/20180723_185544.jpg"
33+
img = numpy.array(Image.open(requests.get(url, stream=True).raw)).tolist()
34+
newimg = img
35+
transpose = numpy.transpose(img)
36+
37+
plt.imshow(img)
38+
plt.show()
39+
40+
#write code to create newimg here
41+
42+
plt.imshow(newimg)
43+
plt.show()
44+
45+
plt.imshow(transpose)
46+
plt.show()
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
""" Here is the challenge problem for 2d loops>
2+
Images are often represented as 3d arrays,
3+
where the rows and columns are the pixels in the image,
4+
and each pixel has an r, g, and b value.
5+
6+
The interesting thing is that we can iterate over images.
7+
The challenge is, given an image, create a program that
8+
will return a different image where each pixel is the average
9+
of the pixels surrounding it in the original image.
10+
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)
14+
15+
The code to grab an image from the internet and make it
16+
into an array is given to you. The code also displays the new image
17+
you create in the end.
18+
19+
NOTE: The image is 3 dimensional because each pixel has rgb values.
20+
To find the average value of all of a pixels neighbors, you must
21+
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]
25+
"""
26+
27+
from PIL import Image
28+
import requests
29+
import numpy
30+
import matplotlib.pyplot as plt
31+
32+
url = "https://images.dog.ceo/breeds/waterdog-spanish/20180723_185544.jpg"
33+
img = numpy.array(Image.open(requests.get(url, stream=True).raw))
34+
newimg = img
35+
transpose = numpy.transpose(img)
36+
37+
38+
#write code to create newimg here
39+
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"""
43+
for i in range(len(img)):
44+
for j in range(len(img[0])):
45+
x_n = [0]
46+
y_n = [0]
47+
48+
if(i == 0):
49+
x_n.append(1)
50+
elif(i == len(img)-1):
51+
x_n.append(-1)
52+
else:
53+
x_n.append(1)
54+
x_n.append(-1)
55+
56+
if(j == 0):
57+
y_n.append(1)
58+
elif(j == len(img[0])-1):
59+
y_n.append(-1)
60+
else:
61+
y_n.append(1)
62+
y_n.append(-1)
63+
64+
r_avg = -1*img[i][j][0]
65+
g_avg = -1*img[i][j][1]
66+
b_avg = -1*img[i][j][2]
67+
c = -1
68+
69+
for x in x_n:
70+
for y in y_n:
71+
r_avg += img[i+x][j+y][0]
72+
g_avg += img[i+x][j+y][1]
73+
b_avg += img[i+x][j+y][2]
74+
c+=1
75+
r_avg = r_avg/c
76+
g_avg = g_avg/c
77+
b_avg = b_avg/c
78+
79+
newimg[i][j] = [r_avg, g_avg, b_avg]
80+
81+
82+
83+
84+
solution1()
85+
86+
plt.imshow(newimg)
87+
plt.show()
88+
89+
plt.imshow(transpose)
90+
plt.show()

0 commit comments

Comments
 (0)