14
2
Your task is to write a program or function that determines whether a number is divisible by another. The catch is that it should give an answer as soon as possible, even if not all digits of the number have been given.
Your program should take an integer D ≥ 2 and then a series of digits as input. These represent the digits of another integer N ≥ 1, starting at the least significant digit. At the first point that N either must or must not be divisble by D, your program should output the appropriate answer and exit. If the end of the input is reached, it should output whether the full N is divisible by D.
Here is a list of acceptable input formats for N (leave a comment if you think something that isn't included should be allowed):
Standard input: digits are given on separate lines; end of input is EOF or a special value; exit means that the function returns or the program exits.
Analog input: through e.g. keystrokes or ten buttons representing each digit; end of input is a special value; exit means that the function returns or the program exits.
Function with global state: called repeatedly with successive digits; end of input is a special value; exit means that the function returns a non-null value. Note that if you use global state, it must be cleared after a value is returned or otherwise reset such that the function works multiple times.
Curried function: returns either another function to be called with the next digit or a value; end of input is a special value or calling the function with no argument; exit means that the function returns an answer rather than another function.
GUI prompt or similar: displayed repeatedly; end of input is "cancel" or equivalent, or a special value; exit means that prompts stop appearing.
Iterator function: input is a stateful object or function that returns the next digit when called, end of input is an exception or special value; exit means that the iterator stops being called.
Input for D and the output can be through any acceptable standard method.
Test cases:
2; 6 => true
5; 6 => false
20; 0 3 => false
20; 0 4 => true
100; 1 => false
100; 0 0 => true
100; 0 2 => false
4; 2 4 => false
4; 2 5 => true
4; 2 [eof] => false
4; 4 [eof] => true
625; 5 5 => false
625; 5 7 2 => false
625; 5 7 3 6 => false
625; 5 7 3 4 => true
7; 9 3 4 [eof] => false
7; 9 3 4 5 [eof] => true
140; 0 3 => false
140; 0 4 5 [eof] => false
140; 0 4 5 1 [eof] => true
14; 4 5 1 4 [eof] => false
14; 4 5 1 4 1 [eof] => true
I think we should assume that one digit will be given every time our solution asks for input, right? And, it should be a full program, since this is the objective way to ensure that input is given digit by digit, no? (The challenge says "program or function", hmm...) – Erik the Outgolfer – 2018-09-01T21:47:04.297
1@EriktheOutgolfer The input format is explained in detail in the bulleted list in the question. – Doorknob – 2018-09-01T21:50:55.623
1
I was just thinking about how objective can those formats be...I guess I'll just quit nitpicking and start actually solving this. :-)
– Erik the Outgolfer – 2018-09-01T21:52:40.640Hm, can I actually put the digits in STDIN without them being separated by linefeeds? "Standard input: digits are given on separate lines [...]" – Erik the Outgolfer – 2018-09-01T22:52:54.040
@EriktheOutgolfer STDIN is typically line-buffered; if you can read one character at a time, it's effectively equivalent to the second bullet point. – Doorknob – 2018-09-01T23:06:04.360
Well, almost, but EOT will have to be explicit after each digit (so that the terminal feeds the input to the program), that's why I didn't compare it to the second bullet point. – Erik the Outgolfer – 2018-09-02T00:45:07.080
1Is anything wrong with just taking a list as the
digits
input with a special value for EOF? – Jonathan Allan – 2018-09-02T09:48:55.740@JonathanAllan I'm pretty sure that, if you take a list, then you've already taken it all at once. – Erik the Outgolfer – 2018-09-02T11:33:54.943
1@EriktheOutgolfer not if there is an EOF value, unless I've misunderstood something. For example let's say the whole value is 132 and the potential divisor is 4 then
[]
and[2]
return anything other thanfalse
ortrue
(including the function itself etc...) while[2,3]
,[2,3,1]
, and[2,3,1,EOF]
returntrue
. It strikes me as close to the global state option. – Jonathan Allan – 2018-09-02T11:44:35.277Would a function taking an iterator be ok? – wastl – 2018-09-03T14:23:01.280
@JonathanAllan Being able to access all the input at the same time would eliminate the work involved in accumulating the input, and the idea is that the program takes input one digit at a time and stops as soon as possible (hence "impatient"). It shouldn't be possible to provide an input of
[2,3,1]
, as the program should have stopped after the3
digit. – Doorknob – 2018-09-04T12:26:38.000@wastl Yes, I've edited the post. – Doorknob – 2018-09-04T12:28:57.220
That was not the suggestion - the first call would be with
[]
, the second with[2]
, the third with[2, 3]
(at which pointtrue
would be returned), the enumeration of all possibilities was to show that the EOF would be given if we get to the whole number being given. – Jonathan Allan – 2018-09-04T12:58:19.923