Is this number a repdigit?

33

4

Challenge

A repdigit is a non-negative integer whose digits are all equal.

Create a function or complete program that takes a single integer as input and outputs a truthy value if the input number is a repdigit in base 10 and falsy value otherwise.

The input is guaranteed to be a positive integer.

You may take and use input as a string representation in base 10 with impunity.

Test cases

These are all repdigits below 1000.

1
2
3
4
5
6
7
8
9
11
22
33
44
55
66
77
88
99
111
222
333
444
555
666
777
888
999

A larger list can be found on OEIS.

Winning

The shortest code in bytes wins. That is not to say that clever answers in verbose languages will not be welcome.

Aidan F. Pierce

Posted 2017-06-08T02:50:36.133

Reputation: 1 365

2Related. – Leaky Nun – 2017-06-08T03:02:10.307

@AidanF.Pierce What's the biggest number the input will be? – stevefestl – 2017-06-14T09:00:13.820

Answers

21

Brachylog, 1 byte

=

Try it online!

This acts on integers.

From src/predicates.pl#L1151:

brachylog_equal('integer':0, 'integer':0, 'integer':0).
brachylog_equal('integer':0, 'integer':I, 'integer':I) :-
    H #\= 0,
    integer_value('integer':_:[H|T], I),
    brachylog_equal('integer':0, [H|T], [H|T]).

Leaky Nun

Posted 2017-06-08T02:50:36.133

Reputation: 45 011

I've decided to accept this one because it's the earliest 1-byte submission. – Aidan F. Pierce – 2017-06-25T22:44:50.453

19

C (gcc), 33 30 29 bytes

f(n){n=n%100%11?9/n:f(n/10);}

Try it online!

Dennis

Posted 2017-06-08T02:50:36.133

Reputation: 196 637

Very nice trick with the recursion and assignment instead of return (think I'm going to steal the latter for my answer :) ). – Doorknob – 2017-06-08T04:13:45.870

@Doorknob Go ahead. :) You'll have to specify a compiler though; I expect this to be pretty much gcc/tcc only. – Dennis – 2017-06-08T04:22:42.910

Did you know beforehand that gcc with -O0 will write final result to n from exactly eax, so as to make it the return value? Could you elaborate on the logic why you knew it would work? – Ruslan – 2017-06-09T11:11:47.073

@Ruslan I'm not sure why gcc behaves like this, but the last variable assignment inside a function winds up in eax more often than not. If I had to guess, I'd say it's because it allows return n to be a nop, and there's no reason to assign to a local variable at the end of a function if you're not going to return the result. – Dennis – 2017-06-09T14:25:18.897

9

COBOL, 139 BYTES

I feel like COBOL doesn't get any love in code golfing (probably because there is no way it could win) but here goes:

IF A = ALL '1' OR ALL '2' OR ALL '3' OR ALL '4' OR ALL '5' OR
ALL '6' OR ALL '7' OR ALL '8' OR ALL '9' DISPLAY "TRUE" ELSE   
DISPLAY "FALSE".

A is defined as a PIC 9(4).

SaggingRufus

Posted 2017-06-08T02:50:36.133

Reputation: 191

2You can golf this by changing TRUE and FALSE to 1 and 0 respectively – caird coinheringaahing – 2017-11-04T23:08:45.227

8

05AB1E, 1 byte

Ë

Checks if all digits are equal

Try it online!

Neil A.

Posted 2017-06-08T02:50:36.133

Reputation: 2 038

6

Python 3, 25, 24 19 bytes.

len({*input()})>1>t

A stdin => error code variant.

Returns error code 0 if it's a repdigit - or an error on failure.

Thanks to Dennis for helping me in the comments.

Shadow

Posted 2017-06-08T02:50:36.133

Reputation: 161

Since exit code 0 indicates success, I think you should test >1 rather than <2. Raising an actual error would be shorter than using exit btw. – Dennis – 2017-06-08T05:14:01.543

I was wondering about that. The challenge says "a truthy value". I'll change it to raise an error. – Shadow – 2017-06-08T05:15:24.573

1Yes, if python3 repdigit.py; then echo truthy; else echo falsy; fi has to work according to out definition, so 0 is truthy and everything else is falsy. – Dennis – 2017-06-08T05:17:43.980

That makes sense. Ok I'll make that change too. – Shadow – 2017-06-08T05:18:48.920

<2 or t was correct as t is only referenced if the comparison is false. >1 and t would also work, but len(set(input()))>1>t is shorter. Also, in Python 3.5+, set(input()) can become {*input()}. https://tio.run/##HY3BCsMgEETP8Sv2ppYelN4Czb8Ys22FdBXdQELot1vNZWBmeDPp4E@kR/VxQXiClLKuSOq8BUobK/3Tk524tlyIV8xAEAiyozcqewdrjNGjGDgfTYeLaSur@86LG6FwVqRbgTt61S@6STkQXznuHhN3MrlS6h8 – Dennis – 2017-06-08T05:24:29.657

Good points. Thanks for your help - I've updated it again. – Shadow – 2017-06-08T05:29:10.310

@Dennis What does the * do? – Arc676 – 2017-06-14T06:17:10.763

2@Arc676 Unary * unpacks an iterable. For example, {*'123'} generates the set {'1','2','3'}. – Dennis – 2017-06-14T06:18:55.167

If you change it to "exit code 0 = failure" (which kind of makes more sense, since 0 is a falsey value) you can shorten it to [*{*input()}][1] which is only 16 bytes. – mypetlion – 2018-10-09T00:54:13.083

