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!

Top comments (34)

Collapse
Β 
badrecordlength profile image
Henry πŸ‘¨β€πŸ’» β€’

This was quite a simple one so I decided to spice it up with emoji! Also, next-gen sleep simulation at the end.

Code:

# -*- coding: UTF-8 -*-

def countSheep(sheepToCount):
        sheepString = ""
        outputString = ""
        for i in range(sheepToCount):
                sheepString += "πŸ‘ "
                outputString += "{}... ".format(sheepString)
        outputString +="ZZZzzzzzzz"
        print(outputString)

countSheep(5)

Output:

πŸ‘ ... πŸ‘ πŸ‘ ... πŸ‘ πŸ‘ πŸ‘ ... πŸ‘ πŸ‘ πŸ‘ πŸ‘ ... πŸ‘ πŸ‘ πŸ‘ πŸ‘ πŸ‘ ... ZZZzzzzzzz
Collapse
Β 
ynndvn profile image
La blatte β€’

Did anybody talk about oneliner?

f=(i)=>[...Array(i)].map((_,i)=>`${i+1} sheep... `).join``

And the results

f(10);
// "1 sheep... 2 sheep... 3 sheep... 4 sheep... 5 sheep... 6 sheep... 7 sheep... 8 sheep... 9 sheep... 10 sheep... "
Collapse
Β 
andymardell profile image
Andy Mardell β€’ β€’ Edited

This is sweet. Something similar but with more Array and less map:

const f = i => Array.from(Array(i), (_, i) => `${i + 1} sheep... `).join``

or

const f = i => Array.from({ length: i }, (_, i) => `${i + 1} sheep... `).join``
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..."

Collapse
Β 
itsdarrylnorris profile image
Darryl Norris β€’

PHP

<?php

/**
 * Daily Challenge #40 - Counting Sheep
 *
 * @param  int    $number Number of sheeps.
 * @return string
 */
function countingSheep(int $number): string
{
  $text = '';
  for ($i = 1; $i <= $number; $i++) {
    $text .= "$i sheep...";
  }

  return $text;
}


echo countingSheep(3);
// Output: 1 sheep...2 sheep...3 sheep...
Collapse
Β 
itsdarrylnorris profile image
Darryl Norris β€’ β€’ Edited

JavaScript

/**
 * Daily Challenge #40 - Counting Sheep
 *
 * @param  {number}        Number of sheeps.
 * @return {string}
 */
const countingSheep = number => {
  // Checking if positive integer or not.
  if (!(number >>> 0 === parseFloat(number))) {
    return `${number} is not a positive integer`;
  }

  let text = '';

  // Building text.
  for (let i = 1; i <= number; i++) {
    text += `${i} sheep...`;
  }
  return text;
};

console.log(countingSheep(3));
// Output: 1 sheep...2 sheep...3 sheep...

Collapse
Β 
danielsclet profile image
Daniel Santos β€’

JavaScript

function cs(n) {
    if (typeof n != "number" && n < 0) return `${n} is not a valid number`;

    let text = "";

    for(let x = 0; x < n; x++) {
        text += `${x} sheep... `
    }

    return text;
}

cs(3) // 0 sheep... 1 sheep... 2 sheep...
Collapse
Β 
dak425 profile image
Donald Feury β€’

PHP

I counted bugs because that is more reflective of what I would do while working :)

I also added a message for if you are able to go to sleep immediately

<?php

function countBugs(int $bugs): string {
  if ($bugs === 0) {
    return "sleep tight..." . PHP_EOL;
  }

  $count = 0;
  $text = ++$count . " bug..." . PHP_EOL;

  while ($count < $bugs) {
    $text .= ++$count . " bugs.." . PHP_EOL;
  }

  return $text;
}

echo "Counting three bugs..." . PHP_EOL;
echo countBugs(3);

echo "Counting no bugs..." . PHP_EOL;
echo countBugs(0);
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 β€’

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
Β 
hanachin profile image
Seiei Miyagi β€’

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

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
Β 
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;
}