0

Selecting the right product in python

How to implement a function selection(products, conditions) Input: phones and conditions Output: the list of products satisfying all conditions Examples: >>> selection(phones, [cheap, large_screen]) [[‘Reno Z’, ‘Oppo’, 6.4, 4035, 397]) >>> selection(phones, [not_apple]) [[‘GalaxyS20’, ‘Samsung’, 6.2, 4000, 1348],[‘Nova 5T’, ‘Huawei’, 6.26, 3750, 497],[‘Reno Z’, ‘Oppo’, 6.4, 4035, 397]]

30th Apr 2020, 6:25 AM
Casc
Casc - avatar
7 odpowiedzi
+ 4
Try using a Named Tuple. I think I have an example to post for you to play with. Let me look
30th Apr 2020, 8:52 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 3
Ok - Named Tuple attached. Input 01 or 02 or 03 to get a result. Then you can see how the code works https://code.sololearn.com/cYtN0UazY6Qr/?ref=app
30th Apr 2020, 9:02 AM
Rik Wittkopp
Rik Wittkopp - avatar
30th Apr 2020, 9:04 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
this is my attempt: Cheap = (4, ‘<=‘, 400) Large_screen = (2, ‘>=‘, 6.3) not_apple = (1, ‘!=‘, ‘Apple’) def selection(products, conditions): for i in range(len(conditions)-1): if conditions[i] == cheap: index = cheap[0] value = cheap[2] if conditions[i+1] == large_screen: index1 = large_screen[0] value1 = large_screen[2] for j in products: if products[j][index] <= value and products[j][index1] >= value1: return products[j]
30th Apr 2020, 6:30 AM
Casc
Casc - avatar
+ 2
But it seems to be not working. If anyone could help, please correct me whereever i went wrong in my code.
30th Apr 2020, 6:32 AM
Casc
Casc - avatar
+ 2
yes... please send it through. Thank you Rik Wittkopp
30th Apr 2020, 8:54 AM
Casc
Casc - avatar
+ 1
Phones = [[‘Iphone11’, ‘Apple’, 6.1, 3110, 1280], [‘GalaxyS20’, ‘Samsung’, 6.2, 4000, 1348], [‘Nova 5T’, ‘Huawei’, 6.26, 3750, 497], [‘Reno Z’, ‘Oppo’, 6.4, 4035, 397]] Conditions: Representing the conditions as triples (i, c, v) where i is and integer feature index, c is a relation symbol and v is some feature value. Cheap = (4, ‘<=‘, 400) Large_screen = (2, ‘>=‘, 6.3) not_apple = (1, ‘!=‘, ‘Apple’)
30th Apr 2020, 6:25 AM
Casc
Casc - avatar