Showing posts with label dictionaries. Show all posts
Showing posts with label dictionaries. Show all posts

PYTHON ASSIGNMENT 6: DICTIONARIES



Write a program to generate a dictionary called phonebook containing the list of customers and their telephone numbers.
Lookup: Read a customer name and print the associated entry from the phonebook on a new line in the form name=phonenumber; If an entry for name is not found, print “Not found" instead.
Reverse Lookup: Given a phone number find the customer details. If number is not present, print “No match found”

Create a dictionary containing employee name and id (name:id). Display
Employee names, sorted in alphabetical order
Employee name and id, sorted alphabetically by name
Inverse the dictionary (id : name)

Consider two lists
NAMES = [’Alice’, ’Bob’, ’Cathy’, ’Dan’, ’Ed’, ’Frank’, ’Gary’, ’Helen’, ’Irene’, ’Jack’, ’Kelly’, ’Larry’]
AGES = [20, 21, 18, 18, 19, 20, 20, 19, 19, 19, 22, 19]
These lists match up, so Alice’s age is 20, Bob’s age is 21, and so on. Write a program that
Combines these lists into a dictionary.
Reads a name and returns the age.
Reads an age and returns the names of all the people who are that age.

Histogram: Read a string and create a dictionary containing the frequency of the characters in string.

Histogram: Read a string and create a dictionary containing the frequency of the words in string.

Histogram - String - Word frequency

# Histogram - String - Word Frequency

import operator

string1 = "An algorithm is a step-by-step procedure to solve a given problem. It is a well-defined computational procedure that takes some values as input, manipulates them using a set of instructions and produces some values as output and terminates in a finite amount of time. An algorithm, when formally written in a programming language is called a program, or code. Derivation of an algorithm that solves the problem and conversion of the algorithm into code, together,  is known as Algorithmic Problem Solving."
string1 = string1.lower()

string1 = string1.replace(".", "")
string1 = string1.replace(",", "")
string1 = string1.replace("-", " ")

List1 = string1.split(" ")

dict1 = {}
for ele in List1:
    if ele in dict1:
        dict1[ele] += 1
    else:
        dict1[ele] = 1
print("Histogram")
print("Length of dictionary = ", len(dict1))
print(dict1)

List2 = sorted(dict1.items(), key=operator.itemgetter(1), reverse=True)

dict2 = dict(List2[:3])
print("Most frequently used 3 words:")
print(dict2)

Histogram - String - Character frequency

# Histogram - Character frequency

import operator

string1 = "An algorithm is a step-by-step procedure to solve a given problem. It is a well-defined computational procedure that takes some values as input, manipulates them using a set of instructions and produces some values as output and terminates in a finite amount of time. An algorithm, when formally written in a programming language is called a program, or code. Derivation of an algorithm that solves the problem and conversion of the algorithm into code, together,  is known as Algorithmic Problem Solving."
string1 = string1.lower()

dict1 = {}
for idx in range(97, 123, 1):
    count = string1.count(chr(idx))
    if(count != 0):
        dict1[chr(idx)] = count

print("Histogram")
print(dict1)


List1 = sorted(dict1.items(), key=operator.itemgetter(1), reverse=True)
dict2 = dict(List1[:5])
print("Most frequent 5 characters")
print(dict2)