Skip to content

Commit 4f2ebd3

Browse files
authored
Merge pull request #35 from code-for-tomorrow/ch11-exercises
Add remove_duplicates.py to Ch.11
2 parents 84f9185 + 6cc6537 commit 4f2ebd3

File tree

10 files changed

+100
-57
lines changed

10 files changed

+100
-57
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: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,4 @@
77
# write code here
88

99

10-
11-
12-
#Try using the other loop and do the same p[roblem again
10+
# Try using the other loop and do the same p[roblem again

1_beginner/chapter5/practice/alternating.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
1010
"""
1111

12-
#Write code here.
12+
# Write code here.
1313

1414
number = int(input("Enter Number Here: "))
1515

1616

17-
#Now try it with a while loop
17+
# Now try it with a while loop

1_beginner/chapter5/practice/fibonnaci.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
''' CHALLENGE PROBLEM!! NOT FOR THE FAINT OF HEART!
1+
""" CHALLENGE PROBLEM!! NOT FOR THE FAINT OF HEART!
22
33
The Fibonacci numbers, discovered by Leonardo di Fibonacci,
44
is a sequence of numbers that often shows up in mathematics and,
@@ -12,9 +12,9 @@
1212
1313
The challenge is to use a for loop (not recursion, if you know what that is),
1414
to find the 100th Fibonnaci number.
15-
'''
15+
"""
1616

17-
#write code here
17+
# write code here
1818

1919

20-
#Can you do it with a while loop?
20+
# Can you do it with a while loop?

1_beginner/chapter5/practice/prime.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
1010
"""
1111

12-
#write code here
12+
# write code here
1313

1414
numer = int(input("Enter number here: "))
1515

@@ -25,4 +25,4 @@
2525
2626
"""
2727

28-
#write code here
28+
# write code here

1_beginner/chapter5/practice/series.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
Also, try adding up 1 -1/3 + 1/5 - 1/7 + 1/9 - 1/11 ... 10 million times. then
1414
multiply this result by 4. What number is this close to?
1515
16-
"""
16+
"""

2_intermediate/chapter10/practice/img_avg.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@
3737
plt.imshow(img)
3838
plt.show()
3939

40-
#write code to create newimg here
40+
# write code to create newimg here
4141

4242
plt.imshow(newimg)
4343
plt.show()
4444

4545
plt.imshow(transpose)
46-
plt.show()
46+
plt.show()

2_intermediate/chapter10/solutions/img_avg.py

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -35,50 +35,48 @@
3535
transpose = numpy.transpose(img)
3636

3737

38-
#write code to create newimg here
38+
# write code to create newimg here
3939
def solution1():
40-
"""Iterating over the image here. i is a variable from 0 to the width of the image.
40+
"""Iterating over the image here. i is a variable from 0 to the width of the image.
4141
j is a variable that ranges from 0 to the height of the image. i is associated with
4242
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-
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]
8280

8381

8482
solution1()
@@ -87,4 +85,4 @@ def solution1():
8785
plt.show()
8886

8987
plt.imshow(transpose)
90-
plt.show()
88+
plt.show()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Write a function called remove_duplicates
2+
# The sole parameter of the function should be a list
3+
# The function should look through a list,
4+
# Find all duplicate elements, and remove them
5+
# Sort the resulting list
6+
# YOU MAY NOT USE THE set() function IN PYTHON.
7+
# Hint: To sort a list, use sorted(list)
8+
# Another hint: Use dict.removekeys(list)
9+
# To take the elements from a list,
10+
# and convert them to keys in a dictionary
11+
12+
# Example: array = [1,1,2,5,4,6,12,3,4,6]
13+
# Result should print [1,2,3,4,5,6,12]
14+
15+
# Write code here
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Write a function called remove_duplicates
2+
# The sole parameter of the function should be a list
3+
# The function should look through a list,
4+
# Find all duplicate elements, and remove them
5+
# Sort the resulting list
6+
# YOU MAY NOT USE THE set() function IN PYTHON.
7+
# Hint: To sort a list, use sorted(list)
8+
# Another hint: Use dict.fromkeys(list)
9+
# To take the elements from a list,
10+
# and convert them to keys in a dictionary
11+
12+
# Example: array = [1,1,2,5,4,6,12,3,4,6]
13+
# Result should print [1,2,3,4,5,6,12]
14+
15+
# Write code here
16+
17+
list1 = [1, 1, 2, 5, 4, 6, 12, 3, 4, 6] # Define your list
18+
19+
20+
# Define your Function
21+
def remove_duplicates(array):
22+
my_list = list(dict.fromkeys(array))
23+
# Converts the list into a dictionary.
24+
# Fromkeys(array) turns each item into a key
25+
# There cannot be multiple keys,
26+
# So all the duplicate keys are removed
27+
# Convert the keys back into a list
28+
return sorted(my_list)
29+
# Returns the sorted list of keys that are not duplicate.
30+
31+
32+
print(remove_duplicates(list1)) # Call the function

0 commit comments

Comments
 (0)