I have a problem with this exercise - I have to find the biggest descending sequence in a list of numbers, but I can "skip" numbers which would screw up my sequence. Output is the length of the biggest sequence.
Example:
Input:
22.155 23.252 22.586 22.450 23.372 22.228 22.087 23.091 22.190 22.140 22.057 22.123 22.359 22.190 22.140 22.523 22.384 22.488 23.201 23.050
Output:
7
The biggest sequence in the example is:
22.155 23.252 22.586 22.450 23.372 22.228 22.087 23.091 22.190 22.140 22.057 22.123 22.359 22.190 22.140 22.523 22.384 22.488 23.201 23.050
I had some ideas, but I couldn't implement them/it turned out they are wrong.
- find all descending sequences and return the biggest one (I have no idea how to do it and I think that this is not the smartest way)
- the second idea is quite complicated and it's hard for me to explain it, because my English is bad
- Find the mainNumber, which has the most numbers lower than the mainNumber (those numbers have to be on the right side of the mainNumber - for example:
22.155 23.252 22.586 22.450 23.372 22.228 22.087 23.091 22.190 22.140 22.057 22.123 22.359 22.190 22.140 22.523 22.384 22.488 23.201 23.050
when the mainNumber is 22.450 you only analyze: 23.372, 22.228, 22.087, etc., not the first three - Now find the new mainNumber, but your sequence starts from the actual mainNumber
- Repeat until there are no more number lower than mainNumber
Here is my code (I am aware that probably you will say it is terrible) I also can't find out what condition should I put in my while loop, so just for now I put 1. And I know that the printed indexes are wrong.
times = [22.155, 23.252, 22.586, 22.45, 23.372, 22.228, 22.087, 23.091, 22.19, 22.14, 22.057, 22.123, 22.359, 22.19, 22.14, 22.523, 22.384, 22.488, 23.201, 23.05]
best = 0
count = 0
index = 0
while 1:
for i in range(index+1, len(times)):
for j in range(i+1, len(times)):
if times[j] < times[i]:
count += 1
if count > best:
best = count
index = i
count = 0
best = 0
print(index)
I would be very grateful for any help. I have no clue what can I do more. Sorry for my bad english.