18
\$\begingroup\$

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 and false), 1s and 0s, 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
\$\endgroup\$
9
  • \$\begingroup\$ 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? \$\endgroup\$ Mar 5, 2018 at 19:19
  • \$\begingroup\$ @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) \$\endgroup\$ Mar 5, 2018 at 19:23
  • \$\begingroup\$ Can 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. \$\endgroup\$
    – xnor
    Mar 5, 2018 at 21:57
  • 1
    \$\begingroup\$ @ConorO'Brien In that case you should add (n -> n > 0 applied to 0) to the test cases because most answers fail on it. \$\endgroup\$ Mar 7, 2018 at 8:53
  • 1
    \$\begingroup\$ @EriktheOutgolfer They are [0]. \$\endgroup\$ Mar 7, 2018 at 14:35

20 Answers 20

7
\$\begingroup\$

Brachylog, 8 bytes

ḃ₁₀₀₀↰₁ᵐ

Try it online!

The blackbox function goes on the second line (or the "Footer" on TIO) and the integer is read from STDIN. Prints true. or false. accordingly.

ḃ₁₀₀₀      Compute the base-1000 digits of the input.
     ↰₁ᵐ   Map the blackbox predicate over each digit. We don't care about the
           result of the map, but the predicate must succeed for each digit,
           otherwise the entire map fails.
\$\endgroup\$
5
\$\begingroup\$

APL (Dyalog), 16 13 bytes

3 bytes saved thanks to @Adám

∧/⎕¨1e3⊥⍣¯1⊢⎕

Try it online!

How?

1e3⊥⍣¯1⊢⎕ - input the number and encode in base 1000

⎕¨ - input the function and apply on each

∧/ - reduce with logical and

\$\endgroup\$
3
  • \$\begingroup\$ Save 3 bytes by going explicit: Try it online! \$\endgroup\$
    – Adám
    Mar 6, 2018 at 11:33
  • \$\begingroup\$ @Adám thanks! I liked the alpha-alpha though... \$\endgroup\$
    – Uriel
    Mar 6, 2018 at 13:33
  • \$\begingroup\$ correct me if i'm wrong but i think this is a failure \$\endgroup\$ Mar 7, 2018 at 8:45
4
\$\begingroup\$

Jelly, 5 bytes

bȷÇ€Ạ

Try it online!

The command-line argument is the number. The line above the line this function resides in is the main line of the rest of the program, that is, the code that gets called for each of the groups. Be careful not to refer to the line bȷÇ€Ạ is in! The example used here is the 5th test case.

\$\endgroup\$
3
  • \$\begingroup\$ I'm pretty sure that this is a failure when applying n -> n != 0 to 0 \$\endgroup\$ Mar 7, 2018 at 9:01
  • \$\begingroup\$ @AsoneTuhid It's not; The number is 0, and its digit group list is [0], so then gets mapped to each element (the single 0 here), turning the list into [1] and, since all of the elements of this list are truthy, 1 is returned. Note that if I had the digit group list be [] instead the result wouldn't change, as all of the elements of [] are truthy (vacuous truth). However, the result can be different for different programs, and the rules aren't exactly clear on this (asked OP). \$\endgroup\$ Mar 7, 2018 at 13:45
  • \$\begingroup\$ Sorry then, I barely understand Jelly. Nice solution. \$\endgroup\$ Mar 7, 2018 at 13:47
4
\$\begingroup\$

Python 2, 46 bytes

g=lambda f,n,k=1000:f(n%k)and(n<k or g(f,n/k))

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ 44 bytes by using * in place of and. \$\endgroup\$
    – Bubbler
    Mar 14, 2018 at 7:17
4
\$\begingroup\$

Haskell, 42 40 38 bytes

f#n=f(mod n 1000)&&(n<1||f#div n 1000)

The blackbox function must return True or False.

Try it online!

Edit: -2 -4 bytes thanks to @ovs.

\$\endgroup\$
1
4
\$\begingroup\$

C (gcc), 66 58 48 bytes

-10 bytes thanks to @Neil!

t=1000;g(f,i)int f();{i=f(i%t)&(i<t||g(f,i/t));}

Try it online!

\$\endgroup\$
3
3
\$\begingroup\$

Wolfram Language (Mathematica), 30 bytes

And@@#/@#2~IntegerDigits~1000&

