Skip to main content
Code Review

Return to Answer

deleted 10 characters in body
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134
for array in A:
 found = False
 count, pre_sum, post_sum = 0, 0, sum(array)
 for element in array:
 post_sum -= element
 if(pre_sum == post_sum):
 found = True
 break
 pre_sum += element
 print("YES" if found else "NO")
for array in A:
 found = False
 count, pre_sum, post_sum = 0, 0, sum(array)
 for element in array:
 post_sum -= element
 if(pre_sum == post_sum):
 found = True
 break
 pre_sum += element
 print("YES" if found else "NO")
for array in A:
 found = False
 pre_sum, post_sum = 0, sum(array)
 for element in array:
 post_sum -= element
 if(pre_sum == post_sum):
 found = True
 break
 pre_sum += element
 print("YES" if found else "NO")
added 366 characters in body
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134

Similar to this question , yourYour input could be more succinct:

for i in range(T):
 N = int(input)
 A.append(map(int, input().split()))

This will store a map object in A, instead of a list. But since that is iterable we won't have any problems.

If the input was in one line with the first element being the length of the array and the rest of the line being the array, it would have been even easier:

Similar to this question , your input could be more succinct:

Your input could be more succinct:

for i in range(T):
 N = int(input)
 A.append(map(int, input().split()))

This will store a map object in A, instead of a list. But since that is iterable we won't have any problems.

If the input was in one line with the first element being the length of the array and the rest of the line being the array, it would have been even easier:

added 426 characters in body
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134

General comments

You should follow PEP8, the coding standard for python. This means using underscore_names for variables instead of camelCase.

i is a bad generic name, except when iterating explicitly over integers. Maybe use for arr in A.

I would use more descriptive variable names. Instead of A use maybe arrays?

Similar to this question, your input could be more succinct:

for i in range(T):
 N, *arr = map(int, input().split())
 A.append(arr)

This uses a nice feature of python3. N will take the first element of an iterable and arr will take all the (possible) rest. You can even specify variables at the end. Try these cases out to get a feel for it:

a, *b = [] # ValueError: not enough values to unpack (expected at least 1, got 0)
a, *b = [0] # a = 0, b = []
a, *b = [0,1] # a = 0, b = [1]
a, *b = [0,1,2] # a = 0, b = [1,2]
a, *b, c = [0] # ValueError: not enough values to unpack (expected at least 2, got 1)
a, *b, c = [0,1] # a = 0, b = [], c = 1
a, *b, c = [0,1,2] # a = 0, b = [1], c =2= 2

Performance

Instead of always calculating all sums, you could store the pre_ and post_sum and add/subtract the current element. You should also stop after having found one occurrence.

for array in A:
 found = False
 count, pre_sum, post_sum = 0, 0, sum(array)
 for element in array:
 post_sum -= element
 if(pre_sum == post_sum):
 found = True
 break
 pre_sum += element
 print("YES" if found else "NO")

I'm not exactly sure, but I think there is a small performance difference between these two, for large arr:

arr = list(map(int, arr))
# and
arr = [int(x) for x in arr]

General comments

You should follow PEP8, the coding standard for python. This means using underscore_names for variables instead of camelCase.

i is a bad generic name, except when iterating explicitly over integers. Maybe use for arr in A.

I would use more descriptive variable names. Instead of A use maybe arrays?

Similar to this question, your input could be more succinct:

for i in range(T):
 N, *arr = map(int, input().split())
 A.append(arr)

This uses a nice feature of python3. N will take the first element of an iterable and arr will take all the (possible) rest. You can even specify variables at the end. Try these cases out to get a feel for it:

a, *b = [] # ValueError: not enough values to unpack (expected at least 1, got 0)
a, *b = [0] # a = 0, b = []
a, *b = [0,1] # a = 0, b = [1]
a, *b = [0,1,2] # a = 0, b = [1,2]
a, *b, c = [0] # ValueError: not enough values to unpack (expected at least 2, got 1)
a, *b, c = [0,1] # a = 0, b = [], c = 1
a, *b, c = [0,1,2] # a = 0, b = [1], c =2

Performance

Instead of always calculating all sums, you could store the pre_ and post_sum and add/subtract the current element. You should also stop after having found one occurrence.

for array in A:
 found = False
 count, pre_sum, post_sum = 0, 0, sum(array)
 for element in array:
 post_sum -= element
 if(pre_sum == post_sum):
 found = True
 break
 pre_sum += element
 print("YES" if found else "NO")

I'm not exactly sure, but I think there is a small performance difference between these two, for large arr:

arr = list(map(int, arr))
# and
arr = [int(x) for x in arr]

General comments

You should follow PEP8, the coding standard for python. This means using underscore_names for variables instead of camelCase.

i is a bad generic name, except when iterating explicitly over integers. Maybe use for arr in A.

I would use more descriptive variable names. Instead of A use maybe arrays?

Similar to this question, your input could be more succinct:

for i in range(T):
 N, *arr = map(int, input().split())
 A.append(arr)

This uses a nice feature of python3. N will take the first element of an iterable and arr will take all the (possible) rest. You can even specify variables at the end. Try these cases out to get a feel for it:

a, *b = [] # ValueError: not enough values to unpack (expected at least 1, got 0)
a, *b = [0] # a = 0, b = []
a, *b = [0,1] # a = 0, b = [1]
a, *b = [0,1,2] # a = 0, b = [1,2]
a, *b, c = [0] # ValueError: not enough values to unpack (expected at least 2, got 1)
a, *b, c = [0,1] # a = 0, b = [], c = 1
a, *b, c = [0,1,2] # a = 0, b = [1], c = 2

Performance

Instead of always calculating all sums, you could store the pre_ and post_sum and add/subtract the current element. You should also stop after having found one occurrence.

for array in A:
 found = False
 count, pre_sum, post_sum = 0, 0, sum(array)
 for element in array:
 post_sum -= element
 if(pre_sum == post_sum):
 found = True
 break
 pre_sum += element
 print("YES" if found else "NO")

I'm not exactly sure, but I think there is a small performance difference between these two, for large arr:

arr = list(map(int, arr))
# and
arr = [int(x) for x in arr]
added 426 characters in body
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134
Loading
added 15 characters in body
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134
Loading
added 215 characters in body
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134
Loading
added 171 characters in body
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134
Loading
added 59 characters in body
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134
Loading
added 43 characters in body
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134
Loading
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /