DEV Community

loading...

Discussion on: Daily Challenge #40 - Counting Sheep

Collapse
bhaveshg profile image
BHAVESH GUPTA

Most of the solutions are not taking input
0
into consideration.
Like for 0 the output should be
"0"
not an empty string.

Edit that can be made to question statement.
As according to question input can be a non-negative number. For rest of the inputs it should be like for 3,
"0 1 sheep...2 sheep... 3sheep..."

Pattern is print the numbers upto n starting with 0 and concate the string " sheep..." with them.

Collapse
alvaromontoro profile image
Alvaro Montoro

I kind of understand your suggestion (as zero is not positive nor negative, and the question statement would be more accurate if it was "Given a positive integer"), but I don't see why you suggest that base case.

Why for zero the output should be "0" and not "0 sheep..." or an empty string?

Collapse
bhaveshg profile image
BHAVESH GUPTA

I made some assumptions for the pattern.

As i think the pattern is to print the numbers upto inputted number and concatinating string " sheep...", so as the total number of string " sheep..." in output string is equal to inputted number. that is no string for 0 but the "0" represent the number.

Collapse
thepeoplesbourgeois profile image
Josh • Edited

Oh, alright.

shep = fn
  0 -> ""
  n -> 1..n |> Stream.map(&("#{&1} sheep")) |> Enum.join("...") 
end

IO.puts(shep.(0))
#