何歩歩きましたか?


14

単純な歩数計は、反対のX = 0における側面オンおよびX = 1つの上の2つのスイッチと振り子によってモデル化することができるL。振り子がファースイッチに接触すると、歩行器は半分のステップを踏んだと見なすことができます。近くのスイッチに接触すると、手順は完了です。

振り子の位置を表す整数のリストが与えられたら、歩数計に記録された完全な歩数を決定します。

入力

  • 整数l > 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で近くのスイッチに触れて、1つのステップを完了します。その後、x = 9でt = 8に再びファースイッチに触れますが、ニアスイッチには再び触れません。

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が追加されました。
リルトシアスト

ステップは常に次のようになります:遠端で接触してから近端で接触する つまり、近端、遠端は完全なステップではありません。したがって、入力で8, [0 8 0 8]1なくを与える必要があり2ます。私は正しいですか?
ルイスメンドー

@DonMuesliはい。
リルトシアスト

回答:


4

CJam、27 24バイト

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

入力形式は、振り子の位置のリストとそれに続く l 1行です。

ここでテストしてください。

説明

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

Perl、28バイト

+1を含む -p

STDINで入力をスペースで区切られた1行の整数として入力して実行します。最初の数字は長さです。

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

ルビー、42

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

rとして始まりますfalserトラックの両端で切り替えて、カウントに追加します。次に、カウントを半分にし(切り捨て)、ステップ数を取得します。


0

網膜、34

-1*

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

オンラインでお試しください!または、10進入力で試してください

単項入力を受け取り、負の単項数はとして扱われ-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"がカウントされます。


弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.