Reputation: 3
This is a question form my programming lab:
Consider this data sequence: "3 11 5 5 5 2 4 6 6 7 3 -8". Any value that is the same as the immediately preceding value is considered a CONSECUTIVE DUPLICATE. In this example, there are three such consecutive duplicates: the 2nd and 3rd 5s and the second 6. Note that the last 3 is not a consecutive duplicate because it was preceded by a 7. Write some code that uses a loop to read such a sequence of non-negative integers, terminated by a negative number. When the code finishes executing, the number of consecutive duplicates encountered is printed. In this case,3 would be printed. ASSUME the availability of a variable, stdin, that references a Scanner object associated with standard input.
Here is my code:
firstNumber=-1
secondnumber=-1
count=0
firstNumber=input(int())
while int(firstNumber) > 0:
secondnumber=input(int())
if secondnumber == firstNumber:
count+=1
else:
firstNumber=secondnumber
print(int(count))
when I run the code in the MPL if for example the input is:
stdin.txt:·"1↵ 1↵ 1↵ 1↵ 1↵ 1↵ -1
the result is like this:
Expected Output: _stdout.txt:·"5↵ Actual Output: _stdout.txt:·"00000005↵
would you please guide what is going wrong with my code? Thanks a lot in advance.
Upvotes: 0
Views: 2384
Reputation: 3
I was stuck on this problem for weeks before I figured it out. It can help to put the code you're troubleshooting into IDLE or repl.it/languages/python3; then when you run it you'll often see the mistakes. The stdin variable is completely meaningless in python it seems, MPL includes it seemingly just to confuse you. You were very close to getting it, I'm a beginner too but I pointed out the mistakes I saw and annotated your code with the errors. Hopefully I didn't miss too much and it helps others struggling with the actual concept they're trying to learn from the question.
##your Code##
firstNumber =-1 #Didn't need to define var. here since defining them below
secondnumber =-1 #^^^^^but mpl didn't reject them either, may have no effect
count=0
firstNumber = input(int()) #this was the ERROR, using 'int' here added zeros
while int(firstNumber) > 0:
secondnumber=input(int()) #ERROR, 'int' adds zeros to input line and output
if secondnumber == firstNumber:
count+=1
else:
firstNumber=secondnumber
print(int(count))
###The code MPL wanted (this worked):
firstNumber=input()
count = 0
while int(firstNumber) > 0:
secondNumber=input()
if secondNumber == firstNumber:
count+=1
else:
firstNumber = secondNumber
print(count) # align with while loop so it happens after the loop ends,
#don't need the int here either
Upvotes: 0
Reputation: 71451
You can try this:
import re
import itertools
s = "3 11 5 5 5 2 4 6 6 7 3 -8"
new_data = list(map(int, re.findall("-\d+|\d+", s)))
new_sum = [(a, list(b)) for a, b in itertools.groupby(new_data)]
final_sum = sum(len(b)-1 for a, b in new_sum if len(b) > 1)
Output:
3
Upvotes: 1
Reputation: 402513
I'd recommend doing this by doing an element wise diff on the list using zip
+ sum
.
sum(y - x == 0 for y, x in zip(l[1:], l))
You can do this nicely by defining a function:
def count_consec_duplicates(lst):
return sum(y - x == 0 for y, x in zip(l[1:], l))
And call it appropriately.
data = [1, 1, 1, 1, 1, 1, -1]
print(count_consec_duplicates(data))
5
Upvotes: 1