forked from micanipho/Practice-test
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudent_code.py
More file actions
489 lines (350 loc) · 10.7 KB
/
student_code.py
File metadata and controls
489 lines (350 loc) · 10.7 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import random
"""Learning Outcome: Functions"""
def sum_of_squares(n: int):
total = 0
try:
if n == 0:
return n
elif n > 0:
for _ in range(1, n + 1):
sum = n * n
total += sum
return sum
except:
raise ValueError
sum_of_squares(5)
def evaluate_performance(grades: list, min_pass: int):
total_grade = 0
num_of_grades = 0
for grade in grades:
total_grade += grade
# print(total_grade)
for _ in range(len(grades)):
num_of_grades += 1
# print(num_of_grades)
ave_of_grades = total_grade / num_of_grades
# print(ave_of_grades)
if ave_of_grades >= min_pass:
return "Pass"
elif 0 <= ave_of_grades < min_pass:
return "Fail"
grades = [15, 35, 55, 75, 95]
min_pass = 39
evaluate_performance(grades, min_pass)
def calculate_cumulative_performance(scores: dict):
"""
Calculate the cumulative performance based on student scores.
Parameters:
scores (dict): A dictionary where keys are subject names and values are the corresponding scores.
Returns:
dict: A dictionary containing the average score and a list of subjects where the score is below average.
"""
pass
def analyze_improvement(scores: list):
"""
Analyze the improvement trend based on a list of scores.
Parameters:
scores (list): A list of integers representing scores over time.
Returns:
dict: A dictionary containing the trend of improvement ("positive", "negative", or "neutral")
and a boolean indicating whether there has been an improvement.
"""
pass
def rank_students(students: list[tuple[str, int]]):
"""
Rank students based on their scores.
Parameters:
students (list): A list of tuples where each tuple contains a student's name and their score.
Returns:
list: A sorted list of tuples in descending order based on scores.
"""
pass
"""Learning Outcome: Basic Loops"""
def even_numbers(n: int):
"""
Generate a list of even numbers from 1 to n.
Parameters:
n (int): The upper limit for generating even numbers.
Returns:
list: A list of even integers from 1 to n.
"""
even_numbers_list = []
for i in range(1, n + 1):
if i % 2 == 0:
even_numbers_list.append(i)
return even_numbers_list
even_numbers(19)
def odd_numbers(n: int):
"""
Generate a list of odd numbers from 1 to n.
Parameters:
n (int): The upper limit for generating odd numbers.
Returns:
list: A list of odd integers from 1 to n.
"""
odd_numbers_list = []
for i in range(1, n + 1):
if i % 2 != 0:
odd_numbers_list.append(i)
return odd_numbers_list
odd_numbers(19)
def sum_multiples_of_num(num: int, length: int):
"""
Calculate the sum of multiples of a given number up to a specified length.
Parameters:
num (int): The number whose multiples are to be summed.
length (int): The upper limit for the range of multiples.
Returns:
int: The sum of multiples of num from 1 to length.
"""
total = 0
for i in range(1, length + 1):
sum = i * num
total += sum
return total
sum_multiples_of_num(2, 6)
def skip_num(n: int, length: int):
"""
Generate a list of numbers from 1 to length, skipping a specific number.
Parameters:
n (int): The number to skip.
length (int): The upper limit for generating numbers.
Returns:
list: A list of integers from 1 to length, excluding n.
"""
numbers_list = []
for i in range(1, length + 1):
if i == n:
continue
else:
numbers_list.append(i)
return numbers_list
skip_num(17, 50)
def break_test(n: int, length: int):
"""
Generate a list of numbers from 1 to length, stopping when a specific number is encountered.
Parameters:
n (int): The number at which to stop adding to the list.
length (int): The upper limit for generating numbers.
Returns:
list: A list of integers from 1 to length, excluding n and stopping before it.
"""
list_of_numbers = []
break_list = []
while len(list_of_numbers) < length:
list_of_numbers.append(random.randint(1, 10))
for i in list_of_numbers:
if i == n:
return break_list
else:
break_list.append(i)
return break_list
break_test(1, 10)
def sum_numbers_until_zero(nums: list):
"""
Calculate the sum of numbers in a list until a zero is encountered.
Parameters:
nums (list): A list of integers.
Returns:
int: The sum of integers in the list up to (but not including) the first zero.
"""
sum_of_numbers = 0
for i in nums:
if i == 0:
return sum_of_numbers
else:
sum_of_numbers += i
sum_numbers_until_zero_nums = [12, 13, 20, 0, 100]
sum_numbers_until_zero(sum_numbers_until_zero_nums)
def count_positive_numbers(nums: list):
"""
Count the number of positive integers in a list.
Parameters:
nums (list): A list of integers.
Returns:
int: The count of positive integers in the list.
"""
count = 0
for i in count_positive_numbers_nums:
if i >= 0:
count += 1
return count
count_positive_numbers_nums = [1, -2, 3, -4, 5, -6, 7, -8]
count_positive_numbers(count_positive_numbers_nums)
def sum_dictionary_values(dictionary: dict):
"""
Calculate the sum of all values in a dictionary.
Parameters:
dictionary (dict): A dictionary with numeric values.
Returns:
int: The sum of all values in the dictionary.
"""
pass
def sum_unique_elements(elements: list):
"""
Calculate the sum of unique elements in a list.
Parameters:
elements (list): A list of integers.
Returns:
int: The sum of unique integers in the list.
"""
total = 0
sum_unique_elements_set = set(sum_unique_elements_nums)
for i in sum_unique_elements_set:
total += i
return(total)
sum_unique_elements_nums = [1, 2, 3, 4, 5, 2, 3]
sum_unique_elements(sum_unique_elements_nums)
def skip_divisible_by_num(n: int, length: int):
"""
Generate a list of numbers from 1 to length, skipping those that are divisible by a specific number.
Parameters:
n (int): The number to skip multiples of.
length (int): The upper limit for generating numbers.
Returns:
list: A list of integers from 1 to length, excluding those divisible by n.
"""
num_list = []
for i in range(1, length + 1):
if i % n == 0:
continue
else:
num_list.append(i)
return num_list
# print(skip_divisible_by_num(3, 16))
"""Learning Outcome: Processing Data"""
def square_numbers(nums: list):
"""
Calculate the square of each number in a list.
Parameters:
nums (list): A list of integers.
Returns:
list: A list containing the squares of the input integers.
"""
squared_numbers = []
for i in list_of_square_num:
square_num = i * i
squared_numbers.append(square_num)
return squared_numbers
list_of_square_num = [1, 2, 3, 4, 5]
square_numbers(list_of_square_num)
def transform_string(input: str, transform: str):
"""
Transform a string based on the specified transformation type.
Parameters:
input (str): The string to be transformed.
transform (str): The type of transformation ('capitalize', 'upper', 'lower').
Returns:
str: The transformed string.
Raises:
ValueError: If the transformation type is unknown.
"""
pass
def sum_and_average(nums: list[int]):
"""
Calculate the sum and average of a list of numbers.
Parameters:
nums (list[int]): A list of integers.
Returns:
tuple: A tuple containing the sum and average of the numbers.
"""
total = 0
new_list = []
for i in sum_and_average_list:
total += i
average = total / len(sum_and_average_list)
new_list.append(total)
new_list.append(average)
sum_and_average_set = set(new_list)
return sum_and_average_set
sum_and_average_list = [1, 2, 3, 4, 5]
sum_and_average(sum_and_average_list)
def word_frequency_count(words: list[str]):
"""
Count the frequency of each word in a list.
Parameters:
words (list[str]): A list of words.
Returns:
dict: A dictionary with words as keys and their frequencies as values.
"""
pass
def filter_even_numbers(nums: list[int]):
"""
Filter out even numbers from a list.
Parameters:
nums (list[int]): A list of integers.
Returns:
list: A list containing only the even integers from the input list.
"""
even_nums = []
for i in filtering_nums:
if i % 2 == 0:
even_nums.append(i)
return even_nums
filtering_nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]
filter_even_numbers(filtering_nums)
"""Learning Outcome: Simple Algorithms(Problem Solving)"""
def find_median(nums: list[int]):
"""
Find the median of a list of numbers.
Parameters:
nums (list[int]): A list of integers.
Returns:
float: The median value of the list.
Raises:
ValueError: If the list is empty.
"""
pass
median_nums = [1, 2, 3, 4, 5]
# print(find_median(median_nums))
def reverse_string(input: str):
"""
Reverse the given string.
Parameters:
input (str): The string to be reversed.
Returns:
str: The reversed string.
"""
return reverse_string_input[::-1]
reverse_string_input = "qwerty"
reverse_string(reverse_string_input)
def largest_number(nums: list[int]):
"""
Find the largest number in a list.
Parameters:
nums (list[int]): A list of integers.
Returns:
int or None: The largest number in the list, or None if the list is empty.
"""
largest_num = 0
for i in largest_number_nums:
if i > largest_num:
largest_num = i
# print(largest_num)
return largest_num
largest_number_nums = [1, 3, 5, 9, 3, 7, 4, 6]
largest_number(largest_number_nums)
def is_prime(n: int):
"""
Check if a number is prime.
Parameters:
n (int): The number to check.
Returns:
bool: True if the number is prime, False otherwise.
"""
for i in range(2, n):
if i % n == 0:
return False
else:
return True
is_prime(7)
def count_character_occurrences(word_sentence: str, char_count: str):
"""
Count the occurrences of a character in a given sentence.
Parameters:
word_sentence (str): The sentence in which to count occurrences.
char_count (str): The character to count.
Returns:
int: The number of occurrences of the character in the sentence.
"""
pass