用python比较labtop在列表中的价格和质量

2024-04-18 14:51:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Python解决以下场景。你知道吗

Two guys sindi and isra have collected the price and quality data for laptops they received. They want to know the cheapest laptop with best quality from this data.

现在您必须帮助irsa编写一个应用程序来实现这个逻辑。你知道吗

Input format is:

  1. 第一行将有数据中笔记本电脑总数的数字
  2. 接下来的n行中的每一行都由两个数字组成,表示
    • 笔记本电脑价格的第一个数字
    • 笔记本电脑质量的第二个数字。你知道吗

如果你能找到两台符合irsa规定条件的笔记本电脑?你知道吗

将输出打印为happy irsa,否则打印poor irsa(请注意,字母很小,所有字母都用小写字母书写。)

输入:

2
1 10
7 3

输出:

 "Happy irsa"

输入:

4
1 5
7 9
5 6
20 30

输出: “可怜的irsa”

mylist = []
for i in range(count):
    laptopPrice,laptopQuality = [int(x) for x in input().split()]
    mylist.append(laptopPrice)
    mylist.append(laptopQuality)

请帮我完成这个代码!你知道吗


Tags: andtheinfordata字母场景数字
1条回答
网友
1楼 · 发布于 2024-04-18 14:51:34

我解决了:

number_of_laptops = int(input())
list_of_prices = []
list_of_qualities = []

for i in range(0,number_of_laptops):
    inp = input()
    numbers = []
    numbers = [int(s) for s in inp.split() if s.isdigit()]
    list_of_prices.append(numbers[0])
    list_of_qualities.append(numbers[1])

def find_better_lp(number_of_laptops):

if number_of_laptops == 0:
    return print("empty list")

for i in range(0,number_of_laptops):
    for j in range(0,number_of_laptops):      
        if((list_of_prices[i] <= list_of_prices[j]) and i != j):
            if(list_of_qualities[i] >= list_of_qualities[j]):
                return print("happy irsa")

return print("poor irsa")

find_better_lp(number_of_laptops)

相关问题 更多 >