Posts: 230
Threads: 39
Joined: Mar 2020
Dec-05-2022, 06:36 PM
(This post was last modified: Dec-05-2022, 06:36 PM by astral_travel.)
hey,
i'm trying the advent of code challange,
i currently have the following code:
input_file = open("/home/tal/PycharmProjects/pythonProject2/input")
data = input_file.readlines()
newList = []
for line in data:
if line != "\n":
newList.append(int(line.rstrip('\n')))
else:
newList.append("")
print(newList)
print(type(newList[2])) i attached the input file
i don't know how to sum the chunks of numbers, like, between the '' separators,
any advice ?
thanks
Attached Files
input.txt (Size: 10.22 KB / Downloads: 183)
Posts: 6,779
Threads: 20
Joined: Feb 2020
Dec-05-2022, 06:48 PM
(This post was last modified: Dec-05-2022, 06:48 PM by deanhystad.)
Why are you asking for help solving a challenge? Isn't solving it yourself the "challenging" part? Otherwise, what is the point?
Why are you putting '' in as a separator?
Posts: 230
Threads: 39
Joined: Mar 2020
yea but i'm trying and trying and searching for resources on this on the internet and nothing comes up and i'm not finding anything and am lacking the basic knowledge to somehow figure this out....i simply don't know how to add up the numbers up to/between the separators....
what can i do
Posts: 453
Threads: 16
Joined: Jun 2022
Dec-05-2022, 07:07 PM
(This post was last modified: Dec-05-2022, 07:09 PM by rob101.)
You're almost there.
All you need to do now, is to loop through the list of int values, adding each one to a total, until you hit your separators, then append the total to a new list, reset the total to zero, and go again. Once that's done, you can use the max() function, on the totals list, to get the max value.
Hint:
for number in newList:
print(number) That's how I would do it, but I'm sure there's more than one way.
Sig:
>>> import this
The UNIX philosophy: "Do one thing, and do it well."
"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse
"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Posts: 6,779
Threads: 20
Joined: Feb 2020
Dec-05-2022, 07:31 PM
(This post was last modified: Dec-05-2022, 08:06 PM by deanhystad.)
Hint, get rid of new_list.
Let's say you are a cashier at the grocery store. There are 5 customers in your line. Do you make customers wait as you scan all 5 carts, or do you scan the first cart, computing the total as you scan, and take the customer's money before moving on to the next cart?
Most coding challenges don't have much to do with coding. They are puzzles that test your problem-solving skills. You don't need to know any fancy Python to solve this challenge. Think about how you would solve with pencil and paper, and I guarantee there will be no lists with separators and no wondering how to take the sum of a list. Solve first, then code.
Posts: 230
Threads: 39
Joined: Mar 2020
i need to create a sum of the chunks of numbers (chunks = the numbers between the separators).
Posts: 6,779
Threads: 20
Joined: Feb 2020
Dec-05-2022, 08:08 PM
(This post was last modified: Dec-05-2022, 08:15 PM by deanhystad.)
(Dec-05-2022, 07:53 PM)astral_travel Wrote: i need to create a sum of the chunks of numbers (chunks = the numbers between the separators). Yes, you need to calculate sums. Do you need a list for that?
Also, there is no need for the rstrip() here. int() doesn't care if "\n" following the digits.
int(line.rstrip('\n'))
Posts: 230
Threads: 39
Joined: Mar 2020
(Dec-05-2022, 07:31 PM)deanhystad Wrote: Let's say you are a cashier at the grocery store. There are 5 customers in your line. Do you scan all 5 carts and then compute the totals, making the poor customers wait before taking their money? Or do you scan the first cart, computing the total as you scan? When the cart is empty you know the total for the first customer and are ready to take their money.
Most coding challenges don't have much to do with coding. They are puzzles that test your problem-solving skills. You don't need to know any fancy Python to solve this challenge. Think about how you would solve with pencil and paper, and I guarantee there will be no lists with separators and no wondering how to take the sum of a list. Solve first, then code.
the logic is clear to me, i just don't know how to tell the program to sum it up until it hits a separator/delimiter.
i thought of something like this:
input_file = open("/home/tal/PycharmProjects/pythonProject2/input.txt")
data = input_file.readlines()
newList = []
for line in data:
if line != "\n":
newList.append(int(line.rstrip('\n')))
else:
newList.append("")
print(newList)
print(type(newList[2]))
sum_list = []
for i in newList:
sum_list.append(i)
for x in sum_list:
sum_list = sum(sum_list)
if x == "":
pass
print(sum_list) idk....feeling stuck...
Posts: 230
Threads: 39
Joined: Mar 2020
(Dec-05-2022, 08:08 PM)deanhystad Wrote: (Dec-05-2022, 07:53 PM)astral_travel Wrote: i need to create a sum of the chunks of numbers (chunks = the numbers between the separators). And why do you need a list for that?
okay, you're right, i don't...
but i just don't know how to sum the chunks...i wrote a lil more code in the message above..
if it's one long array of numbers you simply use sum() and that's it....but sum() doesn't work when you're trying to add up the numbers up to the delimiter...
Posts: 6,779
Threads: 20
Joined: Feb 2020
Dec-05-2022, 08:40 PM
(This post was last modified: Dec-05-2022, 08:40 PM by deanhystad.)
Your latest attempt can be compressed to a single loop,
with open("test.txt", "r") as file:
sum_list = []
for line in file:
if line != "\n":
sum_list.append(int(line))
else:
print(sum_list)
sum_list = [] Now get rid of sum_list because we don't need the list. We need the total.
|