ooxx7788 发表于 2017-5-22 19:42:55

Python: 每日一题 51

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

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

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

例子:
dirReduc(["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"]) => ["WEST"]
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"].

a = ["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"]
test.assert_equals(dirReduc(a), ['WEST'])
u=["NORTH", "WEST", "SOUTH", "EAST"]
test.assert_equals(dirReduc(u), ["NORTH", "WEST", "SOUTH", "EAST"])

**** Hidden Message *****

新手·ing 发表于 2017-5-22 20:19:29

@jerryxjr1220 @lumber2388779 @冬雪雪冬 @SixPy @ooxx7788 @李金龙
大召唤术!!!

ooxx7788 发表于 2017-5-22 20:24:10

新手·ing 发表于 2017-5-22 20:19
@jerryxjr1220 @lumber2388779 @冬雪雪冬 @SixPy @ooxx7788 @李金龙
大召唤术!!!

你在我的帖子里面召唤我!

李金龙 发表于 2017-5-23 10:13:54

新手·ing 发表于 2017-5-22 20:19
@jerryxjr1220 @lumber2388779 @冬雪雪冬 @SixPy @ooxx7788 @李金龙
大召唤术!!!

{:10_266:} 谢邀 我不会

冬雪雪冬 发表于 2017-5-23 12:38:24

def dirReduc(lst):
    nsew = [["NORTH", "SOUTH"], ["SOUTH", "NORTH"], ["WEST", "EAST"], ["EAST", "WEST"]]
    while True:
      for i in range(len(lst) - 1):
            if lst in nsew:
                lst.pop(i + 1)
                lst.pop(i)
                break
      else:
            return lst

jerryxjr1220 发表于 2017-5-23 12:48:28

本帖最后由 jerryxjr1220 于 2017-5-23 12:49 编辑

def dirReduc(lst):
    def one_round(lst):
      new = []
      i = 0
      while i < len(lst) - 2:
            if (lst == 'WEST' and lst == 'EAST') or (lst == 'EAST' and lst == 'WEST') or (lst == 'NORTH' and lst == 'SOUTH') or (lst == 'SOUTH' and lst == 'NORTH'):
                i += 2
            else:
                new.append(lst)
                i += 1
      if len(lst) > 1:
            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'):
                pass
            else:
                new.append(lst[-1])
      elif len(lst) < 1:
            return []
      else:
            new.append(lst[-1])
      return new
    new = one_round(lst)
    while new != lst:
      lst = new
      new = one_round(lst)
    return new

jerryxjr1220 发表于 2017-5-23 13:12:29

冬雪雪冬 发表于 2017-5-23 12:38


你这个写法比较简洁,借鉴一下。
不过你是用“减法”,我是用的“加法”。
def dirReduc(lst):
    def one_round(lst):
      ref = [["NORTH", "SOUTH"], ["SOUTH", "NORTH"],
               ["WEST", "EAST"], ["EAST", "WEST"]]
      new = []
      i = 0
      while i < len(lst):
            if lst in ref:
                i += 2
            else:
                new.append(lst)
                i += 1
      return new
    new = one_round(lst)
    while new != lst:
      lst = new
      new = one_round(lst)
    return new

SixPy 发表于 2017-5-23 19:43:19

很简单~
>>> def dirReduc(ls):
        s=({"NORTH", "SOUTH"}, {"WEST", "EAST"})
        rslt=[]
        for i in ls:
                if len(rslt) and ({rslt[-1],i} in s):
                        rslt.pop()
                else:
                        rslt.append(i)
        return rslt


>>> ls=["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"]
>>> dirReduc(ls)
['WEST']
>>> dirReduc(["NORTH", "WEST", "SOUTH", "EAST"])
['NORTH', 'WEST', 'SOUTH', 'EAST']
>>> dirReduc(["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH"])
[]
>>>

ooxx7788 发表于 2017-5-23 20:09:11

SixPy 发表于 2017-5-23 19:43
很简单~

大佬首次参与就给这么6的答案

SixPy 发表于 2017-5-24 10:03:01

ooxx7788 发表于 2017-5-23 20:09
大佬首次参与就给这么6的答案

你和 新手ing 是 同一个人?

ooxx7788 发表于 2017-5-24 10:10:26

SixPy 发表于 2017-5-24 10:03
你和 新手ing 是 同一个人?

不是啊~

余欲渔 发表于 2017-5-24 10:32:30

def dirReduc(fx):
    sc=['NS','SN','WE','EW']
    fx= for i in fx]
    l=len(fx)
    i=0
    while i <len(fx)-1:
      if fx+fx in sc:
            fx=fx[:i]+fx
      else :i+=1
    if len(fx)==l:
      return fx
    else:return dirReduc(fx)

a = ["NORTH", "WEST", "SOUTH","EAST"]
print(dirReduc(a))

solomonxian 发表于 2017-7-11 17:58:17

