我的个人博客

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

题目

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

Directions Reduction-方向减少相关推荐

  1. 华为总裁任正非谈企业管理:正确的方向来自于妥协

    一篇经典的管理文章. 转载一下.from:http://news.csdn.net/a/20100613/218802.html 清晰的方向来自灰度 一个领导人重要的素质是方向.节奏.他的水平就是合适 ...

  2. (转载)计算机视觉当中的专业英语

    转载自: 1.https://blog.csdn.net/vsooda/article/details/7365776 2.https://wenku.baidu.com/view/7cd9528aa ...

  3. LINGO使用指南(转载)

    LINGO是用来求解线性和非线性优化问题的简易工具.LINGO内置了一种建立最优化模型的语言,可以简便地表达大规模问题,利用LINGO高效的求解器可快速求解并分析结果. §1  LINGO快速入门 当 ...

  4. python数据分析课程哪个好-数据分析课程里面,python是重点!

    原标题:数据分析课程里面,python是重点! 数据分析要学习什么内容呢?科多大数据带你一起来看看. IPython IPython 是一个在多种编程语言之间进行交互计算的命令行 shell,最开始是 ...

  5. Numpy 学习看这一篇就够了(整理+理解+精简)

    文章目录 一.Numpy的介绍 1.NumPy简介 2.NumPy的优点 3.NumPy的缺陷 二.Numpy 安装 1.安装 三.Ndarry对象 1.构造函数 np.array (默认深拷贝) n ...

  6. 【论文阅读】7-Discovering Structural Regularity in 3D Geometry

    [论文阅读]7-Discovering Structural Regularity in 3D Geometry 1.Input: 2.Processing pipeline 2.1.Transfor ...

  7. 3DSMAX中英文对照大全(从A-Z分类)

    A Absolute Mode Transform Type-in绝对坐标方式变换输入 Absolute/Relative Snap Toggle Mode绝对/相对捕捉开关模式 ACIS Optio ...

  8. 东北大学 人机交互 复习笔记

    呕心沥血2W字皇家贵族尊享版顶级人机交互课堂笔记知识点总结 --- by DICO --- WORD文件下载地址:https://download.csdn.net/download/DicoY/18 ...

  9. 网络编程 TCP电子网络词库

    电子词典: 要求: 登录注册功能,不能重复登录,重复注册.用户信息也存储在数据库中. 单词查询功能 历史记录功能,存储单词,意思,以及查询时间,存储在数据库 基于TCP,支持多客户端连接 采用数据库保 ...

  10. 2011大学英语四级核心高频词汇表免费下载

    2011年大学英语四级核心高频词汇表 abandon vt.丢弃:放弃,抛弃 ability n.能力:能耐,本领 abnormal a.不正常的:变态的 aboard ad.在船(车)上:上船 ab ...

最新文章

  1. java源码-AQS机制
  2. Oracle数据库常用技术
  3. Dancing_Links总结 【by AbandonZHANG】
  4. 从客户的角度看网站涉及的第一要素
  5. python编程制作接金币游戏_一个简单的pygame接金币游戏
  6. android涂鸦板保存功能,android实现涂鸦,保存涂鸦后的图片,清屏
  7. python 图像分析 边框_Python 去除图片纯色边框(qbit)
  8. 去重复算法(C语言)
  9. Python画图显示中文
  10. php中将图片裁剪为圆形
  11. 【CVPR2022】论文列表与下载——PartThree
  12. 在activity之间传递数据
  13. 通过aosp-latest下载Android源码
  14. 黑马程序员前端JavaScript高级——ES6学习笔记
  15. js怎么显示服务器图片,原生JS上传图片接收服务器端图片并且显示图片(主要描述blob类型)...
  16. 到站提醒APP应用 隐私声明
  17. 市场调研很难做?这些软件帮你理清思绪
  18. ae绘图未指定错误怎么办_设计高手总结47个快捷键50个CAD使用技巧,助你神速绘图拒绝加班!...
  19. Qt中对字符串长度转换成像素长度
  20. 国内即时通讯工具介绍

热门文章

  1. 百度小程序html转码,百度小程序转换工具
  2. 微软造的还原精灵EWF(Enhanced Write Filter)
  3. java gsoap mtom_mtom_xop 通过gsoap实现高效MTOM协议的数据传输 联合开发网 - pudn.com
  4. 什么样的运动耳机比较好、跑步运动耳机推荐
  5. 【云原生 | 21】Docker运行Web服务实战之Apache
  6. 购物系统 java代码_java购物系统源代码
  7. 大数据数据库:MPP vs MapReduce
  8. 异形图片自动排版之装箱算法
  9. 自己搭建服务器提供IOS IPA包下载
  10. 一起学爬虫(Python) — 09