coding-coworking-club / python-spring-2021

ccClub Python Spring 2021
https://www.ccclub.io/course/2021Spring
23 stars 6 forks source link

[Homework 4#] 推薦系統 #502

Closed andrewyao67 closed 3 years ago

andrewyao67 commented 3 years ago

提交連結

https://judge.ccclub.io/status/57a99e7d61e365fccbc41db307ec040a

程式碼

# input data
inputdata=""
p = eval(input())/100
data = input()
while data != "end":
    inputdata += data + "\n"
    data = input()

# get the data
data = inputdata.split("\n")
data.remove(data[-1])
my_dict = {}
for line in data:
    line = line.split(" ")
    name = line[0]
    items = {i for i in line[1:]}
    my_dict[name] = items

# implement
result = ""
for name in my_dict.keys():
    my_list = my_dict.get(name)
    num = len(my_list)
    diff = {}
    for other_people in my_dict.keys():
        if (other_people == name):
            continue
        count = 0
        other_list = my_dict.get(other_people)
        for other_option in other_list:
            if other_option in my_list:
                count+=1
        if (float((count)/num) >= p):
            for other_option in other_list:
                if other_option not in my_list:
                    diff[other_option] = name
    result+=name+" "
    for d in diff.keys():
        result+=d+" "
    result+="\n"
print(result)

錯誤訊息

問題描述

答案的物品順序不知道怎麼處理,內容都對,圖片是output。(錢包、鑰匙的順序錯) messageImage_1621253509207

TJ72 commented 3 years ago

因為你用 set 來存商品,但 set 裡的資料本身是沒有順序的,所以輸出的資料不會保持原本的順序。要不要試試看用串列的方式存資料,然後再用 set 的 method 進行運算?

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.