From 2fc311f834576f23abbe507d93b4495f9af9b2a2 Mon Sep 17 00:00:00 2001 From: Joel Ross Date: Fri, 23 Dec 2016 16:35:42 -0800 Subject: [PATCH 1/6] Update exercises for students --- exercise-1/exercise.R | 12 +++--------- exercise-2/exercise.R | 19 +++---------------- 2 files changed, 6 insertions(+), 25 deletions(-) diff --git a/exercise-1/exercise.R b/exercise-1/exercise.R index da535d4..7357ab3 100644 --- a/exercise-1/exercise.R +++ b/exercise-1/exercise.R @@ -2,21 +2,15 @@ # Define a function `AddThree` that takes a single argument and # returns a value 3 greater than that input -AddThree <- function(value) { - return(value + 3) -} + # Create a variable `ten` that is the result of passing 7 to your `AddThree` function -ten <- AddThree(7) + # Define a function `ImperialToMetric` that takes in two arguments: a number of feet # and a number of inches # The function should return the total length in meters -ImperialToMetric <- function(feet, inches) { - total.inches <- feet*12 + inches - return(total.inches * 0.0254) -} + # Create a variable `height.in.meters` by passing your height in imperial to the # `ImperialToMetric` function -height.in.meters <- ImperialToMetric(5,11) diff --git a/exercise-2/exercise.R b/exercise-2/exercise.R index 2fcf7a9..6ff1839 100644 --- a/exercise-2/exercise.R +++ b/exercise-2/exercise.R @@ -3,27 +3,14 @@ # Define a function `CompareLength` that takes in 2 character strings, and returns # the sentence of the form # "The difference in length is N" -CompareLength <- function(str1, str2) { - diff <- abs(nchar(str1) - nchar(str2)) - diff.string <- paste('The difference in length is', diff) - return(diff.string) -} + # Pass two strings of different lengths to your `CompareLength` function -CompareLength("Hey", "Hello ladies and gentlemen, how are you today?") + # Define a function `DescribeDifference` that will return one of the following statements: # "Your first string is longer by N characters" # "Your second string is longer by N characters" -DescribeDifference <- function(a,b) { - diff <- nchar(a) - nchar(b) - if(diff > 0) { - sentence <- paste('Your first string is longer by', diff, 'characters') - } else { - sentence <- paste('Your second string is longer by', -diff, 'characters') - } - return(sentence) -} + # Pass two strings of different lengths to your `DescribeDifference` function -DescribeDifference("Abracadabra!", "Presto!") From 462168367f0eb3188f522ad8e88308429be0b6ab Mon Sep 17 00:00:00 2001 From: Joel Ross Date: Sun, 8 Jan 2017 21:16:46 -0800 Subject: [PATCH 2/6] Update exercise for students --- exercise-3/exercise.R | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/exercise-3/exercise.R b/exercise-3/exercise.R index 0a9cb13..7b4de33 100644 --- a/exercise-3/exercise.R +++ b/exercise-3/exercise.R @@ -1,44 +1,40 @@ # Exercise-3: Calling built-in functions # Create a variable `my.name` that contains your name -my.name <- "Joel Ross" + # Create a variable `name.length` that holds how many letters are in your name (use the `nchars()` function) -name.length <- nchar(my.name) + # Print the number of letters in your name -print(name.length) + # Create a variable `now.doing` that is your named followed by "is programming!" to the end of your name # (use the `paste()` function) -now.doing <- paste(my.name, "is programming!") + # Make the `now.doing` variable upper case -toupper(now.doing) + ### Bonus # Pick two of your favorite numbers (between 1 and 100) and assign them to `fav.1` and `fav.2` -fav.1 <- 12 -fav.2 <- 87 + # Divide each number by the square root of 201 and save it as the same variable -fav.1 <- fav.1/sqrt(201) -fav.2 <- fav.2/sqrt(201) + # Create a variable `raw.sum` that is the sum of those two variables. Use the `sum()` function for practice -raw.sum <-sum(fav.1, fav.2) + # Create a variable `round.sum` that is the `raw.sum` rounded to 1 decimal place. Use the `round()` function -round.sum <- round(raw.sum, 1) + # Create two new variables `round.1` and `round.2` that are your `fav.1` and `fav.2` variables rounded # to 1 decimal place -round.1 <- round(fav.1, 2) -round.2 <- round(fav.2, 2) + # Create a variable `sum.round` that is the sum of the rounded values -sum.round <- sum(round.1, round.2) + # Which is bigger, `round.sum` or `sum.round`? (You can use the `max()` function!) -max(sum.round, round.sum) From 58658887f6eeb71efaccf235344e4c9d449158eb Mon Sep 17 00:00:00 2001 From: Daniel Wang Date: Tue, 10 Jan 2017 15:13:56 -0800 Subject: [PATCH 3/6] answer exercise-3 --- exercise-3/desktop.ini | 5 +++++ exercise-3/exercise.R | 26 +++++++++++++++----------- 2 files changed, 20 insertions(+), 11 deletions(-) create mode 100644 exercise-3/desktop.ini diff --git a/exercise-3/desktop.ini b/exercise-3/desktop.ini new file mode 100644 index 0000000..c9cf46d --- /dev/null +++ b/exercise-3/desktop.ini @@ -0,0 +1,5 @@ +[.ShellClassInfo] +InfoTip=這是在線上共用的資料夾。 +IconFile=C:\Program Files (x86)\Google\Drive\googledrivesync.exe +IconIndex=16 + \ No newline at end of file diff --git a/exercise-3/exercise.R b/exercise-3/exercise.R index 7b4de33..aad1eda 100644 --- a/exercise-3/exercise.R +++ b/exercise-3/exercise.R @@ -1,40 +1,44 @@ # Exercise-3: Calling built-in functions # Create a variable `my.name` that contains your name - +my.name <- "Daniel" # Create a variable `name.length` that holds how many letters are in your name (use the `nchars()` function) - +name.length <- nchar(my.name) # Print the number of letters in your name - +print (name.length) # Create a variable `now.doing` that is your named followed by "is programming!" to the end of your name # (use the `paste()` function) - +now.doing <- paste(my.name, "is programming", spe=" ") # Make the `now.doing` variable upper case - +now.doing <- toupper(now.doing) ### Bonus # Pick two of your favorite numbers (between 1 and 100) and assign them to `fav.1` and `fav.2` - +fav.1 <- 2 +fav.2 <- 4 # Divide each number by the square root of 201 and save it as the same variable - +fav.1 <- fav.1 / sqrt(201) +fav.2 <- fav.2 / sqrt(201) # Create a variable `raw.sum` that is the sum of those two variables. Use the `sum()` function for practice - +raw.sum <- sum(fav.1, fav.2) # Create a variable `round.sum` that is the `raw.sum` rounded to 1 decimal place. Use the `round()` function - +round.sum <- round(raw.sum, 1) # Create two new variables `round.1` and `round.2` that are your `fav.1` and `fav.2` variables rounded # to 1 decimal place - +round.1 <- round(fav.1, 1) +round.2 <- round(fav.2, 1) # Create a variable `sum.round` that is the sum of the rounded values - +sum.round <- fav.1 + fav.2 # Which is bigger, `round.sum` or `sum.round`? (You can use the `max()` function!) +bigger <- max(round.sum, sum.round) \ No newline at end of file From 49d21c5d7bf70a093f7da7b2864536bffa65c3d9 Mon Sep 17 00:00:00 2001 From: Daniel Wang Date: Tue, 10 Jan 2017 15:34:44 -0800 Subject: [PATCH 4/6] correct toupper() description --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b9f20a3..43619df 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ As you may have noticed, R comes with a large number of functions that are built | --- | --- | --- | | `sum(a,b,...)` | Calculates the sum of all input values | `sum(1, 5)` returns `6`| | `round(x,digits)` | Rounds the first argument to the given number of digits | `round(3.1415, 3)` returns `3.142` | -| `toupper(str)` | Returns the characters in lowercase | `toupper("hi there")` returns `"HI THERE"` | +| `toupper(str)` | Returns the characters in uppercase | `toupper("hi there")` returns `"HI THERE"` | | `paste(a,b,...)` | _Concatenate_ (combine) characters into one value | `paste("hi", "there")` returns `"hi there"` | | `nchar(str)` | Counts the number of characters in a string | `nchar("hi there")` returns `8` (space is a character!) | | `c(a,b,...)` | _Concatenate_ (combine) multiple items into a _vector_ (see [module-7](https://github.com/info201-w17/module7-vectors)) | `c(1, 2)` returns `1, 2` | From a6f13bb302076ffccf2022bf13a6f672d4511016 Mon Sep 17 00:00:00 2001 From: Daniel Wang Date: Tue, 10 Jan 2017 15:40:32 -0800 Subject: [PATCH 5/6] delete desktop.ini --- exercise-3/desktop.ini | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 exercise-3/desktop.ini diff --git a/exercise-3/desktop.ini b/exercise-3/desktop.ini deleted file mode 100644 index c9cf46d..0000000 --- a/exercise-3/desktop.ini +++ /dev/null @@ -1,5 +0,0 @@ -[.ShellClassInfo] -InfoTip=這是在線上共用的資料夾。 -IconFile=C:\Program Files (x86)\Google\Drive\googledrivesync.exe -IconIndex=16 - \ No newline at end of file From ccf61cc275e11424ae9f0d7626828438c6120657 Mon Sep 17 00:00:00 2001 From: Daniel Wang Date: Tue, 10 Jan 2017 15:45:46 -0800 Subject: [PATCH 6/6] correct exercise-3 error --- exercise-3/exercise.R | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/exercise-3/exercise.R b/exercise-3/exercise.R index aad1eda..34ebd68 100644 --- a/exercise-3/exercise.R +++ b/exercise-3/exercise.R @@ -1,44 +1,40 @@ # Exercise-3: Calling built-in functions # Create a variable `my.name` that contains your name -my.name <- "Daniel" -# Create a variable `name.length` that holds how many letters are in your name (use the `nchars()` function) -name.length <- nchar(my.name) + +# Create a variable `name.length` that holds how many letters are in your name (use the `nchar()` function) + # Print the number of letters in your name -print (name.length) + # Create a variable `now.doing` that is your named followed by "is programming!" to the end of your name # (use the `paste()` function) -now.doing <- paste(my.name, "is programming", spe=" ") + # Make the `now.doing` variable upper case -now.doing <- toupper(now.doing) + ### Bonus # Pick two of your favorite numbers (between 1 and 100) and assign them to `fav.1` and `fav.2` -fav.1 <- 2 -fav.2 <- 4 + # Divide each number by the square root of 201 and save it as the same variable -fav.1 <- fav.1 / sqrt(201) -fav.2 <- fav.2 / sqrt(201) + # Create a variable `raw.sum` that is the sum of those two variables. Use the `sum()` function for practice -raw.sum <- sum(fav.1, fav.2) + # Create a variable `round.sum` that is the `raw.sum` rounded to 1 decimal place. Use the `round()` function -round.sum <- round(raw.sum, 1) + # Create two new variables `round.1` and `round.2` that are your `fav.1` and `fav.2` variables rounded # to 1 decimal place -round.1 <- round(fav.1, 1) -round.2 <- round(fav.2, 1) + # Create a variable `sum.round` that is the sum of the rounded values -sum.round <- fav.1 + fav.2 -# Which is bigger, `round.sum` or `sum.round`? (You can use the `max()` function!) -bigger <- max(round.sum, sum.round) \ No newline at end of file + +# Which is bigger, `round.sum` or `sum.round`? (You can use the `max()` function!) \ No newline at end of file