-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw7-strings.Rmd
73 lines (49 loc) · 5.39 KB
/
hw7-strings.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
---
title: "Homework 7 - Strings"
output:
html_document:
toc: false
number_sections: false
params:
number: 7
submit: "https://u.pcloud.com/#page=puplink&code=pVYkZ0DBDeiPfkj7fgH08J9hSD4jQf07k"
purpose: |
The purposes of this assignment are:
>
> - To practice manipulating strings in R with the **stringr** package.
> - To practice computational problem solving with strings.
---
```{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 `hw7.R` file.
> - **The file must be named `hw7.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/hw7.zip) for your assignment. Inside the "hw7" folder, open and edit the R script called "hw7.R" 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) `strToLower(s)` [SOLO, 10%]
Write a function that does exactly what `str_to_lower()` does (i.e. returns the same string but with all letters in lower case) without using `str_to_lower()` or the Base R `tolower()` function. Hint: check out the `str_replace_all()` function!
### 3) `getMiddleCharacter(s)` [SOLO, 15%]
Write a function that takes a single string, `s`, and returns the middle character of the string. If the string has an even number of characters, then return the two middle characters. So `getMiddleCharacter("one")` should return `"n"`, and `getMiddleCharacter("feet")` should return `"ee"`.
### 4) `rotateStringLeft(s, k)` [COLLABORATIVE, 15%]
Write the function `rotateStringLeft(s, k)` that takes a string `s` and a non-negative integer `k`, and returns the string `s` rotated `k` places to the left. So, if `s = "iknowkungfu"` and `k = 2`, then the result should be `"nowkungfuik"`. If `k` is larger than the length of `s`, the function should continue to rotate the string beyond its starting point. So, if `s = "iknowkungfu"` and `k = 11`, then the result should be `"iknowkungfu"`, but if `k = 12`, the result should be `"knowkungfui"`.
### 5) `isDigit(s)` [COLLABORATIVE, 15%]
Write the function `isDigit(s)` that takes a string `s` and returns `TRUE` if all characters in the string are numeric integers and `FALSE` otherwise. So `isDigit("123")` should return `TRUE`, `isDigit("123N")` should return `FALSE`, and `isDigit("")` should return `FALSE`. Hint: the value `'\\d'` can be used to search for whether a string has as digit, so `str_detect("r2d2", '\\d')` would return `TRUE` but `str_detect("rd", '\\d')` would return `FALSE`.
### 6) `getTheGerunds(sentence)` [COLLABORATIVE, 15%]
Write a function that takes a single string, `sentence`, and returns a vector of all the gerunds in it (i.e. all the words that end in `"ing"`). So `getTheGerunds("I like hiking and swimming")` should return `c("hiking", "swimming")`. If there are no gerunds in `sentence`, the function should return `NULL`. Hint: first solve how you might separate a single-string sentence into a vector of words.
### 7) `sameChars(s1, s2)` [COLLABORATIVE, 15%]
Write the function `sameChars(s1, s2)` that takes two strings and returns `TRUE` if the two strings are composed of the same characters (though perhaps in different numbers and in different orders); that is, if every character that is in the first string is in the second (and vice versa), and `FALSE` otherwise. This test is case-sensitive, so `"ABC"` and `"abc"` do not contain the same characters. The function returns `FALSE` if either parameter is not a string, but returns `TRUE` if both strings are empty.
### 8) Read and reflect [SOLO, 10%]
Read and reflect on next week's readings on [using python in R](r8-python.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.
---
### **Bonus**: `hasBalancedParentheses(s)` [SOLO, 3%]
Write the function `hasBalancedParentheses(s)`, which takes a string `s` and returns `TRUE` if the parentheses in `s` are balanced and `FALSE` otherwise (ignoring all non-parentheses in the string). We say that parentheses are "balanced" if each right parenthesis closes (matches) an open (unmatched) left parenthesis, and no left parentheses are left unclosed (unmatched) at the end of the text. So, for example, `"( ( ( ) ( ) ) ( ) )"` is balanced, but `"( ) )"` is not balanced, and `"( ) ) ("` is also not balanced. Hint: keep track of how many right parentheses remain unmatched as you iterate over the string.