Question
In lines computer game, a player sets balls of different colors in a line. When a continuous block of three or more balls of the
In lines computer game, a player sets balls of different colors in a line. When a continuous block of three or more balls of the same color is formed, it is removed from the line. In this case, all the balls are shifted to each other, and the situation may be repeated.
Write a function lines(a) that determines how many balls will be destroyed. There can be at most one continuous block of three or more same-colored balls at the initial moment.
Input data:
The function takes a list aa with initial balls disposition. Balls number is less or equals 1000, balls colors can be from 0 to 9, each color has its own integer.
Output data:
The function has to return one number, the number of the balls that will be destroyed.
Input:[2,2,1,1,1,2,1] Output:6
input : [0, 0, 0, 0, 0], output: 5
input:[2, 3, 1, 4], output: 0
I try to use two pointer approach, but something wrong with my code.
def lines(a): left = 0 right = len(a)-1 s=[] while left < right : if a[left] == a[left:right+1]: s.extend(a[left: right+1]) del a[left: right+1] else: left += 1 right -= 1 return len(s)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started