Skip to content

Commit bf1651f

Browse files
committed
Second test 16-Nov-2020
1 parent 3078527 commit bf1651f

25 files changed

+183
-21
lines changed

Exams/2019-05-24.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ def countMyChars2(string):
3535
def myListHist(lst):
3636
def flattenList(lst_inner):
3737
result = []
38-
for i in lst_inner:
39-
if (isinstance(i, int)) or (isinstance(i, float) and i.is_integer()):
40-
result.append(int(i))
41-
elif isinstance(i, list):
42-
result.extend(flattenList(i))
38+
for j in lst_inner:
39+
if (isinstance(j, int)) or (isinstance(j, float) and j.is_integer()):
40+
result.append(int(j))
41+
elif isinstance(j, list):
42+
result.extend(flattenList(j))
4343
return result
4444

4545
flattenedList = flattenList(lst)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Exams/B/FunctionPlotter.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# 305265514
2+
__author__ = "Haim Adrian"
3+
4+
import numpy as np
5+
from matplotlib import pyplot as plt
6+
7+
8+
def FunctionPlotter(func='sqr'):
9+
if func not in ['x^2', 'sqr', 'lnx', 'All']:
10+
return None
11+
12+
def xSquare(xs):
13+
return 0.5 * (xs ** 2) + 1.5 * xs + 7
14+
15+
def lnx(xs):
16+
return 200 * np.log(xs - 3)
17+
18+
def sqr(xs):
19+
return (0.01 * (xs ** 5) + 1.5) ** 0.5
20+
21+
x = np.arange(10, 50, 1/15)
22+
plt.figure()
23+
plt.title(func)
24+
plt.xlabel('time[sec]')
25+
plt.ylabel('amplitude')
26+
if func == 'x^2' or func == 'All':
27+
plt.plot(x, xSquare(x), 'red')
28+
29+
if func == 'lnx' or func == 'All':
30+
plt.plot(x, lnx(x), 'black')
31+
32+
if func == 'sqr' or func == 'All':
33+
plt.plot(x, sqr(x), 'blue')
34+
35+
plt.show()

Exams/B/LetterInMat.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# 305265514
2+
__author__ = "Haim Adrian"
3+
4+
import numpy as np
5+
from numbers import Number
6+
7+
8+
def LetterInMat(thickness):
9+
# thickness must be a positive integer
10+
if thickness is None or not isinstance(thickness, Number) or thickness != np.int64(thickness) or thickness <= 0:
11+
return None
12+
13+
thickness = np.int64(thickness)
14+
mat = np.ones((6 * thickness, 5 * thickness), dtype=np.uint8)
15+
dim = mat.shape
16+
17+
# Paint the horizontal parts of the letter C
18+
mat[list(range(thickness, thickness * 2)) + list(range(dim[0] - thickness * 2, dim[0] - thickness)), thickness: dim[1] - thickness] = 0
19+
20+
# Paint the vertical part of the letter C, considering we've already painted some of the area while painting the horizontal parts
21+
mat[thickness * 2: dim[0] - thickness * 2, thickness: thickness * 2] = 0
22+
23+
return mat

Exams/B/ListObjectRemoval.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 305265514
2+
__author__ = "Haim Adrian"
3+
4+
from numbers import Number
5+
6+
7+
def ListObjectRemoval(myList):
8+
# For None or not-a-list, just return what we've received
9+
if myList is not None and isinstance(myList, list):
10+
for item in myList:
11+
if not isinstance(item, Number):
12+
myList.remove(item)
13+
14+
return myList

Exams/B/MostCommonColor.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# 305265514
2+
__author__ = "Haim Adrian"
3+
4+
import numpy as np
5+
import operator
6+
7+
8+
def MostCommonColor(myImage):
9+
if not isinstance(myImage, np.ndarray):
10+
return None
11+
12+
if myImage.ndim == 2:
13+
# For 2D image just count each scalar and return the maximum one
14+
unique, counts = np.unique(myImage, return_counts=True)
15+
return unique[np.argmax(counts)]
16+
elif myImage.ndim == 3:
17+
# Define a dictionary to count the occurrences of each color in the specified image
18+
countsDic = {}
19+
20+
# Scan the whole matrix of colors, instead of sub-calling MostCommonColor, cause we need to count colors
21+
# and not finding the maximum scalar in each level of z.
22+
for row in range(myImage.shape[0]):
23+
for col in range(myImage.shape[1]):
24+
# Make current color a tuple so we can register it as key in a dictionary
25+
currColor = (myImage[row, col, 0], myImage[row, col, 1], myImage[row, col, 2])
26+
countsDic[currColor] = countsDic.get(currColor, 0) + 1
27+
28+
# Return the key where its value is the maximum one among all values in the dictionary
29+
return max(countsDic.items(), key=operator.itemgetter(1))[0]
30+
else:
31+
return None

Exams/B/Q00.png

203 KB
Loading

Exams/B/Q01.png

148 KB
Loading

Exams/B/Q02.png

188 KB
Loading

Exams/B/Q03.png

213 KB
Loading

Exams/B/Q04.png

158 KB
Loading

Exams/B/Q05.png

