Thursday, September 21, 2023
HomeSoftware EngineeringIn the event you can’t Sleep, simply depend Sheep in Python

In the event you can’t Sleep, simply depend Sheep in Python


The problem

Given a non-negative integer, 3 for instance, return a string with a murmur: "1 sheep...2 sheep...3 sheep...". Enter will all the time be legitimate, i.e. no unfavourable integers.

The answer in Python code

Possibility 1:

def count_sheep(n):
    sheep = ''
    for i in vary(n):
        sheep+=f"{i+1} sheep..."
    return sheep

Possibility 2:

def count_sheep(n):
    return ''.be part of(f"{i} sheep..." for i in vary(1,n+1))

Possibility 3:

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

Take a look at instances to validate our resolution

import check
from resolution import count_sheep

@check.describe("Fastened Checks")
def fixed_tests():
    @check.it('Primary Take a look at Circumstances')
    def basic_test_cases():
        check.assert_equals(count_sheep(1), "1 sheep...");
        check.assert_equals(count_sheep(2), "1 sheep...2 sheep...")
        check.assert_equals(count_sheep(3), "1 sheep...2 sheep...3 sheep...")
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments