Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Check membership of an infinite list

As input you will receive

  • An integer \$a\$

  • A list of integers that is infinite and strictly-monotonic1.

Your program should check in finite time if \$a\$ appears the list.

You should output one of two distinct values. One if \$a\$ appears in the list and the other if \$a\$ does not.

This is so answers will be scored by their length in bytes with fewer bytes being better.


You may take an infinite lists in any of the following formats:

  • A list, stream, iterator or generator if your language allows them to be infinite.

  • A function or pointer to a function that outputs the next value when queried with no input.

  • A function or pointer to a function that outputs the \$n\$th value when passed \$n\$ as an input.

Additionally you may repeatedly query STDIN with the assumption that each query will provide the next term in the sequence.


Test cases

Since I cannot put infinite lists in the body of a challenge I will provide the first couple terms along with a description of the list and a definition in Haskell.

6
1 2 3 4 5 6 7 8 9 10 ... (positive integers) l=[1..]
 =>
True

6
-1 -2 -3 -4 -5 -6 -7 -8 -9 -10 ... (negative integers) l=[-1,-2..]
 =>
False

109
0 2 4 6 8 10 12 14 16 18 20 ... (non-negative even integers) l=[0,2..]
 =>
False

-5
200 199 198 197 196 195 194 193 192 ... (integers smaller than 201) l=[200,199..]
 =>
True

256
1 2 3 5 8 13 21 34 55 89 144 ... (unique Fibonacci numbers) l=1:2:zipWith(+)l(tail l)
 =>
False

1
1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 ... (integers less than 2) l=[1,0..]
 =>
True

1: A strictly monotonic sequence is either entirely increasing or entirely decreasing. This means if you take the differences between consecutive elements they will all have the same sign.

Answer*

Draft saved
Draft discarded
Cancel
1
  • 1
    \$\begingroup\$ I don't think this works for decreasing input sequences: Try it online! \$\endgroup\$ Commented Apr 3, 2020 at 22:41

AltStyle によって変換されたページ (->オリジナル) /