Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spomenik Monument Database

Authors: Donald Niebyl, FUEL, Damon Murray, Stephen Sorrell

1st Edition

0995745536, 978-0995745537

Students also viewed these Databases questions

Question

Can equilibrium occur below full employment? Why, or why not?

Answered: 1 week ago

Question

Describe the use of tests in the selection process.

Answered: 1 week ago

Question

1. What is Ebola ? 2.Heart is a muscle? 3. Artificial lighting?

Answered: 1 week ago

Question

Where those not participating, encouraged to participate?

Answered: 1 week ago

Question

3. Who would the members be?

Answered: 1 week ago