| Bytes | Lang | Time | Link |
|---|---|---|---|
| 040 | AWK | 250303T160837Z | xrs |
| 047 | Tcl | 180415T234837Z | sergiol |
| 026 | JavaScript Node.js | 230612T152318Z | Fhuvi |
| 015 | Arturo | 230613T181316Z | chunes |
| 076 | Rockstar | 230613T150005Z | Shaggy |
| 001 | Thunno 2 | 230608T135041Z | The Thon |
| 110 | Slashalash | 180620T164201Z | Sophie S |
| 019 | Factor | 220305T202819Z | chunes |
| 241 | Excel | 180416T040441Z | l4m2 |
| nan | Scratch 3.0 | 210314T054236Z | lyxal |
| 001 | Vyxal | 210314T052545Z | lyxal |
| 021 | brainfuck | 210314T021631Z | Mitch Sc |
| 014 | Zsh extendedglob | 210313T204043Z | pxeger |
| 017 | Swift 24 / | 210312T135250Z | Daniel |
| 043 | Prolog | 200913T154022Z | user |
| 003 | Pip | 200913T074552Z | Razetime |
| 004 | Stax | 200220T174816Z | Nanajnai |
| 050 | naz | 200219T212632Z | sporebal |
| 002 | MATL 5 bytes or | 170614T053124Z | DrQuariu |
| 031 | Regex ECMAScript | 190208T174947Z | Grimmy |
| 060 | Pepe | 181008T143130Z | u-ndefin |
| 019 | Kotlin | 180606T050522Z | snail_ |
| 002 | Pyt | 181021T053334Z | u-ndefin |
| 048 | Pepe | 181020T182709Z | RedClove |
| 068 | Pascal FPC | 181008T185734Z | AlexRace |
| 003 | MathGolf | 181009T081547Z | Jo King |
| 032 | APLNARS | 181009T071433Z | user5898 |
| 006 | J | 181009T042327Z | Bubbler |
| 706 | Taxi | 181008T200118Z | JosiahRy |
| 093 | Whitespace | 181008T153519Z | Kevin Cr |
| 032 | QBasic | 180620T044535Z | DLosc |
| 003 | Pyth | 180607T202608Z | hakr14 |
| 087 | Forth gforth | 180607T183608Z | reffu |
| 042 | PHP | 180606T192741Z | Francisc |
| 026 | SmileBASIC 3 | 180416T055843Z | snail_ |
| 021 | Pari/GP | 180416T042429Z | alephalp |
| 012 | ><> | 180416T031952Z | Jo King |
| 038 | F# | 171104T142504Z | Jason Ha |
| 030 | Julia | 171102T215653Z | EricSher |
| 005 | Pushy | 171102T213723Z | FlipTack |
| 039 | Noether | 170608T102137Z | Beta Dec |
| 124 | Check | 170614T072553Z | Esolangi |
| 177 | Batch | 170614T093009Z | stevefes |
| 055 | QBasic 4.5 | 170608T113137Z | steenber |
| 019 | Smalltalk | 170614T033302Z | Donald R |
| 006 | CJam | 170614T034540Z | Esolangi |
| 028 | PHP | 170609T085533Z | Titus |
| 139 | COBOL | 170609T135304Z | SaggingR |
| 021 | Java | 170609T130206Z | Nathan M |
| 022 | Python | 170608T025338Z | praosyle |
| 028 | C# | 170608T085206Z | LiefdeWe |
| 031 | R | 170609T063921Z | Sven Hoh |
| 023 | PowerShell | 170609T010627Z | Tessella |
| 016 | TIBASIC | 170608T210923Z | Scott Mi |
| 015 | Cubix | 170608T205249Z | MickyT |
| 053 | Swift | 170608T183451Z | Leena |
| 005 | APL | 170608T105315Z | Uriel |
| 033 | AWK | 170608T183319Z | Robert B |
| 012 | Perl 6 | 170608T181927Z | Sean |
| 024 | ><> | 170608T131529Z | AGourd |
| 023 | QBIC | 170608T053751Z | steenber |
| 025 | R | 170608T163512Z | user2390 |
| 006 | V | 170608T151750Z | DJMcMayh |
| 023 | Java | 170608T115431Z | Olivier |
| 012 | Clojure | 170608T081141Z | NikoNyrh |
| 013 | V | 170608T123156Z | nmjcman1 |
| 017 | grep | 170608T123121Z | Toby Spe |
| 038 | C# | 170608T123055Z | TheLetha |
| 038 | C | 170608T122626Z | Toby Spe |
| 031 | Excel | 170608T122009Z | Wernisch |
| 034 | PHP | 170608T105423Z | Jör |
| 004 | Ohm | 170608T111351Z | Datboi |
| 025 | PHP | 170608T094424Z | Christop |
| 003 | Pyke | 170608T100249Z | Blue |
| 021 | JavaScript ES6 | 170608T063251Z | Arnauld |
| 053 | Tcl | 170608T082153Z | avl42 |
| 004 | Japt | 170608T071530Z | Tom |
| 006 | Braingolf | 170608T075944Z | Mayube |
| 027 | Mathematica | 170608T074325Z | Martin E |
| 001 | Neim | 170608T061509Z | Okx |
| 1312 | Ruby | 170608T073324Z | G B |
| 030 | PHP | 170608T063908Z | user6395 |
| 001 | Brachylog | 170608T030437Z | Leaky Nu |
| 023 | Python 3 | 170608T053512Z | musicman |
| 011 | Octave | 170608T052454Z | Stewie G |
| 019 | Python 3 | 170608T050008Z | Shadow |
| 001 | Jelly | 170608T030309Z | Dennis |
| 041 | C gcc | 170608T034427Z | Doorknob |
| 029 | C gcc | 170608T040709Z | Dennis |
| 001 | 05AB1E | 170608T035155Z | Neil A. |
| 015 | Haskell | 170608T034100Z | xnor |
| 016 | Cheddar | 170608T032234Z | Leaky Nu |
AWK, 40 bytes
{for(x=1;i++<NF-1;)$i~$(i+1)||x=0}1,$0=x
To test:
awk -F "" '{for(x=1;i++<NF-1;)$i~$(i+1)||x=0}1,$0=x' <<< 1111
Tcl, 47 bytes
proc R n {expr 1[regsub -all ^(.)\\1* $n ""]<2}
proc R n {expr [llength [lsort -u [split $n ""]]]<2}
JavaScript (Node.js), 26 27 bytes
(Solutions without regex. They all take input as a string. Luckily for some of them, OP guaranteed that the input can't be zero)
-1 byte thanks to @Shaggy
Checking that there is no character different from the first :
s=>![...s].some(c=>c-s[0])
Checking that every character is the same as the first (27 bytes) :
s=>[...s].every(c=>c==s[0])
Checking that the parameter is equal to the first character repeated parameter.length times (27 bytes) :
s=>s==s[0].repeat(s.length)
Checking that the parameter divided by its first character is equal to 1 repeated parameter.length times (31 bytes) :
s=>s/s[0]=="1".repeat(s.length)
More mathematical ways (could probably be a lot shorter in other languages ?) :
There is a special rule that only repdigits follow.
For example, 4444 +1 in base 4+1 gives 10000 in the same base, and we can recognize this particular pattern (still without using regex).
Using the same example, checking that 10000 (made from concatenation and repetition) from base 4+1 to base 10, minus 4444 (the parameter) with same base conversion, gives exactly 1. The 4 for the base conversion comes from the first character of the parameter (63 bytes) :
s=>parseInt(1+"0".repeat(s.length),a=s[0]*1+1)-parseInt(s,a)==1
Using the same example, checking that 4444 (the parameter) from base 4+1 to base 10, plus 1, converted back to its original base, gives a number that when inverted from right to left (to get rid of the previously trailing zeroes), is exactly 1. The 4 for the base conversion comes from the highest digit of the parameter (76 bytes) :
s=>[...(parseInt(s,a=Math.max(...s)+1)+1).toString(a)].reverse().join``*1==1
Arturo, 15 bytes
$->x[{}=x--x\0]
$->x[ ; a function taking an input x
{}= ; is the empty string equal to...
x--x\0 ; x without its first element
] ; end function
Rockstar, 76 bytes
listen to N
cut N
roll N in D
O's 1
while N and O
let O be D's roll N
say O
Try it (Code will need to be pasted in)
Slashalash, 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
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.
Excel, 24 bytes (assuming Tab is 1 byte)
=LEFT(B1,1);=A1&B1=B1&A1
Excel, 28 27 bytes
=A1=MID(A1,2,A1)&LEFT(A1,1)
Scratch 3.0, 20 blocks/148 bytes
define(z)
set[o v]to(1
set[i v]to(1
repeat(length of(z)
if <not<(letter(i)of(z))=(letter(1)of(z))>>then
set[o v]to(0
end
set[i v]to((i)+(1
end
say(o
I did the most logical thing and answer this with scratch. Essentially, we loop through every character and see if it equals the first character. If any character doesn't equal the first, the output is set to falsey
brainfuck, 21 bytes
,<,[[>->+<<-]>[>],]>.
With a little formatting:
,<,
[
[>->+<<-]
>[>]
,
]
>.
Prints the repeated digit for true, and NUL for false.
Compares adjacent digits in a loop, and in the case of inequality, shifts pointer so that all subsequent comparisons will also result in inequality, and the cell to the right will be zero upon exiting the loop.
Variant for stricter output rules, 25 bytes
,<,[[>->+<<-]>[>>->],]<+.
Formatted:
,<,
[
[>->+<<-]
>[>>->]
,
]
<+.
This version prints \x01 for true.
Zsh --extendedglob, 14 bytes
>$1
: ^{1..9}#
Outputs via exit code: 0 is not a repdigit, 1 is a repdigit.
>$1- create the file according to the input^{1..9}#- construct the string^1# ^2# ... ^9#- then find a file matching all of the patterns
^1#^2#...^9##means zero or more of the preceding digit (but since a file must be at least 1 character, it actually means 1 or more)^means logical NOT- by DeMorgan's law, this creates a logical OR of the patterns
^1#^2#...^9#(just with inverted output, but we can cope with that)
- by DeMorgan's law, this creates a logical OR of the patterns
- if no file matching all of these patterns exists - that is, the file is not not a repeated digit - there will be an error and the exit code will be
1.
:- and do nothing with it (otherwise it would try to execute the file as a command, which would also exit with an error)
--extendedglob is required to enable the ^ and # glob operators.
Swift (24 / 17 bytes)
Console App (24 bytes):
Set(readLine()!).count<2
Function (17 bytes):
{Set($0).count<2}
Usage example:
let isRepdigit: (String) -> Void = {Set($0).count<2}
print(isRepdigit("234")) // prits false
print(isRepdigit("66666")) // prints true
Prolog, 43 bytes
f(N,D):-N==0;O is N//10,D is N-O*10,f(O,D).
Run the query with f(666,D) (or whatever other number you have). If it's a repdigit, it will return a value for D, otherwise, it will return false.
Try it in SWISH (By the way, I think if you make changes directly without creating a new notebook, it may modify the original, so please don't mess with it :))
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
MATL (6 5 bytes or 2 bytes for Luis Mendo solution)
Vun1=
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.
Regex (ECMAScript), 31 bytes
^(x{0,9})((x+)\3{8}(?=\3$)\1)*$
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
Pepe, 69 60 bytes
REeErEEEEEREEEeeEREEEEeEeEREEEEEEeREEEeREEEEEerrEEreEErEereE
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
Kotlin, 28 19 bytes
{it.toSet().size<2}
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}
Pepe, 48 bytes
rrEEreeeEEeeeErEeREEeREEREEEEEEErEEEEreErEEEeReE
Outputs 1 if number is a repdigit, nothing otherwise.
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.
Pascal (FPC), 81 75 68 bytes
var n:string;begin read(n);write(n=StringOfChar(n[1],length(n)))end.
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.
$64, $B and $A are hexadecimal constants, they eliminate some whitespace that would be needed for their decimal counterparts.
MathGolf, 3 bytes
▀£┴
Takes input as a string.
Explanation:
▀ Get unique characters
£ Get length of list
┴ Is equal to one?
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
J, 6 bytes
1=#@~.
Take unique elements; does its length equal 1?
/:-:\:
Does ascending sorting order equal descending sorting order?
-:##{.
Does the first element, duplicated to the length, equal the original?
-:1&|.
Does the input, rotated once, equal itself?
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 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 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...).
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
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.
Forth (gforth), 87 bytes
: m 10 /mod swap ; : f 1 swap m >r begin ?dup 0> while m r@ = rot * swap repeat rdrop ;
Explanation
The algorithm is approximately:
- Divide by 10, take remainder and store on return stack
- Repeatedly divide by 10 and compare remainder to stored value
- 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
PHP, 42 Bytes
(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.
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.
><>, 12 bytes
1l2=?n@:}=*!
Takes input through the -s flag and checks whether every digit is the same.
Julia, 30 bytes
f(n)=length(Set(string(n)))==1
Pushy, 5 bytes
sK=P#
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#
\ 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.
Noether, 39 bytes
""~bI~aL(ai/~c{bc/}{}{bc+~b}i1+~i)bL1=P
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.
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:
- Read input and call it i.
- Create an empty array and store it in the register. (
[r) - If i is 0, go to step 13.
- Take the number modulo 10. Call it x.
- Prepend x to the register and store it back in the register.
- Subtract x from i.
- Create a counter, starting at 0.
- If i is 0, go to step 12.
- Increment the counter.
- Decrement i by 10.
- Go back to step 8.
- Set i to whatever value is now in the counter.
- Go back to step 3.
- Load the register, which is now an array containing the digits of the input. Call this d.
- Get the first element in d and store it in the register.
- Create a counter called j, initialized to the length of d.
- Decrement j.
- 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.)
- Otherwise, print some random junk !j times. This only prints something if j == 0.
- Create an array of length 1 and get the !jth element. This will crash the program if j == 0.
- If the program has not yet crashed, j must not yet be zero, so go back to step 17.
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.
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.
Smalltalk, 21 19 bytes
[:s|s asSet size<2]
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.
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)
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.
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).
Java, 21 bytes:
l->l.toSet().size()<2
l is a MutableList<Character> from eclipse collections.
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.
C#, 42 33 28 bytes
i=>i.Replace(i[0]+"","")==""
i has to be a string.
Shaved down a lot thanks to @LethalCoder
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
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).
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.
Cubix, 15 bytes
uOn@ii?-?;.$@<_
u O
n @
i i ? - ? ; . $
@ < _ . . . . .
. .
. .
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
Swift , 53 Bytes
var s=readLine()!,r=Set(s.characters);r.count>1 ?0:1
AWK, 33 bytes
BEGIN{FS=""}{$0=NF==gsub($1,"")}1
Replaces all characters in the input with the first character and compares the changed count to the total number of characters.
Perl 6, 12 bytes
{[==] .comb}
Reduces the list of characters in the input number with the numeric equality operator.
><>, 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
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
R, 25 bytes
grepl("^(.)\\1*$",scan())
Best non-regex solution I could come up with was 36 bytes:
is.na(unique(el(strsplit(x,"")))[2])
V, 6 bytes
ø^±*$
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.
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.
Saves
- -5 bytes: used
Stringsince "You may take and use input as a string with impunity." - -10 bytes: regex is apparently a good fit.
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 %))
V, 13 bytes
ylÍ"/.
ñ/ä
d
Leaves a string of .s if truthy, nothing if falsy (empty strings are truthy in Vim/V and vice versa)
grep, 17 bytes
grep -xP '(.)\1*'
Matches any string that's a repetition of its first character.
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
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");
}
Excel, 31 bytes
=MOD(A2,10)*(10^LEN(A2)-1)/9=A2
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.
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))
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.
Braingolf, 6 bytes
iul1-n
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.
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.
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.
PHP, 30 bytes
<?=($a=$argn).$a[0]==$a[0].$a;
Brachylog, 1 byte
=
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]).
Python 3, 23 bytes
lambda s:s==s[0]*len(s)
Not shorter than @shadow's answer, but I thought it was interesting. Should work in Python 2 as well.
Octave, 11 bytes
@(s)s==s(1)
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.
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.
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.
Thanks to @Dennis for indirectly reminding me of the assign-instead-of-return trick via his insanely impressive answer, saving 4 bytes!
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]
