-
Code def replicate(t, n): return [] if t <= 0 else [n]+replicate(t-1,n)
Test Cases from solution import * .describe('Replicate') def fixture(): def _ref(times, num): return [num] * times .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... .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)
Output:
-
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)
- All
- {{group.name}} ({{group.count}})
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}