How Do I Check RegEx In Integer Form

Question:

I am trying to do the Advent Of Code 2022, 1st problem. (DONT TELL THE ANSWER). What i am doing is reading the file and taking each number and adding it to a sum value. What happens is, when I come across the "n", it doesn’t understand it and I am having trouble trying to create the array of sums. Can anyone help?

`

with open("input.txt") as f:
  list_array = f.read().split("n")
  print(list_array)
  new_array = []
  sum = 0
  for i in list_array:
    print(i)
    if i == "n":
      new_array.append(sum)
      sum = 0
    sum += int(str(i))
    print(sum)

`

I was trying to convert to back to a str then an int, but it doesn’t work

Asked By: Riyan Nayak

||

Answers:

You can check if i is an integer or not using check=i.isnumeric().
Put an if condition:

for i in list_array:
    if (check==True):
        sum+=i
        new_array.append(sum)
Answered By: Abhinav Varshney
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.