Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Getting if num[i] >= connections[i]: IndexError: list index out of range how to fix that Sample Input 0 3 2 1 0 2 7 12

Getting if num[i] >= connections[i]: IndexError: list index out of range how to fix that

Sample Input 0

3 2 1 0 2 7 12 

Sample Output 0

070 021 

Explanation 0

From the connections list, Gina has connected the entrance trigger to display 1. Display 1 is connected to display 0 and display 0 in turn, is connected to display 2. (1 --> 0 --> 2). Recall that display 0 is the rightmost display and display 2 is the leftmost one (See figure below). We are asked to determine what number would be reading on the displays after the 7th and 12th spectators have entered.

The table below indicates the digits that would show on the displays (arranged in the order that they would be read) after several increments, including for the two specific ones in which we are interested.

spectator Display 2 Display 1 Display 0 Explanation
0 0 0 0 The initial setting
1 0 1 0 Hammy wired the input to Display 1, so it advances
2 0 2 0 The same display increments
... ... ... ... Display 1 would continue to increment
7 0 7 0 Up to now, display 1 is the only one being changed
... ... ... ... Display 1 would continue to increment
9 0 9 0 Up to now, display 1 is still the only one being changed
10 0 0 1 Now, when display 1 rolls over, it triggers display 0 because of where its output was connected
11 0 1 1 display 1 advances from 0 to 1
12 0 2 1 display 1 advances from 1 to 2

From the table, we can see that the display was showing 070 after the 7th spectator and 021 after the 12th, so we output those strings on separate lines

def getCounterValues(connections, spectatorCounts): res = [] for s in spectatorCounts: num = [int(d) for d in str(s)] for i in range(len(connections)): if num[i] >= connections[i]: num[i] = 0 if i + 1 < len(num): num[i + 1] += 1 else: num[i] = connections[i] - num[i] res.append(''.join(map(str, num))) return res

if __name__ == '__main__': fptr = open(os.environ['OUTPUT_PATH'], 'w')

first_multiple_input = input().rstrip().split()

k = int(first_multiple_input[0])

n = int(first_multiple_input[1])

connections = list(map(int, input().rstrip().split()))

spectators = list(map(int, input().rstrip().split()))

results = getCounterValues(connections, spectators)

fptr.write(' '.join(results)) fptr.write(' ')

fptr.close()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions