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

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 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...
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,
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...
Write a C++ program that reads a students name followed by 7 numeric grades from a...
Write a C++ program that reads a students name followed by 7 numeric grades from a file.  Compute the average grade after dropping the  lowest grade.   Assign letter grades via this scale .Please show steps in the comments . A 90 – 100 B 80  --89 C 70 –79 D 60 -69 F 0  - 59 Input format: Sam 100 90 87 23 12 67 95 Mary 30 20 90 90 90 90 88 Mark 80 90 80 80 90 87 100 End of file...
Write a program that reads three values from the keyboard representing, respectively, an investors name, an...
Write a program that reads three values from the keyboard representing, respectively, an investors name, an investment amount, and an interest rate (you will expect the user to enter a number such as .065 for the interest rate, representing a 6.5% interest rate) . Your program should calculate and output (in currency notation) the future value of the investment in 10, 2 20 an 30 years using the following formula:   Future value =investment(1+interest rate)year Example of a test run: Enter...
Write a program that defines an animal data type, with an animal name, age, and category...
Write a program that defines an animal data type, with an animal name, age, and category (cat, dog, etc.), as well as an animal array type that stores an array of animal pointers. (ex. structType *arr[size];) Your program will prompt the user to enter the data for as many animals as they wish. It will initialize a dynamically allocated animal structure for each animal, and it will store each animal in an instance of the animal array structure. Your program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT