东北冻耳朵

.csv

Bob	Carol	Leanne	Mark	Paul	Will
Carol	Leanne	Mark			
Farley	Paul				
Leanne	Sarai				
Larry	Carol	Leanne	Mark	Will	
Mark	Philip	Zach			
Paul	Zach				
Will	Leanne	Mark			
Zach	Philip				

Code:

### utf-8 ###
import csv
touch_figure = []
temp_touch = []
f = open(r"./DataSet1.txt", "w")
touch_file = open('touch.csv', 'r')
reader = csv.reader(touch_file)

for i in reader:
    temp_touch = []
    for j in range(len(i)):
        if (i[j] != ""):
            temp_touch.append(i[j])
    touch_figure.append(temp_touch)
    for j in range(len(i)):
        if j==0:
            f.write("{} had contact with".format(i[j]))
        else:
            if j+2<len(i) and i[j]!="":
                if i[j+2]!="":
                    f.write(" {},".format(i[j]))
                elif i[j+1]=="":
                    f.write(" {}.".format(i[j]))
                    break
                else:
                    f.write(" {} and {}.".format(i[j],i[j+1]))
                    break
            if j+2==len(i) and i[j+1]!="":
                f.write(" {} and {}.".format(i[j], i[j + 1]))
                break
    f.write("\n")
touch_file.close()

touch_tree = {}
for k in touch_figure:
    for j in k:
        touch_tree[j] = 0
touch_head = {}
for k in touch_figure:
    touch_head[k[0]] = 0
touch_num = {}
touch_num = touch_tree
changed = True
while (changed):
    changed = False
    for p1 in touch_head.keys():
        for i in range(len(touch_figure)):
            if p1 == touch_figure[i][0]:
                for j in range(len(touch_figure[i]) - 1):
                    if touch_tree[p1] <= touch_tree[touch_figure[i][j + 1]]:
                        touch_tree[p1] = touch_tree[touch_figure[i][j + 1]] + 1
                        changed = True
po_head = {}
touch_tree = sorted(touch_tree.items(), key=lambda d: d[1], reverse=True)
f.write("Patient Zero(s):")
for p1 in touch_head.keys():
    flag = True
    for i in range(len(touch_figure)):
        if p1 in touch_figure[i][1:]:
            flag = False
    if flag:
        f.write(" {}".format(p1))
        po_head[p1] = 0
f.write("\n")

f.write("Potential Zombies:")
for i in touch_tree[:]:
    if i[1] == 0:
        f.write(" {}".format(i[0]))
        po_head[i[0]] = 0
f.write("\n")
f.write("Neither Patient Zero or Potential Zombie:")
for j in range(len(touch_tree)):
    if touch_tree[j][0] not in po_head.keys():
        f.write(" {}".format(touch_tree[j][0]))
f.write("\n")
j = -99999999
jj = -1
temp = 0
for i in range(len(touch_figure)):
    jj = j
    j = max(len(touch_figure[i][1:]), j)
    if jj != j:
        temp = i
f.write("Most viral People: {}\n".format(touch_figure[temp][0]))
Tastiest = {}
for i in range(len(touch_figure)):
    for j in range(len(touch_figure[i][1:])):
        Tastiest[touch_figure[i][j + 1]] = 0
for i in range(len(touch_figure)):
    for j in touch_figure[i][1:]:
        Tastiest[j] += 1
touch_min1 = -99999999
for i in Tastiest:
    touch_min1 = max(Tastiest[i], touch_min1)
f.write("Tastiest:")
for j in Tastiest.items():
    if j[1] == touch_min1:
        f.write(" {}".format(j[0]))
f.write("\n")
f.write("Heights:\n")
for i in range(len(touch_tree)):
    f.write("  {}: {}\n".format(touch_tree[i][0], touch_tree[i][1]))


Result:

Bob had contact with Carol, Leanne, Mark, Paul and Will.
Carol had contact with Leanne and Mark.
Farley had contact with Paul.
Leanne had contact with Sarai.
Larry had contact with Carol, Leanne, Mark and Will.
Mark had contact with Philip and Zach.
Paul had contact with Zach.
Will had contact with Leanne and Mark.
Zach had contact with Philip.
Patient Zero(s): Bob Farley Larry
Potential Zombies: Sarai Philip
Neither Patient Zero or Potential Zombie: Carol Will Mark Paul Leanne Zach
Most viral People: Bob
Tastiest: Leanne Mark
Heights:
  Bob: 4
  Larry: 4
  Carol: 3
  Will: 3
  Farley: 3
  Mark: 2
  Paul: 2
  Leanne: 1
  Zach: 1
  Sarai: 0
  Philip: 0

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿的探索之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值