Question

write a Python3 code that finds the mode of any numerical list, without using: statistics.mode() max()...

write a Python3 code that finds the mode of any numerical list, without using:
statistics.mode()
max()
.sort
.count
squiggly brackets (like {})
Please also make it so that the code is able to return the number of times it loops.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have implemented the program as required.Please find the code and the screenshots below.

Code:

def find_mode(lst):
nums = [] # list to store the numbers without repetitions
mx = -1 # variable to store max value
mx2 = -1 # variable to store 2nd max value
temp1 = [] # list to store occurences for each variable
temp = lst.copy() # copy lst in temp
# temp will be used in this way: if lst = [2, 5, 3, 2, 5, 5, 2] then temp = [1, 1, 1, 2, 2, 3, 2]
multimode = False # assume there is only one mode
count = 0 # variable to store number of times it loops
for i in range(len(lst)):
count += 1
if lst[i] not in nums:
nums.append(lst[i])   
temp1.append(1)   
temp[i] = 1   
else:
ind = nums.index(lst[i])
temp1[ind] += 1
temp[i] = temp1[ind]
if temp[i]>=mx:
mx2 = mx
mx = temp[i]
mxind = i
if mx2 == mx:
multimode = True
if mx == 1:
print('No Mode Found')
print('The program looped {} times'.format(count))
else:
if multimode:
print('The modes are:',end = ' ')
for j in range(len(temp)):
count += 1
if temp[j] == mx:
print(lst[j],end = ', ')
print('\nThe program looped {} times'.format(count))
else:
print('Mode: ')
print(lst[mxind])
print('The program looped {} times'.format(count))
find_mode([1, 2, 3, 5, 4, 9])

Screenshot:

Case 1: Multiple Modes:

Case 2: Single Mode:

Case 3: No Mode:

Values of lst, temp, temp1 (this is just for easier understanding of the code):

Add a comment
Know the answer?
Add Answer to:
write a Python3 code that finds the mode of any numerical list, without using: statistics.mode() max()...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PYTHON. Write the following functions without using any predefined functions unless specified. def min (dataList) :      ...

    PYTHON. Write the following functions without using any predefined functions unless specified. def min (dataList) :       #returns the smallest value of the data values in dataList def max (dataList):        #returns the largest value def getRange (dataList) def mean (dataList) :     #returns the average of data values def median (dataList):    #returns the middle value of an ordered list #note: to sort the list first, may use the predefined sort function Then write a test1 function to test the above functions using...

  • . Please write a function that will do the following Decryption of a message encrypted with a substitution cipher given cipher text only without any key Please provide comments on each line of code ou...

    . Please write a function that will do the following Decryption of a message encrypted with a substitution cipher given cipher text only without any key Please provide comments on each line of code outlining what that line is doing. Note: Only the Function is to be written, all user inputs i.e the text to be decrypted will be entered earlier in the program. Code must be written in C and be able to be compiled using GCC If any...

  • Comma Code (Using Python Language) Say you have a list value like this: spam = [...

    Comma Code (Using Python Language) Say you have a list value like this: spam = [ 'apples' , 'bananas' , 'tofu', 'cats' ] Write a function that takes a list value as an argument and returns a string will all the items separated by comma and space, with and inserted before the last item. For example, passing the previous spam list to the function would return 'apples, bananas, tofu and cats'. but your function should be able to work with...

  • The following code below must be able to run using linked list instead of using arrays...

    The following code below must be able to run using linked list instead of using arrays ------------------------------------------------------------------------------------------------------- #include #include using namespace std; //Class for standard deviation class stdDev { private:        int max;        double value[100];        double mean; public:        double CalMean()        {               double sum = 0;               for (int i = 0; i < max; i++)                      sum += value[i];               return (sum / max);        }        double CalVariane()        {               mean = CalMean();...

  • Can you write the following code without using any logical operators? cout << “Are you hungry?...

    Can you write the following code without using any logical operators? cout << “Are you hungry? (0-no, 1, yes) << endl; cin >> choice; if(choice == 1 && store == OPEN) { //get some food from the store } else if(choice == 1 && store == CLOSED) { //make some food at home } else { //I’m not hungry }

  • Use the FDR to design and write a function, maxValTimes, that takes a list of integers...

    Use the FDR to design and write a function, maxValTimes, that takes a list of integers and returns a set containing the value(s) that occur the same number of times as the maximum value in the list. If the list is empty, return an empty set.   For example if the list was [2,1,1,2,3,3,1] the function would return {2,3} as the maximum value is 3 which occurs twice, and 2 also occurs twice (but 1 occurs 3 times). For full marks,...

  • Using Python3, How can I aggregate the output from each iteration to one single list? Because...

    Using Python3, How can I aggregate the output from each iteration to one single list? Because from the program I have right now, it gives me the result of every iteration and gives me something like this as individual results and not one large list: None None ... None 130.0 None ... 1764 1765 None To clarify, I have this program where it calculates the sum of bytes sent within each incrementation of every 1 second, but there are instances...

  • Using Python3, I have this list called L1 which contains the following: L1 = [4420, 4421,...

    Using Python3, I have this list called L1 which contains the following: L1 = [4420, 4421, 4422, 4423, 4424, 4425, 4427, 4435, 5135, 5136, 5137, 5139, 5149, 6216, 6217, 6218, 6219, 6221, 6232, 7458, 7459, 7460, 7461, 7462, 7464, 7473] However, I need to filter this list by intervals of 8 and record the first instance where it is inside this interval, and the filtered outputs are then put into a separate list called L2. To clarify what I mean,...

  • Could anyone help add to my python code? I now need to calculate the mean and...

    Could anyone help add to my python code? I now need to calculate the mean and median. In this programming assignment you are to extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file. You are to create a program called numstat2.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The...

  • How would I code this method in Java using a scanner? Begin by asking how many...

    How would I code this method in Java using a scanner? Begin by asking how many spaces the row should be with a prompt using System.out.print like the following, with 40 as the maximally allowed number of spaces: (user input shown bold) Please enter the number of spaces for the game (1-40): eight To do this, use the promptNumberReadLine method that you will write, described on the back page. If the user does not type a number in the correct...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT