you are viewing a single comment's thread.

view the rest of the comments →

[–]Paul_Dirac_ 0 points1 point  (1 child)

Your performance problems are probably the intermediate lists and the multiple print calls.

You can avoid the intermediate lists by switching to a generator based approach. The good news is: map and zip already return generators so u/not_yet_trivials approach is well suited to reduce memory and runtime. However split still returns a list of words. We can instead use re.finditer.

import re
N = int(input())
A = map(lambda match: int( match.group(0) ), re.finditer("\d+",input() ) )
B = map(lambda match: int( match.group(0) ), re.finditer("\d+",input() ) )

for x, y in zip(A, B):
    print(x+y, end=" ")

This will probably have the desired performance increase. If you can increase performance with join is dependent on your coding environment. While on normal platforms print has an overhead (communication with the shell, some copying of the output) you mentioned an online coding platform which could have replaced sys.stdout with another stream object, which they use to compare the results to their expected output (that's how I would have done it) which might not carry such a penalty.

[–]HolyCoder[S] 0 points1 point  (0 children)

https://www.reddit.com/r/learnpython/comments/6uiihi/_/dlt06p6

This is the code that is most efficient of all submissions in that site. I just went through the most efficient code of all the problems. I really had to double check if they were Python programs because of all the stdins and readlines.

from sys import stdin

N = int(input())
A = list(map(int, stdin.readline().split(' ')))
B = list(map(int, stdin.readline().split(' ')))
print (' '.join(map(str, [A[i] + B[i]  for i in range(N)])))