Tuesday, April 23, 2024
HomeSoftware EngineeringMethods to Generate Vary of Integers in Python

# Methods to Generate Vary of Integers in Python

## The problem#

Implement a operate named generateRange(min, max, step), which takes three arguments and generates a spread of integers from min to max, with the step. The primary integer is the minimal worth, the second is the utmost of the vary and the third is the step. (min < max)

### Job#

Implement a operate named

``````generate_range(2, 10, 2) # ought to return record of [2,4,6,8,10]
generate_range(1, 10, 3) # ought to return record of [1,4,7,10]
``````
``````generate_range(2, 10, 2) # ought to return array of [2, 4, 6, 8, 10]
generate_range(1, 10, 3) # ought to return array of [1, 4, 7, 10]
``````

### Word#

• min < max
• step > 0
• the vary doesn’t HAVE to incorporate max (relying on the step)

## The answer in Python code#

Choice 1:

``````def generate_range(min, max, step):
out = []
for i in vary(min, max+1, step):
out.append(i)
return out
``````

Choice 2:

``````def generate_range(min, max, step):
return [i for i in range(min, max+1, step)]
``````

Choice 3:

``````generate_range=lambda a,b,c:record(vary(a,b+1,c))
``````

## Take a look at instances to validate our answer#

``````import check

@check.describe("Pattern assessments")
def test_group():
@check.it("Easy case")
def test_case1():
check.assert_equals(generate_range(1, 10, 1), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
@check.it('Detrimental numbers')
def test_case2():
check.assert_equals(generate_range(-10, 1, 1), [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1])
@check.it('Step > max')
def test_case3():
check.assert_equals(generate_range(1, 15, 20), [1])
@check.it('Step = 2')
def test_case4():
check.assert_equals(generate_range(1, 7, 2), [1, 3, 5, 7])
@check.it('Step = 3')
def test_case5():
check.assert_equals(generate_range(0, 20, 3), [0, 3, 6, 9, 12, 15, 18])
``````
RELATED ARTICLES