Got this question as one of three Capgemini OA questions. only I/P O/P was given no explanation.
Spiderman's webs: As everyone knowns spiderman shoots his web to climb onto buildings. Assume He shoots minimum of 1 web to move from one building to another consecutive building of same height or lower height, and he throws one more than the previous webs if the building's height is more than the previous building. Considering this scenario in the mathematical way, buildings' heights are given as input and your need to print the minimum number of webs required to reach the last building. Initially consider he is starting from the adjacent building which is of the same height as the initial building.
I/P 1 2 3
O/P 6
I/P 2 2 3
O/P 4
I/P 3 2 4 5
O/P 8
I/P 4 6 8 2 4 6
O/P 12
it would be helpful if someone can tell the approach as i passed some test cases but failed
Here is my code
web, total = 1, 1
for i in range(1,len(arr)):
if arr[i]>arr[i-1]:
web += 1
else:
web = 1
total += web
print(total)