14
0
Recently, my reputation was 25,121
. I noticed that each digit grouping (i.e. the numbers separated by commas) was a perfect square.
Your challenge is, given a non-negative integer N and a unary boolean Black Box Function f : Z* → B , yield a truthy value if each value of f applied to the digit groupings of N is truthy, and falsey otherwise.
One can find the digit groupings by splitting the number into groups of 3, starting from the right side. The leftmost group may have 1, 2, or 3 digits. Some examples:
12398123 -> 12,398,123 (3 digit groupings)
10 -> 10 (1 digit grouping)
23045 -> 23,045 (2 digit groupings)
100000001 -> 100,000,001 (3 digit groupings)
1337 -> 1,337 (2 digit groupings)
0 -> 0 (1 digit grouping)
Additional rules
- This function can map to either booleans (e.g.
true
andfalse
),1
s and0
s, or any truthy/falsey value. Please specify which format(s) are supported by your answer. - You may take an integer as input, or an integer string (i.e. a string composed of digits).
- You may write a program or a function.
- When passing the digital groups to the function f, you should trim all unnecessary leading zeroes. E.g., f, when applied to N = 123,000 should be executed as f(123) and f(0).
Test cases
Function notation is n -> f(n)
, e.g., n -> n == 0
. All operators assume integer arithmetic. (E.g., sqrt(3) == 1
)
function f
integer N
boolean result
n -> n == n
1230192
true
n -> n != n
42
false
n -> n > 400
420000
false
n -> n > 0
0
false
n -> n -> 0
1
true
n -> sqrt(n) ** 2 == n
25121
true
n -> sqrt(n) ** 2 == n
4101
false
n -> mod(n, 2) == 0
2902414
true
n -> n % 10 > max(digits(n / 10))
10239120
false
n -> n % 10 > max(digits(n / 10))
123456789
true
If we are unable to take functions as arguments, are we allowed to assume that the function is defined as a variable and we reference that in our program? – caird coinheringaahing – 2018-03-05T19:19:47.673
@cairdcoinheringaahing Please read the reference for Black box functions, specifically the references at the end of that post. To summarize, yes, you can, even if your language is capable of taking functions as arguments (afaict)
– Conor O'Brien – 2018-03-05T19:23:42.200Can the input be negative? Zero? You talk about integers, but all the examples are positive. I'd also suggest including test cases where a grouping of 000 needs to be handled. – xnor – 2018-03-05T21:57:34.470
@xnor Good point, I'll do that – Conor O'Brien – 2018-03-05T22:00:22.393
Can the input number be just
0
? – Asone Tuhid – 2018-03-06T19:51:14.967@AsoneTuhid Yes, as you are "given a non-negative integer N", which includes
0
. – Conor O'Brien – 2018-03-06T23:42:46.9271@ConorO'Brien In that case you should add (
n -> n > 0
applied to0
) to the test cases because most answers fail on it. – Asone Tuhid – 2018-03-07T08:53:45.947Are the digit groups of
0
[0]
or[]
? – Erik the Outgolfer – 2018-03-07T13:48:37.9071@EriktheOutgolfer They are
[0]
. – Conor O'Brien – 2018-03-07T14:35:27.897