From 9d20fbff3765de92e6f1b7de299bf310c107bb5c Mon Sep 17 00:00:00 2001 From: Punpun <86565283+ppskpunpun@users.noreply.github.com> Date: Wed, 11 Sep 2024 04:47:59 +0700 Subject: [PATCH] Add recursive example for finding a key in nested boxes (Python) (#290) --- 03_recursion/python/08_look_for_key.py | 50 ++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 03_recursion/python/08_look_for_key.py diff --git a/03_recursion/python/08_look_for_key.py b/03_recursion/python/08_look_for_key.py new file mode 100644 index 0000000..8367b89 --- /dev/null +++ b/03_recursion/python/08_look_for_key.py @@ -0,0 +1,50 @@ +# Item have 2 types: key or box +class Item: + def __init__(self, is_key=False): + self.is_key = is_key + + # If not a key, it's a box that can hold items + self.items_in_box = [] + + def is_a_box(self): + return not self.is_key + + def is_a_key(self): + return self.is_key + + +def look_for_key(box: Item): + for item in box.items_in_box: + if item.is_a_box(): + # recursive case + look_for_key(item) + elif item.is_a_key(): + # base case + print("found the key!") + + +""" +main_box +├── box_A +│ ├── box_B +│ └── box_C +└── box_D + └── box_E + └── key +""" +main_box = Item() + +box_A = Item() +box_B = Item() +box_C = Item() +box_A.items_in_box = [box_B, box_C] + +box_D = Item() +box_E = Item() +key = Item(is_key = True) +box_E.items_in_box = [key] +box_D.items_in_box = [box_E] + +main_box.items_in_box = [box_A, box_D] + +look_for_key(main_box) \ No newline at end of file