codewars--js--ten minutes walk
题目:
You live in the city of Cartesia where all roads are laid out in a perfect grid. You arrived ten minutes too early to an appointment, so you decided to take the opportunity to go for a short walk. The city provides its citizens with a Walk Generating App on their phones -- everytime you press the button it sends you an array of one-letter strings representing directions to walk (eg. ['n', 's', 'w', 'e']). You know it takes you one minute to traverse one city block, so create a function that will return true if the walk the app gives you will take you exactly ten minutes (you don't want to be early or late!) and will, of course, return you to your starting point. Return false otherwise.
Note: you will always receive a valid array containing a random assortment of direction letters ('n', 's', 'e', or 'w' only). It will never give you an empty array (that's not a walk, that's standing still!).
我的答案:
- function isValidWalk(walk) {
- //insert brilliant code here
- if(walk.length!=10){ return false;}
- var x=0,y=0;
- for(var i=0;i<walk.length;i++){
- switch(walk[i]){
- case "n":y++;break;
- case "s":y--;break;
- case "w":x++;break;
- case "e":x--;break;
- }
- }
- return x==0 && y==0;
- }
优秀答案:
- function isValidWalk(walk) {
- function count(val) {
- return walk.filter(function(a){return a==val;}).length;
- }
- return walk.length==10 && count('n')==count('s') && count('w')==count('e');
- }
- function isValidWalk(walk) {
- var dx = 0
- var dy = 0
- var dt = walk.length
- for (var i = 0; i < walk.length; i++) {
- switch (walk[i]) {
- case 'n': dy--; break
- case 's': dy++; break
- case 'w': dx--; break
- case 'e': dx++; break
- }
- }
- return dt === 10 && dx === 0 && dy === 0
- }
codewars--js--ten minutes walk的更多相关文章
- [CodeWars][JS]实现链式加法
在知乎上看到这样一个问题:http://www.zhihu.com/question/31805304; 简单地说就是实现这样一个add函数: add(x1)(x2)(x3)...(xn) == x1 ...
- [CodeWars][JS]实现大整数加法
问题描述 实现‘字符串加法’,即将两个以字符串形式表示的数字相加,得到结果然后返回一个新的字符串. 例如:输入‘123’,‘321’,返回‘444’. 这样在进行两个任意大的整数相加的时候,既不会溢出 ...
- [CodeWars][JS]如何判断给定的数字是否整数
问题描述: We are asking for a function to take a positive integer value, and return a list of all positi ...
- English words
英语指路常用单词 the one-way street单行道traffic light红绿灯 fork road三叉路口intersection/crossroad 十字路口T road 丁字路口in ...
- 【oneday_onepage】——Ten Changes To Make A Difference In Your Life
When you want to change something in your life, it can feel overwhelming. Whether it’s losing 50lbs ...
- 当Node.js遇见Docker
Node.js Best Practices - How to Become a Better Developer in 2017提到的几点,我们Fundebug深有同感: 使用ES6 使用Promi ...
- Chapter 6 — Improving ASP.NET Performance
https://msdn.microsoft.com/en-us/library/ff647787.aspx Retired Content This content is outdated and ...
- Build 2017 Revisited: .NET, XAML, Visual Studio
For the next couple months we're going to revisit Build 2017, each post focusing on different aspect ...
- 越狱Season 1- Episode 22: Flight
Season 1, Episode 22: Flight -Franklin: You know you got a couple of foxes in your henhouse, right? ...
随机推荐
- Django 数据库连接缓存的坑
https://www.cnblogs.com/xcsg/p/11446990.html
- python IO非阻塞模型
server端 import socket sk = socket.socket() sk.bind(('127.0.0.1', 8010)) sk.setblocking(False) # sk.l ...
- linux 安装virtualbox5.2
一.安装 1.下载package https://www.virtualbox.org/wiki/Linux_Downloads 2.添加源. $ cat /etc/lsb-release DISTR ...
- 2019杭电多校赛第九场 Rikka with Mista
Problem Description Rikka is a fervent fan of JoJo's Bizarre Adventure. As the last episode of Golde ...
- quick-cocos2d-x项目《狂点小怪兽》总结
最近找了个公司实习,领导让我把公司的<狂点小怪兽>C++游戏用Lua重写.大概用了一个星期完成. 第一次使用quick-cocos2d-x,磕磕碰碰的也算是走了一遍流程. 1. quick ...
- 【转】在VS2010上使用C#调用非托管C++生成的DLL文件(图文讲解)
原文:http://www.cyqdata.com/cnblogs/article-detail-35876# 背景 在项目过程中,有时候你需要调用非C#编写的DLL文件,尤其在使用一些第三方通讯组件 ...
- 有关PyCharm安装库失败的问题的解决方法
最近因为要使用Python的缘故,安装了python IDE+pycharm,如此安装的原因是因为Pycharn 并不像anaconda一样拥有相对完整的依赖库和开发包,可以直接运行python,但因 ...
- UML--->用例图梳理
用例图梳理 概述 用例图主要用来描述"用户.需求.系统功能单元"之间的关系.它展示了一个外部用户能够观察到的系统功能模型图. 用例图的主要目的是帮助开发团队以一种可视化的方式理解系 ...
- mysql--->innodb引擎什么时候表锁什么时候行锁?
mysql innodb引擎什么时候表锁什么时候行锁? InnoDB基于索引的行锁 InnoDB行锁是通过索引上的索引项来实现的,这一点MySQL与Oracle不同,后者是通过在数据中对相应数据行加锁 ...
- SpringCloud与微服务Ⅹ --- SpringCloud Config分布式配置中心
一.SpringCloud Config是什么 分布式系统面临的问题 --- 配置问题 微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务.由于每个 ...