鱼C论坛

 找回密码
 立即注册
查看: 4192|回复: 38

[已解决]Python:每日一题 377

[复制链接]
发表于 2020-4-18 16:59:55 | 显示全部楼层
  1. def f(list1):
  2.     def x(list1):
  3.         return list1[0]
  4.     def y(list1):
  5.         return list1[1]
  6.     list2 = []
  7.     list1 = sorted(list1,key=x)
  8.     xmin = list1[0][0]
  9.     xmin_list = []
  10.     xmax = list1[-1][0]
  11.     xmax_list = []
  12.     for each in list1:
  13.         if each[0] == xmin:
  14.             list2.append(each)
  15.             xmin_list.append(each[1])
  16.         elif each[0] == xmax:
  17.             list2.append(each)
  18.             xmax_list.append(each[1])
  19.    
  20.     list1 = sorted(list1,key=y)
  21.     ymin = list1[0][1]
  22.     ymin_list = []
  23.     ymax = list1[-1][1]
  24.     ymax_list = []
  25.     for each in list1:
  26.         if each[1] == ymin:
  27.             list2.append(each)
  28.             ymin_list.append(each[0])
  29.         elif each[1] == ymax:
  30.             list2.append(each)
  31.             ymax_list.append(each[0])

  32.     xmin_list.sort()
  33.     xmax_list.sort()
  34.     ymin_list.sort()
  35.     ymax_list.sort()

  36.     def f1(p1,p2,p3):
  37.         if abs((p2[1]-p1[1])/(p2[0]-p1[0]))<abs((p3[1]-p1[1])/(p3[0]-p1[0])):
  38.             return 2
  39.         elif abs((p2[1]-p1[1])/(p2[0]-p1[0]))==abs((p3[1]-p1[1])/(p3[0]-p1[0])):
  40.             return 1
  41.         else:
  42.             return 0
  43.         
  44.     leftup,leftdown,rightup,rightdown = list1[:],list1[:],list1[:],list1[:]
  45.     p1,p2,p3,p4,p5,p6,p7,p8 = [xmin,xmin_list[-1]],[ymax_list[0],ymax],[ymax_list[-1],ymax],[xmax,xmax_list[-1]],[xmax,xmax_list[0]],[ymin_list[-1],ymin],[ymin_list[0],ymin],[xmin,xmin_list[0]]
  46.     h1,h2,h3,h4 = [],[],[],[]
  47.     while leftup != []:
  48.         for each in leftup.copy():
  49.             if each[0]>=p2[0] or each[1]<=p1[1]:
  50.                 leftup.remove(each)
  51.             else:
  52.                 if f1(p1,p2,each)==2:
  53.                     h1 = []
  54.                     h1.append(each)
  55.                     p2 = each
  56.                 elif f1(p1,p2,each)==1:
  57.                     h1.append(each)
  58.         if h1 != []:
  59.             list2 += h1
  60.             h1 = sorted(h1,key=y)
  61.             p1 = h1[-1]
  62.             p2 = [ymax_list[0],ymax]
  63.             for m in h1:
  64.                 leftup.remove(m)
  65.         else:
  66.             break
  67.     while rightup != []:
  68.         for each in rightup.copy():
  69.             if each[0]<=p3[0] or each[1]<=p4[1]:
  70.                 rightup.remove(each)
  71.             else:
  72.                 if f1(p4,p3,each)==2:
  73.                     h2 = []
  74.                     h2.append(each)
  75.                     p3 = each
  76.                 elif f1(p4,p3,each)==1:
  77.                     h2.append(each)
  78.         if h2 != []:
  79.             list2 += h2
  80.             h2 = sorted(h2,key=y)
  81.             p4 = h2[-1]
  82.             p3 = [ymax_list[-1],ymax]
  83.             for m in h2:
  84.                 rightup.remove(m)
  85.         else:
  86.             break
  87.     while leftdown != []:
  88.         for each in leftdown.copy():
  89.             if each[0]>=p7[0] or each[1]>=p8[1]:
  90.                 leftdown.remove(each)
  91.             else:
  92.                 if f1(p8,p7,each)==2:
  93.                     h4 = []
  94.                     h4.append(each)
  95.                     p7 = each
  96.                 elif f1(p8,p7,each)==1:
  97.                     h4.append(each)
  98.         if h4 != []:
  99.             list2 += h4
  100.             h4 = sorted(h4,key=x)
  101.             p8 = h4[-1]
  102.             p7 = [ymin_list[0],ymin]
  103.             for m in h4:
  104.                 leftdown.remove(m)
  105.         else:
  106.             break
  107.     while rightdown != []:
  108.         for each in rightdown.copy():
  109.             if each[0]<=p6[0] or each[1]>=p5[1]:
  110.                 rightdown.remove(each)
  111.             else:
  112.                 if f1(p5,p6,each)==2:
  113.                     h3 = []
  114.                     h3.append(each)
  115.                     p6 = each
  116.                 elif f1(p5,p6,each)==1:
  117.                     h3.append(each)
  118.         if h3 != []:
  119.             list2 += h3
  120.             h3 = sorted(h3,key=x)
  121.             p5 = h3[0]
  122.             p6 = [ymin_list[-1],ymin]
  123.             for m in h3:
  124.                 rightdown.remove(m)
  125.         else:
  126.             break
  127.     return list2
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
zltzlt + 3 + 3

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-19 05:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表