Directions Reduction-方向减少

我的个人博客

更多内容,请跳转我的个人博客

题目

Directions Reduction

方向减少

描述

Once upon a time, on a way through the old wild mountainous west,…
… a man was given directions to go from one point to another. The directions were “NORTH”, “SOUTH”, “WEST”, “EAST”. Clearly “NORTH” and “SOUTH” are opposite, “WEST” and “EAST” too.

Going to one direction and coming back the opposite direction right away is a needless effort. Since this is the wild west, with dreadfull weather and not much water, it’s important to save yourself some energy, otherwise you might die of thirst!

How I crossed a mountainous desert the smart way.
The directions given to the man are, for example, the following (depending on the language):

["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"].
or
{ "NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST" };
or
[North, South, South, East, West, North, West]

You can immediatly see that going “NORTH” and immediately “SOUTH” is not reasonable, better stay to the same place! So the task is to give to the man a simplified version of the plan. A better plan in this case is simply:

["WEST"]
or
{ "WEST" }
or
[West]

Other examples:

In [“NORTH”, “SOUTH”, “EAST”, “WEST”], the direction “NORTH” + “SOUTH” is going north and coming back right away.

The path becomes [“EAST”, “WEST”], now “EAST” and “WEST” annihilate each other, therefore, the final result is [] (nil in Clojure).

In [“NORTH”, “EAST”, “WEST”, “SOUTH”, “WEST”, “WEST”], “NORTH” and “SOUTH” are not directly opposite but they become directly opposite after the reduction of “EAST” and “WEST” so the whole path is reducible to [“WEST”, “WEST”].

Task
Write a function dirReduc which will take an array of strings and returns an array of strings with the needless directions removed (W<->E or S<->N side by side).

The Haskell version takes a list of directions with data Direction = North | East | West | South.
The Clojure version returns nil when the path is reduced to nothing.
The Rust version takes a slice of enum Direction {North, East, West, South}.
See more examples in “Sample Tests:”
Notes
Not all paths can be made simpler. The path [“NORTH”, “WEST”, “SOUTH”, “EAST”] is not reducible. “NORTH” and “WEST”, “WEST” and “SOUTH”, “SOUTH” and “EAST” are not directly opposite of each other and can’t become such. Hence the result path is itself : [“NORTH”, “WEST”, “SOUTH”, “EAST”].

很久以前,在穿越古老的西部山区的路上,…
…一个人被告知要从一个点走到另一个点。方向是 “北”、“南”、“西”、“东”。显然,"北 "和 "南 "是相反的,"西 "和 "东 "也是。

去到一个方向,马上又回到相反的方向,这是一种不必要的努力。由于这里是荒凉的西部,天气恶劣,水也不多,所以必须为自己节省一些体力,否则你可能会渴死!

我是如何用聪明的方法穿越山区沙漠的。
给这个人的指示是,例如,以下内容(取决于语言)。

["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"].
or
{ "NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST" };
or
[North, South, South, East, West, North, West]

你可以立即看到,去 "北 "和立即去 "南 "是不合理的,最好是呆在同一个地方! 所以我们的任务是给这个人一个简化版的计划。在这种情况下,一个更好的计划是简单的。

["WEST"]
或
{ "WEST" }
或
["WEST"]

其他的例子。
在[“NORTH”, “SOUTH”, “EAST”, “WEST”]中,"NORTH "+"SOUTH "的方向是向北走,马上就回来了。

路径变成了[“EAST”, “WEST”],现在 "EAST "和 "WEST "相互湮灭,因此,最后的结果是[](在Clojure中为零)。

在[“NORTH”, “EAST”, “WEST”, “SOUTH”, “WEST”, “WEST”]中,"NORTH "和 "SOUTH "不是直接相对的,但在减少了 "EAST "和 "WEST "之后,它们变成了直接相对的,所以整个路径可以还原为[“WEST”, “WEST”]。

任务
编写一个函数dirReduc,它将接收一个字符串数组,并返回一个去除不必要方向的字符串数组(W<->E或S<->N并列)。

Haskell版本接收一个方向列表,数据为Direction = North | East | West | South。

Clojure版本在路径被简化为空时返回nil。

Rust版本需要一个enum Direction {North, East, West, South}的片断。
更多的例子见 “样本测试:”。

注意事项

并非所有的路径都可以变得更简单。路径[“NORTH”, “WEST”, “SOUTH”, “EAST”]是不能被简化的。"北 "和 “西”,"西 "和 “南”,"南 "和 "东 "不是直接相对的,不能成为直接相对的。因此,结果路径本身就是:[“NORTH”, “WEST”, “SOUTH”, “EAST”] 。

思路

这个题描述看起来挺多的,其实很简单,类似之前做的步行十分钟这个题,但是两个题的区别是,步行十分钟只看最终的结果,不管对立方向是否在一起都可以抵消,但是本题就不一样,对立方向必须是相邻的才可以抵消或者变相相邻也可以。

全部代码

def dirReduc(arr):
    # 创建一个列表,用来模拟栈
    result = []
    # 遍历
    for val in arr:
        if result == []:
            result.append(val)
        else:
            # "NORTH"<->"SOUTH" "EAST" <->"WEST"
            if (val == "NORTH"  and result[-1] == "SOUTH") or\
            (val == "SOUTH"  and result[-1] == "NORTH") or\
                (val == "WEST"  and result[-1] == "EAST") or\
                    (val == "EAST"  and result[-1] == "WEST"):
                # 出栈
                result.pop()
            else:
                # 入栈
                result.append(val)
    return result
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值