Amazon | Tech Screen| SDE 1

Python

Your previous Python 3 content is preserved below:

# Step 1

# Write a function that takes a list of rows and a column name

# It should return the summed price for each unique value of column name

rows = [

{'country': 'US', 'page': 'SUBCATEGORY', 'site': 'IOS', 'price': 0.25},

{'country': 'US', 'page': 'SUBCATEGORY', 'site': 'IOS', 'price': 0.25},

{'country': 'US', 'page': 'SUBCATEGORY', 'site': 'IOS', 'price': 0.25},

{'country': 'US', 'page': 'SUBCATEGORY', 'site': 'IOS', 'price': 0.25},

{'country': 'US', 'page': 'CATEGORY', 'site': 'IOS', 'price': 0.25},

{'country': 'US', 'page': 'CATEGORY', 'site': 'IOS', 'price': 0.25},

{'country': 'US', 'page': 'CATEGORY', 'site': 'IOS', 'price': 0.25},

{'country': 'US', 'page': 'CATEGORY', 'site': 'IOS', 'price': 0.25},

{'country': 'US', 'page': 'CATEGORY', 'site': 'WEB', 'price': 0.25},

{'country': 'US', 'page': 'CATEGORY', 'site': 'WEB', 'price': 0.25},

{'country': 'CA', 'page': 'CATEGORY', 'site': 'WEB', 'price': 0.25},

{'country': 'CA', 'page': 'CATEGORY', 'site': 'WEB', 'price': 0.25},

]

def groupBy(rows, column):

print(groupBy(rows,'country'))

'''expected = [

['US', 2.5],

['CA', 0.5]

]

assert groupBy(rows, 'country') == expected

expected = [

['SUBCATEGORY', 1.0],

['CATEGORY', 2.0]

]

assert groupBy(rows, 'page') == expected

'''

# Step 2

# Write a function that takes a list of rows and a list of column names

# It should return the summed price for each unique combination of column names

'''

def groupByMultiple(rows, columns):

pass

expected = [

['US', 'SUBCATEGORY', 1.0],

['US', 'CATEGORY', 1.5],

['CA', 'CATEGORY', 0.5],

]

assert groupByMultiple(rows, ['country', 'page']) == expected

'''

Comments (1)