鱼C论坛

 找回密码
 立即注册
查看: 4694|回复: 26

[技术交流] Python: 每日一题 51

[复制链接]
发表于 2017-5-22 19:42:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 ooxx7788 于 2017-5-23 14:30 编辑

简单了几天了,今天是不是要弄点难的呢?那么我们就来点难的吧!5kyu的题目!(不过我感觉还好!)

一个人获得了前往另一个地方的方向指示。指示是这样的,"NORTH", "SOUTH", "WEST", "EAST".
显然"NORTH", "SOUTH"是相反的,"WEST", "EAST"是相反的。连续的两个不同方向的走是无效的。
那么现在就请去除掉这些相对的方向,找出真正的方向吧。

例子:
  1. dirReduc(["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"]) => ["WEST"]
  2. dirReduc(["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH"]) => []
复制代码


注意:
["NORTH", "WEST", "SOUTH", "EAST"]是不可以约除"NORTH", "SOUTH"的哦。"NORTH" and "WEST", "WEST" and "SOUTH", "SOUTH" and "EAST" 不是直接的反方向哦。所以答案就是他本身"NORTH", "WEST", "SOUTH", "EAST"].

  1. a = ["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"]
  2. test.assert_equals(dirReduc(a), ['WEST'])
  3. u=["NORTH", "WEST", "SOUTH", "EAST"]
  4. test.assert_equals(dirReduc(u), ["NORTH", "WEST", "SOUTH", "EAST"])
复制代码


游客,如果您要查看本帖隐藏内容请回复

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-5-22 20:19:29 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-5-22 20:24:10 | 显示全部楼层
新手·ing 发表于 2017-5-22 20:19
@jerryxjr1220 @lumber2388779 @冬雪雪冬 @SixPy @ooxx7788 @李金龙
大召唤术!!!

你在我的帖子里面召唤我!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-23 10:13:54 | 显示全部楼层
新手·ing 发表于 2017-5-22 20:19
@jerryxjr1220 @lumber2388779 @冬雪雪冬 @SixPy @ooxx7788 @李金龙
大召唤术!!!

谢邀 我不会
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2017-5-23 12:38:24 | 显示全部楼层
  1. def dirReduc(lst):
  2.     nsew = [["NORTH", "SOUTH"], ["SOUTH", "NORTH"], ["WEST", "EAST"], ["EAST", "WEST"]]
  3.     while True:
  4.         for i in range(len(lst) - 1):
  5.             if lst[i: i + 2] in nsew:
  6.                 lst.pop(i + 1)
  7.                 lst.pop(i)
  8.                 break
  9.         else:
  10.             return lst
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-23 12:48:28 | 显示全部楼层
本帖最后由 jerryxjr1220 于 2017-5-23 12:49 编辑
  1. def dirReduc(lst):
  2.     def one_round(lst):
  3.         new = []
  4.         i = 0
  5.         while i < len(lst) - 2:
  6.             if (lst[i] == 'WEST' and lst[i + 1] == 'EAST') or (lst[i] == 'EAST' and lst[i + 1] == 'WEST') or (lst[i] == 'NORTH' and lst[i + 1] == 'SOUTH') or (lst[i] == 'SOUTH' and lst[i + 1] == 'NORTH'):
  7.                 i += 2
  8.             else:
  9.                 new.append(lst[i])
  10.                 i += 1
  11.         if len(lst) > 1:
  12.             if (lst[-1] == 'WEST' and lst[-2] == 'EAST') or (lst[-1] == 'EAST' and lst[-2] == 'WEST') or (lst[-1] == 'NORTH' and lst[-2] == 'SOUTH') or (lst[-1] == 'SOUTH' and lst[-2] == 'NORTH'):
  13.                 pass
  14.             else:
  15.                 new.append(lst[-1])
  16.         elif len(lst) < 1:
  17.             return []
  18.         else:
  19.             new.append(lst[-1])
  20.         return new
  21.     new = one_round(lst)
  22.     while new != lst:
  23.         lst = new
  24.         new = one_round(lst)
  25.     return new
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-23 13:12:29 | 显示全部楼层

