Question

In: Computer Science

PYTHON: Develop an algorithm for finding the most frequently occurring value in a list of numbers....

PYTHON: Develop an algorithm for finding the most frequently occurring value in a list of numbers. Use a sequence of coins. Place paper clips below each coin that count how many other coins of the same value are in the sequence. Give the pseudocode for an algorithm that yields the correct answer, and describe how using the coins and paper clips helped you find the algorithm.

Please solve in python!

Solutions

Expert Solution

Lets consider we have 4 coins

first coin : value = 1
second coin : value = 2
third coin : value = 5
fourth coin : value = 10

Creating a random sequence of coins :

1   1   2   1   10   2   5   10   10   5   2   1    1

To decide how many paper clips to be put below coins of value 1, we need to count total number of coins with value as 1
so , count(coin of value 1) => 5
similarly ,repeat same thing for remaining coins,
count(coin of value 2) => 3
count(coin of value 5) => 2
count(coin of value 10) => 3

to find the coin with maximum occurance , we need to find the max of counts that we have found for each of the coins.

Find the code below explanation of each step given in comments :

python version used : 3.7.0

-----------------------------------------------------------------------------------------------------------------------------------

coins=[1,1,2,1,10,2,5,10,10,5,2,1,1] #input given to us is the sequence of coins

#step1
#counting number of similar coins by iterating over the list
#for storing the coin value and its occurance we will use a data structure called dictionary (map in other languages)

occurance_map = {} #initializing an empty map , this map will have coin value as key and number of occurances as value


for i in range(0,len(coins)): #iterating over all the coins
if(coins[i] not in occurance_map.keys()): #if there is no entry of that coin in occurance_map then make one and set the value to 1
occurance_map[coins[i]] = 1
else : #if there is already an entry of that coin then just increase the value by 1
occurance_map[coins[i]] = occurance_map[coins[i]] + 1

#step 2
#once the occurance map is ready , the only thing remaining is finding the key value pair where value is the highest
#we will do this by iterating over the disctionary


max_occurance = 0 #this variable keeps track of max occurance
coin_with_max_occurance = 0
for key in occurance_map:
if(occurance_map[key] > max_occurance): #if occurance of a coin is greater than max_occurance then update the value of max_occurance and coint_with_ma_occurance
coin_with_max_occurance = key
max_occurance = occurance_map[key]


print ("The coin which occured most number of times is : "+str(coin_with_max_occurance))
print ("The total number of occurances is :"+str(max_occurance))


  


Related Solutions

The mode is which of the following? The most frequently occurring value in a data set...
The mode is which of the following? The most frequently occurring value in a data set The middle most occurring value in a set of values The difference between the highest and lowest values in a set The arithmetic average of a set of values.
Write a python program to finish a big-data processing task --- finding out most frequently used...
Write a python program to finish a big-data processing task --- finding out most frequently used words on Wikipedia pages. The execution of the program generates a list of distinct words used in the wikipedia pages and the number of occurrences of each word on these web pages. The words are sorted by the number of occurrences in ascending order. The following is a sample of output generated for 4 Wikipedia pages. 126 that 128 by 133 as 149 or...
// For an input string of words, find the most frequently occurring word. In case of...
// For an input string of words, find the most frequently occurring word. In case of any ties, report any in output. // Your algorithm should be of the runtime O(n) time where n is the number of words in the string. #include <iostream> #include <vector> #include <sstream> using namespace std; string findWord(vector<string>& tokens); int main() {    string line = "FDo all the good you can, by all the means you can,                    in all...
Write a method called mode that returns the most frequently occurring element of an array of...
Write a method called mode that returns the most frequently occurring element of an array of integers. Assume that the array has at least one element and that every element in the array has a value between 0 and 100 inclusive. Break ties by choosing the lower value. For example, if the array passed contains the values [27, 15, 15, 11, 27], your method should return 15. write a version of this method that does not rely on the values...
The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to...
The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to any given limit,” Write a method called sieve that takes an integer parameter, n, and returns a boolean array that indicates, for each number from 0 to n - 1, whether the number is prime. In Java
Analyzing Selection Sort Algorithm The selection sort algorithm works by first finding the smallest value in...
Analyzing Selection Sort Algorithm The selection sort algorithm works by first finding the smallest value in a list and swapping the first value with it, then finding the second smallest value and swapping the second value with it, continuing until all the values are in order. Implement this algorithm, then determine its growth function, and hence the order of the algorithm. Find how many swaps are made. Use Java Code to create algorithm
Develop a python program to convert two decimal numbers (A and B) to binary numbers. You...
Develop a python program to convert two decimal numbers (A and B) to binary numbers. You should generate B complement signal (flip all the bits of Input B,
In Python syntax, create a list of 10 numbers (any numbers). Create your list 3 times,...
In Python syntax, create a list of 10 numbers (any numbers). Create your list 3 times, each time using a different syntax to define the list. Write a while loop that prints the numbers from 1 to 10. Convert your previous loop into a for loop. Rewrite your for loop to only have one number in the range. Write a for loop to print out each item of your list of 10 numbers, all on the same line, separated by...
In Python syntax, create a list of 10 numbers (any numbers). Create your list 3 times,...
In Python syntax, create a list of 10 numbers (any numbers). Create your list 3 times, each time using a different syntax to define the list. Write a while loop that prints the numbers from 1 to 10. Convert your previous loop into a for loop. Rewrite your for loop to only have one number in the range. Write a for loop to print out each item of your list of 10 numbers, all on the same line, separated by...
Write an algorithm that finds both the smallest and largest numbers in a list of n...
Write an algorithm that finds both the smallest and largest numbers in a list of n numbers. Try to find a method that does at most 1.5n comparisons of array items.(but please code in java).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT