Question

In: Computer Science

Python Program : simplist form and include comments Shopping Data by Zip Code Have you ever...

Python Program : simplist form and include comments

Shopping Data by Zip Code

Have you ever noticed that some stores ask you for your zip code before “ringing up” your transaction? Suppose you have been given three (3) text files (named kroger.txt, publix.txt, and ingles.txt) from popular grocery stores, containing zip codes of customers at each store. Additionally, you have been given a file named zipDirectory.txt which contains the zip code and city name (each on its own line). This file will be used as the "dictionary"/"lookup" for zip codes.

Text Files for this problem: Lab7Files.zip

You have been asked to write a Python program to complete the following tasks:

  • Read in the data from the first 3 text files into three (3) sets named kroger, publix, and ingles, respectively.
  • Read in the data from the last text file into a dictionary named zipDirectory.
  • Find and display the zip codes from all of the stores
  • Find and display the zip codes that are in the Kroger file but are not in the Ingles file
  • Find and display the zip codes that appear in the Publix file and the Kroger file
  • Find and display the total number of shoppers from City5

zipDirectory file

11111
City1
22222

public txt file

55555
66666

ingles txt file

77777
88888
99999
22222
44444

kroger.txt

11111
22222
33333
44444
55555

Solutions

Expert Solution

Python3 code:

# opening the zipDirectory.txt file
zipDirectory_filename="zipDirectory.txt"
zipDirectory_file=open(zipDirectory_filename, 'r')
# make a dictionary to store cityname as key and zip code as value
zipDirectory={}

for line in zipDirectory_file.readlines():
   line=line.strip()
   # check if line is not empty
   if line:
       line=line.split()
       zipDirectory[line[1]]=line[0]

# opening the kroger.txt file
kroger_filename="kroger.txt"
kroger_file=open(kroger_filename, 'r')
# make a set to store customers from kroger
kroger_data=set({})

for line in kroger_file.readlines():
   line=line.strip()
   # check if line is not empty
   if line:
       # adding code in kroger set
       kroger_data.add(line)

# opening the publix file
publix_filename="publix.txt"
publix_file=open(publix_filename, 'r')
# make a set to store customers from publix
publix_data=set({})

for line in publix_file.readlines():
   line=line.strip()
   # check if line is not empty
   if line:
       # adding code in publix set
       publix_data.add(line)

# opening the ingles file
ingles_filename="ingles.txt"
ingles_file=open(ingles_filename, 'r')
# make a set to store customers from ingles
ingles_data=set({})

for line in ingles_file.readlines():
   line=line.strip()
   # check if line is not empty
   if line:
       # adding code in ingles set
       ingles_data.add(line)

# make a set of all zipcodes
all_zipcodes=set({})
# add codes from kroger, publix, ingles sets into all_zipcodes set
all_zipcodes.update(kroger_data)
all_zipcodes.update(publix_data)
all_zipcodes.update(ingles_data)

print("Displaying the zip codes from all of the stores:")
for code in all_zipcodes:
   # print each code from all stores
   print(code)

print("Displaying the zip codes that are in the Kroger file but are not in the Ingles file")
for code in kroger_data:
   # for all code in kroger data check if code not present in ingles set
   if code not in ingles_data:
       print(code)

print("Displaying the zip codes that appear in the Publix file and the Kroger file")
for code in publix_data:
   # for all codes in publix check if code also present in kroger
   if code in kroger_data:
       print(code)

# get city5 zipcode from zipDirectory dictionary
city5_code=zipDirectory['City5']
# count variable counts number of customers from city5 in all stores
count=0
# add to count if customer code present in sets of three stores
if city5_code in kroger_data:
   count+=1
if city5_code in ingles_data:
   count+=1
if city5_code in publix_data:
   count+=1
print("Displaying the total number of shoppers from City5")
print(count)

Output:


Related Solutions

