Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

my codewars function is failing tests but unsure why

$
0
0

I'm trying to complete 'Help the bookseller !' on codewars.

Here are the details:

A bookseller has lots of books classified in 26 categories labeled A, B, ... Z. Each book has a code c of 3, 4, 5 or more capitals letters. The 1st letter of a code is the capital letter of the book category. In the bookseller's stocklist each code c is followed by a space and by a positive integer n (int n >= 0) which indicates the quantity of books of this code in stock. For example an extract of one of the stocklists could be:

L = c("ABART 20", "CDXEF 50", "BKWRK 25", "BTSQZ 89", "DRTYM 60")

You will be given a stocklist (e.g. : L) and a list of categories in capital letters e.g :

  M = c("A", "B", "C", "W")

*and your task is to find all the books of L with codes belonging to each category of M and to sum their quantity according to each category.

For the lists L and M of example you have to return the string:*

  (A : 20) - (B : 114) - (C : 50) - (W : 0)

where A, B, C, W are the categories, 20 is the sum of the unique book of category A, 114 the sum corresponding to "BKWRK" and "BTSQZ", 50 corresponding to "CDXEF" and 0 to category 'W' since there are no code beginning with W. If L or M are empty return string is "". Note: In the result codes and their values are in the same order as in M.

I have finished my function and codewars is returning 3 errors. All of the errors are giving coercion warnings and they all show the value of 'R' as not matching.

As codewars don't show the data input into the function for these errors and i'm unable to find anything wrong with the code in my function i've attempted to replicate the error by creating various input data, none of which outputs warnings and all are giving the correct result.

errors

my function:

stockList <- function(listOfArt, listOfCat) {

  listOfArt <- listOfArt[grepl("\\d", listOfArt)]

  if(length(listOfArt) == 0){return("")}

  s <- sapply(listOfCat, function(x){

    sums <- sum(ifelse(startsWith(listOfArt, x),
                       as.integer(gsub("\\D+(\\d+$)", "\\1", listOfArt)),
                       0), na.rm = T)

    paste0("(", x, " : ", sums, ") - ")

  })

  s <- paste0(s, collapse = "")

  return(substr(s, 1, nchar(s)-3))

}

Here's what i was playing around with to try and output an error:

L = c( "UZZ ", "VZZ ", "RZZ 100", "RZZ 250", "RZZ ", "RZZ 0")
M = c("U", "V", "R")

stockList(L, M)

Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>