-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignments-09.qmd
115 lines (85 loc) · 3.29 KB
/
assignments-09.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
---
title: "Module 9 Assignments"
---
```{r}
#| echo: false
#| message: false
#| warning: false
library(tidyverse)
library(readxl)
assignments <- read_excel("assessment_schedule.xlsx") %>%
mutate(formatted_date = format(due_date, "%A, %B %d, %Y"))
```
# Short Project 07
Short Programming projects are submitted during our weekly 45-minute in-person lab sessions. Each lab sessions is guided by two TAs. The instructions for the short project will be available only during the lab sessions. To schedule your lab session go to the weekly lab session spreadsheet in [Short Project Dates and Instructions](short-projects.html).
# Programming Problems
Programming Problems should be submitted to [gradescope](https://www.gradescope.com/courses/934148).
## Programming Problem 17
**Due date: `r assignments %>% filter(assessment == "Module 9 Programming Problems") %>% pull(formatted_date)` at 9pm**
Write a Python function that does the following:
1. Its name is `read_csv`
2. It takes a string as argument called `file_name`
3. It opens the file with `file_name` in read mode
4. It iterates over each line in the file, splitting each line by `","`
5. It creates a dictionary where the keys are the first item in each line, and the values are the other values (in a list)
6. It returns the dictionary
Test cases:
contents of *stipends.csv*:
```{HTML}
Peter,1000
Joan,50500
Mary,2400
```
```{python}
#| echo: true
#| eval: false
print( read_csv("stipends.csv") ) # {"Peter": [1000],
# "Joan": [50500],
# "Mary": [2400]}
```
contents of *population.csv*:
```{HTML}
Country,United States,Brazil,Mexico,Canada
Population (in mil),331.00,212.56,128.93,37.74
```
```{python}
#| echo: true
#| eval: false
print( read_csv("population.csv") ) # {"Country": ["United States", "Brazil", "Mexico", "Canada"],
# "Population (in mil)": [331.00, 212.56, 128.93, 37.74]}
```
Name the program `read_data_file.py`. Make sure that gradescope gives you the points for passing the test case.
## Programming Problem 18
**Due date: `r assignments %>% filter(assessment == "Module 9 Programming Problems") %>% pull(formatted_date)` at 9pm**
Write a Python function that does the following:
1. Its name is `write_csv`
2. It takes a `dictionary` and a string `file_name` as arguments
3. It opens the file with `file_name` in write mode
4. It iterates over the `dictionary` to write lines to the opened `.csv`
5. Each key is the first element of the line, the values are lists that contain the other values in the line
Test cases:
```{python}
#| echo: true
#| eval: false
my_data = {"Peter": [1000], "Joan": [50500], "Mary": [2400]}
write_csv(my_data, "stipends.csv")
```
This is what *stipends.csv* should contain:
```{HTML}
Peter,1000
Joan,50500
Mary,2400
```
```{python}
#| echo: true
#| eval: false
my_data = {"Country": ["United States", "Brazil", "Mexico", "Canada"],
"Population (in mil)": [331.00, 212.56, 128.93, 37.74]}
write_csv(my_data, "population.csv")
```
This is what *population.csv* should contain:
```{HTML}
Country,United States,Brazil,Mexico,Canada
Population (in mil),331.0,212.56,128.93,37.74
```
Name the program `write_data_file.py`. Make sure that gradescope gives you the points for passing the test case.