Question

Help needed with Python 3: Dictionaries and Sets.

The problem is one that asks the user to create a completed program that prompts the user for the name of a data file, and reads the contents of that data file, then creates variables in a form suitable for computing the answer to some questions.

The format should be in the same manner as the attached .py file. All subsequent assignment details (.py and .csv files) can be found at this media fire link: http://www.mediafire.com/folder/qn5fao1hzlxid/Definitions_and_Sets_Data

Thank you in advance for all your help! Please feel free to write in the comment lines to help further my understanding of your proccess!

The instructions below state that your work must appear in several functions. Empty versions of these functions are providedThe Eaters and the Eaten Problem description Your task in this assignment is to analyze the data on food-consumption behaviorFunctions read food file(filename) and dump data(eat data) The first function will open the file provided and create a dictioFunction compute_fussiest_eaters (eat_data) This fourth function uses the dictionary constructed by read_food_file() and look

The instructions below state that your work must appear in several functions. Empty versions of these functions are provided to you with this assignment in assign6.py Although you are welcome to add more functions that help with the writing of your solution, your final solution must (a) include the main function precisely as it appears in the provided assign6.py and (b) include no additional global code (i.e., code outside of any function). Failure to do so many result in a failing grade for this assignment.
The Eaters and the Eaten Problem description Your task in this assignment is to analyze the data on food-consumption behavior Several files are provided containing data regarding which organisms eat which foods (with "foods" here possibly other organisms). The data itself is not rigorously scientific (!!) and is rather structured in a such a way as to give you practice working with Python programs using dictionaries and sets. All provided data files use a comma-separated format. Each line in the file contains data about the eating habits of an organism and is provided in this format: ,, , . . ., For example, the third line in food01.csv is: jackal,grass,zebra,gazelle which means that jackals eat grass, zebras, and gazelles. The last line in food01.csv is: zebra,grass which means that zebras eat only grass. The completed program prompts the user for the name of a data file and will read the contents of that data file, and create variables in a form suitable for computing the answer to some questions. Below is a transcript of one execution of a completed program $python assign6.py Name of CSV file? food01.csv gazelle: acacia leaves, grass jackal: gazelle, grass, zebra lion: gazelle zebra: grass Only eaters: lion, jackal Only eaten: acacia leaves, grass Fussiest eaters: lion, zebra Tastiest entry:grass The functions listed in the remainder of this assignment description are best completed in their order of appearance
Functions read food file(filename) and dump data(eat data) The first function will open the file provided and create a dictionary of lists. That is each entry in the dictionary will have as its key an eater (i.e., first item in a line) and as a value all of the things which that eater will eat (i.e., second and following items in a line). All items in a line are separated by commas and you can assume all files provided have the correct format. However, you should print out an error message if the file corresponding to the filename doesn't exist, afterwards returning an empty dictionary The second function will print a readable version of the data constructed by the first function. Also note the eaters are printed in sorted order (i.e., alphabetical), and within each line the eaten are also printed in sorted order. For example, the output expected from this function given food01.csv is seen below. In this output, note the use of colons, commas, spaces, and the alphabetical order of eaters and eaten gazelle: acacia leaves, grass jackal: gazelle, grass, zebra lion: gazelle zebra: grass Function compute only eaters (eat data) This third function uses the dictionary constructed by read_food_file() and computes the eaters who are also not eaten. Once this is computed, these eaters are returned in a list in alphabetical order. For example, the output expected from this function given foodo1.csv is used by the main) function to produce this output line: Only eaters: lion, jackal Function compute only_eaten(eat data) This fourth function uses the dictionary constructed by read_food_file) and computes all which is eaten but which is not also an eater. (Hint: Something which is eaten but is not an eater will have no key in the dictionary.) Once computed the only eaten items are returned in a list in alphabetical order. For example, the output expected from this function given food01.csv is used by the main() function to produce this output line Only eaten: acacia leaves, grass
Function compute_fussiest_eaters (eat_data) This fourth function uses the dictionary constructed by read_food_file() and looks again at all of the eaters. However, the function will determine the eaters having the fewest number of items eaten. (Hint: You will want to compute the minimum of the eaten-item list lengths.) Once this is computed, the fussiest eaters are returned in a list in alphabetical order. For example, the output expected from this function given food01.csv is used by the main) function to produce this output line: Fussiest eaters: lion, zebra Function compute_most_delicious (eat_data) This last function uses the dictionary constructed by read_food_file() and looks again at all of the eaten. However, the function will determine which, of those that are eaten, have the largest number of eaters. (Hint: You may want to compute a local dictionary where each of the eaten is a key and the corresponding value is a list of those that eat the eaten.) Once computed, the most delicious of the eaten is returned in a list in alphabetical order. For example, the output expected from this function given food01.csv is used by the main() function to produce this output line Tastiest entry: grass What you are given A starter program named assign6.py containing a main) function plus empty versions of the functions you must complete. Six comma-separate tet files . The output expected from your program for each of the six provided data files. What you are to submit A completed solution named assign6.py Please ensure your program only outputs data required by the assignment. Any extra print statements used for debugging must be either deleted or commented out before submission.
0 0
Add a comment Improve this question Transcribed image text
Answer #1
# Please find the required solution
# I have given solution for read_from_file and dump_data
# please ask rest of them as separate question

import os.path
from os import path