Try it online!

\$\endgroup\$
3
\$\begingroup\$

JavaScript (ES6), 40 36 bytes

f=>g=i=>f(i%1e3)&(i<1e3||g(i/1e3|0))

Takes the function and value by currying and returns 0 or 1. Edit: Saved 4 bytes thanks to @Shaggy.

\$\endgroup\$
4
  • \$\begingroup\$ 1000 -> 1e3 to save a couple of bytes. And could you replace && with & for another byte? \$\endgroup\$
    – Shaggy
    Mar 6, 2018 at 15:32
  • \$\begingroup\$ @Shaggy Yes, I think that's safe enough. Same goes for betseg's answer? \$\endgroup\$
    – Neil
    Mar 6, 2018 at 16:29
  • \$\begingroup\$ fails for function_name(n=>n>0)(0) (returns true) \$\endgroup\$ Mar 7, 2018 at 8:38
  • \$\begingroup\$ @AsoneTuhid Thanks, fixed. \$\endgroup\$
    – Neil
    Mar 7, 2018 at 10:43
2
\$\begingroup\$

Pyth, 9 bytes

.AyMjQ^T3

Try it online! (uses the third test case)

Assumes the black-box function is named y. You can declare such a function using L (argument: b), as shown on TIO. I will implement all the test cases later, if I have time.

\$\endgroup\$
0
2
\$\begingroup\$

Stax, 8 bytes

Vk|Eym|A

Stax programs do not have function calls or arguments, so we store a block in the Y register that consumes and produces a single value. This can be done before the program code.

{...}Yd     store a block in the Y register that executes ...
Vk|E        get "digits" of input using base 1000
    ym      map "digits" to array using y as mapping function
      |A    all elements are truthy?

Here's an example using the perfect square function.

\$\endgroup\$
2
\$\begingroup\$

Clean, 54 bytes

import StdEnv
$n=if(n<1)True($(n/1000))&&f(n rem 1000)

Try it online!

Defines the function $ :: Int -> Bool, expecting a function f :: Int -> Bool to be defined elsewhere.

\$\endgroup\$
2
2
\$\begingroup\$

Java (OpenJDK 9), 94 bytes

f->n->{int r=0,l=n.length();for(;l>0;l-=3)r+=f.test(n.substring(l<4?0:l-3,l))?0:1;return r<1;}

Try it online!

\$\endgroup\$
2
2
\$\begingroup\$

Common Lisp, 73 72 bytes

(defun g(x f)(and(funcall f(mod x 1000))(or(< x 1e3)(g(floor x 1e3)f))))

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ This is my first Lisp answer, so it's probably terrible. \$\endgroup\$
    – Dennis
    Mar 10, 2018 at 18:36
1
\$\begingroup\$

05AB1E, 8 bytes

₄вεI.V}P

Try it online!

Explanation

₄в         # convert first input to base-1000
  ε   }    # apply to each element
   I.V     # execute second input as code
       P   # product of the resulting list

Takes the number as the first line of input and the function as the second.
Outputs 1 for truthy and 0 for falsy.

\$\endgroup\$
5
  • \$\begingroup\$ This doesn't execute the code on each element, but on the whole list. \$\endgroup\$ Mar 6, 2018 at 11:48
  • \$\begingroup\$ @EriktheOutgolfer: With 05AB1E's automatic vectorization it will in most cases. I just realized that it won't work for Qand Ê though. I'll revert back to the 8-byte version. \$\endgroup\$
    – Emigna
    Mar 6, 2018 at 11:50
  • \$\begingroup\$ Still, it's not .V that vectorizes, it doesn't even take the list as an argument itself to begin with. \$\endgroup\$ Mar 6, 2018 at 11:53
  • \$\begingroup\$ @EriktheOutgolfer: I've never said that .V vectorizes. In the example in my link it's È. \$\endgroup\$
    – Emigna
    Mar 6, 2018 at 11:55
  • \$\begingroup\$ Actually Q and Ê would work with vectorization unlike I previously stated, but using automatic vectorization would make these commands map on the whole list which feels outside the spirit of the challenge so we need ε. \$\endgroup\$
    – Emigna
    Mar 6, 2018 at 12:11
1
\$\begingroup\$

Excel VBA, 79 bytes

