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

0 min read 99 words

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

Option 2:

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

Option 3:

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

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...")
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags