Question

In: Computer Science

Write a program that: (a) Reads name and age of 10 different persons (b) Stores this...

Write a program that:

(a) Reads name and age of 10 different persons

(b) Stores this information in a dictionary (remember, dictionary contains keys and values)

(c) Sorts your dictionary based on keys

(d) Sorts your dictionary based on values

(e) Displays the original and both sorted dictionaries (step c and d)

(f) You search for a specific person and the program displays their name and age on the screen. If the person’s information is not in the dictionary, the program displays “Person not Found”.

NOTE

Answer should be in python. and according to the different types of sorting algorithms including Monkey Sort, Bubble Sort and Selection Sort.

Solutions

Expert Solution

# I have created a list of tuples to just for example.

persons = [('jack', 32), ('rachel', 35), ('ross', 22), ('raphael', 54), ('andrew', 28), ('jackson', 52),
           ('mike', 12), ('emma', 29), ('monica', 30), ('ryan', 43)]

#This function wil read and convert the list into a dictionary.
def read_person(persons):
  person_dict = {}
  for i in persons:
    person_dict[i[0]] = i[1]
  return person_dict

# this function will sort the dictionary using keys. I am using sorted function to sort the dict.It may not be available in older python version.
def sort_keys(person_dict):
  dict_by_keys = {}
  sorted_keys = sorted(person_dict.items())
  for i in sorted_keys:
    x = i[0]
    y = i[1]
    dict_by_keys[x] = y  
  return dict_by_keys

# this function will sort the dictionary using values. I am using sorted function to sort the dict.It may not be available in older python version.
def sort_values(person_dict):
  dict_by_values = {}
  d_sorted_by_value = sorted(person_dict.items(), key=lambda x: x[1])
  for i in d_sorted_by_value:
    x = i[0]
    y = i[1]
    dict_by_values[x] = y  
  return dict_by_values


# displaying all the dictionaries.
person_dict = read_person(persons)
sort__by_key = sort_keys(person_dict)
sort_by_value = sort_values(person_dict)
print("Original Dict:", person_dict)
print()
print("Sorted by keys dict:",sort__by_key)
print()
print("Sorted by values dict:",sort_by_value)

#This function will search the entered name and display it's name along with it's age. It will ask for an input 
def search_person(person_dict):
  key = input('Enter the name:')
  if key in person_dict.keys():
    print("Name: {}, Age: {}".format(key, person_dict[key]))
  else:
    print("Person not found")

search_person(person_dict)

Please let me know ,If you have any doubts.


Related Solutions

Write a program in python that reads the elements of a set from the keyboard, stores...
Write a program in python that reads the elements of a set from the keyboard, stores them in a set, and then determines its powerset. Specifically, the program should repeatedly ask the user: Enter one more element ? [Y/N] If the user answers Y then an new element is read from the keyboard: Enter the new element in the set: This cycle continues until the user answers N to the first question. At that point the program shall compute the...
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
Write a program that reads in the name and salary of an employee. Here the salary...
Write a program that reads in the name and salary of an employee. Here the salary will denote an hourly wage, such as $9.25. Then, ask how many hours the employee worked in the past week. Be sure to accept fractional hours. Compute the pay. Any overtime work (over 40 hours per week) is paid at 150 percent of the regular wage.4 pts Your code with comments A screenshot of the execution Test Cases: Enter name: Jorge Enter wage: 9.25...
Write a program in c that reads the content from the file and stores each line...
Write a program in c that reads the content from the file and stores each line in an int array in heap(using dynamic memory allocation). For example, let the file has elements following (we do not know the size of files, it could be above 100,000 and contents of the file and make sure to convert file elements to int): 10067 26789 6789 3467
C Programming Write a program in C that reads in a file, stores its contents as...
C Programming Write a program in C that reads in a file, stores its contents as a character array/pointer (char*) into an unsigned character array/pointer (unsigned char* message). Note: the input file can have one line or multiple lines and vary in length
Design and write a python program that reads a file of text and stores each unique...
Design and write a python program that reads a file of text and stores each unique word in some node of binary search tree while maintaining a count of the number appearance of that word. The word is stored only one time; if it appears more than once, the count is increased. The program then prints out 1) the number of distinct words stored un the tree, Function name: nword 2) the longest word in the input, function name: longest...
Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
10) Create a java program that will ask for name and age with method age where...
10) Create a java program that will ask for name and age with method age where it will check the condition that id you are greater than 18 you can vote.
Part A In PyCharm, write a program that prompts the user for their name and age....
Part A In PyCharm, write a program that prompts the user for their name and age. Your program should then tell the user the year they were born. Here is a sample execution of the program with the user input in bold: What is your name? Amanda How old are you? 15 Hello Amanda! You were born in 2005. Write the program. Format your code using best practices. Refer to the zyBooks style guide, if needed, to use proper naming...
C program, please Write a program that reads a sequence of 10 integer inputs and prints...
C program, please Write a program that reads a sequence of 10 integer inputs and prints the smallest and largest of the inputs and the number of even and odd inputs. for a beginner please, you could use a while loop,if-else,
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT