Skip to content

Commit

Permalink
Added recursion problems
Browse files Browse the repository at this point in the history
  • Loading branch information
vaargueta committed Dec 21, 2022
1 parent 70710d0 commit 215b0ba
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 0 deletions.
29 changes: 29 additions & 0 deletions problems/recursion/short-exercises.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Short Exercises: Recursion

## Recursion with numbers and lists

### Exercise #1

**File**: [sum_cubes.py](sum_cubes.py)

Complete the function ``sum_cubes``, which takes an integer :math:`n` and computes the sum
$$1^3 + 2^3 + 3^3 + \dots + n^3.$$

Do not use loops nor list comprehensions.

### Exercise #2

**File**: [sublists.py](sublists.py)

A *sublist* of a list ``lst`` is a list that contains some (possibly all or none) of the elements of `lst`, in the same order. For example, the following are all the sublists of ``['A', 'B', 'C']``:

```python
[] ['A']
['C'] ['A', 'C']
['B'] ['A', 'B']
['B', 'C'] ['A', 'B', 'C']
```

You will complete the function ``sublists``, which takes a list of values and returns a list of all sublists of the input.

To figure out how to solve the sublists problem recursively, focus on the example above, which shows the solution to the sublists problem on the input ``['A', 'B', 'C']``. Identify how the first column above is the solution to the sublists problem on a simpler input. Then, find a relationship between the first column and the second column.
97 changes: 97 additions & 0 deletions problems/recursion/sublists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
def sublists(lst):
"""
Computes all sublists of the input list.
Input:
lst: list of values
Returns: (list of list of values) list of all sublists of lst.
"""

pass


#############################################################
### ###
### Testing code. ###
### !!! DO NOT MODIFY ANY CODE BELOW THIS POINT !!! ###
### ###
#############################################################

import sys
sys.path.append('../')

import test_utils as utils

def do_test_sublists(lst):
original_lst = list(lst)
recreate_msg = utils.gen_recreate_msg('sublists', original_lst)
expected = [[x for j, x in enumerate(lst) if i ^ (2 ** j) < i]
for i in range(2 ** len(lst))]
actual = sublists(lst)
utils.check_none(actual, recreate_msg)
utils.check_type(actual, expected, recreate_msg)
for el in actual:
utils.check_type(el, [], recreate_msg)
utils.check_equals(sorted(actual), sorted(expected), recreate_msg)
utils.check_parameter_unmodified(lst, original_lst, "lst", recreate_msg)


def test_sublists_1():
do_test_sublists(['apple'])

def test_sublists_2():
do_test_sublists([12])

def test_sublists_3():
do_test_sublists([True])

def test_sublists_4():
do_test_sublists(['A', 'B'])

def test_sublists_5():
do_test_sublists(["apple", "tomato"])

def test_sublists_6():
do_test_sublists(['A', 'B', 'C'])

def test_sublists_7():
do_test_sublists([50, 150, 100])

def test_sublists_8():
do_test_sublists(['A', 'B', 'C', 'D'])

def test_sublists_9():
do_test_sublists([50, 0, -1, 10])

def test_sublists_10():
do_test_sublists([50, 0, -1, 10, 5])

def test_sublists_11():
do_test_sublists(['A', 'B', 'C', 'D', 'E'])

def test_sublists_12():
do_test_sublists(['U', 'V', 'W', 'X', 'Y', 'Z'])

def test_sublists_13():
do_test_sublists(['water', 'apple', 'tomato', 'zucchini', 'corn', 'stew'])

def test_sublists_14():
do_test_sublists(list(range(0, 70, 10)))

def test_sublists_15():
do_test_sublists(list(range(70, 0, -10)))

def test_sublists_16():
do_test_sublists(list('estuary'))

def test_sublists_17():
do_test_sublists([1, 2, 3, 5, 8, 13, 21])

def test_sublists_18():
do_test_sublists([1, -2, 3, -5, 8, -13, 21])

def test_sublists_19():
do_test_sublists([8, 6, 7, 5, 3, 0, 9])

def test_sublists_20():
do_test_sublists([1, 2, 3, 5, 8, 13, 21, 34])
94 changes: 94 additions & 0 deletions problems/recursion/sum_cubes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
def sum_cubes(n):
"""
Recursively calculates the sum of the first n positive cubes.
Input:
n: positive integer.
Returns: (integer) the value of the sum 1^3 + 2^3 + ... + n^3.
This function may not use any loops or list comprehensions.
"""

pass


#############################################################
### ###
### Testing code. ###
### !!! DO NOT MODIFY ANY CODE BELOW THIS POINT !!! ###
### ###
#############################################################

import sys
sys.path.append('../')

import test_utils as utils

def do_test_sum_cubes(n):
recreate_msg = utils.gen_recreate_msg('sum_cubes', n)
actual = sum_cubes(n)
expected = sum(n ** 3 for n in range(n + 1))
utils.check_none(actual, recreate_msg)
utils.check_type(actual, expected, recreate_msg)
utils.check_equals(actual, expected, recreate_msg)


def test_sum_cubes_1():
do_test_sum_cubes(1)

def test_sum_cubes_2():
do_test_sum_cubes(2)

def test_sum_cubes_3():
do_test_sum_cubes(3)

def test_sum_cubes_4():
do_test_sum_cubes(4)

def test_sum_cubes_5():
do_test_sum_cubes(5)

def test_sum_cubes_6():
do_test_sum_cubes(6)

def test_sum_cubes_7():
do_test_sum_cubes(7)

def test_sum_cubes_8():
do_test_sum_cubes(8)

def test_sum_cubes_9():
do_test_sum_cubes(9)

def test_sum_cubes_10():
do_test_sum_cubes(10)

def test_sum_cubes_11():
do_test_sum_cubes(15)

def test_sum_cubes_12():
do_test_sum_cubes(19)

def test_sum_cubes_13():
do_test_sum_cubes(24)

def test_sum_cubes_14():
do_test_sum_cubes(30)

def test_sum_cubes_15():
do_test_sum_cubes(31)

def test_sum_cubes_16():
do_test_sum_cubes(36)

def test_sum_cubes_17():
do_test_sum_cubes(42)

def test_sum_cubes_18():
do_test_sum_cubes(50)

def test_sum_cubes_19():
do_test_sum_cubes(81)

def test_sum_cubes_20():
do_test_sum_cubes(100)

0 comments on commit 215b0ba

Please sign in to comment.