Due: March 02 by 11:59pm

Submit: To submit this assignment, create a zip file of all the files in your R project folder for this assignment. Name the zip file hw7-netID.zip, replacing netID with your netID (e.g., hw7-jph.zip). Use this link to submit your file.

Weight: This assignment is worth 5% of your final grade.

Purpose: The purposes of this assignment are:

Assessment: Each question indicates the % of the assignment grade, summing to 100%. The credit for each question will be assigned as follows:

The reflection portion is always worth 10% and graded for completion.


Using the autograder

1) Staying organized [SOLO, 5%]

Download and use this template 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.

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. 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.

EMSE 4571: Intro to Programming for Analytics (Spring 2022)
Thursdays | 12:45 - 3:15 PM EST | Tompkins 208 | Dr. John Paul Helveston | jph@gwu.edu