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? ...
随机推荐
- 从零开始ming的多人联机游戏(3)为socket通讯添加mysql数据库
macOS下visual studio C#加载mySql 本文在上一节的基础上,添加了mysql数据库的功能.client发送信息给服务器后,服务器将收到的消息保存在数据库中. 如果client发送 ...
- HGE引擎改进
基于HGEDX9版本修改. hge库: 1.全UNICODE化 2.增加时间模块:Timer_StartTick(),Timer_NowTick()等六个函数 3.增加服从正态分布的随机数生成函数:R ...
- [校内训练20_01_20]ABC
1.问有多少个大小为N的无标号无根树,直径恰好为L.$N,L \leq 200$ 2.问一个竞赛图中有多少个长度为3.4.5的环.$N \leq 2000$ 3.给出一些直线和单个点A,问这些直线的交 ...
- 【Nginx入门系列】第一章 手把手带你搭建Nginx服务器
1 nginx安装环境 nginx是C语言开发,建议在linux上运行,本教程使用Centos6.5作为安装环境,搭建前请先按如下语句配置好环境. GCC 安装nginx需要先将官网下载的源码进行编译 ...
- 深入源码解析spring aop实现的三个过程
Spring AOP的面向切面编程,是面向对象编程的一种补充,用于处理系统中分布的各个模块的横切关注点,比如说事务管理.日志.缓存等.它是使用动态代理实现的,在内存中临时为方法生成一个AOP对象,这个 ...
- 【WPF学习】第三十四章 资源基础
WPF允许在代码中以及在标记中的各个位置定义资源(和特定的控件.窗口一起定义,或在整个应用程序中定义). 资源具有许多重要的优点,如下所述: 高效.可以通过资源定义对象,并在标记中的多个地方使用.这会 ...
- python学习Day06--编码
[主要内容] 1. is 和 == 区别 id()函数 == 判断两边的值 is 判断内存地址回顾编码: 1. ASCII: 英文, 特殊字符, 数字, 8bit, 1byte 2. GBK: 中文 ...
- 基于 H5 和 webGL 的 3d 智慧城市
前言 中共中央.国务院在今年12月印发了<长江三角洲区域一体化发展规划纲要>(下文简称<纲要>),并发出通知,要求各地区各部门结合实际认真贯彻落实. <纲要>强调, ...
- HDU 5558 Alice's Classified Message(后缀数组+二分+rmq(+线段树?))
题意 大概就是给你一个串,对于每个\(i\),在\([1,i-1]\)中找到一个\(j\),使得\(lcp(i,j)\)最长,若有多个最大\(j\)选最小,求\(j\)和这个\(lcp\)长度 思路 ...
- ELK搭建(docker环境)
ELK是Elasticsearch.Logstash.Kibana的简称,这三者是核心套件,但并非全部. Elasticsearch是实时全文搜索和分析引擎,提供搜集.分析.存储数据三大功能:是一套开 ...