Assignment 1

pdf

School

New York University *

*We aren’t endorsed by this school

Course

0479

Subject

Computer Science

Date

Feb 20, 2024

Type

pdf

Pages

3

Uploaded by dj1380

Report
CS 0479 Sections 2 and 3, Fall 2022 Instructor: Matt Zeidenberg Assignment 1 Each of the problems below is worth 10 points. For each problem, embed print statements in your code to show your results. Comment your code well. 1. Write a function called "factors" that returns a set of all the factors of an integer passed to it, other than the number itself and one. So, for instance, it should return {3, 5} if passed 15. 2. Write another function called "factors2" which takes an arbitrary number of integer arguments and uses the function factors you just wrote to construct and return a set of all the factors of its arguments. So factors2(15,30) should return {2, 3, 5, 6, 10, 15}. (Note that 15 is included because it is a factor of 30.) 3. Read data from the following two csv (comma-delimited) files on Github: https://raw.githubusercontent.com/mzeidenberg/0479data/main/StarbucksExampl e/four_starbacks_stores.csv https://raw.githubusercontent.com/mzeidenberg/0479data/main/StarbucksExampl e/starbucks_workers.csv You can view these online here and here . They contain data on four actual Starbucks stores and six hypothetical workers at those stores. The column Store_Number links the two files (each worker has a Store_Number that indicates what store they work at.) For instance, the following code will read all the lines in the first file into a list of strings, one string for each line including the header: import urllib url="https://raw.githubusercontent.com/mzeidenberg/0479data/main/StarbucksE xample/four_starbacks_stores.csv" f=urllib.request.urlopen(url) lines=f.read().decode('utf-8-sig').split("\r\n" Read each file into a list of dictionaries in the following format, where each key is the column name, and the values are the values found in each record for those columns. I show the beginning of the list for the first file; the second file should be read in a similar format but using its own headers for the dictionary keys. [{'Address': '166 7th Ave', 'City': 'Brooklyn', 'State': 'NY', 'Store_Number': '1', 'Zip': '11215'}, {'Address': '341 Eastern Parkway',
'City': 'Brooklyn', 'State': 'NY', 'Store_Number': '2', 'Zip': '11238'}, etc. 4. Use the two lists of dictionaries you created above to create a list of dictionaries which contains the store information merged with the employee information for each employee. It should be in the following format. [{'Address': '166 7th Ave', 'City': 'Brooklyn', 'Employee_Number': '1', 'Hourly_Pay': '15.75', 'Name': 'John Liu', 'State': 'NY', 'Store_Number': '1', 'Weekly_Hours': '40', 'Zip': '11215'}, {'Address': '166 7th Ave', 'City': 'Brooklyn', 'Employee_Number': '2', 'Hourly_Pay': '16', 'Name': 'Ann Leonard', 'State': 'NY', 'Store_Number': '1', 'Weekly_Hours': '30', 'Zip': '11215'}, etc. 5. Write the store data as the following nested dictionary. At the top level, the store number is the key and a dictionary containing the other information for the store is the value. {'1': {'Address': '166 7th Ave', 'City': 'Brooklyn', 'State': 'NY', 'Zip': '11215'}, '2': {'Address': '341 Eastern Parkway', 'City': 'Brooklyn', 'State': 'NY', 'Zip': '11238'}, etc. 6. Re-do the merge of problem 4 but using the object you created in problem 5. 7. Using the merged data, find the data just for those employees making at least 16 dollars an hour who work at a store in Brooklyn. Output the result as a CSV file with a header line containing the column names (as in the input files) called "merged_data_select.csv". 8. Encode the four-byte UTF-8 representation for the emoji that looks like a bone as a bytes object, and then encode it into a string object, and print that string object. 9. Bubble sort is a very poor sorting algorithm, but it is relatively easy to implement. Write a function in Python that will sort a list of numbers using bubble sort.
10. Write a function called "interlace" that takes two lists and creates a list consisting of the first element of the first list followed by the first element of the second list followed by the second element of the first list, etc. When you exhaust one of the lists, just append the remainder of the other. Here's how it should work: # these print statements print(interlace([1,2,3],[20,21,22])) print(interlace([1,2,3,4,5],[20,21,22])) print(interlace([1,2,3],[20,21,22,23,24])) # output these three lines [1, 20, 2, 21, 3, 22] [1, 20, 2, 21, 3, 22, 4, 5] [1, 20, 2, 21, 3, 22, 23, 24]
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help