I am a software engineer currently working @ShopPad, previously @HPE. I like to build websites and web application in PHP, JavaScript, and Golang. I have an unhealthy obsession with Mexican food (🌯)
I am a software engineer currently working @ShopPad, previously @HPE. I like to build websites and web application in PHP, JavaScript, and Golang. I have an unhealthy obsession with Mexican food (🌯)
/**
* Daily Challenge #40 - Counting Sheep
*
* @param {number} Number of sheeps.
* @return {string}
*/constcountingSheep=number=>{// Checking if positive integer or not.if(!(number>>>0===parseFloat(number))){return`${number} is not a positive integer`;}lettext='';// Building text.for(leti=1;i<=number;i++){text+=`${i} sheep...`;}returntext;};console.log(countingSheep(3));// Output: 1 sheep...2 sheep...3 sheep...
functioncs(n){if(typeofn!="number"&&n<0)return`${n} is not a valid number`;lettext="";for(letx=0;x<n;x++){text+=`${x} sheep... `}returntext;}cs(3)// 0 sheep... 1 sheep... 2 sheep...
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
<?phpfunctioncountBugs(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;echocountBugs(3);echo"Counting no bugs...".PHP_EOL;echocountBugs(0);
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.
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?
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.
Hi! I'm Arctic Kona, an Icelandic arctic fox (/Vulpes lagopus fuliginosus/). Interested in all things technology and already enjoyed 2 great summers (~ 16 "fox years" old).
Game Developer, Metaverse Creater.
With over four years of experience working in the metaverse, Theo's had the opportunity to work on top experiences, spearheading technical and creative design.
This was quite a simple one so I decided to spice it up with emoji! Also, next-gen sleep simulation at the end.
Code:
Output:
Did anybody talk about oneliner?
And the results
This is sweet. Something similar but with more
Arrayand lessmap:or
Scheme
A demo can be seen in Repl.it. The function would be called
(countSheep 4)and the result would be:PHP
JavaScript
JavaScript
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
ruby <3
Ruby has pipes now?
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.Sadly, pipeline operator is reverted.
github.com/ruby/ruby/commit/2ed68d...
My solution in js
Here is the simple solution with PHP:
⬆️elixir
That was fast... might as well do it in another language too.
⬇️javascript
Rust solution: [Playground](
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.
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?
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.
Oh, alright.
Some recursivity in JavaScript
or something like that.
Python :
In one line :
My solution in ruby
What about a range?
Oh, yeah.
Forgot about range.
Nice!
Trusty repl.it
Had no idea you could embed that,
how do you do that if you don't mind my asking?
Basically, you should add your '@username/replit_name' inside
See dev.to/p/editor_guide for more options.
PHP
Python
Perl solution, the integer is specified as an argument to the program:
Welcome to my fantasy world. :D
Output:
Erlang:
[gist.github.com/devparkk/334ee3d1f...]