Home > Mobile >  Multiple lists inside a list nested for certain conditions
Multiple lists inside a list nested for certain conditions

Time:02-14

I am solving a problem from hackerrank that requires collections.ordereddict() to be used. In the program, I am supposed to take a bunch of input and store it in the ordereddict, sum the values of the duplicate items and represent it in order. So for the first part, I wrote the following code which probably isn't the problem here..

ordi = OrderedDict()
N = int(input())
for i in range(N):
    item_info = input().split()
    if (len(item_info)>2):
        item_name = str(item_info[0] " " item_info[1])
        price = item_info[-1]
    else:
        item_name= str(item_info[0])
        price = item_info[-1]
    if item_name not in ordi:
        ordi[item_name]= price
    else:
        ordi[item_name]=[ordi[item_name]]
        ordi[item_name].append(price)
# print(ordi)

Test input given by hackerrank is:

9
BANANA FRIES 12
POTATO CHIPS 30
APPLE JUICE 10
CANDY 5
APPLE JUICE 10
CANDY 5
CANDY 5
CANDY 5
POTATO CHIPS 30

the next part of the code loops through the dictionary and is supposed to give the output sum in order

for keys, values in ordi.items():
    total = 0
    if isinstance(values, list):
        print(f"this is what's inside {keys} value ",values)
        int_vals = list(map(int, values))
        total = sum(int_vals)
        print(keys, total)
    else:
        print(keys,values)

This works for input that has two worded strings but doesn't work for one-worded strings i.e. Candy in this case. Now to debug what's inside of the value of candy key I printed it's values:

OUTPUT:
BANANA FRIES 12
this is what's inside POTATO CHIPS value  ['30', '30']
POTATO CHIPS 60
this is what's inside APPLE JUICE value  ['10', '10']
APPLE JUICE 20
this is what's inside CANDY value  [[['5', '5'], '5'], '5']

As you can see, inside of the candy value, there is lists of lists inside of a list which is uncalled for, I can't understand what am I doing wrong.

CodePudding user response:

Here is a working solution:

ordi = OrderedDict()

N = int(input())
for i in range(N):
    item_info = input().split()
    
    item_name = " ".join(item_info[:-1])
    price = int(item_info[-1])

    if item_name not in ordi.keys():
        ordi[item_name] = price
    else:
        ordi[item_name]  = price

print(ordi)

Output:

OrderedDict([('BANANA FRIES', 12), ('POTATO CHIPS', 60), ('APPLE JUICE', 20), ('CANDY', 20)])

I submit it in hackerrank and it passed the tests, I don't exactly know what was the problem with your code, but I think it has something to do with the way you handled adding the values to the dict, this part:

    if item_name not in ordi:
        ordi[item_name]= price
    else:
        ordi[item_name]=[ordi[item_name]]
        ordi[item_name].append(price)

I hope my solution will help you learn something new

if you want your code to work just do this:

    if item_name not in ordi.keys():
        ordi[item_name] = list(price)
    else:
        ordi[item_name].append(price)

The problem was with this part: [ordi[item_name]] if the item was a list you warped it in another list, and since the "CANDY" key had the most inputs he was problematic, not because the key was one word. When I enter custom input with a lot of POTATO CHIPS this is what I get:

this is what's inside BANANA FRIES value  ['12', '12']
BANANA FRIES 24
this is what's inside POTATO CHIPS value  [[['30', '30'], '30'], '30']
  • Related