From f6403e92eb7fd881f35007905021be55179414c0 Mon Sep 17 00:00:00 2001 From: Kamal Abdelrahman Date: Sun, 24 Feb 2019 22:19:14 -0500 Subject: [PATCH 1/2] Kamal Abdelrahman attempt Worked around with the if else statements, realizing where I went wrong with my initial code --- fizzbuzz.Rmd | 45 +++++++++++++++++--- fizzbuzz.html | 115 +++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 148 insertions(+), 12 deletions(-) diff --git a/fizzbuzz.Rmd b/fizzbuzz.Rmd index 58e4ceb..f4c0670 100644 --- a/fizzbuzz.Rmd +++ b/fizzbuzz.Rmd @@ -37,12 +37,45 @@ print(total) ### Matt's Code ```{r,eval=F} -answer <- 1:100 -for(i in 1:100){ - if(i%%3 == 0) answer[i] <-"fizz" - if(i%%5 == 0) answer[i] <-"buzz" - if(i%%5 == 0 & i%%3 ==0) answer[i] <-"fizzbuzz" +answer <- 1:100 # Creates initial variable of answer of values 1 to 100 +for(i in 1:100){ #Recreates loop to alter "answer" where it meets necessary conditions + if(i%%3 == 0) answer[i] <-"fizz" #Replaces number in "answer" array with "fizz" because it is divisible by 3 + if(i%%5 == 0) answer[i] <-"buzz" #Replaces number in "answer" array with "buzz" because it is divisible by 5 + if(i%%5 == 0 & i%%3 ==0) answer[i] <-"fizzbuzz" #Replaces number in "answer" array with "fizzbuzz" because it is divisible by both 5 and 3 } -print(answer) +print(answer) #prints array of "answer" +``` + +### Kamal Abdelrahman's Code_p1 + +```{r} +for (i in c(1:100)){ + if(i%%3 == 0){ + print("Fizz") + } + if(i%%5 == 0){ + print("Buzz") + } + if (i%%5 == 0 & i%%3 ==0) { + print("FizzBuzz") + } +} +``` + +### Kamal Abdelrahman's Code_p2 +####Minor remix of Crump Code. Was curious to see if the "else if" was necessary. Realized that the as long as the the first "if " is false, function will go to next if until it hits true or it runs out of ifs. Same principle with if then else if statements. +####Also realized that storing the variables and editing them with the if statements is way more accurate than just using if statements. Use the conditions to replace the numbers with "Fizz", "Buzz" or "FizzBuzz" as necessary +```{r} +NumOrWor <- c(1:100) +for (i in c(1:100)){ + if (i%%3 == 0) { + NumOrWor[i] <- "Fizz" + } else if(i%%5 == 0) { + NumOrWor[i] <- "Buzz" + } else if(i%%5 == 0 & i%%3 ==0) { + NumOrWor[i] <- "FizzBuzz" + } +} +print(NumOrWor) ``` diff --git a/fizzbuzz.html b/fizzbuzz.html index 7457e94..140f435 100644 --- a/fizzbuzz.html +++ b/fizzbuzz.html @@ -216,13 +216,116 @@

Jeff Kravitz

Matt’s Code

