JS 实现 python 中 Array.count
来源:互联网 发布:我知你非血肉之躯 编辑:程序博客网 时间:2024/06/04 17:44
来源
由codewar中一道题引发的惨案,题目原型如下:
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!).
解析后大意是:
每次给你包含’w’||’s’||’e’||’n’的非空数组(元素代表东西南北方向),判断 长度==10 && 方向向量==(0,0)
python
利用 python
的 Array.count
不知道有多好写哦2333:
def takeAWalk(walk): return len(walk)==10 && walk.count('n')==walk.count('s') && walk.count('w')==walk.count('e')
哈哈,是不是 Pythonic 大法打遍天下2333.
JS
javascript没有 Array.count 这种东西,但是我们可以通过 filter
伪造一个:
let takeAWalk = function(walk) { function count(val) { return walk.filter((item) => {item == val;}).lenth; } return walk.length==10 && count('n')==count('s') && count('w')==count('e')}
- JS 实现 python 中 Array.count
- js 中Array.concat()实现方法
- js中Array.sort()实现原理
- python中count()方法
- JS 中Array用法
- JS中从Array.slice()与Array.splice()的底层实现原理分析区别
- Informatica中实现count(distinct)
- JS 中Array中的Sort
- js中String转Array
- js中Array的使用
- JS中Array详细用法
- JS中数组Array方法
- js中array-like object
- js中数组定义Array
- 【python】用asq实现count(distinct cln)
- python中array的使用
- python Numpy中array详解
- [codility]Array-inversion-count
- AVL树
- ubuntu16.04安装ibus中文输入法
- SoapUI5.2.1之REST参数
- Struts2,Spring,Hibernate三大框架的整合(SSH)
- USACO-Section1.3 Wormholes [搜索]
- JS 实现 python 中 Array.count
- 算法导论——13.1红黑树的性质
- Understanding LSTM Networks
- 最小树形图
- Golang学习笔记 数据类型和变量
- 权限概念
- vue开发:vue,angular,react数据双向绑定原理分析
- 在字符串中找出第一个只出现一次的字符
- java动态编译实现