你这个写法比较简洁,借鉴一下。
不过你是用“减法”,我是用的“加法”。
  1. def dirReduc(lst):
  2.     def one_round(lst):
  3.         ref = [["NORTH", "SOUTH"], ["SOUTH", "NORTH"],
  4.                ["WEST", "EAST"], ["EAST", "WEST"]]
  5.         new = []
  6.         i = 0
  7.         while i < len(lst):
  8.             if lst[i:i + 2] in ref:
  9.                 i += 2
  10.             else:
  11.                 new.append(lst[i])
  12.                 i += 1
  13.         return new
  14.     new = one_round(lst)
  15.     while new != lst:
  16.         lst = new
  17.         new = one_round(lst)
  18.     return new
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-23 19:43:19 | 显示全部楼层
很简单~
  1. >>> def dirReduc(ls):
  2.         s=({"NORTH", "SOUTH"}, {"WEST", "EAST"})
  3.         rslt=[]
  4.         for i in ls:
  5.                 if len(rslt) and ({rslt[-1],i} in s):
  6.                         rslt.pop()
  7.                 else:
  8.                         rslt.append(i)
  9.         return rslt
复制代码

  1. >>> ls=["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"]
  2. >>> dirReduc(ls)
  3. ['WEST']
  4. >>> dirReduc(["NORTH", "WEST", "SOUTH", "EAST"])
  5. ['NORTH', 'WEST', 'SOUTH', 'EAST']
  6. >>> dirReduc(["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH"])
  7. []
  8. >>>
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-5-23 20:09:11 | 显示全部楼层


大佬首次参与就给这么6的答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-24 10:03:01 | 显示全部楼层
ooxx7788 发表于 2017-5-23 20:09
大佬首次参与就给这么6的答案

你和 新手ing 是 同一个人?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-5-24 10:10:26 | 显示全部楼层
SixPy 发表于 2017-5-24 10:03
你和 新手ing 是 同一个人?

不是啊~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-24 10:32:30 | 显示全部楼层
  1. def dirReduc(fx):
  2.     sc=['NS','SN','WE','EW']
  3.     fx=[i[0] for i in fx]
  4.     l=len(fx)
  5.     i=0
  6.     while i <len(fx)-1:
  7.         if fx[i]+fx[i+1] in sc:
  8.             fx=fx[:i]+fx[i+2:]
  9.         else :i+=1
  10.     if len(fx)==l:
  11.         return fx
  12.     else:return dirReduc(fx)

  13. a = ["NORTH", "WEST", "SOUTH","EAST"]
  14. print(dirReduc(a))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-7-11 17:58:17 | 显示全部楼层
问题关键在删除中间一对元素后,左右合并的元素有可能相反吧
那递归处理
  1. def dirReduc(a):
  2.     direction = [("NORTH","SOUTH"),("WEST","EAST"),("SOUTH","NORTH"),("EAST","WEST")]
  3.     lst, length = a[:],len(a)-1  # 避免更改输入;控制递归条件
  4.    
  5.     def re_del(lst,length):
  6.         if length == len(lst):  # 退出条件是列表长度递归前后无变化
  7.             return lst
  8.         else:
  9.             length = len(lst)
  10.             j=0
  11.             while j < len(lst)-1:
  12.                 if (lst[j],lst[j+1]) in direction:
  13.                     lst.pop(j)  # 删除会影响index
  14.                     lst.pop(j)
  15.                 else:
  16.                     j += 1
  17.             return re_del(lst,length)
  18.     return re_del(lst,length)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-28 17:08:59 | 显示全部楼层
