-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab assignment 5.py
More file actions
231 lines (166 loc) · 4.53 KB
/
Copy pathLab assignment 5.py
File metadata and controls
231 lines (166 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#........................ Task_1a..................#
def factorial(n):
if n == 0 | n == 1:
return 1
else:
return n * factorial(n - 1)
x = factorial(6)
print(x)
#..........................Task_1b................#
def fibonacci(n):
if(n == 0):
return 0
elif(n == 1):
return 1
else:
return fibonacci(n - 1) + fibonacci(n - 2)
x = fibonacci(4)
print(x)
#....................Task_1c.....................#
def print_array(list, index):
if index == len(list):
return
else:
print(list[index])
print_array(list, index+1)
list = [1, 2, 3, 4, 5, 7]
print_array(list, 0)
# Task_1d
def powerN(base, n):
if n == 0:
return 1
else:
return base*powerN(base, n-1)
pow = powerN(3, 3)
print(pow)
#........................Task_2a...................#
def binary_converter(n):
if n == 0:
return 0
else:
return binary_converter(n//2)*10 + (n % 2)
int_val = binary_converter(11)
print(int_val)
#.....................Task_2b.....................#
class Node:
def __init__(self, value):
self.value = value
self.next = None
class singly_linkedlist:
def __init__(self, array):
self.head = None
tail = None
for x in array:
new_node = Node(x)
if self.head == None:
self.head = new_node
tail = new_node
else:
tail.next = new_node
tail = new_node
def addElem(self, head):
if head is None:
return 0
else:
return head.value + self.addElem(head.next)
def sum(self):
return self.addElem(self.head)
array = [1, 2, 3, 4, 5, 6, 7]
listlinked = singly_linkedlist(array)
print(listlinked.sum())
#...............Task_2c.....................#
class Node:
def __init__(self, value):
self.value = value
self.next = None
class linklist:
def __init__(self, array):
self.head = None
tail = None
for x in array:
new_node = Node(x)
if self.head == None:
self.head = new_node
tail = new_node
else:
tail.next = new_node
tail = new_node
def reverse(self, head):
if head.next is None:
print(head.value)
else:
self.reverse(head.next)
print(head.value)
def reverse_print(self):
return self.reverse(self.head)
array = [10, 20, 30, 40, 50]
listlinked = linklist(array)
listlinked.reverse_print()
#.......................task_3....................#
def hocBuilder(height):
if(height == 0):
return str(0) + "\n not build a house at all."
elif height == 1:
return 8
else:
return 5 + hocBuilder(height - 1)
height = int(input("Enter height: "))
print(hocBuilder(height))
# task_4a
def pattern(n):
if(n == 0):
return 0
else:
pattern(n-1)
print(n, end="")
def print_pattern(n):
if(n == 0):
return 0
else:
print_pattern(n - 1)
pattern(n)
print()
n = int(input("Enter n: "))
print_pattern(n)
#..............................task_4b........................#
def pattern(n):
if n == 0:
return 0
else:
pattern(n-1)
print(n, end="")
def print_pattern(n, i):
if n == 0:
return 0
else:
space(n-1)
pattern((i)-n+1)
print()
print_pattern(n-1, i)
def space(n):
if n == 0:
return 0
else:
space(n - 1)
print("", end=" ")
print_pattern(6, 6)
#...............task_5...................#
class FinalQ:
def print(self, array, idx):
if(idx < len(array)):
profit = self.calcProfit(array[idx])
print(
f"{str(idx + 1)}. Investment: {array[idx]}; Profit: {profit} ")
self.print(array, idx+1)
def calcProfit(self, investment):
if investment <= 25000:
return 0.0
elif investment > 25000 and investment <= 100000:
return 45 + self.calcProfit(investment-1000)
elif investment > 100000:
return 80 + self.calcProfit(investment-1000)
else:
return 0
array = [25000, 100000, 250000, 350000]
f = FinalQ()
f.print(array, 0)