If you can’t Sleep, just count Sheep in Python

The challenge

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.

The solution in Python code

Option 1:

def count_sheep(n): sheep = '' for i in range(n): sheep+=f"{i+1} sheep..." return sheep
Code language: Python (python)

Option 2:

def count_sheep(n): return ''.join(f"{i} sheep..." for i in range(1,n+1))
Code language: Python (python)

Option 3:

def count_sheep(n): return ('{} sheep...'*n).format(*list(range(1,n+1)))
Code language: Python (python)

Test cases to validate our solution

import test from solution import count_sheep @test.describe("Fixed Tests") def fixed_tests(): @test.it('Basic Test Cases') def basic_test_cases(): test.assert_equals(count_sheep(1), "1 sheep..."); test.assert_equals(count_sheep(2), "1 sheep...2 sheep...") test.assert_equals(count_sheep(3), "1 sheep...2 sheep...3 sheep...")
Code language: Python (python)
Notify of
Inline Feedbacks
View all comments