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? ...
随机推荐
- 异数OS谈发展国产操作系统的问题
异数OS谈发展国产操作系统的问题 为什么写本文 最近中兴被美制裁的问题以及红芯使用开源技术宣称国产自主技术引发了舆论不少对国产CPU以及国产操作系统自主技术的讨论,为什么我们国家有BAT,有原子弹,能 ...
- 使用eNSP配置QinQ
参考链接:https://blog.csdn.net/alone_map/article/details/52217094 本文主要记录使用华为eNSP模拟器来实现配置QinQ,并对QinQ的报文进行 ...
- python实现数据结构-队列
注:本文档主要是学习<Python核心编程(第二版)>时的练习题. 队列是一种"先进先出"的数据结构(FIFO),是一种操作受限的线性结构,先进队列的成员先出队列.示意 ...
- 超越队西柚考勤系统——beta冲刺1
这个作业属于哪个课程 http://edu.cnblogs.com/campus/xnsy/GeographicInformationScience 这个作业的要求在哪里 https://www.cn ...
- CF990G
题意 https://codeforces.com/contest/990/problem/G 思考 在200000以内,因数个数最多的数位166320,共有160个因数.可以知道,从一个节点向下走最 ...
- 【模板】普通平衡树(权值splay)
安利splay讲解: [洛谷日报第62期]Splay简易教程 [模板]普通平衡树(luogu) Description 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下 ...
- 老哈佛H3的空调控制器的维修记录
这部哈弗车是2005年出厂的,应该差不多是第一批了吧,小毛病很多了.夏天到了,空调也不能启动,灯不亮,按键没反应令我很是着急.于是开始研究. 这款空调的控制器在cd机中控下面,需要拆中控取出.是一个黑 ...
- 2020年大厂Java面试题(基础+框架+系统架构+分布式+实战)
前言 作为一个Java开发者,Java架构师应该是大家的一个职业目标了吧. 要成为Java架构师,首先你要是一个高级Java工程师,熟练使用各种框架,并知道它们实现的原理.jvm虚拟机原理.调优,懂得 ...
- 图像GIST特征和LMGIST包的python实现(有github)
1什么是Gist特征 (1) 一种宏观意义的场景特征描述 (2) 只识别"大街上有一些行人"这个场景,无需知道图像中在那些位置有多少人,或者有其他什么对 ...
- 实验15: STP
实验12: STP Ø 实验目的通过本实验,读者可以掌握如下技能:(1) 理解STP 的工作原理(2) 掌握STP 树的控制(3) 利用PVST 进行负载平衡 Ø 实验拓扑 SW1 和S ...