🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of katrinleinweber

katrinleinweber's solution

to Largest Series Product in the R Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Given a string of digits, calculate the largest product for a contiguous substring of digits of length n.

For example, for the input '1027839564', the largest product for a series of 3 digits is 270 (9 * 5 * 6), and the largest product for a series of 5 digits is 7560 (7 * 8 * 3 * 9 * 5).

Note that these series are only required to occupy adjacent positions in the input; the digits need not be numerically consecutive.

For the input '73167176531330624919225119674426574742355349194934', the largest product for a series of 6 digits is 23520.

Installation

See this guide for instructions on how to setup your local R environment.

How to implement your solution

In each problem folder, there is a file named <exercise_name>.R containing a function that returns a NULL value. Place your implementation inside the body of the function.

How to run tests

Inside of RStudio, simply execute the test_<exercise_name>.R script. This can be conveniently done with testthat's auto_test function. Because exercism code and tests are in the same folder, use this same path for both code_path and test_path parameters. On the command-line, you can also run Rscript test_<exercise_name>.R.

Source

A variation on Problem 8 at Project Euler http://projecteuler.net/problem=8

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

test_largest-series-product.R

source("./largest-series-product.R")
library(testthat)

context("largest series product")

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

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

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

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

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

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

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

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

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

test_that("rejects span longer than string length", {
  digits <- "123"
  span <- 4
  expect_error(largest_series_product(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(largest_series_product(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(largest_series_product(digits, span), 1)
})

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

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

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

message("All tests passed for exercise: largest-series-product")
library(magrittr)

largest_series_product <- function(digits, span){
  
  # catch edge cases & invalid input
  if (nchar(digits) < span)
    stop("Span must not be longer then the digit string!")
  else if (digits == "" | span == 0)
    return(1)
  else if (stringi::stri_detect_regex(digits, "[^0-9]"))
    stop("There are non-digits in the digit string!")

  # turn digit string into integer vector
  list <- digits %>% 
    strsplit("") %>% 
    unlist %>% 
    as.numeric

  # calculate number of steps
  steps <- length(list) - span + 1
  
  products <- c()
  # walk along vector
  for (s in 1:steps) {
    
    products <- c(products,
                  prod(list[s:(s + span - 1)]))
  }
  
  max(products)
  
  # [ ] speed up by ignoring steps with 0
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?