-
answer <- 1:100
-for(i in 1:100){
-  if(i%%3 == 0) answer[i] <-"fizz"
-  if(i%%5 == 0) answer[i] <-"buzz"
-  if(i%%5 == 0 & i%%3 ==0) answer[i] <-"fizzbuzz"
+
answer <- 1:100 # Creates initial variable of answer of values 1 to 100
+for(i in 1:100){ #Recreates loop to alter "answer" where it meets necessary conditions
+  if(i%%3 == 0) answer[i] <-"fizz" #Replaces number in "answer" array with "fizz" because it is divisible by 3
+  if(i%%5 == 0) answer[i] <-"buzz" #Replaces number in "answer" array with "buzz" because it is divisible by 5
+  if(i%%5 == 0 & i%%3 ==0) answer[i] <-"fizzbuzz" #Replaces number in "answer" array with "fizzbuzz" because it is divisible by both 5 and 3
 }
-print(answer)
+print(answer) #prints array of "answer"
+
+
+

Kamal Abdelrahman’s Code_p1

+
for (i in c(1:100)){
+ if(i%%3 == 0){
+  print("Fizz")
+ } 
+ if(i%%5 == 0){
+  print("Buzz")
+ }
+ if (i%%5 == 0 & i%%3 ==0) {
+  print("FizzBuzz")
+ }
+}
+
## [1] "Fizz"
+## [1] "Buzz"
+## [1] "Fizz"
+## [1] "Fizz"
+## [1] "Buzz"
+## [1] "Fizz"
+## [1] "Fizz"
+## [1] "Buzz"
+## [1] "FizzBuzz"
+## [1] "Fizz"
+## [1] "Buzz"
+## [1] "Fizz"
+## [1] "Fizz"
+## [1] "Buzz"
+## [1] "Fizz"
+## [1] "Fizz"
+## [1] "Buzz"
+## [1] "FizzBuzz"
+## [1] "Fizz"
+## [1] "Buzz"
+## [1] "Fizz"
+## [1] "Fizz"
+## [1] "Buzz"
+## [1] "Fizz"
+## [1] "Fizz"
+## [1] "Buzz"
+## [1] "FizzBuzz"
+## [1] "Fizz"
+## [1] "Buzz"
+## [1] "Fizz"
+## [1] "Fizz"
+## [1] "Buzz"
+## [1] "Fizz"
+## [1] "Fizz"
+## [1] "Buzz"
+## [1] "FizzBuzz"
+## [1] "Fizz"
+## [1] "Buzz"
+## [1] "Fizz"
+## [1] "Fizz"
+## [1] "Buzz"
+## [1] "Fizz"
+## [1] "Fizz"
+## [1] "Buzz"
+## [1] "FizzBuzz"
+## [1] "Fizz"
+## [1] "Buzz"
+## [1] "Fizz"
+## [1] "Fizz"
+## [1] "Buzz"
+## [1] "Fizz"
+## [1] "Fizz"
+## [1] "Buzz"
+## [1] "FizzBuzz"
+## [1] "Fizz"
+## [1] "Buzz"
+## [1] "Fizz"
+## [1] "Fizz"
+## [1] "Buzz"
+
+
+

Kamal Abdelrahman’s Code_p2

+
+

Minor remix of Crump Code. Was curious to see if the “else if” was necessary. Realized that the as long as the the first “if” is false, function will go to next if until it hits true or it runs out of ifs. Same principle with if then else if statements.

+
+
+

Also realized that storing the variables and editing them with the if statements is way more accurate than just using if statements. Use the conditions to replace the numbers with “Fizz”, “Buzz” or “FizzBuzz” as necessary

+
NumOrWor <- c(1:100)
+for (i in c(1:100)){
+ if (i%%3 == 0) {
+  NumOrWor[i] <- "Fizz"
+    } else if(i%%5 == 0) {
+        NumOrWor[i] <- "Buzz"
+    } else if(i%%5 == 0 & i%%3 ==0) {
+        NumOrWor[i] <- "FizzBuzz"
+    }
+}
+print(NumOrWor)
+
##   [1] "1"    "2"    "Fizz" "4"    "Buzz" "Fizz" "7"    "8"    "Fizz" "Buzz"
+##  [11] "11"   "Fizz" "13"   "14"   "Fizz" "16"   "17"   "Fizz" "19"   "Buzz"
+##  [21] "Fizz" "22"   "23"   "Fizz" "Buzz" "26"   "Fizz" "28"   "29"   "Fizz"
+##  [31] "31"   "32"   "Fizz" "34"   "Buzz" "Fizz" "37"   "38"   "Fizz" "Buzz"
+##  [41] "41"   "Fizz" "43"   "44"   "Fizz" "46"   "47"   "Fizz" "49"   "Buzz"
+##  [51] "Fizz" "52"   "53"   "Fizz" "Buzz" "56"   "Fizz" "58"   "59"   "Fizz"
+##  [61] "61"   "62"   "Fizz" "64"   "Buzz" "Fizz" "67"   "68"   "Fizz" "Buzz"
+##  [71] "71"   "Fizz" "73"   "74"   "Fizz" "76"   "77"   "Fizz" "79"   "Buzz"
+##  [81] "Fizz" "82"   "83"   "Fizz" "Buzz" "86"   "Fizz" "88"   "89"   "Fizz"
+##  [91] "91"   "92"   "Fizz" "94"   "Buzz" "Fizz" "97"   "98"   "Fizz" "Buzz"
+
From 15ce91c2c4454c6c4497865618e1db3046c43757 Mon Sep 17 00:00:00 2001 From: Kamal Abdelrahman Date: Wed, 22 May 2019 07:18:33 -0400 Subject: [PATCH 2/2] Update snakesladders.Rmd --- snakesladders.Rmd | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/snakesladders.Rmd b/snakesladders.Rmd index 045da5c..b617e70 100644 --- a/snakesladders.Rmd +++ b/snakesladders.Rmd @@ -34,3 +34,26 @@ avg_moves <- mean(count) print(avg_moves) ``` +```{r} +move <- 0 +count <- replicate(1000, 0) +game_grid <- data.frame(c(3,6,9,10,14,19,22,24),c(11,17,18,12,4,8,20,16)) +for (i in 1:1000) { + spot <- 1 + for (i in spot) { + if (spot <= 25) { + move <- sample(1:6, 1) + spot <- spot + move + } + for (i in 1:8) { + if (spot == game_grid[i,1]) { + spot <- game_grid[i,2] + } + } + count[i] = count[i] + 1 + } +} +avg_moves <- mean(count) +print(avg_moves) +``` +