Cuerdas divisibles
Tarea
Dada una cadena s, genera un valor de verdad si el código ASCII de cada letra es divisible por la longitud de s, y un falsey en caso contrario.
De entrada y salida
La entrada es una cadena no vacía que contiene solo ASCII [32-126]. La salida es un valor estándar de verdad / falsedad. Tenga en cuenta que puede cambiar los valores, por ejemplo, devolver 0/ Falsesi es divisible y viceversa
Casos de prueba
Input Output
Hello False (72 101 108 108 111), 5
lol True (108 111 108), 3
Codegolf False (67 111 100 101 103 111 108 102), 8
A True (65), 1
nope False (110 111 112 101),4
8 8 True (56 32 32 56), 4
Respuestas
MATL , 4 bytes
tn\~
- Para cadenas divisibles, la salida es un vector que contiene solo
1s, que es veraz . - De lo contrario, la salida es un vector que contiene varios
1sy al menos uno0, que es falso .
¡Pruébelo en línea! O verifique todos los casos de prueba, incluida la prueba de veracidad / falsedad.
Cómo funciona
t % Implicit input. Duplicate
n % Number of elements
\ % Modulo
~ % Negate. Implicit display
Befunge-98 (FBBI) , 31 bytes
La salida es a través del código de salida, 1para veracidad, 0para casos falsos.
#v~\1+
v>53p
>:#v_1q
^ >' %#@_
¡Pruébelo en línea!
Código que se ejecuta con entradas loly ab:
los números pequeños representan valores de bytes literales
Haskell , 42 39 bytes
(<1).sum.(map=<<flip(mod.fromEnum).length)
f s=sum[fromEnum c`mod`length s|c<-s]<1
¡3 bytes menos gracias a ovs y xnor!
¡Pruébelo en línea!
05AB1E , 5 bytes
ÇsgÖP
¡Pruébelo en línea!
Comentado
# implicit input "lol"
Ç # push ASCII value [108, 111, 108]
s # swap (with input) [108, 111, 108], "lol"
g # length [108, 111, 108], 3
Ö # is divisible? [1, 1, 1]
P # product 1
Rockstar , 205 192 175 162 bytes
Bueno, esto fue divertido. Rockstar no tiene forma de leer la longitud de una cadena directamente, no puede convertir caracteres en puntos de código y no tiene operador de módulo. ¡Sorprendido que haya funcionado este corto!
listen to S
cut S
X's0
D's0
while S at X
N's32
while N-127
cast N into C
if C is S at X
let M be N/S
turn down M
let D be+N-S*M
let N be+1
let X be+1
say not D
Pruébelo aquí (deberá pegar el código)
Pyth , 8 bytes
!sm%CdlQ
¡Pruébelo en línea!
!sm%CdlQ
m : map implicit input on
: lambda d:
Cd : Ascii value of d
% lQ : mod length of input
s : sum result of map
! : logical negate it
JavaScript, 32 bytes
Ouput se invierte.
s=>Buffer(s).some(c=>c%s.length)
¡Pruébelo en línea!
PHP , 56 52 bytes
for(;$c=ord($argn[$i++]);$c%strlen($argn)?die(f):1);
¡Pruébelo en línea!
La salida se invierte
La ejecución se detiene fsi algún carácter no es divisible, o una cadena vacía (falsa en PHP) si todos son divisibles
EDITAR: 4 bytes guardados gracias a @ 640KB
Python 2 , 41 39 bytes
lambda s:all(ord(i)%len(s)<1for i in s)
¡Pruébelo en línea!
-2 bytes gracias a @ovs
K (OK) , 11 bytes
{~+/(#x)!x}
¡Pruébelo en línea!
Óxido , 36 bytes
|s|s.iter().all(|x|1>x%s.len()as u8)
¡Pruébelo en línea!
Toma la entrada como a &[u8], salidas a bool.
Pip , 12 bytes
!$+(A_Ma)%#a
¡Pruébelo en línea!
Explicación
!$+(A_Ma)%#a a → input
(A_Ma) Map a to Unicode/ASCII codepoints
%#a Modulo the list by it's length
$+ Sum up the remainders
! Not(returns 0 for any positive number, 1 for 0)
Rubí , 43 37 36 32 bytes
->a{a.bytes.all?{|n|n%a.size<1}}
si solo el mapa pudiera usarse en cadenas ...
-10 bytes de ovs.
-1 byte de Dingus.
¡Pruébelo en línea!
Perl 5 -pF , 20 bytes
$_=!grep ord()%@F,@F
¡Pruébelo en línea!
C (gcc) , 54 53 bytes
l;r;f(char*s){l=strlen(s);for(r=0;*s;)r|=*s++%l;l=r;}
¡Pruébelo en línea!
Devuelve falsey si el valor ASCII de cada carácter es divisible por la longitud de la cadena de entrada o verdadero en caso contrario.
Explicación:
l;r;f(char*s){l=strlen(s);for(r=0;*s;)r|=*s++%l;l=!r;}
l;r; // Declare 2 int variables
f( // Function f taking
char*s){ // string parameter s
l=strlen(s); // Store length of s in l
for( // Loop
r=0; // initialising r to 0
*s;) // until end of s
r|= // Bitwise or r with
*s // the ASCII value of the next
// character...
++ // Aside: push s pointer forward
%l; // ... mod the string length
r=l; // Return r (r will be 0
// iff every character was
// divisible by l)
Wolfram Language (Mathematica) , 40 bytes
{0}==##&@@ToCharacterCode@#~Mod~Tr[1^#]&
¡Pruébelo en línea!
gracias a @att por guardar algunos bytes
APL (Dyalog Extended) , 7 bytes ( SBCS )
Función de prefijo tácito anónimo
⍱≢|⎕UCS
¡Pruébelo en línea!
⍱ ¿No es cierto alguno de los siguientes (distinto de cero)?
≢ la longitud
| divide (lit. resto de división al dividir)
⎕UCS los puntos del código
C # (.NET Core) , 25 bytes
a=>a.All(x=>x%a.Length<1)
¡Pruébelo en línea!
Japonés -e , 6 bytes
c vNÎÊ
Intentalo
MathGolf , 4 bytes
$h÷╓
Ingrese como una lista de caracteres.
Pruébelo en línea.
Explicación:
$ # Get the codepoint of each character in the (implicit) input-list
h # Push the length of this list (without popping the list itself)
÷ # Check for each codepoint if it's divisible by this length
╓ # Pop and push the minimum of the list
# (after which the entire stack joined together is output implicitly as result)
Gelatina , 4 bytes
LḍOP
¡Pruébelo en línea! o ¡ Verifique todos los casos!
Comentado: (Al menos creo que funciona así)
P # product of ...
L # does the length
ḍ # ... divide ...
O # the char codes
R , 39 38 bytes
Editar: -1byte gracias a la nueva regla que podemos generar TRUE para FALSE y FALSE para TRUE
function(s)any(utf8ToInt(s)%%nchar(s))
¡Pruébelo en línea!
O pruebe la versión original de 39 bytes que genera VERDADERO por VERDADERO ...
Clojure, 41 caracteres
(every? #(= 0 (mod (int %) (count x))) x)
Eliminar espacios después del comentario 37 caracteres
(every? #(= 0(mod(int %)(count x)))x)
MAWP , 34 33 24 23 bytes
`|_=M0=A0/[M%{0:.}?`]1:
¡Intentalo!
¡Gracias a @Razetime por ahorrar 9 bytes!
Explicación:
` Remove starting 1 on stack
| Push input on stack as ASCII codes
_=M Set variable M to length of stack (length of input)
0=A Set variable A to 0
0/ Push 0 and cycle stack
[ Start of loop
M% Modulo by M
{0:.} If not 0 then print 0 and terminate
?` If 0 then pop value
] End of loop
1: Print 1
Brachylog , 8 bytes
ạfᵐ∋ᵛ~l?
¡Pruébelo en línea!
ạfᵐ∋ᵛ~l?
ạ characters to integer
fᵐ find all factors
∋ᵛ every list of factors contain …
~l? the length of the input
Versión alternativa,
⟨ạzl⟩%ᵛ0
⟨fhg⟩ forks! fA & gB ∧ [A, B]h
ạzl zip the code blocks with the length;
[[108, 3], [111, 3], [108, 3]]
%ᵛ0 every list must be 0 after modulo
Python 3 , 55 52 bytes
N=input();print(not sum([ord(i)%len(N) for i in N]))
¡Pruébelo en línea!
GolfScript , 20 bytes
.,0@{(3$%@+\}3$*;!\;
¡Pruébelo en línea!
Esto genera 1 si la cadena es divisible y 0 si no lo es. Sea S la cuerda y L su longitud.
.,0@ # The stack from bottom up will be: L 0 S
{ }3$* # Execute this block L times ( # Separate first char from the string as a number 3$% # Previous number mod L
@+\ # Add result to the acumulator
; # Discard the ""
! # 1 iff the acumulator is 0
\; # Discard L
Carbón , 8 bytes
¬⊙θ﹪℅ιLθ
¡Pruébelo en línea! El enlace corresponde a la versión detallada del código. La salida es un booleano Charcoal, es decir -, verdadero, nada para falso. Explicación:
θ Input string
⊙ Is there a character where
ι Current character
℅ Ordinal
﹪ Modulo (i.e. is not divisible by)
θ Input string
L Length
¬ Boolean NOT
Implicitly print
⬤θ¬﹪℅ιLθ también funciona por supuesto.
Factor , 62 bytes
: f ( s -- ? ) dup length [ mod ] curry [ + ] map-reduce 0 = ;
¡Pruébelo en línea!
C # (compilador interactivo de Visual C #) , 81 bytes
(s)=>{var bs = ASCIIEncoding.ASCII.GetBytes(s);return bs.All(b=>b%s.Length==0);};
¡Pruébelo en línea!
C # (compilador interactivo de Visual C #) ,
27
26 bytes
s=>s.All(c=>c%s.Length<1);
¡Pruébelo en línea!