本帖最后由 张大象 于 2017-9-28 17:19 编辑
  1. Direct=['NORTH','WEST','EAST','SOUTH']
  2. def dirReduc(L):
  3.         for i in range(len(L)//2):
  4.                 for j in range(len(L)):
  5.                         if j+1 == len(L):
  6.                                 break
  7.                         else:
  8.                                 if Direct.index(L[j])+Direct.index(L[j+1]) == 3:
  9.                                         L.pop(j+1)
  10.                                         L.pop(j)
  11.                                         break
  12.         return L
  13. a = ["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"]
  14. print(dirReduc(a))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-4 15:37:38 | 显示全部楼层
本帖最后由 shigure_takimi 于 2017-12-4 15:38 编辑
  1. def hasOppositeDir(directions): # 任意两个相邻的相反的返回True
  2.     oppositeDir = {'NORTH':'SOUTH', 'EAST':'WEST', 'SOUTH':'NORTH', 'WEST':'EAST'}
  3.     length = len(directions)
  4.     if length < 2:
  5.         return False
  6.     else:
  7.         for index in range(length -1):
  8.             if oppositeDir[directions[index]] == directions[index+1]:
  9.                 return True
  10.         return False
  11.    
  12. def getRealDir(directions):
  13.     oppositeDir = {'NORTH':'SOUTH', 'EAST':'WEST', 'SOUTH':'NORTH', 'WEST':'EAST'}
  14.     index = 0
  15.     while hasOppositeDir(directions):
  16.         if oppositeDir[directions[index]] == directions[index+1]:
  17.             directions.pop(index)
  18.             directions.pop(index) # pop掉index的方向后,index+1的方向变为index,pop掉。
  19.         else:
  20.             index += 1
  21.         if index > len(directions)-2:
  22.             index = 0
  23.     return directions


  24. a = ["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"]
  25. b = ["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH"]

  26. print(getRealDir(a))
  27. print(getRealDir(b))
  28.    
  29. ##  符合预期。
  30. ##  ['WEST']
  31. ##  []
  32. ##  看了别人写的,发现自己的比较笨呢。
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-26 14:53:58 | 显示全部楼层
  1. '''
  2. 一个人获得了前往另一个地方的方向指示。指示是这样的,"NORTH", "SOUTH", "WEST", "EAST".
  3. 显然"NORTH", "SOUTH"是相反的,"WEST", "EAST"是相反的。连续的两个不同方向的走是无效的。
  4. 那么现在就请去除掉这些相对的方向,找出真正的方向吧。
  5. 示例:
  6. dirReduc(["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"]) => ["WEST"]
  7. dirReduc(["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH"]) => []
  8. '''
  9. def mystrip(ds):
  10.     while '--' in ds:
  11.         ds = ds.replace('--','-')
  12.       
  13.     return ds

  14. def dirReduc(dl):
  15.     GS = ['SOUTH-NORTH','NORTH-SOUTH','WEST-EAST','EAST-WEST']
  16.     ds = '-'.join(dl)
  17.     while GS[0] in ds or GS[1] in ds or GS[2] in ds or GS[3] in ds:
  18.         if GS[0] in ds:
  19.             ds = ''.join(ds.split(GS[0]))
  20.             ds = mystrip(ds)
  21.         if GS[1] in ds:
  22.             ds = ''.join(ds.split(GS[1]))
  23.             ds = mystrip(ds)
  24.         if GS[2] in ds:
  25.             ds = ''.join(ds.split(GS[2]))
  26.             ds = mystrip(ds)
  27.         if GS[3] in ds:
  28.             ds = ''.join(ds.split(GS[3]))
  29.             ds = mystrip(ds)
  30.         dl = ds.strip('-').split('-')
  31.     return dl
  32.             
  33. print(dirReduc(["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"]))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-23 18:38:29 | 显示全部楼层
理解了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-1-9 19:40:43 | 显示全部楼层
kankan
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-1-10 13:54:44 From FishC Mobile | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-8-24 11:33:11 | 显示全部楼层
  1. lst_1 = ["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"]
  2. lst_2 = ["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH"]
  3. lst_3 = ["NORTH", "WEST", "SOUTH", "EAST"]
  4. def way(lst):
  5.     a = 0
  6.     while a < len(lst):
  7.         lst2 = []
  8.         for i in range(1, len(lst)):
  9.             # print(i)
  10.             if lst[i-1] == 'NORTH' and lst[i] == 'SOUTH':
  11.                 lst[i-1], lst[i] = 0, 0
  12.             elif lst[i-1] == 'SOUTH' and lst[i] == 'NORTH':
  13.                 lst[i-1], lst[i] = 0, 0
  14.             elif lst[i-1] == 'WEST' and lst[i] == 'EAST':
  15.                 lst[i - 1], lst[i] = 0, 0
  16.             elif lst[i-1] == 'EAST' and lst[i] == 'WEST':
  17.                 lst[i - 1], lst[i] = 0, 0
  18.         for i in lst:
  19.             if i != 0:
  20.                 lst2.append(i)
  21.         a += 1
  22.         lst = lst2
  23.     return (lst)
  24. print (way(lst_1))
  25. print (way(lst_2))
  26. print (way(lst_3))
  27. '''
  28. ['WEST']
  29. []
  30. ['NORTH', 'WEST', 'SOUTH', 'EAST']
  31. '''
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-29 09:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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