How to Count by X in Python


The challenge

Create a function with two arguments that will return an array of the first (n) multiples of (x).

Assume both the given number and the number of times to count will be positive numbers greater than 0.

Return the results as a list.

Examples

count_by(1,10) #should return [1,2,3,4,5,6,7,8,9,10]
count_by(2,5) #should return [2,4,6,8,10]

The solution in Python code

Option 1:

def count_by(x, n):
    out = []
    for i in range(n):
        out.append(x*(i+1))
    return out

Option 2:

def count_by(x, n):
    return range(x, x * n + 1, x)

Option 3:

def count_by(x, n):
    return [i * x for i in range(1, n + 1)]

Test cases to validate our solution

import test
from solution import count_by

@test.describe("Fixed Tests")
def basic_tests():
    @test.it("Fixed tests")
    def fixed_tests():   
        test.assert_equals(count_by(1, 5), [1, 2, 3, 4, 5])
        test.assert_equals(count_by(2, 5), [2, 4, 6, 8, 10])
        test.assert_equals(count_by(3, 5), [3, 6, 9, 12, 15])
        test.assert_equals(count_by(50, 5), [50, 100, 150, 200, 250])
        test.assert_equals(count_by(100, 5), [100, 200, 300, 400, 500])