def read_food_file(filename):
   dictionary = {}
   if not path.exists(filename):
      print(filename, ' doesnot exist')
   else:
      food_file = open(filename)
      entries = food_file.readlines()
      for line in entries:
         data = line.rstrip('\n').split(',')
         dictionary[data[0]] = data[1:]
   return dictionary

def dump_data(eat_data):
   for key in sorted(eat_data):
      eat_data[key].sort()
      print(key, ":", ', '.join(eat_data[key]))
def main():
   food_file=input('Name of the CSV file? ')
   eat_data=read_food_file(food_file)
   dump_data(eat_data)
   
if __name__ == "__main__":
   main()

Sample output:

food01.csv

jackle,grass,zebra,gazella
gazella,acacia leaves,grass
lion,gazella
zebra,grass

$ python assign6.py Name of the CSV file? a1.cSV al.csv doesnot exist kasula@N-20HEPFOWS81U MINGW64 /c/HDM/code/npm/trunk/MyL

Add a comment
Know the answer?
Add Answer to:
Help needed with Python 3: Dictionaries and Sets. The problem is one that asks the user to create...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Assignment: You are to create seven classes that represent a Zoo. The classes are define as follo...

    Assignment: You are to create seven classes that represent a Zoo. The classes are define as follows: 1. Zoo 2. Enclosure 3. Animal 4. Crocodile 5. Gazelle 6. Lion 7. Zebra 1. Zoo: Required member variables: private String name; private String address; private Enclosure[] enclosures; private int area; private double budget; 2. Enclosure: Required member variables: private String biome; private Animal[] animals; 3. Animal Required member variables: private String name; private String genus; private String species; private Zoo currentZoo; protected...

  • (IN PYTHON) You are to develop a Python program that will read the file Grades-1.txt that...

    (IN PYTHON) You are to develop a Python program that will read the file Grades-1.txt that you have been provided on Canvas. That file has a name and 3 grades on each line. You are to ask the user for the name of the file and how many grades there are per line. In this case, it is 3 but your program should work if the files was changed to have more or fewer grades per line. The name and...

  • I need help on these functions PYTHON 3. The information about the functions is in the...

    I need help on these functions PYTHON 3. The information about the functions is in the first two pics. Scenario The Pokemon franchise consists of over 60 video games, 15 movies, several TV shows, a trading card game, and even a musical. It has been around for over two decades and at this point has fans of all ages. Because of this, people have become somewhat analytical about how they play the games. To help players of the Pokemon video...

  • NOTE: LANGUAGE IS PYTHON CS160 Computer Science Lab 17 Objectives Work with dictionaries Work with functions...

    NOTE: LANGUAGE IS PYTHON CS160 Computer Science Lab 17 Objectives Work with dictionaries Work with functions Work with files Overview This lab will have you store the birthdays for a group of people. This program will store the information with the focus on the date of the month and not on the individual. The point of this program is to be able to see who has a birthday on a given day or to create a table of birthdays, in...

  • NOTE: LANGUAGE IS PYTHON CS160 Computer Science Lab 17 Objectives Work with dictionaries Work with functions...

    NOTE: LANGUAGE IS PYTHON CS160 Computer Science Lab 17 Objectives Work with dictionaries Work with functions Work with files Overview This lab will have you store the birthdays for a group of people. This program will store the information with the focus on the date of the month and not on the individual. The point of this program is to be able to see who has a birthday on a given day or to create a table of birthdays, in...

  • Python Help Please! This is a problem that I have been stuck on.I am only suppose...

    Python Help Please! This is a problem that I have been stuck on.I am only suppose to use the basic python coding principles, including for loops, if statements, elif statements, lists, counters, functions, nested statements, .read, .write, while, local variables or global variables, etc. Thank you! I am using python 3.4.1. ***( The bottom photo is a continuation of the first one)**** Problem statement For this program, you are to design and implement text search engine, similar to the one...

  • According to Wikipedia , a comma-separated values (CSV) file is a delimited text file that uses...

    According to Wikipedia , a comma-separated values (CSV) file is a delimited text file that uses a comma to separate values. A CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format. A company has text data that is not...

  • Python lab: Problem Statement: In this lab, you will be building a software application that will...

    Python lab: Problem Statement: In this lab, you will be building a software application that will print the most popular ice-cream flavor among kids. Lab Scenario: 1. You are given an input file flavors.txt. One sample example of the file is given below. 2. You will design a user-defined function most_favorite_flavor() which will return the name of the popular flavor. 3. The main function is responsible reading from the file and calling the most_favorite_flavor() function. 4. For this lab, you...

  • In Python and in one file please. (Simple functions with an expressions) Create a function called...

    In Python and in one file please. (Simple functions with an expressions) Create a function called load_inventory(filename). The filename argument in this case specifies the name of a file that contains all the inventory/product information for the store, including product names, descriptions, prices, and stock levels. This function should clear any information already in the product list (i.e., a fresh start) and then re-initialize the product list using the file specified by the filename argument. You can structure your file...

  • In Python and in one file please. (Simple functions with an expressions) Create a function called load_inventory(filenam...

    In Python and in one file please. (Simple functions with an expressions) Create a function called load_inventory(filename). The filename argument in this case specifies the name of a file that contains all the inventory/product information for the store, including product names, descriptions, prices, and stock levels. This function should clear any information already in the product list (i.e., a fresh start) and then re-initialize the product list using the file specified by the filename argument. You can structure your file...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT