DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #40 - Counting Sheep

You're having trouble falling asleep, so your challenge today is to count sheep!

Given a non-negative integer, 3 for example, return a string with a murmur: "1 sheep...2 sheep...3 sheep...". Input will always be valid, i.e. no negative integers.

Happy Coding!


This challenge comes from joshra on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge for a future post? Email yo+challenge@dev.to with your suggestions!

Latest comments (34)

Collapse
 
peter279k profile image
peter279k

Here is the simple solution with PHP:

function countsheep($num){
  $sheepString = "";

  for ($index=1; $index<=$num; $index++) {
    $sheepString .= (string)$index . " sheep...";
  }

  return $sheepString;
}
Collapse
 
matrossuch profile image
Mat-R-Such

Python

def count_sheep(n):
    return ''.join(map(lambda n: '%d sheep...' %(n),range(1,n+1)))
Collapse
 
kvharish profile image
K.V.Harish

My solution in js

const murmur = (times) => Array(times).fill()
                                      .map((value, index) => `${index+1} sheep...`)
                                      .join(' ');
Collapse
 
devparkk profile image
Dev Prakash
Collapse
 
oscherler profile image
Olivier β€œΓ–lbaum” Scherler

Erlang:

% erl
1> Sheep = fun(N) -> lists:flatmap(fun(X) -> lists:concat([X, " sheep..."]) end, lists:seq(1, N)) end.
#Fun<erl_eval.7.91303403>
2> Sheep(3).
"1 sheep...2 sheep...3 sheep..."
Collapse
 
hectorpascual profile image

Python :

def count_sheep(n):
    for i in range(1,n+1):
        print(f"{i} sheep...", end=' ')

In one line :

count_sheep = lambda n : print(''.join([f"{i} sheep... " for i in range(1,n+1)]))
Collapse
 
jay profile image
Jay • Edited

Rust solution: [Playground](

fn count_sheeps(num: u32) -> String {
    let sheep = "πŸ‘"; 
    (1..=num)
        // .map(|n| format!("{} sheep...", n))
        .map(|n| format!("{}... ", sheep.repeat(n as usize)))
        .collect::<Vec<String>>()
        .join("")
}

// -> πŸ‘... πŸ‘πŸ‘... πŸ‘πŸ‘πŸ‘... 
Collapse
 
thepeoplesbourgeois profile image
Josh • Edited
shep = fn n -> 1..n |> Stream.map(&("#{&1} sheep")) |> Enum.join("...") end

IO.puts(shep.(5))
# 1 sheep...2 sheep...3 sheep...4 sheep...5 sheep

⬆️elixir
That was fast... might as well do it in another language too.
⬇️javascript

const shep = (n) => new Array(n).fill(null).map((_, i) => `${i+1} sheep`).join("...")

console.log(shep(5))
// 1 sheep...2 sheep...3 sheep...4 sheep...5 sheep
Collapse
 
hanachin profile image
Seiei Miyagi

ruby <3

def count_sheep(n)
  1..n |> map { "#@1 sheep..." } |> join
end
Collapse
 
thepeoplesbourgeois profile image
Josh

Ruby has pipes now?

Collapse
 
hanachin profile image
Seiei Miyagi

Sadly, pipeline operator is reverted.
github.com/ruby/ruby/commit/2ed68d...

Collapse
 
hanachin profile image
Seiei Miyagi

Yes! pipeline operator added.

Feature #15799: pipeline operator - Ruby master - Ruby Issue Tracking System

And Numbered parameters.

Feature #4475: default variable name for parameter - Ruby master - Ruby Issue Tracking System

It's not released yet. You can try those new syntax by rbenv install 2.7.0-dev.

Collapse
 
alvaromontoro profile image
Alvaro Montoro

Scheme

(define (countSheep n)
  (if (< n 1) 
    ""
    (string-append (countSheep (- n 1)) (number->string n) " sheep...")
  )
)

A demo can be seen in Repl.it. The function would be called (countSheep 4) and the result would be:

"1 sheep...2 sheep...3 sheep...4 sheep..."