Skip to content

Commit

Permalink
Merge pull request #31 from jonmcalder/master
Browse files Browse the repository at this point in the history
Add more exercises
  • Loading branch information
jonmcalder committed Mar 31, 2017
2 parents c5df25a + 5b995f5 commit 56ae3a5
Show file tree
Hide file tree
Showing 25 changed files with 786 additions and 0 deletions.
40 changes: 40 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,46 @@
"difficulty": 1,
"slug": "prime-factors",
"topics": []
},
{
"difficulty": 1,
"slug": "largest-series-product",
"topics": []
},
{
"difficulty": 1,
"slug": "scrabble-score",
"topics": []
},
{
"difficulty": 1,
"slug": "perfect-numbers",
"topics": []
},
{
"difficulty": 1,
"slug": "pascals-triangle",
"topics": []
},
{
"difficulty": 1,
"slug": "rotational-cipher",
"topics": []
},
{
"difficulty": 1,
"slug": "tournament",
"topics": []
},
{
"difficulty": 1,
"slug": "rna-transcription",
"topics": []
},
{
"difficulty": 1,
"slug": "isogram",
"topics": []
}
]
}
9 changes: 9 additions & 0 deletions exercises/isogram/example.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
is_isogram <- function(word) {

func <- function(x) {
sum(x == unlist(strsplit(tolower(word), "")))
}

all(lapply(letters, FUN = func)<=1)

}
3 changes: 3 additions & 0 deletions exercises/isogram/isogram.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
is_isogram <- function(word) {

}
44 changes: 44 additions & 0 deletions exercises/isogram/test_isogram.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
source('./isogram.R')
library(testthat)

test_that("empty string", {
word <- ""
expect_equal(is_isogram(word), TRUE)
})

test_that("isogram with only lower case characters", {
word <- "isogram"
expect_equal(is_isogram(word), TRUE)
})

test_that("word with one duplicated character", {
word <- "eleven"
expect_equal(is_isogram(word), FALSE)
})

test_that("longest reported english isogram", {
word <- "subdermatoglyphic"
expect_equal(is_isogram(word), TRUE)
})

test_that("word with duplicated character in mixed case", {
word <- "Alphabet"
expect_equal(is_isogram(word), FALSE)
})

test_that("hypothetical isogrammic word with hyphen", {
word <- "thumbscrew-japingly"
expect_equal(is_isogram(word), TRUE)
})

test_that("isogram with duplicated non letter character", {
word <- "Hjelmqvist-Gryb-Zock-Pfund-Wax"
expect_equal(is_isogram(word), TRUE)
})

test_that("made-up name that is an isogram", {
word <- "Emily Jung Schwartzkopf"
expect_equal(is_isogram(word), TRUE)
})

print("All tests passed!")
22 changes: 22 additions & 0 deletions exercises/largest-series-product/example.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
largestSeriesProduct <- function(digits, span){

nums <- as.numeric(unlist(strsplit(digits, "")))

if (any(is.na(nums)) || span < 0 || span > length(nums)) {
stop("Non-numeric characters or span less than digit length")
}

if (span == 0) {
return (1)
}

indices <- 1:(length(nums)-span+1)

get_prod <- function(index, nums, span) {
prod(nums[index:(index+span-1)])
}

products <- sapply(indices, FUN = get_prod, nums, span)

max(products)
}
3 changes: 3 additions & 0 deletions exercises/largest-series-product/largest-series-product.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
largestSeriesProduct <- function(digits, span){

}
110 changes: 110 additions & 0 deletions exercises/largest-series-product/test_largest-series-product.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
source('./largest-series-product.R')
library(testthat)

test_that("finds the largest product if span equals length", {
digits <- "29"
span <- 2
expect_equal(largestSeriesProduct(digits, span), 18)
})

test_that("can find the largest product of 2 with numbers in order", {
digits <- "0123456789"
span <- 2
expect_equal(largestSeriesProduct(digits, span), 72)
})

test_that("can find the largest product of 2", {
digits <- "576802143"
span <- 2
expect_equal(largestSeriesProduct(digits, span), 48)
})

test_that("can find the largest product of 3 with numbers in order", {
digits <- "0123456789"
span <- 3
expect_equal(largestSeriesProduct(digits, span), 504)
})

test_that("can find the largest product of 3", {
digits <- "1027839564"
span <- 3
expect_equal(largestSeriesProduct(digits, span), 270)
})

test_that("can find the largest product of 5 with numbers in order", {
digits <- "0123456789"
span <- 5
expect_equal(largestSeriesProduct(digits, span), 15120)
})

test_that("can get the largest product of a big number", {
digits <- "73167176531330624919225119674426574742355349194934"
span <- 6
expect_equal(largestSeriesProduct(digits, span), 23520)
})

test_that("reports zero if the only digits are zero", {
digits <- "0000"
span <- 2
expect_equal(largestSeriesProduct(digits, span), 0)
})

test_that("reports zero if all spans include zero", {
digits <- "99099"
span <- 3
expect_equal(largestSeriesProduct(digits, span), 0)
})

