Question

In: Computer Science

Write a Python program to count occurrences of items (and retrieve the most 3 or least...

  1. Write a Python program to count occurrences of items (and retrieve the most 3 or least 3 words).
  2. Write a Python program to sort a dictionary by keys or values in ascending or descending order by 2 methods.

Solutions

Expert Solution

Program 1:

l=[1,1,1,1,1,2,2,2,2,3,3,3,4,5,6]
#In the above list the most three occurrences are 1,2,3 and least three occurrences are 4,5,6
d=dict()
for i in l:
d[i]=0
  
for i in l:#for each element in list
d[i]+=1#increment count value in dictionary
  
#sort the dictionary by value and get the results
l=sorted(d.items())#obtain the list of tuples
l.sort(key=lambda i:i[1])#sort the list based on second field in ascending order
print("The least three occuring elements are :")
for i in range(3):
print(l[i][0])

l.sort(key=lambda i:i[1],reverse=True)#sort the list based on second field in descending order
print("The most three occuring elements are :")
for i in range(3):
print(l[i][0])

Program 2:

d={1:"word1",3:"temp",2:"value3"}

#sorting by key
print("Sorting by Key in ascending order")
temp=(sorted(d.items()))
for i in temp:
print(i[0],"=>",i[1])#print after Sorting

print("Sorting by Value in asscending order")
#Sorting by value
l=[]
for k,v in d.items():
l.append((k,v))#append the tuples to list

l.sort(key=lambda i:i[1])#sort using second element of tuple which is value of dict
for i in l:
print(i[0],"=>",i[1])#print after Sorting


Related Solutions

In this program: ================================================================== /* Program to count number of occurrences of a given string in...
In this program: ================================================================== /* Program to count number of occurrences of a given string in original string*/ #include <iostream> #include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() { const int SIZE = 40; char str[SIZE]; char str1[SIZE]; char searchString[SIZE]; int n; int l1, l2; int count = 0; printf("Enter a sentence: \n"); fgets(str,SIZE,stdin); printf("Enter a search word: \n"); fgets(searchString,SIZE,stdin); if (str1[strlen(str1) - 1] == '\n') { str1[strlen(str1)-1] = '\0'; } if (str[strlen(str) - 1] == '\n')...
Given a string, such as x = ‘itm330’, write a Python program to count the number...
Given a string, such as x = ‘itm330’, write a Python program to count the number of digits and the number of letters in it. For example, in ‘itm330’, there are 3 letters and 3 digits. Hint: Very similar to page 11 on the slides. To check if a character c is a digit, use c.isdigit(). If c is a digit, c.isdigit() will be a True.
for Python 3 Write a python program that Creates a list that has these values in...
for Python 3 Write a python program that Creates a list that has these values in this order, 'Python', 'JavaScript', and 'PHP' Define a displayMyClasses function that sorts the list and then displays each item on the list, one per line, in sorted order. Also, number the displayed list as shown in the "Running a Sample Program" shown below. Define a guessNext function that selects a random element from the list and returns it to the call of the function....
A customer comes into a grocery store and buys 8 items. Write a PYTHON program that...
A customer comes into a grocery store and buys 8 items. Write a PYTHON program that asks for the name of the item and the price of each item, and then displays these on the screen. Refer to the print() and input() statements covered in the module. Do NOT use loops as it will be covered in later modules. Apples 2.10 Hamburger 3.25 Milk 3.49 Sugar 1.99 Bread 1.76 Deli Turkey 7.99 Pickles 3.42 Butter 2.79
A customer comes into a grocery store and buys 8 items.   Write a PYTHON program that...
A customer comes into a grocery store and buys 8 items.   Write a PYTHON program that prompts the user for the name of the item AND the price of each item, and then simply displays whatever the user typed in on the screen nicely formatted.
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a numeric grade (on a scale of 0-100) from the user and convert it to a letter grade based on the following table. A: 90% - 100% B 80% - 89% C 70% - 79% D 60% - 69% F <60% The program should be written so that if the user entered either a non-numeric input or a numeric input out of the 0-100 range,...
USE Python 3, please type thanks! Write a Python 3 program to calculate the Body Mass...
USE Python 3, please type thanks! Write a Python 3 program to calculate the Body Mass Index (BMI) of a person if they enter their weight and their height to your program. Check out the formula here: http://www.freebmicalculator.net/calculate-bmi.php Your program should first print "Body Mass Index Calculator" The program will then ask the user if they want to enter Metric Units or English Units. Using the appropriate formula (see link above) calculate their BMI. Depending on their BMI show their...
Write a Python program to (a) create a new empty stack. Then, (b) add items onto...
Write a Python program to (a) create a new empty stack. Then, (b) add items onto the stack. (c) Print the stack contents. (d) Remove an item off the stack, and (e) print the removed item. At the end, (f) print the new stack contents:
1) a. Write a C++ program for the recursive algorithm that removes all occurrences of a...
1) a. Write a C++ program for the recursive algorithm that removes all occurrences of a specific character from a string b. Write the pseudocode for the program.
Python 1. A salesman can sell five different items. Write a program that lets the salesman...
Python 1. A salesman can sell five different items. Write a program that lets the salesman enter the quantity of each item sold, calculates the total sales, and prints as below. Use a for loop to ask the salesman how many of each product he sold. tem 1 $2.50 Item 2 $1.98 Item 3 $5.75 Item 4 $3.45 Item 5 $4.00 2. Rewrite program #1 using a for loop to run for 3 salesmen and print the total sales for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT