-
Notifications
You must be signed in to change notification settings - Fork 2
/
hw7-strings.Rmd
70 lines (49 loc) · 5.35 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
---
title: "Homework 7 - Strings"
output:
html_document:
number_sections: false
toc: no
---
> **Due**: 26 October by 11:00 pm
>
> **Weight**: This assignment is worth **4%** of your final grade.
>
> **Purpose, Skills, & Knowledge**: The purposes of this assignment are:
>
> - To practice manipulating strings in R with the **stringr** library.
> - To practice computational problem solving with strings.
>
> **Assessment**: Each question indicates the % of the assignment grade, summing to 100%. The credit for each question will be assigned as follows:
>
> - 0% for not attempting a response.
> - 50% for attempting the question but with _major_ errors.
> - 75% for attempting the question but with _minor_ errors.
> - 100% for correctly answering the question.
>
> **Rules**:
>
> - Problems marked **SOLO** may not be worked on with other classmates, though you may consult instructors for help.
> - For problems marked **COLLABORATIVE**, you may work in groups of up to 3 students who are in this course this semester. You may not split up the work -- everyone must work on every problem. And you may not simply copy any code but rather truly work together.
> - Even though you work collaboratively, you still must submit your own solutions.
### 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.
### 2) `strToLower(s)` [SOLO, 15%]
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) Submit your files [SOLO, 5%]
Create a zip file of all the files in your R project folder for this assignment and submit the zip file on Blackboard (note: to receive full credit, your submission must follow the above format of using a correctly-named R Project and `.R` script).
---
### **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.