-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw8-python.Rmd
74 lines (51 loc) · 4.54 KB
/
hw8-python.Rmd
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
---
title: "Homework 8 - Python"
output:
html_document:
toc: false
number_sections: false
params:
number: 8
submit: "https://u.pcloud.com/#page=puplink&code=HVYkZd8RNYCkcLESxYLMkK5XYNzLNWdn7"
purpose: |
The purposes of this assignment are:
>
> - To practice the same problem-solving skills we've learning thus far, but in Python.
> - To practice running Python scripts from R.
---
```{r child = file.path("child", "setup.Rmd")}
```
```{r child = file.path("child", "hw.Rmd")}
```
> ### Using the [autograder](autograder.html)
>
> - You can check your solutions to problems 2 - 7 by logging into the [autograder](autograder.html) and uploading your `hw8.R` file.
> - **The file must be named `hw8.R` or it won't work.**
> - Your user name is your netID, and your password is inside the `readme.txt` file in the Box folder I shared with you.
### 1) Staying organized [SOLO, 5%]
Download and use [this template](templates/hw8.zip) for your assignment. Inside the "hw8" folder, open and edit the R script called "hw8.py" and fill out your name, GW Net ID, and the names of anyone you worked with on this assignment.
> ### **Writing test functions**
>
> For each of the following functions, write a test function first, and then write the function. **Your test functions will count for half of the available credit for each problem**. Think carefully about the test cases to include in your test functions.
> ### **Using good style**
>
> For this assignment, you must use good style to receive full credit. Follow the best practices described in [this style guide](http://adv-r.had.co.nz/Style.html).
### 2) `kthDigit(x, k)` [SOLO, 10%]
Given two integers, `x` and `k`, return the kth digit of `x`, counting from the right. So:
- `kthDigit(789, 1)` returns `9`
- `kthDigit(789, 2)` returns `8`
- `kthDigit(789, 3)` returns `7`
- `kthDigit(789, 4)` returns `0`
Negative numbers should work, too, so `kthDigit(-789, 1)` returns `9`.
### 3) `isEvenPositiveInt(x)` [SOLO, 15%]
Given an arbitrary value `x`, return `True` if it is an integer, and it is positive, and it is even (all 3 must be true), or `False` otherwise. If the value `x` is not an integer, the function should return `False` rather than error. So, `isEvenPositiveInt("yikes!")` returns `False`, and `isEvenPositiveInt(123456)` returns `True`.
### 4) `getTheCents(n)` [SOLO, 15%]
Write the function `getTheCents(n)` which takes a value `n` that represents a payment in US dollars and returns the number of cents in the payment. For example, if `n` is `2.45`, the function should return `45`. If `n` is an integer, the function should return `0`, as it has `0` cents; if it isn't a number, it should return `None`, because a non-number payment make no cents (ha!). If the payment has partial cents (for example, `3.953`), it should be rounded to the nearest cent (in this case, `95` cents).
#### 5) `isPrime(n)` [COLLABORATIVE, 15%]
Write the function `isPrime(n)` which takes a non-negative integer, `n`, and returns `True` if it is a prime number and `False` otherwise.
### 6) `numDigits(n)` [SOLO, 15%]
Write the function `numDigits(n)` that takes a _possibly-negative_ integer and returns the number of digits in it. So, `numDigits(12345)` returns `5`, `numDigits(0)` returns `1`, and `numDigits(-111)` returns `3`. One way you could solve this is to convert `n` to a string and use `str_length()`, but you cannot do that since you may not use strings here.
### 7) `reverseString(s)` [COLLABORATIVE, 15%]
Write a function that returns the string in reverse order. So if `s` equals `"abcde"`, `reverseString(s)` should equal `"edcba"`. You may assume that `s` only contains upper and/or lower case letters, but your solution must correctly return capital letters in their appropriate order. For example, `reverseString("aWordWithCaps")` should return `"spaChtiWdroWa"`.
### 8) Read and reflect [SOLO, 10%]
When we come back from spring break, we'll be shifting the focus of the class to working with data. Read and reflect on the readings to get started with this topic, including the [data analysis prelude](r9.1-data-analysis-prelude.html) chapter and the chapter on [data frames](https://p4a.seas.gwu.edu/2022-Spring/r9.2-data-frames.html). Afterwards, in a comment (`#`) in your R file, write a short reflection on what you've learned and any questions or points of confusion you have about what we've covered thus far. This can just few a few sentences related to this assignment, next week's readings, things going on in the world that remind you something from class, etc. If there's anything that jumped out at you, write it down.