test_that("rejects span longer than string length", {
digits <- "123"
span <- 4
expect_error(largestSeriesProduct(digits, span))
})

# There may be some confusion about whether this should be 1 or error.
# The reasoning for it being 1 is this:
# There is one 0-character string contained in the empty string.
# That's the empty string itself.
# The empty product is 1 (the identity for multiplication).
# Therefore LSP('', 0) is 1.
# It's NOT the case that LSP('', 0) takes max of an empty list.
# So there is no error.
# Compare against LSP('123', 4):
# There are zero 4-character strings in '123'.
# So LSP('123', 4) really DOES take the max of an empty list.
# So LSP('123', 4) errors and LSP('', 0) does NOT.

test_that("reports 1 for empty string and empty product (0 span)", {
digits <- ""
span <- 0
expect_equal(largestSeriesProduct(digits, span), 1)
})

# As above, there is one 0-character string in '123'.
# So again no error. It's the empty product, 1.

test_that("reports 1 for nonempty string and empty product (0 span)", {
digits <- "123"
span <- 0
expect_equal(largestSeriesProduct(digits, span), 1)
})

test_that("rejects empty string and nonzero span", {
digits <- ""
span <- 1
expect_error(largestSeriesProduct(digits, span))
})

test_that("rejects invalid character in digits", {
digits <- "1234a5"
span <- 2
expect_error(largestSeriesProduct(digits, span))
})

test_that("rejects negative span", {
digits <- "12345"
span <- -1
expect_error(largestSeriesProduct(digits, span))
})

print("All tests passed!")
27 changes: 27 additions & 0 deletions exercises/pascals-triangle/example.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
pascalsTriangle <- function(n) {

if (n == 0) {
return (list())
}
else if (n == 1) {
return (list(1))
}
else if (n == 2) {
return (list(1, c(1,1)))
}
else if (n >= 3) {
triangle <- list(1, c(1,1))
for (x in 3:n) {
row <- rep(1, x)
for (i in 2:(x-1)) {
row[i] = sum(triangle[[x-1]][(i-1):i])
}
triangle[[x]] = row
}
return (triangle)
}
else {
stop("argument n needs to be an integer")
}

}
3 changes: 3 additions & 0 deletions exercises/pascals-triangle/pascals-triangle.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pascalsTriangle <- function(n) {

}
32 changes: 32 additions & 0 deletions exercises/pascals-triangle/test_pascals-triangle.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
source('./pascals-triangle.R')
library(testthat)

test_that("zero rows", {
expect_equal(pascalsTriangle(0), list())
})

test_that("single row", {
expect_equal(pascalsTriangle(1), list(1))
})

test_that("two rows", {
expect_equal(pascalsTriangle(2), list(1, c(1, 1)))
})

test_that("three rows", {
expect_equal(pascalsTriangle(3), list(1, c(1, 1), c(1, 2, 1)))
})

test_that("four rows", {
expect_equal(pascalsTriangle(4), list(1, c(1, 1), c(1, 2, 1), c(1, 3, 3, 1)))
})

test_that("negative rows", {
expect_error(pascalsTriangle(-1))
})

test_that("null/no rows", {
expect_error(pascalsTriangle(NULL))
})

print("All tests passed!")
22 changes: 22 additions & 0 deletions exercises/perfect-numbers/example.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
isPerfect <- function(n){

findFactors <- function(n) {
factors <- c()
for (i in 2:floor(n^0.5 + 1)) {

if (n %% i == 0) {
if (i^2 != n) {
factors <- c(factors, i, n / i)
}
else {
factors <- c(factors, i)
}
}

}
unique(factors)
}

sum(findFactors(n)) + 1 == n

}
3 changes: 3 additions & 0 deletions exercises/perfect-numbers/perfect-numbers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
isPerfect <- function(n){

}
59 changes: 59 additions & 0 deletions exercises/perfect-numbers/test_perfect-numbers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
source('./perfect-number.R')
library(testthat)

test_that("first perfect number", {
n <- 6
expect_equal(isPerfect(n), TRUE)
})

test_that("no perfect number", {
n <- 8
expect_equal(isPerfect(n), FALSE)
})

test_that("second perfect number", {
n <- 28
expect_equal(isPerfect(n), TRUE)
})

test_that("abundant", {
n <- 20
expect_equal(isPerfect(n), FALSE)
})

test_that("answer to the ultimate question of life", {
n <- 42
expect_equal(isPerfect(n), FALSE)
})

test_that("third perfect number", {
n <- 496
expect_equal(isPerfect(n), TRUE)
})

test_that("odd abundant", {
n <- 945
expect_equal(isPerfect(n), FALSE)
})

test_that("fourth perfect number", {
n <- 8128
expect_equal(isPerfect(n), TRUE)
})

test_that("fifth perfect number", {
n <- 33550336
expect_equal(isPerfect(n), TRUE)
})

test_that("sixth perfect number", {
n <- 8589869056
expect_equal(isPerfect(n), TRUE)
})

test_that("seventh perfect number", {
n <- 137438691328
expect_equal(isPerfect(n), TRUE)
})

print("All tests passed!")
Loading

0 comments on commit 56ae3a5

Please sign in to comment.