An anonymous VBE immediate window function that takes input, n as type integer from range [A1], and the name of a publically defined VBA function from range [B1].

t=1:n=[A1]:While n:t=t*-Application.Run(""&[B1],n Mod 1E3):n=Int(n/1E3):Wend:?t

Usage example

In a public module, the input function, in this case f() is defined.

Public Function f(ByVal n As Integer) As Boolean
    Let f = (n Mod 2 = 0)
End Function

The input variables are set.

[A1]=2902414    ''  Input Integer
[B1]="f"        ''  input function

The immediate window function is then called.

t=1:n=[A1]:While n:t=t*-Application.Run(""&[B1],n Mod 1E3):n=Int(n/1E3):Wend:?t
 1              ''  Function output (truthy)
\$\endgroup\$
1
\$\begingroup\$

Ruby, 37 bytes

g=->f,n{f[n%x=1000]&&(n<x||g[f,n/x])}

Try it online!

A recursive lambda, taking function and integer and returning boolean.

36 bytes (only positive n)

g=->f,n{n>0?f[n%k=1000]&&g[f,n/k]:1}

This version returns 1 for truthy, false for falsey. Unfortunately it can fail when n = 0

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ I think you have to count g= if it's recursive \$\endgroup\$ Mar 5, 2018 at 23:53
  • \$\begingroup\$ @AsoneTuhid Oh, that makes sense. I'll add it in. \$\endgroup\$
    – benj2240
    Mar 6, 2018 at 0:06
  • \$\begingroup\$ Also, try this (-1 byte), it returns 1 as the truthy value \$\endgroup\$ Mar 6, 2018 at 0:11
  • \$\begingroup\$ That wrinkled my brain a little... I had to tinker around a bit to convince myself it worked in all cases. Thanks! \$\endgroup\$
    – benj2240
    Mar 6, 2018 at 19:29
  • \$\begingroup\$ I was wrong, this version doesn't work for g[->n{n>0},0] (returns true). It only fails if the input is 0 but the question says "non-negative" so you should go back to 37. sorry \$\endgroup\$ Mar 7, 2018 at 8:37
1
\$\begingroup\$

Appleseed, 51 bytes

(lambda(n f)(all(map f(or(to-base 1000 n)(q(0))))))

Anonymous lambda function that takes a number and a function and returns a boolean value.

Try it online!

(lambda (n f)         ; Function with parameters n and f
 (all                 ; Return true if all elements of this list are truthy:
  (map f              ; Map the function f to each element of
   (or                ; This list if it is nonempty:
    (to-base 1000 n)  ; Convert n to a list of "digits" in base 1000
    (q (0))           ; Or if that list is empty (when n=0), then use the list (0) instead
   ))))
\$\endgroup\$
1
\$\begingroup\$

Add++, 15 bytes

L,1000$bbbUª{f}

Try it online!

Requires a function f to be declared in the TIO header.

How it works

D,f,@,0.5^i2^A=	; Declares a function 'f' to check if a perfect square
		; E.g. 25 -> 1; 26 -> 0

L,		; Declare the main lambda function
		; Example argument: 		[25121]
	1000$bb	; Convert to base 1000	STACK = [[25 121]]
	bUª{f}	; Is 'f' true for all?	STACK = [1]
\$\endgroup\$
0
\$\begingroup\$

Husk, 7 bytes

`ΛmrC_3

Try it online! Takes N as a string and f as a function object. Given a number, f can return any truthy or falsy value.

\$\endgroup\$
0
\$\begingroup\$

Japt, 9 bytes

Had to sacrifice 3 bytes and take input as a string in order to handle the n=>n>0 test case when n=0. I could get them back if it was permitted to pass the groupings to the function as strings and convert them to integers within the function, when necessary

ò3n)mn eV

Try it

The function, as allowed by consensus, is preassigned to variable V on the second line of the header - the empty first line is essential.

Here are all the test case functions rewritten for Japt.

n -> n == n                           X{X==X}
n -> n != n                           X{X!=X}
n -> n > 400                          X{X>400}
n -> n > 0                            X{X>0}
n -> n -> 0                           X{X{0}}
n -> sqrt(n) ** 2 == n                X{Xq v1}
n -> mod(n, 2) == 0                   X{X%2==0}
n -> n % 10 > max(digits(n / 10))     X{X%10>Xz10 ì rw}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.