173 KB
Loading

Exams/B/Q06.png

179 KB
Loading

Exams/B/Q07.png

216 KB
Loading

Exams/B/Q08.png

166 KB
Loading

Exams/B/Q09.png

174 KB
Loading

Exams/B/Q10.png

149 KB
Loading

Exams/B/countMyStrings.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# 305265514
2+
__author__ = "Haim Adrian"
3+
4+
5+
# 24-05-2019 exam.
6+
def countMyStrings(string):
7+
if string is None or not isinstance(string, str):
8+
return None
9+
10+
# First, lowercase the string so we will count characters ignoring case
11+
string = string.lower()
12+
13+
# Second, make the characters of a string into set so we will count each character without repeating ourselves
14+
counts = {c: string.count(c) for c in set(string)}
15+
16+
return counts

Exams/B/my_unique_mat.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# 305265514
2+
__author__ = "Haim Adrian"
3+
4+
import numpy as np
5+
6+
7+
def my_unique_mat(size):
8+
if size != np.uint64(size):
9+
return None
10+
11+
myMat, myMat[np.eye(size) == 1], myMat[np.eye(size)[::-1] == 1], myMat[int(np.round(size / 2 - 0.5)), int(np.round(size / 2 - 0.5))] = np.ones((size, size), dtype=np.uint8), 0, 2, (1 if size % 2 == 1 else 0)
12+
13+
return myMat

Exams/Test.py

+46-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
__author__ = "Haim Adrian"
22

3+
from operator import itemgetter
4+
35
from matplotlib import pyplot as plt
46
import numpy as np
57
import cv2
@@ -57,29 +59,57 @@ def myMasking(myImage, myMask):
5759

5860

5961
# ex8
60-
img = cv2.imread('eagle.jpeg', 0)
62+
img = cv2.imread('img.jpg', 0)
63+
img2 = cv2.imread('img.jpg')
6164
# img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
6265
SharpenMask = np.array([[-1, -2, -1],
6366
[-2, 13, -2],
6467
[-1, -2, -1]], dtype=np.float64)
68+
Sharpen2Mask = np.array([[2, 2, 2],
69+
[2, 16, 2],
70+
[2, 2, 2]], dtype=np.float64)
6571
GausMask = (1.0 / 273) * np.array([[1, 4, 7, 4, 1],
6672
[4, 16, 26, 16, 4],
6773
[7, 26, 41, 26, 7],
6874
[4, 16, 26, 16, 4],
6975
[1, 4, 7, 4, 1]],
7076
dtype=np.float64)
71-
my_sol = mmyMaskKernel(img, SharpenMask)
72-
print(np.min(my_sol), np.max(my_sol))
73-
my_sol = np.uint8((my_sol - np.min(my_sol)) / np.max(my_sol) * 255)
74-
masked = myMaskKernel(img, SharpenMask)
75-
masked = np.uint8((masked - np.min(masked)) / np.max(masked) * 255)
76-
third = myMasking(img, SharpenMask)
77-
print(np.min(third), np.max(third))
78-
third = np.uint8((third - np.min(third)) / np.max(third) * 255)
79-
plt.subplot(131)
80-
plt.imshow(masked, CMAP='gray')
81-
plt.subplot(132)
82-
plt.imshow(my_sol, CMAP='gray')
83-
plt.subplot(133)
84-
plt.imshow(third, CMAP='gray')
85-
plt.show()
77+
78+
79+
def MostCommonColor(myImage):
80+
if not isinstance(myImage, np.ndarray):
81+
return None
82+
83+
if myImage.ndim == 2:
84+
unique, counts = np.unique(myImage, return_counts=True)
85+
return unique[np.argmax(counts)]
86+
elif myImage.ndim == 3:
87+
countsDic = {}
88+
89+
for row in range(myImage.shape[0]):
90+
for col in range(myImage.shape[1]):
91+
currColor = (myImage[row, col, 0], myImage[row, col, 1], myImage[row, col, 2])
92+
countsDic[currColor] = countsDic.get(currColor, 0) + 1
93+
94+
return max(countsDic.items(), key=itemgetter(1))[0]
95+
else:
96+
return None
97+
98+
99+
print(MostCommonColor(img))
100+
print(MostCommonColor(img2))
101+
# my_sol = myMasking(img, Sharpen2Mask)
102+
# print(np.min(my_sol), np.max(my_sol))
103+
# my_sol = np.uint8((my_sol - np.min(my_sol)) / np.max(my_sol) * 255)
104+
# masked = myMaskKernel(img, SharpenMask)
105+
# masked = np.uint8((masked - np.min(masked)) / np.max(masked) * 255)
106+
# third = myMasking(img, SharpenMask)
107+
# print(np.min(third), np.max(third))
108+
# third = np.uint8((third - np.min(third)) / np.max(third) * 255)
109+
# plt.subplot(131)
110+
# plt.imshow(masked, CMAP='gray')
111+
# plt.subplot(132)
112+
# plt.imshow(my_sol, CMAP='gray')
113+
# plt.subplot(133)
114+
# plt.imshow(third, CMAP='gray')
115+
# plt.show()

0 commit comments

Comments
 (0)