P ython p rogramming e xercises



Yüklə 1,51 Mb.
Pdf görüntüsü
səhifə42/124
tarix14.05.2023
ölçüsü1,51 Mb.
#113537
1   ...   38   39   40   41   42   43   44   45   ...   124
PythonProgrammingExercisesGentlyExplained

Exercise Description 
Write a mode() function that has a numbers parameter. This function returns the mode, or 
most frequently appearing number, of the list of integer and floating-point numbers passed to the 
function. 
These Python assert statements stop the program if their condition is False. Copy them to 
the bottom of your solution program. Your solution is correct if the following assert statements’ 
conditions are all True: 
assert mode([]) == None 
assert mode([1, 2, 3, 4, 4]) == 4 
assert mode([1, 1, 2, 3, 4]) == 1 
import random 
random.seed(42) 
testData = [1, 2, 3, 4, 4] 
for i in range(1000): 
random.shuffle(testData) 
assert mode(testData) == 4 
Shuffling the order of the numbers should not affect the mode. The for loop does 1,000 such 
random shuffles to thoroughly check that this fact remains true. For an explanation of the 
random.seed()
function, see the Further Reading section of Exercise #19, ―Password 
Generator‖. 
Try to write a solution based on the information in this description. If you still have trouble 
solving this exercise, read the Solution Design and Special Cases and Gotchas sections for 
additional hints. 
Prerequisite concepts: for loops, augmented assignment operators, indexes, not in operator 


Python Programming Exercises, Gently Explained 
50 
Solution Design 
The solution uses a dictionary to track how often each number appears in the list. The keys of the 
dictionary will be the number, and the values will be a count of how often the number appears in the 
list. 
Start with an empty dictionary and set up two variables to keep track of the most frequent 
number and how many times this number has appeared in the list. Use a for loop to loop over 
numbers in the numbers list. If the current number we are looping on doesn’t appear in the 
dictionary’s keys, then create a key-value pair for it with the value starting at 0. Then increment the 
count for this number in the dictionary. Finally, if this count is larger than the most frequent number’s 
count, update the most frequent number variable and most frequent number’s count variable with the 
current number and its count. 
By the time the for loop has finished, the most frequent number variable contains the mode of 
the numbers list. Calculating the mode is similar Exercise #12, ―Smallest & Biggest‖. Both solutions 
loop over the list of numbers, using another variable to (in Exercise #12) keep track of the 
smallest/biggest number found so far or (in this exercise) keep track of the most frequently occurring 
number found so far. 

Yüklə 1,51 Mb.

Dostları ilə paylaş:
1   ...   38   39   40   41   42   43   44   45   ...   124




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2025
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin