The question is to solve the following question in Sedgewick Wayne's Python book:
Given an array of integers, compose a program that finds the length and location of the longest contiguous sequence of equal values where the values of the elements just before and just after this sequence are smaller.
I tried on this problem, and encountered some problems.
Here are my codes:
import sys
import stdio
# Ask the user input for the list of integers
numList = list(sys.argv[1])
maxcount = 0
value = None
location = None
i = 1
while i < len(numList) - 1:
resList = []
count = 0
# If i > i-1, then we start taking i into the resList
if numList[i] > numList[i - 1]:
# start counting
resList += [numList[i]]
# Iterating through the rest of the numbers
j = i + 1
while j < len(numList):
# If the j element equals the i, then append it to resList
if numList[i] == numList[j]:
resList += [numList[j]]
j += 1
elif numList[i] < numList[j]:
# if j element is greater than i, break out the loop
i = j
break
else:
# if j element is smaller than i, count equals length of resList
count = len(resList)
if count > maxcount:
maxcount = count
value = resList[1]
location = i
i = j
else:
# if not greater than the previous one, increment by 1
i += 1
stdio.writeln("The longest continuous plateau is at location: " + str(location))
stdio.writeln("Length is: " + str(maxcount))
stdio.writeln("Number is: " + str(value))
The result shows:
python exercise1_4_21.py 553223334445554
The longest continuous plateau is at location: 11
Length is: 3
Number is: 5
python exercise1_4_21.py 1234567
The longest continuous plateau is at location: None
Length is: 0
Number is: None
But somehow, if the list given is in the format of having a group of continuous integers that is greater than the previous one, but then this group ends the list with no integer following it, my program simply doesn't end....
exercise1_4_21.py 11112222111444
Traceback (most recent call last):
File "exercise1_4_21.py", line 32, in <module>
if numList[i] == numList[j]:
KeyboardInterrupt
exercise1_4_21.py 111222211112223333
Traceback (most recent call last):
File "exercise1_4_21.py", line 25, in <module>
if numList[i] > numList[i - 1]:
KeyboardInterrupt
Not quite sure where the logical error is...thank you very much for your help and kindness!
Seems you overcomplicated the solution (while correctly selected key cases).
It requires only single run through the list.
def maxplat(l):
if (len(l)==0):
return 0, 0
start, leng = 0, 1
maxlen, maxstart = 0, 1
for i in range(1, len(l) + 1):
if (i == len(l)) or (l[i] < l[i-1]):
if (leng > maxlen):
maxlen, maxstart = leng, start
elif (l[i] == l[i-1]):
leng += 1
else:
start, leng = i, 1
return maxlen, maxstart
#test cases
print(maxplat([])) #empty case
print(maxplat([3])) #single element
print(maxplat([3,2,4,4,2,5,5,5,3])) #simple case
print(maxplat([3,2,4,4,2,5,5,5,6])) #up after long run
print(maxplat([3,2,4,4,2,5,5,5])) #run at the end
print(maxplat([3,3,3,3,2,4,4,2])) #run at the start
>>>
(0, 0)
(1, 0)
(3, 5)
(2, 2)
(3, 5)
(4, 0)
You need to add an extra check in your code to exit.
if j == len(numList):
maxcount = len(resList)
value = resList[1]
location = i
break
In your code it'll look like this:
import sys
import stdio
# Ask the user input for the list of integers
numList = list(sys.argv[1])
maxcount = 0
value = None
location = None
i = 1
while i < len(numList) - 1:
resList = []
count = 0
# If i > i-1, then we start taking i into the resList
if numList[i] > numList[i - 1]:
# start counting
resList += [numList[i]]
# Iterating through the rest of the numbers
j = i + 1
while j < len(numList):
# If the j element equals the i, then append it to resList
if numList[i] == numList[j]:
resList += [numList[j]]
j += 1
elif numList[i] < numList[j]:
# if j element is greater than i, break out the loop
i = j
break
else:
# if j element is smaller than i, count equals length of resList
count = len(resList)
if count > maxcount:
maxcount = count
value = resList[1]
location = i
i = j
#EXTRA CHECK HERE
if j == len(numList):
maxcount = len(resList)
value = resList[1]
location = i
break
else:
# if not greater than the previous one, increment by 1
i += 1
stdio.writeln("The longest continuous plateau is at location: " + str(location))
stdio.writeln("Length is: " + str(maxcount))
stdio.writeln("Number is: " + str(value))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.