## DEV Community is a community of 871,688 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Daily Challenge #194 - Spread Number

### Setup

Implement a function that will create an array and fill it with numbers ranging from `1` to `n`. The numbers will always be positive.

### Examples

`spreadNumber(1)` => ``

`spreadNumber(2)` => `[1, 2]`

`spreadNumber(5)` => `[1, 2, 3, 4, 5]`

### Tests

`spreadNumber(3)`

`spreadNumber(6)`

`spreadNumber(9)`

Good luck!

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

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

## Discussion (29) ``````spreadNumber n = [1..n]

-- The most flexible way
spreadEnum :: Enum a => a -> a
spreadEnum = enumFromTo \$ toEnum 1
`````` This challenge was simple, so here's a brainfuck solution too. Only works with characters 1-9 as inputs (I have an idea for reading multidigit numbers as input, but I'm on my phone and don't want to type it out).

``````,>++++++[-<-------->>++++++++<]<[->>+.<<]
`````` Michael Kohl

Nice, and kudos for typing it out on a phone 😀 Sagiv ben giat

JavaScript:

``````const spreadNumber = n => [...Array(n).keys()]
`````` Matt Ellen

Another way:

``````const spreadNumber = n => Array.from({length:n}).map((_, i) => i+1);
`````` Cent | Shannon Myers

Javascript, just using a for loop

``````
const spreadNumber = (number) => {
let returnArray = [];

for (let i = 0; i < number; i++) {
returnArray.push(i+1);
}

return returnArray;
}

``````

Codepen @nobody • Edited on

Clojure short and simple

``````(ns daily-challenge.one-ninety-four)

;; precondition check to bound domain
{:pre [(> 0 n)]}

(unless (= n 1)
(vec (range 1 n))
))
``````

and some tests...

``````(deftest one-ninety-four
(is (= [1 2 3] (spreadNumber 3)))
(is (= [1 2 3 4 5 6] (spreadNumber 6)))
(is (= [1 2 3 4 5 6 7 8 9] (spreadNumber 9))))

(run-tests 'daily-challenge.one-ninety-four)

=> Testing daily-challenge.one-ninety-four

Ran 1 tests containing 3 assertions.
0 failures, 0 errors.
{:type :summary, :test 1, :pass 3, :fail 0, :error 0}
`````` David Mendoza (He/Him) • Edited on

Python

``````def spreadNumber(n):
return range(1,n+1)
`````` Rafael Acioly

this will not work, it doesn't include the last digit:

`spreadNumber(4)` => `[1, 2, 3]`

you forgot to add `+1` on `n` David Mendoza (He/Him)

You are right, I didnt take that in consideration, but it just range(1,n+1) Natamo • Edited on

Late to the party, as always.

``````Func<int, int[]> SpreadNumber = (int n) => Enumerable.Range(1, n).ToArray();
``````

Example:

``````foreach(int i in SpreadNumber(10)) {
Console.WriteLine(i);
}
`````` Michael Kohl • Edited on

Golfscript:

``````{,{1+}%}:spread;
``````

Usage:

``````5 spread
``````

Or if we don't need a function just

``````5,{1+}%
``````

Explanation: `,` takes the top of the stack and turns it into an array from 0 to n - 1. `{1+}` is a block adding 1 to each argument and `%` is the map function. The surrounding `{}` turns everything into a block which we assign (`:`) to the name `spread`. dart

``````List<int> spreadNumber(int n) {
var nums = List<int>();
for(var x = 1; x <= n.abs(); x++) {
}
return nums;
}
``````

lol damn, while this is one way, looking at other solutions. I forgot range was a thing.

``````List<int> spreadNumber(int n) {
return List<int>.generate(n.abs(), (n) => n + 1);
}
`````` Kosta Zivic

Python, shortest approach

``````lambda  x: [i+1 for i in range(x)]
`````` Avalander • Edited on

The easiest challenge yet.

``````spreadNumber :: Int -> [Int]
`````` Sabin Pandelovitch

Simple JS

``````const spreadNumber = nr => Array.from({ length: nr }, (i, n) => n + 1);
`````` Eduard Giménez

Ruby solution below. We could also return a Range directly, but here I'm being purist with the exercise.

``````def spreadNumber(n)
Array(1..n)
end
`````` Nijeesh Joshy

### GO

``````func main() {
fmt.Println(x)
}

ar := make([]int, n)
for i := range ar {
ar[i] = i + 1
}
return ar
}
`````` Vidit Sarkar

C++

``````vector<int> spreadNumber(int number){
vector<int> v(number);
generate(v.begin(),v.end(),[i=1]()mutable{return i++;});
return v;
}
`````` Javascript

``````spreadNumber = n => Array.from(Array(n).keys()).map(x => x+1);
``````

Zero based ;)

``````spreadNumber = n => Array.from(Array(n).keys());
``````