问题关键在删除中间一对元素后,左右合并的元素有可能相反吧
那递归处理
def dirReduc(a):
    direction = [("NORTH","SOUTH"),("WEST","EAST"),("SOUTH","NORTH"),("EAST","WEST")]
    lst, length = a[:],len(a)-1# 避免更改输入;控制递归条件
   
    def re_del(lst,length):
      if length == len(lst):# 退出条件是列表长度递归前后无变化
            return lst
      else:
            length = len(lst)
            j=0
            while j < len(lst)-1:
                if (lst,lst) in direction:
                  lst.pop(j)# 删除会影响index
                  lst.pop(j)
                else:
                  j += 1
            return re_del(lst,length)
    return re_del(lst,length)

张大象 发表于 2017-9-28 17:08:59

本帖最后由 张大象 于 2017-9-28 17:19 编辑

Direct=['NORTH','WEST','EAST','SOUTH']
def dirReduc(L):
        for i in range(len(L)//2):
                for j in range(len(L)):
                        if j+1 == len(L):
                                break
                        else:
                                if Direct.index(L)+Direct.index(L) == 3:
                                        L.pop(j+1)
                                        L.pop(j)
                                        break
        return L
a = ["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"]
print(dirReduc(a))

shigure_takimi 发表于 2017-12-4 15:37:38

本帖最后由 shigure_takimi 于 2017-12-4 15:38 编辑

def hasOppositeDir(directions): # 任意两个相邻的相反的返回True
    oppositeDir = {'NORTH':'SOUTH', 'EAST':'WEST', 'SOUTH':'NORTH', 'WEST':'EAST'}
    length = len(directions)
    if length < 2:
      return False
    else:
      for index in range(length -1):
            if oppositeDir] == directions:
                return True
      return False
   
def getRealDir(directions):
    oppositeDir = {'NORTH':'SOUTH', 'EAST':'WEST', 'SOUTH':'NORTH', 'WEST':'EAST'}
    index = 0
    while hasOppositeDir(directions):
      if oppositeDir] == directions:
            directions.pop(index)
            directions.pop(index) # pop掉index的方向后,index+1的方向变为index,pop掉。
      else:
            index += 1
      if index > len(directions)-2:
            index = 0
    return directions


a = ["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"]
b = ["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH"]

print(getRealDir(a))
print(getRealDir(b))
   
##符合预期。
##['WEST']
##[]
##看了别人写的,发现自己的比较笨呢。

tsembrace 发表于 2018-4-26 14:53:58

'''
一个人获得了前往另一个地方的方向指示。指示是这样的,"NORTH", "SOUTH", "WEST", "EAST".
显然"NORTH", "SOUTH"是相反的,"WEST", "EAST"是相反的。连续的两个不同方向的走是无效的。
那么现在就请去除掉这些相对的方向,找出真正的方向吧。
示例:
dirReduc(["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"]) => ["WEST"]
dirReduc(["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH"]) => []
'''
def mystrip(ds):
    while '--' in ds:
      ds = ds.replace('--','-')
      
    return ds

def dirReduc(dl):
    GS = ['SOUTH-NORTH','NORTH-SOUTH','WEST-EAST','EAST-WEST']
    ds = '-'.join(dl)
    while GS in ds or GS in ds or GS in ds or GS in ds:
      if GS in ds:
            ds = ''.join(ds.split(GS))
            ds = mystrip(ds)
      if GS in ds:
            ds = ''.join(ds.split(GS))
            ds = mystrip(ds)
      if GS in ds:
            ds = ''.join(ds.split(GS))
            ds = mystrip(ds)
      if GS in ds:
            ds = ''.join(ds.split(GS))
            ds = mystrip(ds)
      dl = ds.strip('-').split('-')
    return dl
            
print(dirReduc(["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"]))

小强工作室 发表于 2018-7-23 18:38:29

理解了

咕咕鸡鸽鸽 发表于 2019-1-9 19:40:43

kankan

鱼小 发表于 2019-1-10 13:54:44

jluzhpanda 发表于 2019-8-24 11:33:11

lst_1 = ["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"]
lst_2 = ["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH"]
lst_3 = ["NORTH", "WEST", "SOUTH", "EAST"]
def way(lst):
    a = 0
    while a < len(lst):
      lst2 = []
      for i in range(1, len(lst)):
            # print(i)
            if lst == 'NORTH' and lst == 'SOUTH':
                lst, lst = 0, 0
            elif lst == 'SOUTH' and lst == 'NORTH':
                lst, lst = 0, 0
            elif lst == 'WEST' and lst == 'EAST':
                lst, lst = 0, 0
            elif lst == 'EAST' and lst == 'WEST':
                lst, lst = 0, 0
      for i in lst:
            if i != 0:
                lst2.append(i)
      a += 1
      lst = lst2
    return (lst)
print (way(lst_1))
print (way(lst_2))
print (way(lst_3))
'''
['WEST']
[]
['NORTH', 'WEST', 'SOUTH', 'EAST']
'''
页: [1] 2
查看完整版本: Python: 每日一题 51