Reto
Un repdigit es un número entero no negativo cuyos dígitos son todos iguales.
Cree una función o un programa completo que tome un entero entero como entrada y genere un valor verdadero si el número de entrada es un repdigit en la base 10 y un valor falso de lo contrario.
Se garantiza que la entrada sea un número entero positivo .
Puede tomar y usar input como una representación de cadena en la base 10 con impunidad.
Casos de prueba
Todos estos son dígitos por debajo de 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
Se puede encontrar una lista más grande en OEIS .
Victorioso
El código más corto en bytes gana. Eso no quiere decir que las respuestas inteligentes en idiomas detallados no serán bienvenidas.
code-golf
math
number
arithmetic
decision-problem
Aidan F. Pierce
fuente
fuente
Respuestas:
Brachylog , 1 byte
Pruébalo en línea!
Esto actúa en enteros.
De
src/predicates.pl#L1151
:fuente
C (gcc) ,
333029 bytesPruébalo en línea!
fuente
return
(think I'm going to steal the latter for my answer :) ).-O0
will write final result ton
from exactlyeax
, so as to make it the return value? Could you elaborate on the logic why you knew it would work?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.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:
A is defined as a PIC 9(4).
fuente
TRUE
andFALSE
to 1 and 0 respectively05AB1E, 1 byte
Checks if all digits are equal
Try it online!
fuente
Python 3,
25, 2419 bytes.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.
fuente
>1
rather than<2
. Raising an actual error would be shorter than usingexit
btw.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.*
unpacks an iterable. For example,{*'123'}
generates the set{'1','2','3'}
.Mathematica, 27 bytes
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.
fuente
Haskell, 15 bytes
Try it online! Takes string input.
Equivalent to
\s->all(==head s)s
. Narrowly beats out alternatives:fuente
f s=(s<*s)==(s*>s)
is a very interesting idea, I wasn't aware of this behaviour of<*
before.C (gcc), 41 bytes
This is a function that takes input as a string and returns
1
if it is a repdigit and0
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 bestrlen(s)
. Then, indexing intos
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 whethers
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!
fuente
strlen
and creating a new string from*s
:c;f(char*s){c=*s;c=!s[strspn(s,&c)];}
for 37.PHP,
252825remove all chars from the right that are equal to the first and print
1
if all chars were removed.fuente
R, 31 bytes
This functions works with string inputs and uses a regular expression to determine whether the input is a repdigit.
Example
fuente
///, 110 bytes
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.
fuente
Jelly,
21 byteTry it online!
fuente
1
-9
count as repdigits?Octave, 11 bytes
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 one0
(false in Octave). Here's a proof.fuente
all(...)
to get a truthy/falsy value output?grep, 17 bytes
Matches any string that's a repetition of its first character.
fuente
C#,
423328 bytesi
has to be a string.Shaved down a lot thanks to @LethalCoder
fuente
i[0].ToString()
can be shortened toi[0]+""
,<1
is shorter than==0
..Length<1
can just be==""
Braingolf, 6 bytes
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:
After
u
, the length of the stack equals the number of unique characters in the input, subtracting 1 means it will be0
if and only if there is exactly 1 unique character in the input,0
is the only falsey number in Python, son
will replace0
with1
, and everything else with0
.fuente
Japt, 4 bytes
Try it online!
fuente
JavaScript (ES6),
2321 bytesSaved 2 bytes thanks to Neil
Takes input as either an integer or a string. Returns a boolean.
Demo
Show code snippet
fuente
test
instead of!!exec
save 2 bytes?Ohm, 4 bytes
Try it online!
Explanation
fuente
Ul≤
should work.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".Ul1E
should also work (though I don't know Ohm) because it doesn't need to handle 0.APL, 5 bytes
2 bytes saved thanks to @KritixiLithos
Try it online!
fuente
⊢≡1⌽⊢
.⊢
with⍕
to handle both strings and numbers.Java, 21 bytes:
l
is aMutableList<Character>
from eclipse collections.fuente
l
could also be a CharAdapter.Kotlin,
2819 bytesTry it online!
Takes input as a
String
becauseExplanation
If you don't like the fact it takes a
String
, you can have one that takes anInt
for 24 bytes.fuente
Regex (ECMAScript), 31 bytes
Try it online!
Takes input in unary, as usual for math regexes (note that the problem is trivial with decimal input: just
^(.)\1*$
).Explanation:
fuente
PHP, 30 bytes
fuente
Neim, 1 byte
Simply checks that all elements are equal.
Without builtin, 2 bytes:
Explanation:
This works because only
1
is considered truthy in Neim, and everything else is falsy.Alternatively, for 4 bytes:
Explanation:
Try it!
fuente
C, 38 bytes
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
fuente
Java,
383323 bytesn
is aString
, naturally.Note that there is no need for
^...$
in the regex since it's automatically used for exact matching (such as thematch
method), compared to finding in the string.Try it!
Saves
String
since "You may take and use input as a string with impunity."fuente
matches
not requiring^$
because it matches the entire String. So a definite +1 from me. ;)R, 25 bytes
Try it online
Best non-regex solution I could come up with was 36 bytes:
fuente
rle(charToRaw(scan(,'')))$v[2]<1
Cubix, 15 bytes
Try it online!
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
fuente
QBasic 4.5, 55 bytes
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.
fuente