Move History

Fork Selected
  • Code
    def replicate(t, n):
        return [] if t <= 0 else [n]+replicate(t-1,n)
    
    Test Cases
    from solution import *
    
    @test.describe('Replicate')
    def fixture():
    
        def _ref(times, num):
            return [num] * times
        
        @test.it('Basic Tests')
        def basic():
            data = [(3, 5), (-1, 2), (5, 10), (0, 4)]
            for t, n in data:
                test.assert_equals(replicate(t, n), _ref(t, n))
                
        def counts(f):
            def wrapped(*args, **kwargs):
                wrapped.calls += 1
                return f(*args, **kwargs)
            wrapped.calls = 0
            return wrapped
        
        #@test.it('Recursion Tests')
        def recurse():
            import sys
            r = sys.modules['solution'].replicate = counts(sys.modules['solution'].replicate)
            data = [(3, 5), (5, 10), (-1, 2)]
            for t, n in data:
                r.calls = 0
                test.assert_equals(r(t, n), _ref(t, n))
                if t <= 0: t = 1
                test.expect(t <= r.calls <= t + 1)
                print(r.calls)
        
        #or...
        @test.it('Recursion Tests')
        def recurse2():
            import solution
            r = solution.replicate = counts(solution.replicate)
            data = [(3, 5), (5, 10), (-1, 2)]
            for t, n in data:
                r.calls = 0
                test.assert_equals(r(t, n), _ref(t, n))
                if t <= 0: t = 1
                test.expect(t <= r.calls <= t + 1)
                print(r.calls)
  • Code
    • def replicate(times, num):
    • if times<=0:
    • return []
    • else:
    • return [num]+replicate(times-1,num)
    • print(replicate(8,5))
    • def replicate(t, n):
    • return [] if t <= 0 else [n]+replicate(t-1,n)