Due: Sunday, 27-Oct. at 8pm
Rules:
Instructions:
Before beginning this assignment, be sure to have read the Strings and Vectors lessons.
Open RStudio and create a new project called “hw4-lastName”, replacing “lastName” with your last name.
Download the hw4.R template script and place it in RStudio project folder you just created.
Fill out your name, GW Net ID, and the names of anyone you worked with in the header of the “hw4.R” file.
Type all of your answers to the questions below in the “hw4.R” script.
After completing the questions, create a zip file of all files in your R project folder for this assignment.
Submit the zip file on Blackboard by the due deadline.
Write solutions to the following functions in your “hw4.R” script.
vowelCount(s)
[10 pts]Write the function vowelCount(s)
that takes a string
s
, and returns the number of vowels in s
,
ignoring case (so "A"
and "a"
are both
vowels). The vowels are "a"
, "e"
,
"i"
, "o"
, and "u"
. So, for
example, vowelCount("Abc def!!! a? yzyzyz!")
returns
3
(two "a"
’s and one "e"
).
strToLower(s)
[10 pts]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!
getMiddleCharacter(s)
[10 pts]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"
.
rotateStringLeft(s, k)
[10 pts]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"
.
vectorToEnglishList(v)
[10 pts]Write a function that takes a vector, v
, and returns a
single string that contains the elements of v
using proper
English grammar for describing a list of items, i.e. the items are
separated by commas, and the word “and” is inserted before the last
item. Your function should use the Oxford comma for
the last list item. For example, if
v <- c("a", "b", "c")
, then
vectorToEnglishList(v)
would return the string
"a, b, and c"
. Think carefully about what it should do if
given a vector of length 0, 1, or 2.
isDigit(s)
[10 pts]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
.
getTheGerunds(sentence)
[10 pts]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.
interleave(s1, s2)
[15 pts]Write the function interleave(s1, s2)
that takes two
strings, s1
and s2
, and interleaves their
characters starting with the first character in s1
. For
example, interleave('pto', 'yhn')
would return the string
"python"
. If one string is longer than the other,
concatenate the rest of the remaining string onto the end of the new
string. For example ('a#'
, 'cD!f2'
) would
return the string "ac#D!f2"
. Assume that both
s1
and s2
will always be strings.
sameChars(s1, s2)
[15 pts]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.
hasBalancedParentheses(s)
[SOLO, 2.5
pts]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.
largestNumberInText(text)
[SOLO, 2.5
pts]Write the function largestNumberInText(text)
that takes
a string of text and returns the largest integer value that occurs
within that text, or NULL
if no such value occurs. You may
assume that the only numbers in the text are non-negative integers and
that numbers are always composed of consecutive digits (without commas,
for example). For example:
largestNumberInText("I saw 3 dogs, 17 cats, and 14 cows!")
17 # The numeric value 17, not the string "17"
largestNumberInText("One person ate two hot dogs!")
NULL # Value NULL, not the string "NULL"
Hint: You may want to use isDigit()
as a helper
function!
Page sources:
Some content on this page has been modified from other courses, including: