Home > Back-end >  How Do I Check RegEx In Integer Form
How Do I Check RegEx In Integer Form

Time:12-07

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

CodePudding user response:

You can check if i is a integer or not by check=i.isnumeric() Put a if condition-

for i in list_array:

if (check==True):
    sum =i
    new_array.append(sum)
  • Related