Find relevant digit groupings

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 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

Conor O'Brien

Posted 2018-03-05T18:45:11.843

Reputation: 36 228

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.200

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. – 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.927

1@ConorO'Brien In that case you should add (n -> n > 0 applied to 0) to the test cases because most answers fail on it. – Asone Tuhid – 2018-03-07T08:53:45.947

Are the digit groups of 0 [0] or []? – Erik the Outgolfer – 2018-03-07T13:48:37.907

1@EriktheOutgolfer They are [0]. – Conor O'Brien – 2018-03-07T14:35:27.897

Answers

4

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.

Erik the Outgolfer

Posted 2018-03-05T18:45:11.843

Reputation: 38 134

I'm pretty sure that this is a failure when applying n -> n != 0 to 0

– Asone Tuhid – 2018-03-07T09:01:51.043

@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).

– Erik the Outgolfer – 2018-03-07T13:45:37.070

Sorry then, I barely understand Jelly. Nice solution. – Asone Tuhid – 2018-03-07T13:47:46.130

7

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.

Martin Ender

Posted 2018-03-05T18:45:11.843

Reputation: 184 808

5

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

Uriel

Posted 2018-03-05T18:45:11.843

Reputation: 11 708

Save 3 bytes by going explicit: Try it online!

– Adám – 2018-03-06T11:33:23.577

@Adám thanks! I liked the alpha-alpha though... – Uriel – 2018-03-06T13:33:45.160

correct me if i'm wrong but i think this is a failure

– Asone Tuhid – 2018-03-07T08:45:07.463

4

Python 2, 46 bytes

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

Try it online!

Chas Brown

Posted 2018-03-05T18:45:11.843

Reputation: 8 959

44 bytes by using * in place of and. – Bubbler – 2018-03-14T07:17:48.457

4

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.

nimi

Posted 2018-03-05T18:45:11.843

Reputation: 34 639

2 bytes more using && instead of and – ovs – 2018-03-06T15:26:32.970

4

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!

betseg

Posted 2018-03-05T18:45:11.843

Reputation: 8 493

45 bytes? g(f,i)int f();{i=!i||f(i%1000)&&g(f,i/1000);} – Neil – 2018-03-05T20:46:07.013

fails when applying f(n){return n > n;} to 0 – Asone Tuhid – 2018-03-07T08:56:05.337

Best fix I could do cost 3 bytes: Try it online!

– Neil – 2018-03-07T10:48:11.060

3

Wolfram Language (Mathematica), 30 bytes

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

Try it online!

Martin Ender

Posted 2018-03-05T18:45:11.843

Reputation: 184 808

3

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.

Neil

Posted 2018-03-05T18:45:11.843

Reputation: 95 035

1000 -> 1e3 to save a couple of bytes. And could you replace && with & for another byte? – Shaggy – 2018-03-06T15:32:46.770

@Shaggy Yes, I think that's safe enough. Same goes for betseg's answer? – Neil – 2018-03-06T16:29:47.967

fails for function_name(n=>n>0)(0) (returns true) – Asone Tuhid – 2018-03-07T08:38:48.857

@AsoneTuhid Thanks, fixed. – Neil – 2018-03-07T10:43:39.800

2

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.

Mr. Xcoder

Posted 2018-03-05T18:45:11.843

Reputation: 39 774

2

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.

recursive

Posted 2018-03-05T18:45:11.843

Reputation: 8 616

2

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.

Οurous

Posted 2018-03-05T18:45:11.843

Reputation: 7 916

fails when applying n -> n > 0 to 0 – Asone Tuhid – 2018-03-07T08:52:46.940

@AsoneTuhid Fixed. – Οurous – 2018-03-07T09:16:45.560

2

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!

Olivier Grégoire

Posted 2018-03-05T18:45:11.843

Reputation: 10 647

fails when applying n -> n > 0 to 0 – Asone Tuhid – 2018-03-07T08:47:23.353

@AsoneTuhid fixed at great cost of bytes. – Olivier Grégoire – 2018-03-07T20:28:13.073

2

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!

Dennis

Posted 2018-03-05T18:45:11.843

Reputation: 196 637

This is my first Lisp answer, so it's probably terrible. – Dennis – 2018-03-10T18:36:43.333

1

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!

benj2240

Posted 2018-03-05T18:45:11.843

Reputation: 801

I think you have to count g= if it's recursive – Asone Tuhid – 2018-03-05T23:53:20.643

@AsoneTuhid Oh, that makes sense. I'll add it in. – benj2240 – 2018-03-06T00:06:55.370

Also, try this (-1 byte), it returns 1 as the truthy value

– Asone Tuhid – 2018-03-06T00:11:35.780

That wrinkled my brain a little... I had to tinker around a bit to convince myself it worked in all cases. Thanks! – benj2240 – 2018-03-06T19:29:35.277

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 – Asone Tuhid – 2018-03-07T08:37:15.763

1

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.

Emigna

Posted 2018-03-05T18:45:11.843

Reputation: 50 798

This doesn't execute the code on each element, but on the whole list. – Erik the Outgolfer – 2018-03-06T11:48:47.627

@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. – Emigna – 2018-03-06T11:50:43.280

Still, it's not .V that vectorizes, it doesn't even take the list as an argument itself to begin with. – Erik the Outgolfer – 2018-03-06T11:53:34.677

@EriktheOutgolfer: I've never said that .V vectorizes. In the example in my link it's È. – Emigna – 2018-03-06T11:55:11.047

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 ε. – Emigna – 2018-03-06T12:11:20.297

1

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)

Taylor Scott

Posted 2018-03-05T18:45:11.843

Reputation: 6 709

1

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
   ))))

DLosc

Posted 2018-03-05T18:45:11.843

Reputation: 21 213

1

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]

caird coinheringaahing

Posted 2018-03-05T18:45:11.843

Reputation: 13 702