我走了几步?


14

一个简单的计步器可以用一个摆锤来建模,该摆锤在相对的两侧具有两个开关-一个在x = 0处,一个在x = 1处。当摆锤接触到远处的开关时,可以认为步行者走了半步。当它接触附近的开关时,该步骤完成。

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

输入值

  • 的整数 > 0,轨道的长度。

  • 代表每次计步器摆的位置的整数列表。

输出量

测量的完整步骤数。当摆锤接触远开关(x> = l),然后接触近开关(x <= 0)时,将采取步骤。

测试用例

8, [8, 3, 0, 1, 0, 2, 2, 9, 4, 7]
1

摆在t = 0时立即在x = 8处与远端开关接触。然后,它在t = 2和t = 4处触摸接近开关,完成一个步骤。此后,它在t = 8时再次触及x = 9的远处开关,但再也没有触及近处的开关。

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   

1
7, [5, 4, 0]呢 是0还是1?也就是说-您是否认为挥杆总是“全长”?还是7, [5, 8, 6, 1, 2]?是0还是1?
并非查尔斯

1
@NotthatCharles添加了。
lirtosiast

我认为总是有一个步骤:在远端接触,然后在远端接触。那就是:近端,那么远端还不是完整的一步。所以输入8, [0 8 0 8]应该给1,而不是2。我对么?
路易斯·门多

@DonMuesli是的。
lirtosiast '16

Answers:


4

CJam,27个 24字节

l~:Xfe<0fe>_[XT]:Y--Y/,(

输入格式是摆位置的列表,后跟l一行。

在这里测试。

说明

l~     e# Read and evaluate input.
:X     e# Store track length in X.
fe<    e# Clamp each position to X from above.
0f>    e# Clamp each position to 0 from below.
_      e# Duplicate.
[XT]   e# Push the array [X 0].
:Y     e# Store it in Y.
-      e# Set subtraction from the clamped input list. This gives all the
       e# intermediate values.
-      e# Another set subtraction. Remove intermediate values from input list.
Y/     e# Split remaining list around occurrences of ...X 0...
,(     e# Count them and decrement. This is the number of times the pendulum
       e# moved from X to 0.

2

MATL,22字节

>~2G0>~-XzY'nw1)0<-H/k

这使用语言/编译器的当前版本(14.0.0)

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

在线尝试!

说明

>~     % 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

1

Javascript ES6 57字节

(t,a)=>a.map(a=>a<t?a>0?'':0:1).join``.split`10`.length-1

感谢@NotThatCharles -4


1
为什么不分开/10/
并非查尔斯

@NotthatCharles我非常确定那没有用,只是尝试了一下,感觉很棒!-谢谢
Charlie Wynn

4
一个查尔斯到另一个;)
不是查尔斯

1

Perl,28个字节

包括+1的 -p

将输入作为STDIN上用空格分隔的整数的长行运行,第一个数字是长度:

perl -p steps.pl <<< "8 8 3 0 1 0 2 2 9 4 7"

steps.pl

s; ;$'>=$_..$'<1;eg;$_=y;E;

使用perl触发器运算符并计算返回到false的次数


1

Pyth,18个字节

/.:@J,Q0m@S+Jd1E2J

测试套件

说明:

/.:@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.

0

Ruby,42岁

->i,a{r=!0
a.count{|v|r^r=v>=i||r&&v>0}/2}

r从开始false。我们r在曲目的每个末端进行切换,并将其添加到我们的计数中。然后,将计数减半(四舍五入)以获取步数。


0

视网膜,34

-1*

^(1*)((?>.*?\1.*? \D))*.*
$#2

在线尝试!尝试使用十进制输入

以一元形式输入,负一元数被视为-111...,零为空字符串。计算第一个数字出现后跟零的次数。使用原子组来确保匹配最小(不幸的是原子组无法捕获,因此必须将其包装在另一个组中...)。


0

Python 3、82

DSM节省了2个字节。

还不是超级高尔夫。

def f(x,s):
 c=l=0
 for a in s:
  if a>=x:l=x
  elif a<1:c+=l==x;l*=l!=x
 return c

测试用例:

assert f(8, [8, 3, 0, 1, 0, 2, 2, 9, 4, 7]) == 1
assert f(1, [1, 5, -1, -4, -1, 1, -2, 8, 0, -4]) == 3
assert f(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

0

Clojure,64字节

#(count(re-seq #"10"(apply str(for[i %2](condp > i 1 0 %""1)))))

将小于或等于零的值映射0为长度大于或等于长度的值,将1其他映射为空字符串""。然后将其连接到字符串,并"10"计算的出现次数。


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.