write code with proper comments for ever step the code should be in form of pseudocode                            &
write code with proper comments for ever step the code should be in form of pseudocode                                    To Print Triangle of any size, by taking size as input. To Print Triangle of any size, by taking size as input. If size is 4 then triangle is: To calculate Factorial of any number. To calculate the power of any given number. To Print Table of Any Number up till 10: as shown in this figure. for a program which reads 10 integers...
This is a python program. Put comments explaining the code, please. Suppose you have been tasked...
This is a python program. Put comments explaining the code, please. Suppose you have been tasked with writing a Python program (using linked lists) to keep track of computer equipment. For each piece of equipment, we track its name, purchase date, purchase amount, and quantity on hand. Write a program that completes the following tasks: allow the user to add a piece of equipment to the front of the list; allow the user to update the quantity of a piece...
Please include comments on what you are doing.   Using linked lists, write a Python program that...
Please include comments on what you are doing.   Using linked lists, write a Python program that performs the following tasks: store the records for each college found in the input file - colleges.csv - into a linked list. (File includes name and state data fields) allow the user to search the linked list for a college’s name; display a message indicating whether or not the college’s name was in the database allow the user to enter a state's name and...
The code that creates this program using Python: Your program must include: You will generate a...
The code that creates this program using Python: Your program must include: You will generate a random number between 1 and 100 You will repeatedly ask the user to guess a number between 1 and 100 until they guess the random number. When their guess is too high – let them know When their guess is too low – let them know If they use more than 5 guesses, tell them they lose, you only get 5 guesses. And stop...
python code You are to write a program which produces a triangle as seen below. •Include...
python code You are to write a program which produces a triangle as seen below. •Include a function named goingUpwhich accepts a value and prints lines of stars beginning at 1 star and ending at the number of stars which was sent to it. For example, if the value sent to it is 4, the function would print this:********** •Include a function named goingDown which accepts a value and prints lines of stars beginning at 1 LESS THAN the value...
Python This week you will write a program in Python that mimics an online shopping cart...
Python This week you will write a program in Python that mimics an online shopping cart . You will use all the programming techniques we have learned thus far this semester, including if statements, loops, lists and functions. It should include all of the following: The basics - Add items to a cart until the user says "done" and then print out a list of items in the cart and a total price. - Ensure the user types in numbers...
Please write python code for the following. Implement the functions defined below. Include a triple-quoted comments...
Please write python code for the following. Implement the functions defined below. Include a triple-quoted comments string at the bottom displaying your output. Using sets (described in Deitel chapter 6) will simplify your work. Below is the starter template for the code: def overlap(user1, user2, interests): """ Return the number of interests that user1 and user2 have in common """ return 0    def most_similar(user, interests): """ Determine the name of user who is most similar to the input user...
In python make a simple code. You are writing a code for a program that converts...
In python make a simple code. You are writing a code for a program that converts Celsius and Fahrenheit degrees together. The program should first ask the user in which unit they are entering the temperature degree (c or C for Celcius, and f or F for Fahrenheit). Then it should ask for the temperature and call the proper function to do the conversion and display the result in another unit. It should display the result with a proper message....
Python This week you will write a program in Pyhton that mimics an online shopping cart...
Python This week you will write a program in Pyhton that mimics an online shopping cart . You will use all the programming techniques we have learned thus far this semester, including if statements, loops, lists and functions. It should include all of the following: The basics - Add items to a cart until the user says "done" and then print out a list of items in the cart and a total price. - Ensure the user types in numbers...
write this program in java... don't forget to put comments. You are writing code for a...
write this program in java... don't forget to put comments. You are writing code for a calendar app, and need to determine the end time of a meeting. Write a method that takes a String for a time in H:MM or HH:MM format (the program must accept times that look like 9:21, 10:52, and 09:35) and prints the time 25 minutes later. For example, if the user enters 9:21 the method should output 9:46 and if the user enters 10:52...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT