I’m confuse how to create recursive function to print JSON file without any loops in Python3.
So i have home work from my college, the subject is about recursion in function, the lecturer told me to to use recursion only instead of loops.
My json file in Category.json is:
JavaScript
x
20
20
1
[{'catId': 1, 'id': 1, 'name': 'Buku & Bangunan'},
2
{'catId': 1, 'id': 2, 'name': 'Buku Codes & Standar'},
3
{'catId': 1, 'id': 3, 'name': 'Buku Dekorasi & Ornamen'},
4
{'catId': 1, 'id': 4, 'name': 'Buku Desain Dapur'},
5
{'catId': 1, 'id': 5, 'name': 'Buku Desain Kamar'},
6
{'catId': 1, 'id': 6, 'name': 'Buku Desain Ruang Keluarga'},
7
{'catId': 1, 'id': 7, 'name': 'Buku Desain Ruang Tamu'},
8
{'catId': 1, 'id': 8, 'name': 'Buku Desain Rumah'},
9
{'catId': 1, 'id': 9, 'name': 'Buku Interior & Eksterior'},
10
{'catId': 1, 'id': 10, 'name': 'Buku Metode & Material Bangunan'},
11
{'catId': 1, 'id': 11, 'name': 'Buku Taman'},
12
{'catId': 2, 'id': 12, 'name': 'Persiapan Tes CPNS'},
13
{'catId': 2, 'id': 13, 'name': 'Persiapan Tes TOELF & IELTS'},
14
{'catId': 2, 'id': 14, 'name': 'Persiapan TPA & Psikotest'},
15
{'catId': 3, 'id': 15, 'name': 'Buku Aktivitas'},
16
{'catId': 3, 'id': 16, 'name': 'Buku Cerita Anak'},
17
{'catId': 3, 'id': 17, 'name': 'Buku Dongeng'},
18
{'catId': 3, 'id': 18, 'name': 'Buku Dunia Pengetahuan'},
19
{'catId': 3, 'id': 19, 'name': 'Buku Fabel'}]
20
My python code is:
JavaScript
1
10
10
1
import json
2
3
def showData(file):
4
file_json = open(file)
5
data = json.loads(file_json.read())
6
7
return data
8
9
print(showData("Category.json"))
10
I’m not finishing my code because i’m still confuse how to do recursion without loops.
Do you have any idea?
Advertisement
Answer
I don’t understand why you need to use recursion
just to print a JSON file instead of iterations(for, while).
This is how you print your data using Recursion.
JavaScript
1
24
24
1
d = [{'catId': 1, 'id': 1, 'name': 'Buku & Bangunan'},
2
{'catId': 1, 'id': 2, 'name': 'Buku Codes & Standar'},
3
{'catId': 1, 'id': 3, 'name': 'Buku Dekorasi & Ornamen'},
4
{'catId': 1, 'id': 4, 'name': 'Buku Desain Dapur'},
5
{'catId': 1, 'id': 5, 'name': 'Buku Desain Kamar'},
6
{'catId': 1, 'id': 6, 'name': 'Buku Desain Ruang Keluarga'},
7
{'catId': 1, 'id': 7, 'name': 'Buku Desain Ruang Tamu'},
8
{'catId': 1, 'id': 8, 'name': 'Buku Desain Rumah'},
9
{'catId': 1, 'id': 9, 'name': 'Buku Interior & Eksterior'},
10
{'catId': 1, 'id': 10, 'name': 'Buku Metode & Material Bangunan'}]
11
12
# Recursive Code
13
def print_json(j):
14
# Base condition
15
if len(j) == 0:
16
return
17
# Print the first item of j
18
print(j[0])
19
# Call the same function on j[1:]
20
# Skipping the first item of j in every call
21
print_json(j[1:])
22
23
print_json(d)
24
JavaScript
1
11
11
1
{'catId': 1, 'id': 1, 'name': 'Buku & Bangunan'}
2
{'catId': 1, 'id': 2, 'name': 'Buku Codes & Standar'}
3
{'catId': 1, 'id': 3, 'name': 'Buku Dekorasi & Ornamen'}
4
{'catId': 1, 'id': 4, 'name': 'Buku Desain Dapur'}
5
{'catId': 1, 'id': 5, 'name': 'Buku Desain Kamar'}
6
{'catId': 1, 'id': 6, 'name': 'Buku Desain Ruang Keluarga'}
7
{'catId': 1, 'id': 7, 'name': 'Buku Desain Ruang Tamu'}
8
{'catId': 1, 'id': 8, 'name': 'Buku Desain Rumah'}
9
{'catId': 1, 'id': 9, 'name': 'Buku Interior & Eksterior'}
10
{'catId': 1, 'id': 10, 'name': 'Buku Metode & Material Bangunan'}
11
Do you know that Recursion uses something called as a Recursion Stack internally ?
That’s some extra space overhead with recursive codes.