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
  • If possible, write this code in object oriented programming, if not in object oriented that's okay...

    If possible, write this code in object oriented programming, if not in object oriented that's okay I can convert it. All in C++ language Generate a list of random numbers using srand and rand, sort the random numbers and put the sorted list into a text file (.txt) using for loop and use stream insertion operator to a text file. Also, put it in a binary file (not using a for loop or stream insertion [use right system caller]). All...

  • 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();...

  • In python and without using Libraries (comment the code and please don't use lambda code): Write...

    In python and without using Libraries (comment the code and please don't use lambda code): Write a function called mostfrequent that takes one string argument containing a list of words separated by commas (e.g., “apple,banana,apple,pear”, note there are no spaces). The function must return the word that occurs most frequently in the input string. You can assume there will be no ties in frequency. Examples: mostfrequent(“apple,apple,banana,apple,peach,banana”)  “apple” mostfrequent(“apple,banana,banana,apple,peach,banana”)  “banana” mostfrequent(“apple”)  “apple”

  • 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...

  • 1. Using list comprehension and a single line of code, flatten a given list of lists....

    1. Using list comprehension and a single line of code, flatten a given list of lists. For example your line of code must give [1, 2, 3, 4, 5, 6] out of [[1, 2], [3, 4], [5, 6]]. 2. Using list comprehension, and without using any imported module, write a Python function to return a list of email addresses in a given phrase of text. Inside your function you must do this with a single line of code. For example,...

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