KickItLikeShika's blog

By KickItLikeShika, history, 4 years ago, In English

I'm not good at Python but i'm trying to learn it, when i always submit a solution to Codeforces i always get run time error, but if i made the same solution in c++ it's okay and accepted, and here is an example. This is the problem set: https://codeforces.com/problemset/problem/1206/B

and here is my solution, all is well in my terminal window but i get run time error even in the first test case, i submit PyPy 3.6 or Python 3.7.2, i tried both

n = int(input())
    # ls = []
    counter = 0
    i = 0
    for i in range(n):
        k = int(input())
        # ls.append(k)
        if k > 1:
            counter += (k - 1)
        elif k < -1:
            counter += abs((k + 1))
        else:
            counter += 1
    print(counter)

and thank you.

  • Vote: I like it
  • +5
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
  Vote: I like it +5 Vote: I do not like it

Auto comment: topic has been updated by KickItLikeShika (previous revision, new revision, compare).

»
4 years ago, # |
  Vote: I like it +5 Vote: I do not like it

Your problem is input. input() returns string — line of input. For example, first call in statement test returns "2" and second returns "-1 1". You can't make integer from the second line. It you want to get numbers from line of input you must read line, than split it and make every element of line an integer.

line = input().split()
for i in range(len(line)):
    line[i] = int(line[i])

shorter version of this code using python magic:

line = list(map(int, input().split()))
  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Thank you for everything, can you explain more cuz i didn't get it, or just give me an article or something like that to read and understand more and i will be very thankful.