0

How would I write a python code for this using split function? Example of input file (.txt) :

3 
Day 1
Option A, 30
Option B, 60
Option C, 90
Day 2
Option A, 16
Option B, 24
Option C, 30
Day 3 
Option A, 64
Option B, 49
Option C, 39

This should be the output:

List A = [3] #total options available
List B = [110, 133, 159] #sums of each individual option
List C = [1, 2, 3] #number of days
Option A = 110
Option B = 133
Option C = 159

I tried utilising the split function but I don't get the above output

option_num_list = []
total_list =[]
input_file = open(userfile, "r")  
for next_line in input_file: 
x = next_line.strip().split(",")
option_number = x[0]
option_num_list.append(option_number)
total = x[-1]
total_list.append(total)
print(sum(total_list))
  • 1
    share the code you tried. – Nesi Nov 03 '22 at 18:27
  • Just use regex instead ! It will be much simpler that way ! – Sanmeet Nov 03 '22 at 18:48
  • Welcome to [Stack Overflow](https://stackoverflow.com/tour). Please check [How to Ask](https://stackoverflow.com/help/how-to-ask). Please edit your question and move the code from the comment into the question itself. – MagnusO_O Nov 03 '22 at 19:08

3 Answers3

0
  • open and iterate over the file
  • if a line starts with 'Day' skip it
  • split each line on a comma
    • use the first item of the split as a dictionary key
    • use the second item to add to that key's value
wwii
  • 23,232
  • 7
  • 37
  • 77
0

You just do it line by line. You'll probably read the data from a file, and in that case you'll want to use .strip() to remove the newline.

data="""3 
Day 1
Option A, 30
Option B, 60
Option C, 90
Day 2
Option A, 16
Option B, 24
Option C, 30
Day 3 
Option A, 64
Option B, 49
Option C, 39""".splitlines()

lista = []
listb = []
listc = []
for row in data:
    parts = row.split()
    if parts[0].isdigit():
        lista.append(int(parts[0]))
    elif parts[0] == 'Day':
        listc.append(int(parts[1]))
    elif parts[0] == 'Option':
        opt = parts[1][:-1]
        opt = "ABC".index(opt)
        if opt >= len(listb):
            listb.append(0)
        listb[opt] += int(parts[2]) 
print("A", lista)
print("B", listb)
print("C", listc)

Output:

A [3]
B [110, 133, 159]
C [1, 2, 3]
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
0
with open('input.txt') as f:
    input_file = f.readlines()
    options = {}

    for i in input_file:
        if i[0].isdigit():  # skip lines with just a number
            continue
        if i.startswith('Day'):  # skip lines that starts with 'Day'
            continue
        else:
            option = i.split(',')[0]  # split the line at comma and take first part.
            if option not in options:  # if this not found in dictionary
                options[option] = [int(i.split(',')[1])]  # add the option: and value
            else:
                # if option already there, just add the value to it.
                options[option].append(int(i.split(',')[1]))
                
print(options)
print('total options = ', len(options))

for k, v in options.items():
    print('sum of ', k, '=', sum(v))

Output

{'Option A': [30, 16, 64], 'Option B': [60, 24, 49], 'Option C': [90, 30, 39]}
total options =  3    
sum of  Option A = 110
sum of  Option B = 133
sum of  Option C = 159
Nesi
  • 286
  • 2
  • 15