首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我走了多少步?

我走了多少步?
EN

Code Golf用户
提问于 2016-03-08 18:57:41
回答 7查看 687关注 0票数 14

一个简单的计步器可以用一个摆来模拟,它的两侧有两个开关--一个在x=0,一个在x=l。当钟摆碰到远处的开关时,救护车可以被假定已经走了半步。当它接触到接近开关时,该步骤就完成了。

给定一个代表钟摆位置的整数列表,确定计步器上记录的完整步数。

输入

  • 整数l>0,轨道的长度。
  • 表示计步器钟摆位置的整数列表。

输出

所测量的完整步骤数。当摆接触远开关(x>=l)和近开关(x<=0)时,就采取了一个步骤。

测试用例

代码语言:javascript
复制
8, [8, 3, 0, 1, 0, 2, 2, 9, 4, 7]
1

钟摆立即与位于x=8 at t=0的远开关进行接触。然后,它触及t=2和t=4的近开关,完成了一步。在此之后,它再次触及x=9 at t=8的far开关,但它再也没有触及近开关。

代码语言:javascript
复制
1, [1, 5, -1, -4, -1, 1, -2, 8, 0, -4]
3

15, [10, -7, -13, 19, 0, 22, 8, 9, -6, 21, -14, 12, -5, -12, 5, -3, 5, -15, 0, 2, 11, -11, 12, 5, 16, 14, 27, -5, 13, 0, -7, -2, 11, -8, 27, 15, -10, -10, 4, 21, 29, 21, 2, 5, -7, 15, -7, -14, 13, 27]
7

7, [5, 4, 0]
0

7, [5, 8, 6, 1, 2] 
0   
EN

回答 7

Code Golf用户

发布于 2016-03-08 22:23:04

马蒂尔,22字节

代码语言:javascript
复制
>~2G0>~-XzY'nw1)0<-H/k

这使用了语言/编译器的现行版本(14.0.0)

输入与挑战的顺序和格式相同,由换行符分隔。

在网上试试!

解释

代码语言:javascript
复制
>~     % take the two inputs implicitly. Generate an array that contains true 
       % where the second input (array of positions) is >= the first input (l)
2G     % push second input (positions) again
0      % push a 0
>~     % true where the second input (array of positions) is <= 0
-      % subtract: array of 1, -1 or 0
Xz     % discard the 0 values
Y'     % run-length encoding: push values and number of repetitions
n      % length of the latter array: number of half-steps, perhaps plus 1
w1)    % swap. Get first element
0<     % true if that element is -1. We need to discard one half-step then
-      % subtract
H/k    % divide by 2 and round down
票数 2
EN

Code Golf用户

发布于 2016-03-08 21:23:06

Javascript ES6 57字节

代码语言:javascript
复制
(t,a)=>a.map(a=>a<t?a>0?'':0:1).join``.split`10`.length-1

谢谢@NotThatCharles为-4

票数 1
EN

Code Golf用户

发布于 2016-03-09 17:19:35

Pyth,18字节

代码语言:javascript
复制
/.:@J,Q0m@S+Jd1E2J

测试套件

解释:

代码语言:javascript
复制
/.:@J,Q0m@S+Jd1E2J
                      Implicit: Q is the length of the track.
    J,Q0              Set J to [Q, 0]
        m      E      Map over the list
           +Jd        Add the current element to J
          S           Sort
         @    1       Take the middle element.
                      This is the current element, 
                      clamped above by Q and below by 0.
   @J                 Filter for presence in J.
 .:             2     Form 2 element substrings
/                J    Count occurrences of J.
票数 1
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/75130

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档