-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solutions for mixed EX
255 lines (214 loc) · 7.6 KB
/
Solutions for mixed EX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
"""
Write a program that will get from the user the base and height of a triangle and compute its area.
"""
#++++++++++++++++++++++++++++++++++++++++++++++++++++++Q1+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""
def area_traingle():
height = int(input("Enter the height"))
base = int(input("Enter the base"))
area = float(1/2*(height*base))
return area
print(area_traingle())
"""
#++++++++++++++++++++++++++++++++++++++++++++++++++++++Q2+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""
Write a function that gets a number and returns its sum of digits.
"""
"""
def num_sum(num: int) -> int:
total = 0
for i in range(1, num + 1):
total += i
return total
print(num_sum(20))
"""
#++++++++++++++++++++++++++++++++++++++++++++++++++++++Q3+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""
Write a function that checks if a number is perfect.
"""
"""
def is_perferct_number(num:int)-> bool:
if num <= 1:
return False
divisitor_sum = 0
for i in range(1, num):
if num % i == 0:
divisitor_sum += i
return divisitor_sum == num
print(is_perferct_number(6))
"""
#++++++++++++++++++++++++++++++++++++++++++++++++++++++Q4+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""
Write a function that removes duplicate elements from a given list so that each element appears only once, and returns
the new list. The original list should not be changed.
"""
"""
from collections import Counter
def remove_duplicates(list_elements):
list_elements = Counter(list_elements)
unique_elements = list(list_elements.keys())
print(f" removed duplicates :{unique_elements}")
if __name__ == "__main__":
list_elements = [2, 2, 3, 4, 5, 6, 3, 2, 3, 4]
remove_duplicates(list_elements)
print(f" Orginal list is : {list_elements}")
"""
#++++++++++++++++++++++++++++++++++++++++++++++++++++++Q5+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""
Write a program that prints out all colors from color_list_1 that are not present in color_list_2.
"""
"""
color_list_1 = ["Red", "Blue", "Green"]
color_list_2 = ["Red", "Blue", "Yellow", "White"]
# Should print "Green"
def is_green_color():
for color in color_list_1:
if color == "Green" and color not in color_list_2:
return color
return None
print(is_green_color())
"""
#++++++++++++++++++++++++++++++++++++++++++++++++++++++Q6+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""
Write a function gcd(num1, num2) that returns the greatest common divisor of the two numbers num1 and num2.
Don’t use external libraries, implement the algorithm.
"""
"""
def gcd(num1, num2) :
while num2 != 0:
(num1, num2) = (num2, num1 % num2)
return num1
print(gcd(8,12))
"""
#++++++++++++++++++++++++++++++++++++++++++++++++++++++Q7+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""
def is_substring(str1, str2):
if str1 in str2:
return True
else:
return False
# Example usage #str1: Asmamaw
str1 = input("Enter str1: ") # str2: Asmaamw Chanie Yehun
str2 = input("Enter str2: ")
print(is_substring(str1, str2))
#++++++++++++++++++++++++++++++++++++++Alternetively++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
def is_substring(str1, str2):
str1 = "Asmamaw Chanie Yehun"
str2 = "Asmamaw Chanie"
if (str1.find(str2) >= 0):
print("True")
else:
print("False")
is_substring(str1, str2)
"""
#++++++++++++++++++++++++++++++++++++++++++++++++++++++Q8+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""
Write a function is_substring_while(str1, str2) that returns True if str1 is a substring of str2, otherwise returns
False. Don’t use the in operator or any other external library, use only while loops.
"""
#++++++++++++++++++++++++++++++++++++++++++++++++++++++Q9+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""
def is_substring(str1, str2):
len1 = len(str1)
len2 = len(str2)
i = 0
while i <= len2 - len1:
j = 0
while (j < len1 and str2[i + j] == str1[j]):
j += 1
if j == len1:
return True
i += 1
return False
print(is_substring('Asmamaw', 'Asmamaw Chanie Yehun')) # True
"""
#++++++++++++++++++++++++++++++++++++++++++++++++++++++Q10+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""
Write a function is_palindrome_no_loop(my_str) that returns True if my_str is a palindrome, otherwise returns False.
don't use loop
"""
"""
def is_palindrome_no_loop(my_str):
my_str_lower = my_str.replace(" ", "").lower()
return my_str == my_str_lower[::-1]
user_input = input("Enter the word: ")
print(is_palindrome_no_loop(user_input))
"""
#++++++++++++++++++++++++++++++++++++++++++++++++++++++Q11+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""
Write a function is_palindrome_no_loop(my_str) that returns True if my_str is a palindrome, otherwise returns False.
use loop
"""
"""
def is_palindrome_no_loop(my_str: str) -> bool:
my_str_lower = my_str.replace(" ", "").lower() # Remove spaces and convert to lowercase
n = len(my_str_lower)
for i in range(n // 2): # Iterate up to the middle of the string
if my_str_lower[i] != my_str_lower[n - 1 - i]: # Compare characters from start and end
return False
return True
user_input = input("Enter the word: ")
print(is_palindrome_no_loop(user_input))
"""
"""
#++++++++++++++++++++++++++++++++++++++++++++++++++++++Q12+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Write a function letter_count(my_str) that counts the number of each character in a string.
The function should return a frequency dictionary of the characters in the text.
The dictionary should contain all the letters a-z in lower case, and only letters.
If a letter was not found in the text, the value of the letter in the dictionary should be 0.
"""
my_str = "abcdefghijklmnopqrstuvwxyz"
"""
def letter_count(my_str):
my_str_freq = {} # Initialize an empty dictionary to store character frequencies
for i in my_str:
if i in my_str_freq:
my_str_freq[i] += 1
else:
my_str_freq[i] = 1
print("Count of all characters in my_str:")
print(str(my_str_freq))
letter_count(my_str)
"""
#++++++++++++++++++++++++++++++++++++++++++++++++++++++Q13+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""
Write a function get_key_by_value(my_dict, value) that returns a list of all the keys in the dictionary that are
associated with the given value.
"""
"""
my_dict = {
"Masterschool": "love",
"Hamburger": "love",
"Tomato": "like"
}
def get_key_by_value(my_dict, value):
keys = []
for key, val in my_dict.items():
if val == value:
keys.append(key)
return keys
# Test cases
print(get_key_by_value(my_dict, "love") == ["Masterschool", "Hamburger"])
print(get_key_by_value(my_dict, "like") == ["Tomato"])
print(get_key_by_value(my_dict, "hate") == [])
"""
#++++++++++++++++++++++++++++++++++++++++++++++++++++++Q14++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""
Write a function reverse_dict(my_dict) that gets a dictionary and returns a new “reversed” dictionary.
For example, for this dictionary:
"""
"""
my_map = {
"Masterschool": "love",
"Hamburger": "love",
"Tomato": "like"
}
def reverse_dict():
inv_map = {}
for k, v in my_map.items():
inv_map[v] = inv_map.get(v, []) + [k]
print(my_map)
print(inv_map)
reverse_dict()
"""
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++