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]]
7 Answers
+ 4
Try using a Named Tuple.
I think I have an example to post for you to play with.
Let me look
+ 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
+ 3
Or you could use OOP
https://code.sololearn.com/cgwfEkS3aB1S/?ref=app
+ 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]
+ 2
But it seems to be not working. If anyone could help, please correct me whereever i went wrong in my code.
+ 2
yes... please send it through. Thank you Rik Wittkopp
+ 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â)