@mypetlion - a good idea, but the rules seem pretty set on that one (I think it's well justified in the above comments). Can you think of a way to shorten it but with the current behaviour? – Shadow – 2018-10-09T00:56:42.437

6

Mathematica, 27 bytes

AtomQ@Log10[9#/#~Mod~10+1]&

It doesn't beat Equal@@IntegerDigits@#&, but it beats the other arithmetic-based Mathematica solution.

Repdigits are of the form n = d (10m-1) / 9 where m is the number of digits and d is the repeated digit. We can recover d from n by taking it modulo 10 (because if it's a rep digit, it's last digit will be d). So we can just rearrange this as m = log10(9 n / (n % 10) + 1) and check whether m is an integer.

Martin Ender

Posted 2017-06-08T02:50:36.133

Reputation: 184 808

5

Haskell, 15 bytes

all=<<(==).head

Try it online! Takes string input.

Equivalent to \s->all(==head s)s. Narrowly beats out alternatives:

f s=all(==s!!0)s
f s=s==(s!!0<$s)
f(h:t)=all(==h)t
f(h:t)=(h<$t)==t
f s=(s<*s)==(s*>s)
f(h:t)=h:t==t++[h]

xnor

Posted 2017-06-08T02:50:36.133

Reputation: 115 687

f s=(s<*s)==(s*>s) is a very interesting idea, I wasn't aware of this behaviour of <*before. – Laikoni – 2017-06-08T08:35:59.007

5

C (gcc), 41 bytes

f(char*s){s=!s[strspn(s,s+strlen(s)-1)];}

This is a function that takes input as a string and returns 1 if it is a repdigit and 0 otherwise.

It does this by making use of the strspn function, which takes two strings and returns the length of the longest prefix of the first string consisting of only characters from the second string. Here, the first string is the input, and the second string is the last digit of the input, obtained by passing a pointer to the last character of the input string.

Iff the input is a repdigit, then the result of the call to strspn will be strlen(s). Then, indexing into s will return a null byte if this is the case (str[strlen(str)] is always \0) or the first digit that doesn't match the last digit otherwise. Negating this with ! results in whether s represents a repdigit.

Try it online!

Thanks to @Dennis for indirectly reminding me of the assign-instead-of-return trick via his insanely impressive answer, saving 4 bytes!

Doorknob

Posted 2017-06-08T02:50:36.133

Reputation: 68 138

You can shorten this a bit further by avoiding strlen and creating a new string from *s: c;f(char*s){c=*s;c=!s[strspn(s,&c)];} for 37. – hvd – 2017-06-08T20:48:00.380

5

PHP, 25 28 25

<?=!chop($argn,$argn[0]);

remove all chars from the right that are equal to the first and print 1 if all chars were removed.

Christoph

Posted 2017-06-08T02:50:36.133

Reputation: 1 489

5

R, 31 bytes

function(x)grepl("^(.)\\1*$",x)

This functions works with string inputs and uses a regular expression to determine whether the input is a repdigit.

Example

> f <- function(x)grepl("^(.)\\1*$",x)
> x <- c("1", "2", "11", "12", "100", "121", "333")
> f(x)
[1]  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE

Sven Hohenstein

Posted 2017-06-08T02:50:36.133

Reputation: 2 464

28 bytes by switching from function(x) to using scan(,'') tio.run/##K/r/P70otSBHQylOQ08zJsZQS0VJpzg5MU9DR11dU/O/paXlfwA – Sumner18 – 2018-12-18T17:57:08.710

5

///, 110 bytes

/11/1//22/2//33/3//44/4//55/5//66/6//77/7//88/8//99/9//1/.//2/.//3/.//4/.//5/.//6/.//7/.//8/.//9/.//T..///.//T

Try it online!

The /// language doesn't have any concept of truthy and falsey, so this outputs "T" if the input is a repdigit, and does not output any characters if the input is not a repdigit.

Tanner Swett

Posted 2017-06-08T02:50:36.133

Reputation: 531

4

Jelly, 2 1 byte

E

Try it online!

Dennis

Posted 2017-06-08T02:50:36.133

Reputation: 196 637

2Umm...do 1-9 count as repdigits? – Erik the Outgolfer – 2017-06-08T09:30:46.970

2

According to the OP's answer, yes. Wolfram and OEIS agree.

– Dennis – 2017-06-08T12:48:34.063

4

Octave, 11 bytes

@(s)s==s(1)

Try it online!

Takes the input as a string.

It checks all characters for equality with the first characters. If all are equal, the result will be a vector with only 1 (true in Octave), otherwise there will be at least one 0 (false in Octave). Here's a proof.

Stewie Griffin

Posted 2017-06-08T02:50:36.133

Reputation: 43 471

Wouldn't you need to wrap it in all(...) to get a truthy/falsy value output? – Tom Carpenter – 2017-06-08T23:09:03.040

Did you test the proof? That's piece of code is the definition (meta consensus) of true/false on ppcg. – Stewie Griffin – 2017-06-09T07:12:59.260

4

C#, 42 33 28 bytes

i=>i.Replace(i[0]+"","")==""

i has to be a string.

Shaved down a lot thanks to @LethalCoder

LiefdeWen

Posted 2017-06-08T02:50:36.133

Reputation: 3 381

2i[0].ToString() can be shortened to i[0]+"", <1 is shorter than ==0. – TheLethalCoder – 2017-06-08T10:25:10.350

1Also .Length<1 can just be =="" – TheLethalCoder – 2017-06-08T12:21:54.167

4

grep, 17 bytes

grep -xP '(.)\1*'

Matches any string that's a repetition of its first character.

Toby Speight

Posted 2017-06-08T02:50:36.133

Reputation: 5 058

I think this would be bash. – S.S. Anne – 2020-02-19T22:01:33.930

In what way, @S.S.Anne? There's nothing in that command that isn't valid portable shell. – Toby Speight – 2020-02-20T08:44:16.340

Well, any shell. I didn't mean specifically bash. – S.S. Anne – 2020-02-20T17:42:39.047

3

JavaScript (ES6), 23 21 bytes

Saved 2 bytes thanks to Neil

Takes input as either an integer or a string. Returns a boolean.

n=>/^(.)\1*$/.test(n)

Demo

let f =

n=>/^(.)\1*$/.test(n)

console.log(f(444))
console.log(f(12))

Arnauld

Posted 2017-06-08T02:50:36.133

Reputation: 111 334

Doesn't usingtest instead of !!exec save 2 bytes? – Neil – 2017-06-08T09:07:57.973

(Although, for a string-only input, porting the PHP answer is even shorter.) – Neil – 2017-06-08T09:10:47.200

@Neil I don't know what I was thinking. Thanks! – Arnauld – 2017-06-08T09:11:34.517

3

Japt, 4 bytes

¥çUg

Try it online!

Tom

Posted 2017-06-08T02:50:36.133

Reputation: 3 078

3

Braingolf, 6 bytes

iul1-n

Try it online!

Unfortunately, Braingolf's implicit input from commandline args can't accept an all-digits input as a string, it will always cast it to a number, so instead the solution is to pass it via STDIN, which adds 1 byte for reading STDIN (i)

Explanation:

iul1-n
i       Read from STDIN as string, push each codepoint to stack
 u      Remove duplicates from stack
  l     Push length of stack
   1-   Subtract 1
     n  Boolean negate, replace each item on stack with 1 if it is a python falsey value
        replace each item on stack with 0 if it is a python truthy value
        Implicit output of last item on stack

After u, the length of the stack equals the number of unique characters in the input, subtracting 1 means it will be 0 if and only if there is exactly 1 unique character in the input, 0 is the only falsey number in Python, so n will replace 0 with 1, and everything else with 0.

Skidsdev

Posted 2017-06-08T02:50:36.133

Reputation: 9 656

3

APL, 5 bytes

2 bytes saved thanks to @KritixiLithos

⍕≡1⌽⍕

Try it online!

Uriel

Posted 2017-06-08T02:50:36.133

Reputation: 11 708

You can golf the 7-byte solution to 5 bytes by using a train ⊢≡1⌽⊢. – user41805 – 2017-06-08T11:15:46.403

@KritixiLithos thanks! – Uriel – 2017-06-08T11:54:46.553

Replace with to handle both strings and numbers. – Adám – 2017-06-08T12:27:50.520

@Adám thanks! I didn't think of formatting as a way of getting array of digits. – Uriel – 2017-06-08T18:29:36.260

3

Ohm, 4 bytes

Ul2<

Try it online!

Explanation

 Ul2<
 U    # Push uniquified input
  l   # Length
   2< # Is it smaller than 2?

Datboi

Posted 2017-06-08T02:50:36.133

Reputation: 1 213

I think Ul≤should work. – Christoph – 2017-06-08T11:19:27.107

@Christoph Yee I had that but I wasn't sure if 0 counts as a truthy value. (Quite new to this codegolf thing ^^) – Datboi – 2017-06-08T11:23:02.880

Ah damn 0 is falsey and every other number is truthy. I just noticed that we need exactly the opposite for this challenge (often we're allowed to swap as long as we declare which case is truthy and which is falsey). Truthy is defined by "would take a brench". – Christoph – 2017-06-08T11:31:38.863

Ul1E should also work (though I don't know Ohm) because it doesn't need to handle 0. – Esolanging Fruit – 2017-06-14T03:42:06.977

3

Java, 21 bytes:

l->l.toSet().size()<2

l is a MutableList<Character> from eclipse collections.

Nathan Merrill

Posted 2017-06-08T02:50:36.133

Reputation: 13 591

1l could also be a CharAdapter. – Donald Raab – 2017-06-13T23:37:16.520

@DonaldRaab oooh, I've never seen that class. Nice find. – Nathan Merrill – 2017-06-14T01:59:59.883

There is CodePointAdapter and CodePointList as well. – Donald Raab – 2017-06-17T23:46:28.393

1@DonaldRaab I use eclipse collections quite a bit, but I always struggle to find anything outside of the standard List/Map/Set collections. Is your knowledge based off of development of the libraries, or is there somewhere (other than the javadoc) I can find a better reference for everything EC provides? – Nathan Merrill – 2017-06-18T02:14:17.930

Glad to hear it. I am a committer for the framework... I wrote these particular String related classes a year or so ago. There is a Reference Guide which many folks don't know about. There is a mind-map I recently put together to help folks learn and navigate through the the plethora of features in the library. It's the last link in the TOC of the Ref. Guide. https://github.com/eclipse/eclipse-collections/blob/master/docs/guide.md

– Donald Raab – 2017-06-18T02:22:32.097

There are also a bunch of presentations from conferences and meetups linked from the EC wiki - https://github.com/eclipse/eclipse-collections/wiki/Conference-talks-and-meetups

– Donald Raab – 2017-06-18T02:32:25.640

3

Kotlin, 28 19 bytes

{it.toSet().size<2}

Try it online!

Takes input as a String because

You may take and use input as a string representation in base 10 with impunity.

Explanation

{
    it.toSet()     // create a Set (collection with only unique entries)
                   // out of the characters of this string
        .size < 2  // not a repdigit if the set only has one entry
}

If you don't like the fact it takes a String, you can have one that takes an Int for 24 bytes.

{(""+it).toSet().size<2}

snail_

Posted 2017-06-08T02:50:36.133

Reputation: 1 982

3

Regex (ECMAScript), 31 bytes

^(x{0,9})((x+)\3{8}(?=\3$)\1)*$

Try it online!

Takes input in unary, as usual for math regexes (note that the problem is trivial with decimal input: just ^(.)\1*$).

Explanation:

^(x{0,9})           # \1 = candidate digit, N -= \1
(                   # Loop the following:
  (x+)\3{8}(?=\3$)  # N /= 10 (fails and backtracks if N isn’t a multiple of 10)
  \1                # N -= \1
)* $                # End loop, assert N = 0

Grimmy

Posted 2017-06-08T02:50:36.133

Reputation: 12 521

Try it online! – Deadcode – 2019-02-08T19:08:11.800

@Deadcode Whoops I forgot to put that in, thanks! – Grimmy – 2019-02-09T10:49:07.847

2

Neim, 1 byte


Simply checks that all elements are equal.

Without builtin, 2 bytes:


Explanation:

     Calculate unique digits
     Get the length

This works because only 1 is considered truthy in Neim, and everything else is falsy.

Alternatively, for 4 bytes:

μ

Explanation:

      Calculate unique digits
       Join list into an integer
      Check that is is less than
  μ    Ten.

Try it!

Okx

Posted 2017-06-08T02:50:36.133

Reputation: 15 025

2

PHP, 30 bytes

<?=($a=$argn).$a[0]==$a[0].$a;

user63956

Posted 2017-06-08T02:50:36.133

Reputation: 1 571

@Dada No. It will compare 4344 and 4434. – user63956 – 2017-06-08T08:11:13.060

Oh right, my bad. thanks – Dada – 2017-06-08T09:07:44.110

2

QBasic 4.5, 55 bytes

INPUT a
FOR x=1TO LEN(STR$(a))
c=c*10+1
NEXT
?a MOD c=0

I've mathed it! The FOR-loop checks the number of digits in the input, then creates c, which is a series of 1's of length equal to the input. A number then is repdigit if it modulo the one-string == 0.

Try it online! Note that the online interpreter is a bit quirky and I had to write out a couple of statements that the DOS-based QBasic IDE would expand automatically.

steenbergh

Posted 2017-06-08T02:50:36.133

Reputation: 7 772

2

Java, 38 33 23 bytes

n->n.matches("(.)\\1*")

n is a String, naturally.

Note that there is no need for ^...$ in the regex since it's automatically used for exact matching (such as the match method), compared to finding in the string.

Try it!

Saves

  • -5 bytes: used String since "You may take and use input as a string with impunity."
  • -10 bytes: regex is apparently a good fit.

Olivier Grégoire

Posted 2017-06-08T02:50:36.133

Reputation: 10 647

Was about to post this exact solution, including the explanation about the matches not requiring ^$ because it matches the entire String. So a definite +1 from me. ;) – Kevin Cruijssen – 2017-06-09T11:34:28.260

2

C, 38 bytes

f(char*s){return*s^s[1]?!s[1]:f(s+1);}

Recursively walks a string. If the first two characters differ (*s^s[1]) then we succeed only if we're at the end of the string (!s[1]) otherwise we repeat the test at the next position (f(s+1)).

Test program

#include <stdio.h>
int main(int argc, char **argv)
{
    while (*++argv)
        printf("%s: %s\n", *argv, f(*argv)?"yes":"no");
}

Toby Speight

Posted 2017-06-08T02:50:36.133

Reputation: 5 058

2

R, 25 bytes

grepl("^(.)\\1*$",scan())

Try it online

Best non-regex solution I could come up with was 36 bytes:

is.na(unique(el(strsplit(x,"")))[2])

user2390246

Posted 2017-06-08T02:50:36.133

Reputation: 1 391

1for another option on the non-regex rle(charToRaw(scan(,'')))$v[2]<1 – MickyT – 2017-06-08T19:52:49.830

2

Cubix, 15 bytes

uOn@ii?-?;.$@<_

Try it online!

    u O
    n @
i i ? - ? ; . $
@ < _ . . . . .
    . .
    . .

Watch It Run

Outputs 1 for truthy and nothing for falsey

Very simply read reads in the input one character at a time. It takes the current character away from the previous. If a non zero result then it halts immediately. Otherwise it continues inputting and comparing until the EOI. On EOI (-1), negate and exit

MickyT

Posted 2017-06-08T02:50:36.133

Reputation: 11 735

2

MATL (6 5 bytes or 2 bytes for Luis Mendo solution)

Vun1=

Try it online!

Explanation

V     % convert to string
un    % find unique characters and count them
1=    % if there is only one unique character, then we pass.

Luis Mendo solution (see comments):

&=

Outputs a truthy array (all 1's) if repdigit, or a falsy array (some 0 in the array) if not a perfect repdigit.

Try it online!

DrQuarius

Posted 2017-06-08T02:50:36.133

Reputation: 562

Since any truthy/falsy outputs are valid, you can use V&=; or j&= to avoid floating-point limitations. Or even &=, since inputting a string is allowed

– Luis Mendo – 2019-05-27T09:47:12.140

Hmm, I'm afraid I can't follow. I tried replacing "1=" with "&=" and it didn't work. Also tried "V&=" as the whole code, in case that's what you meant. – DrQuarius – 2019-07-17T11:37:37.987

I mean just &=. A non-empty array containing only ones is truthy, and an array containing some zero is falsy (see link in my previous comment for explanation about truthy/falsy)

– Luis Mendo – 2019-07-17T16:06:27.770

1Wow, ok. That's a very simple solution then. Will add. – DrQuarius – 2019-08-02T04:42:54.937

1

Python 3, 23 bytes

lambda s:s==s[0]*len(s)

Try it online!

Not shorter than @shadow's answer, but I thought it was interesting. Should work in Python 2 as well.

musicman523

Posted 2017-06-08T02:50:36.133

Reputation: 4 472

1

QBIC, 36 23 bytes

[_l!:$||p=p*z+1]?b%p=0

Explanation

    :     Read a number from the cmd line
   ! $    cast it to string
 _l   |   Take its length
[      |  And run a FOR-loop from 1 to that length 
p=        p starts out as 0. set it to 
  p*z       itself multiplied by 10 (z=10 in QBIC) (still 0 on the first run
  +1        then add 1. On consecutive FOR-loops yields 1, 11, 111, ....
]         Close the FOR loop
?b%p=0    PRINT -1 if b mod p is 0 (ie 444 % 111 = 0), or 0 otherwise

steenbergh

Posted 2017-06-08T02:50:36.133

Reputation: 7 772

1

Ruby, 13 bytes (12 + '-n' flag)

p~/^(.)\1*$/

Try it online!

G B

Posted 2017-06-08T02:50:36.133

Reputation: 11 099

1

Clojure, 17 12 bytes

You may take and use input as a string representation in base 10 with impunity.

Oh in that case:

#(apply = %)

Original:

#(apply =(str %))

NikoNyrh

Posted 2017-06-08T02:50:36.133

Reputation: 2 361

1

C#, 38 bytes

using System.Linq;s=>s.All(c=>c==s[0])

Or alternatively for 44 bytes:

using System.Linq;s=>s.Distinct().Count()==1

TheLethalCoder

Posted 2017-06-08T02:50:36.133

Reputation: 6 930

I'm no C# developer, but do you really require using System.Linq? I've seen plenty of C# lambda answers without that. – Olivier Grégoire – 2017-06-08T12:37:26.313

1@OlivierGrégoire Linq is required for All, it's like needing an external package in C and requiring to import it. If I am using Linq I must include the using. – TheLethalCoder – 2017-06-08T12:40:48.063

1

><>, 26 24 bytes

!vi:0(?v::&r&=?
 >0n;n1<

Reads the input as a string and reads every character in a loop

  i               // Take the next character and pushes it onto the stack
   :0(?v          // If the end of the input has been reached, goto the successful termination code
        ::        // Duplicate the top of the stack twice
!v         &r&=?  // Pop the top of the stack, pop the bottom of the stack, and compare them. If they are not equal, goto the failure termination code
 >0n;            // Failure termination code. Prints a 0 and terminates
    ;n1<         // Success termination code. Prints a 1 and terminates

AGourd

Posted 2017-06-08T02:50:36.133

Reputation: 271

1

Perl 6, 12 bytes

{[==] .comb}

Reduces the list of characters in the input number with the numeric equality operator.

Sean

Posted 2017-06-08T02:50:36.133

Reputation: 4 136

1

PowerShell, 23 bytes

"$args"-match'^(.)\1*$'

Save as repdigit.ps1 and run with PS C:\wherever\repdigit.ps1 444, outputs True or False.

If you want the Python len(set(input))==1 style, it costs more at 32 bytes:

@($args-split''|group).count-eq2

(Noting that the split on the space between chars also outputs an empty start and end string as well as the characters).

TessellatingHeckler

Posted 2017-06-08T02:50:36.133

Reputation: 2 412

1

PHP, 28 bytes

<?=!count_chars($argn,3)[1];

count_chars with mode=3 creates a string with all different characters in $argn.
If there is only one, the second character will be empty == falsy.

Run as pipe with -F.

Titus

Posted 2017-06-08T02:50:36.133

Reputation: 13 814

1

Smalltalk, 21 19 bytes

[:s|s asSet size<2]

Try it online!

You can execute the block above by sending the message value: to the block with a String. A String in Smalltalk is written with single quotes. A longer version of the block could accept an Integer. It would take 30 28 bytes.

[:i|i asString asSet size<2]

I tried both solutions in the current online version of Amber Smalltalk and Pharo Smalltalk version 6.0.

Thanks to the suggestion in the comments I was able to remove the spaces before and after the <. It's been many years since I wrote Smalltalk but I doubt I ever tried that in practice. The shorter version worked in both versions of Smalltalk I tested.

Donald Raab

Posted 2017-06-08T02:50:36.133

Reputation: 111

1Are the spaces around the < necessary? I haven't used small talk very much, but most languages wont require that sort of thing. – Post Rock Garf Hunter – 2017-06-14T03:51:56.473

1Yes, the spaces are necessary. The syntax of Smalltalk is Object<space>message. I was able to remove two spaces before and after the block separator |. – Donald Raab – 2017-06-14T03:56:40.563

1I stand corrected. I tried removing the spaces before and after the < and it seems to work. – Donald Raab – 2017-06-14T03:58:48.140

1

Check, 135 134 132 124 bytes

 [r            #v
#:>10%:]R+r->\#v#?
#v
# >10-\)\#     # ?
d #             ^ 
 #R:
:>=r,#v
#ddd[=#(:@:@=R-?
d\:!:R *
o>]=d#^

Input should be passed as a command-line argument. Outputs some unprintables for truthy and only zero bytes for falsey. Always terminates with an IndexError due to the fact that it abuses an interpreter bug.

How does it work?

This code is divided into two segments. The first half turns the input integer into a list of digits, and the second half checks that all of its digits are equal to the first digit (i.e. they are all equal).

It roughly corresponds to the following pseudocode:

  1. Read input and call it i.
  2. Create an empty array and store it in the register. ([r)
  3. If i is 0, go to step 13.
  4. Take the number modulo 10. Call it x.
  5. Prepend x to the register and store it back in the register.
  6. Subtract x from i.
  7. Create a counter, starting at 0.
  8. If i is 0, go to step 12.
  9. Increment the counter.
  10. Decrement i by 10.
  11. Go back to step 8.
  12. Set i to whatever value is now in the counter.
  13. Go back to step 3.
  14. Load the register, which is now an array containing the digits of the input. Call this d.
  15. Get the first element in d and store it in the register.
  16. Create a counter called j, initialized to the length of d.
  17. Decrement j.
  18. If the jth element of d is not equal to the value of the register, crash the program. (We found a digit that is not correct.)
  19. Otherwise, print some random junk !j times. This only prints something if j == 0.
  20. Create an array of length 1 and get the !jth element. This will crash the program if j == 0.
  21. If the program has not yet crashed, j must not yet be zero, so go back to step 17.

Esolanging Fruit

Posted 2017-06-08T02:50:36.133

Reputation: 13 542

1

Julia, 30 bytes

f(n)=length(Set(string(n)))==1

EricShermanCS

Posted 2017-06-08T02:50:36.133

Reputation: 121

1

F#, 38 bytes

let f n=Seq.length(Seq.countBy id n)=1

Try it online!

Jason Handby

Posted 2017-06-08T02:50:36.133

Reputation: 31

1

Excel, 28 27 bytes

=A1=MID(A1,2,A1)&LEFT(A1,1)

l4m2

Posted 2017-06-08T02:50:36.133

Reputation: 5 985

1

QBasic, 37 32 bytes

INPUT n$
?n$=STRING$(LEN(n$),n$)

-5 bytes thanks to steenbergh

The STRING$ function takes two arguments, a number n and a string s$, and constructs a string consisting of n copies of the first character of s$.* So this code reads our number as a string n$, generates a string consisting of LEN(n$) copies of n$'s first character, and checks to see if it is equal to n$.

* The second argument can also be an integer codepoint, in which case it repeats the character corresponding to that codepoint.

DLosc

Posted 2017-06-08T02:50:36.133

Reputation: 21 213

Are you sure you need the ASC? Often QBasic simply takes the 1st char of a string when it expects only 1 char but the string itself is longer. I think, but can't test atm, that this will work fine too: INPUT n$:?n$=STRING$(LEN(n$),n$) – steenbergh – 2018-06-20T13:34:00.777

Confirmed on QB4.5: if we input n$ as 123, then STRING$(3, n$) is 111. – steenbergh – 2018-06-20T16:22:59.530

1@steenbergh [facepalm] I just read that in the help file, too. Thanks! – DLosc – 2018-06-20T20:36:25.223

1

Pepe, 69 60 bytes

REeErEEEEEREEEeeEREEEEeEeEREEEEEEeREEEeREEEEEerrEEreEErEereE

Try it online!

How it works?

It takes the greatest digit and subtracts it from the least digit. Repdigit numbers always evaluate to 0.

REeE             # Takes a number (stack r) 
rEEEEEREEEeeE    # Splits by digits 
REEEEeEeE        # Sorts them
REEEEEEe         # Copies first digit to other stack
REEEe            # Move pointer to last in stack r
REEEEEe          # Subtract stack R to stack r
rrEErEEEEErEereE # Print if 0, else none

u_ndefined

Posted 2017-06-08T02:50:36.133

Reputation: 1 253

1

Whitespace, 96 93 bytes

[S S S N
_Push_0][S N
S _Duplicate][T N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve][N
S S N
_Create_Label_LOOP][S N
S _Duplicate][S S S T   T   S S T   S S N
_Push_100][T    S T T   _Modulo][S S S T    S T T   N
_Push_11][T S T T   _Modulo][N
T   S S N
_If_0_Jump_to_label_NEXT][S S S T   S S T   N
_Push_9][S N
T   _Swap_top_two][T    S T S _Integer_division][T  N
S T _Print_as_integer][N
S S S N
_Create_Label_NEXT][S S S T S T S N
_Push_10][T S T S _Integer_division][N
S N
N
_Jump_to_Label_LOOP]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Port of @Dennis♦' C answer, so also outputs a positive digit ([1,9]) as truthy and 0 as falsey.

Try it online (with raw spaces, tabs and new-lines only).

Pseudo-code:

Integer i = STDIN as integer
Start LOOP:
  If(i modulo-100 modulo-11 == 0):
    i = i integer-divided by 10
    Go to next iteration of LOOP
  i = 9 / i
  Print i to STDOUT as integer
  Stop the program with an error

Kevin Cruijssen

Posted 2017-06-08T02:50:36.133

Reputation: 67 575

1

Pascal (FPC), 81 75 68 bytes

var n:string;begin read(n);write(n=StringOfChar(n[1],length(n)))end.

Try it online!

It seems that taking a number as string is shorter.


75 bytes - taking the number as integer:

Thanks to @Ørjan Johansen for -6 bytes - mod 100 mod 11 trick

var n:word;begin read(n);while n mod$64mod$B=0do n:=n div$A;write(n<=9)end.

Try it online!

$64, $B and $A are hexadecimal constants, they eliminate some whitespace that would be needed for their decimal counterparts.

AlexRacer

Posted 2017-06-08T02:50:36.133

Reputation: 979

I think you can shave off a bit with the mod 100 mod 11 trick some others are using. – Ørjan Johansen – 2018-10-08T20:08:01.990

@ØrjanJohansen Thanks, a good one! – AlexRacer – 2018-10-08T21:19:15.710

1

Taxi, 730 706 bytes

-24 bytes by eliminating linebreaks.

Clever answer in a verbose language? Check and check.

Go to Post Office:w 1 l 1 r 1 l.Pickup a passenger going to Chop Suey.Go to Chop Suey:n 1 r 1 l 4 r 1 l.Pickup a passenger going to Cyclone.[B]Switch to plan D if no one is waiting.Pickup a passenger going to Crime Lab.Go to Cyclone:n 1 l 3 l.Pickup a passenger going to Crime Lab.Pickup a passenger going to Crime Lab.Go to Crime Lab:n 2 r 2 r.Switch to plan C if no one is waiting.Pickup a passenger going to Cyclone.Go to Fueler Up:n.Go to Chop Suey:n 3 r 1 l.Switch to plan B.[C]0 is waiting at Writer's Depot.Go to Writer's Depot:n 4 l 2 l.Pickup a passenger going to Cyclone.Go to Chop Suey:n 3 r 3 r.[D]Go to Cyclone:n 1 l 3 l.Pickup a passenger going to Post Office.Go to Post Office:s 1 l 2 r 1 l.

Try it online!

Try it online with linebreaks!

Doesn't return to the Taxi Garage after the program ends, so the boss fires me, and it exits with an error.

What this program actually does is checks whether or not the input consists of any repeating character, and outputs that character if it does, or 0 if it doesn't.

Alternative solution, 602 582 541 445 bytes

-20 bytes by eliminating linebreaks.

-41 bytes by having the program error out upon a false result.

-96 bytes by having the program output via error message.

Go to Post Office:w 1 l 1 r 1 l.Pickup a passenger going to Chop Suey.Go to Chop Suey:n 1 r 1 l 4 r 1 l.Pickup a passenger going to Cyclone.[B]Switch to plan C if no one is waiting.Pickup a passenger going to Crime Lab.Go to Cyclone:n 1 l 3 l.Pickup a passenger going to Crime Lab.Pickup a passenger going to Crime Lab.Go to Crime Lab:n 2 r 2 r.Pickup a passenger going to Cyclone.Go to Fueler Up:n.Go to Chop Suey:n 3 r 1 l.Switch to plan B.[C]

Try it online!

Try it online with linebreaks!

This alternative solution errors out with The boss couldn't find your taxi in the garage. You're fired! if the input is a repdigit, and no outgoing passengers found if the input is not a repdigit (meaning this also works with 0...or 00, or 000...).

JosiahRyanW

Posted 2017-06-08T02:50:36.133

Reputation: 2 600

For the second program? Sure, I could exit another way. – JosiahRyanW – 2018-10-09T00:45:24.697

I know, I made the modification. – JosiahRyanW – 2018-10-09T00:49:26.560

Note that a program can output via exit code, so theoretically you could have it crash if not a repdigit and exit normally otherwise instead of outputting

– Jo King – 2018-10-09T00:55:56.150

Could it crash in two different ways depending on result? – JosiahRyanW – 2018-10-09T00:57:17.290

I think so? Relevant meta, but I don't see any reason not to allow it, since it is easily observable. Edit: ah, I realise it would be two different outputs to STDERR, so you're good

– Jo King – 2018-10-09T01:05:10.260

1

J, 6 bytes

1=#@~.

Try it online!

Take unique elements; does its length equal 1?

/:-:\:

Try it online!

Does ascending sorting order equal descending sorting order?

-:##{.

Try it online!

Does the first element, duplicated to the length, equal the original?

-:1&|.

Try it online!

Does the input, rotated once, equal itself?

Bubbler

Posted 2017-06-08T02:50:36.133

Reputation: 16 616

1

Pepe, 48 bytes

rrEEreeeEEeeeErEeREEeREEREEEEEEErEEEEreErEEEeReE

Outputs 1 if number is a repdigit, nothing otherwise.

Try it online!

Explanation

(for input 45. 52 and 53 are charcodes of digits)

rrEE          # Create label 0, implicitly push 0 to the second stack [] [0]
  reeeEEeeeE    # Print "1"
rEe           # Return

REEe          # Start: Take input as charcodes to the first stack [52,52,53] [0]
REE           # Create label I (1)
  REEEEEEE      # Move first item in first stack to the other [52,53] [0, 52]
  rEEEE         # Reset second stack pointer position [>52, 53] [>0, 52]
  reE           # If item in first stack equals the first item in the second stack (0), 
                #   call label 0. It will only be true if first stack is empty
                #   because it will implicitly give 0.
rEEEe           # Move second stack pointer to the end [>52, 52] [0, >52]
ReE           # Repeat if items are the same (52 == 52), end program otherwise
reeeEEeeee    # Optional: Print 0.

RedClover

Posted 2017-06-08T02:50:36.133

Reputation: 719

0

Python, 27 22 bytes:

Just to get things started.

lambda x:len(set(x))<2

Converts to a string, converts the string into a set of all distinct characters in that string, and checks whether there aren't multiple elements in the set.

5 bytes saved thanks to @LeakyNun's reminder.

Aidan F. Pierce

Posted 2017-06-08T02:50:36.133

Reputation: 1 365

Please specify the version of Python. – Leaky Nun – 2017-06-08T03:03:05.407

3@LeakyNun It's not customary to specify a version if the code works in all recent ones. – Dennis – 2017-06-08T03:04:38.993

@Dennis If it is Python 2, I can suggest a golf. – Leaky Nun – 2017-06-08T03:23:03.267

Now that the input specs are more lax, you can replace str(x) with x. – Leaky Nun – 2017-06-08T03:23:29.980

0

Cheddar, 16 bytes

s->s.len*s[0]==s

Try it online!

Integer input, 29 bytes

n->(s->s.len*s[0]==s)("%d"%n)

Try it online!

Leaky Nun

Posted 2017-06-08T02:50:36.133

Reputation: 45 011

0

Tcl, 53 bytes

proc r n {expr 1==1[string trim $n [string in $n 0]]}

"string trim" removes leading and trailing occurrences of all its second argument's characters from its first argument (usually used for whitespace). Second argument here is the first digit of $n. For a repdigit an empty string remains, and attaching that to 1 remains 1. For number 42424, 242 would remain after the trim, so 1 is not equal to 1242.

PS: attaching to 1 is a golfing-thing that saved two bytes versus comparing the trim with empty string.

avl42

Posted 2017-06-08T02:50:36.133

Reputation: 111

0

Pyke, 3 bytes

}t!

Try it here!

}   -   uniquify(input)
 t  -  ^[:-1]
  ! - not ^

Blue

Posted 2017-06-08T02:50:36.133

Reputation: 26 661

0

Noether, 39 bytes

""~bI~aL(ai/~c{bc/}{}{bc+~b}i1+~i)bL1=P

Try it here!

It takes input as a number enclosed in quotation marks.

This works by looping through the input string, and, at the start, adding the first character to the string, B. As it loops, if the current character in the input isn't in B, it is appended to B. At the end, if the number is a repdigit, B will only be one character long.

Outputs 1 for true, 0 for false.

Beta Decay

Posted 2017-06-08T02:50:36.133

Reputation: 21 478

0

PHP, 34 bytes

<?=preg_match('#^(.)\1*$#',$argn);

Try it online!

Jörg Hülsermann

Posted 2017-06-08T02:50:36.133

Reputation: 13 026

0

Excel, 31 bytes

=MOD(A2,10)*(10^LEN(A2)-1)/9=A2

Wernisch

Posted 2017-06-08T02:50:36.133

Reputation: 2 534

0

V, 13 bytes

ylÍ"/.
ñ/ä
d

Try it online!

Leaves a string of .s if truthy, nothing if falsy (empty strings are truthy in Vim/V and vice versa)

nmjcman101

Posted 2017-06-08T02:50:36.133

Reputation: 3 274

0

V, 6 bytes

ø^ˆ±*$

Try it online!

Hexdump:

00000000: f85e 88b1 2a24                           .^..*$

This uses a brand new operator that I haven't used in any PPCG answers before. The ø operator will count the number of matches of a given regex. In this case, the (compressed) regex is:

/^\(.\)\1*$/

That is, any character at the start of the line, followed by only that character until the end of the line.

James

Posted 2017-06-08T02:50:36.133

Reputation: 54 537

0

AWK, 33 bytes

BEGIN{FS=""}{$0=NF==gsub($1,"")}1

Try it online!

Replaces all characters in the input with the first character and compares the changed count to the total number of characters.

Robert Benson

Posted 2017-06-08T02:50:36.133

Reputation: 1 339

0

Swift , 53 Bytes

var s=readLine()!,r=Set(s.characters);r.count>1 ?0:1

Leena

Posted 2017-06-08T02:50:36.133

Reputation: 131

0

TI-BASIC, 17 16 bytes

log(1+.9X/fPart(.1X:Ans=int(Ans

Takes input on X. TI-BASIC is token based, all tokens are one byte.

Scott Milner

Posted 2017-06-08T02:50:36.133

Reputation: 1 806

0

CJam, 6 bytes

qL|,1=

Explanation:

q  e# Get the input: "1311211"
L| e# Get unique elements: "132"
,  e# Length: 3
1= e# Equal to 1: 0 (false)

Esolanging Fruit

Posted 2017-06-08T02:50:36.133

Reputation: 13 542

0

Batch, 177 bytes

@echo off
set s=
set z=
<nul set/p=.%1>t
for /f "usebackq" %%G in ('t')do set z=%%~zG
for /l %%G in (2,1,%z%)do call set s=1%%s%%
set/ar=%1%%%s%
if %r%==0 echo %r%&exit/b
echo 1

Not properly golfed, yet.

stevefestl

Posted 2017-06-08T02:50:36.133

Reputation: 539

0

Pushy, 5 bytes

sK=P#

Try it online!

                                              Falsy Example:      Truthy Example:
       \ Implicit: Input on stack             [22121]             [5555]
s      \ Split into digits                    [2, 2, 1, 2, 2]     [5, 5, 5, 5]
 K=    \ Pop last, compare with all others    [1, 1, 0, 1]        [1, 1, 1]
   P#  \ Print the stack's product            PRINT: 0            PRINT: 1

6 bytes

I prefer this method as it's sweet and simple, but unfortunately it's one byte longer:

suLtn#

Try it online!

        \ Implicit: Input on stack
s       \ Split into digits
 u      \ Make stack into a set of itself
  Lt    \ Get the stack length - 1
        \ This will be 0 for a valid repdigit, a positive value otherwise
    n   \ Boolean negate: this maps 0 -> 1, and everything else to 0.
     #  \ Print result.

FlipTack

Posted 2017-06-08T02:50:36.133

Reputation: 13 242

0

Tcl, 48 bytes

proc R n {expr 1[regsub -all ^(.)\\1* $n ""]==1}

Try it online!


Tcl, 52 bytes

proc R n {expr [llength [lsort -u [split $n ""]]]<2}

Try it online!

sergiol

Posted 2017-06-08T02:50:36.133

Reputation: 3 055

0

><>, 12 bytes

1l2=?n@:}=*!

Try it online!

Takes input through the -s flag and checks whether every digit is the same.

Jo King

Posted 2017-06-08T02:50:36.133

Reputation: 38 234

0

Pari/GP, 21 bytes

n->#Set(digits(n))==1

Try it online!

alephalpha

Posted 2017-06-08T02:50:36.133

Reputation: 23 988

0

SmileBASIC 3, 26 bytes

INPUT N$?N$[0]*LEN(N$)==N$

Takes input as a string, prints 0 for false and 1 for true. If we repeat the first character of the input length(input) times and that is equal to the input, then it's a repdigit.

snail_

Posted 2017-06-08T02:50:36.133

Reputation: 1 982

0

PHP, 42 Bytes

Try it online

(42 Bytes because i forgot Towel day on may 25)

Code

<?=!(($a=$argv)%str_repeat(1,strlen($a)));

Explanation

Every number like 11, 22222,5555555555 always will be dvisible by 1 times the number length it just checks that, if the % between the numbers is 0 outputs 1 else empty.

Francisco Hahn

Posted 2017-06-08T02:50:36.133

Reputation: 591

0

Forth (gforth), 87 bytes

: m 10 /mod swap ; : f 1 swap m >r begin ?dup 0> while m r@ = rot * swap repeat rdrop ;

Try it online!

Explanation

The algorithm is approximately:

  1. Divide by 10, take remainder and store on return stack
  2. Repeatedly divide by 10 and compare remainder to stored value
  3. When quotient equals 0, stop loop and clean return stack

Code Explanation

: m 10 /mod swap ;        \ helper to grab the quotient and remainder after dividing by 10

1 swap                    \ place 1 as a result value on the stack and move to the bottom
m >r                      \ divide by 10 and place remainder on the return stack
begin                     \ start an indefinite loop
  ?dup 0>                 \ duplicate if not equal to 0 and check if greater than 0
while                     \ if greater than 0 execute loop body, else go the end of the loop
  m r@ =                  \ divide by 10, check if remainder equals our check value
  rot *                   \ move the result value to the top and multiply by result
  swap                    \ move the result value back to the boottom
repeat                    \ end the loop body
rdrop                     \ clean the return stack

reffu

Posted 2017-06-08T02:50:36.133

Reputation: 1 361

0

Pyth, 4 3 bytes

!t{

Try it online!
-1 thanks to Erik the Outgolfer!

Takes input as a string.

hakr14

Posted 2017-06-08T02:50:36.133

Reputation: 1 295

1You don't need l. – Erik the Outgolfer – 2018-06-07T20:32:27.237

@EriktheOutgolfer huh. Not how i expected t to behave, but certainly useful. Thanks! – hakr14 – 2018-06-07T20:36:26.537

Some of Pyth's built-ins are type-overloaded. – Erik the Outgolfer – 2018-06-07T21:35:50.343

0

APL(NARS), 16 chars, 32 bytes

{(⍴w)=+/w=↑w←⍕⍵}

test:

  {(⍴w)=+/w=↑w←⍕⍵}¨1 2 23 33 93 9999999
 1  1  0  1  0  1 

RosLuP

Posted 2017-06-08T02:50:36.133

Reputation: 3 036

0

MathGolf, 3 bytes

▀£┴

Try it online!

Takes input as a string.

Explanation:

▀    Get unique characters
 £   Get length of list
  ┴  Is equal to one?

Jo King

Posted 2017-06-08T02:50:36.133

Reputation: 38 234

0

Pyt, 2 bytes

ą≡

Try it online!

Explanation:

   Implicit input
ą  Split digits
 ≡ Are all elements equal?

u_ndefined

Posted 2017-06-08T02:50:36.133

Reputation: 1 253

0

naz, 50 bytes

2a2x1v1r2x2v1x1f1r3x1v2e3x2v1e0m1o0x1x2f0m1a1o0x1f

Works for any input integer, provided it's passed as a file terminated with the control character STX (U+0002).

Explanation (with 0x commands removed)

2a2x1v                 # Set variable 1 equal to 2
1r2x2v                 # Read the first byte of input and store it in variable 2
1x1f                   # Function 1
    1r                 # Read a byte of input
      3x1v2e           # Jump to function 2 if it equals variable 1
            3x2v1e     # Jump back to the start of the function if it equals variable 2
                  0m1o # Otherwise, output 0
1x2f                   # Function 2
    0m1a1o             # Output 1
1f                     # Call function 1

sporeball

Posted 2017-06-08T02:50:36.133

Reputation: 461

0

Stax, 4 bytes

ë@╛α

Run and debug it

Unpacked

Eu%v!

Nanajnaiojneg

Posted 2017-06-08T02:50:36.133

Reputation: 11

0

Stax, 4 bytes

ë@╛α

Run and debug it

Unpacked

Eu%v!

Nanajnaiojneg

Posted 2017-06-08T02:50:36.133

Reputation: 11