Home > OS >  I am getting error in code in taking input and append it to array
I am getting error in code in taking input and append it to array

Time:03-06

You are given N sticks, where the length of each stick is a positive integer. A cut operation is performed on the sticks such that all of them are reduced by the length of the smallest stick. Given the length of N sticks, print the number of sticks that are left before each subsequent cut operations. Note: For each cut operation, you have to recalculate the length of smallest sticks (excluding zero-length sticks).

Input The first line contains a single integer N.

The next line contains N integers separated by space, where each integer represents the length of the ith stick.

6

5 4 4 2 2 8

Output For each operation, print the number of sticks that are cut, on separate lines.

6

4

2

1

Explanation

import array as arr

n = int(input())
a = arr.array('i',[1002])
for i in range(n):
    c = [int(x) for x in input().split()]
    a.append(c)

t=n
for i in range(0,1001):
    if a[i] > 0:
        print(t)
        t=t-a[i]

CodePudding user response:

You can't append a list to an integer array. If you want to merge two arrays you can use the extend method.

    a.extend(c)

CodePudding user response:

if a is list then below all satisfies but here a is array so we cant append list with array

a = [1,2,3] # a is a list
c = [4] # c is list

it won't give you correct result in any case

print(a.append(c)) # if we do a.append(c) result is like a = [1,2,3,[4]]

gives you correct result in any case

print(a.extend(c)) # if we do a.extend(c) result is like a = [1,2,3,4]

satisfies if "a" is list and "c" is list or "a" is array and "c" is also an array

a  = c # a  = c result is same as a.extend(c)
print(a) # a = [1,2,3,4]
  • Related