Stringhe divisibili
Compito
Data una stringa s, restituisce un valore true se il codice ASCII di ciascuna lettera è divisibile per la lunghezza di s, altrimenti false.
Input Output
L'input è una stringa non vuota contenente solo ASCII [32-126]. L'output è un valore standard di verità / falsità. Nota che puoi cambiare i valori, ad esempio restituendo 0/ Falsese divisibile e viceversa
Casi test
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
Risposte
MATL , 4 byte
tn\~
- Per le stringhe divisibili l'output è un vettore contenente solo
1s, il che è vero . - Altrimenti l'output è un vettore contenente diversi se
1almeno uno0, il che è falso .
Provalo online! Oppure verifica tutti i casi di test, inclusi i test di veridicità / falsità.
Come funziona
t % Implicit input. Duplicate
n % Number of elements
\ % Modulo
~ % Negate. Implicit display
Befunge-98 (FBBI) , 31 byte
L'output avviene tramite codice di uscita, 1per casi veritieri, 0per falsi.
#v~\1+
v>53p
>:#v_1q
^ >' %#@_
Provalo online!
Codice in esecuzione con ingressi lole ab:
i numeri piccoli rappresentano valori di byte letterali
Haskell , 42 39 byte
(<1).sum.(map=<<flip(mod.fromEnum).length)
f s=sum[fromEnum c`mod`length s|c<-s]<1
3 byte in meno grazie a ovs e xnor!
Provalo online!
05AB1E , 5 byte
ÇsgÖP
Provalo online!
Commentato
# 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 byte
Bene, questo è stato divertente. Rockstar non ha modo di leggere direttamente la lunghezza di una stringa, non può convertire i caratteri in punti di codice e non ha un operatore modulo. Sorpreso che abbia funzionato così 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
Provalo qui (il codice dovrà essere incollato)
Pyth , 8 byte
!sm%CdlQ
Provalo online!
!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 byte
L'uscita è invertita.
s=>Buffer(s).some(c=>c%s.length)
Provalo online!
PHP , 56 52 byte
for(;$c=ord($argn[$i++]);$c%strlen($argn)?die(f):1);
Provalo online!
L'uscita è invertita
L'esecuzione si interrompe con fse un carattere non è divisibile o una stringa vuota (falsa in PHP) se tutti sono divisibili
EDIT: salvato 4 byte grazie a @ 640KB
Python 2 , 41 39 byte
lambda s:all(ord(i)%len(s)<1for i in s)
Provalo online!
-2 byte grazie a @ovs
K (oK) , 11 byte
{~+/(#x)!x}
Provalo online!
Ruggine , 36 byte
|s|s.iter().all(|x|1>x%s.len()as u8)
Provalo online!
Prende l'input come a &[u8], restituisce a bool.
Pip , 12 byte
!$+(A_Ma)%#a
Provalo online!
Spiegazione
!$+(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)
Ruby , 43 37 36 32 byte
->a{a.bytes.all?{|n|n%a.size<1}}
se solo la mappa potesse essere usata sulle stringhe ..
-10 byte da ovs.
-1 byte da Dingus.
Provalo online!
Perl 5 -pF , 20 byte
$_=!grep ord()%@F,@F
Provalo online!
C (gcc) , 54 53 byte
l;r;f(char*s){l=strlen(s);for(r=0;*s;)r|=*s++%l;l=r;}
Provalo online!
Restituisce falsey se il valore ASCII di ogni carattere è divisibile per la lunghezza della stringa di input o true altrimenti.
Spiegazione:
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 byte
{0}==##&@@ToCharacterCode@#~Mod~Tr[1^#]&
Provalo online!
grazie a @att per aver salvato alcuni byte
APL (Dyalog Extended) , 7 byte ( SBCS )
Funzione di prefisso tacito anonimo
⍱≢|⎕UCS
Provalo online!
⍱ non è vero nessuno dei seguenti (diverso da zero)?
≢ la lunghezza
| divide (lett. resto della divisione durante la divisione)
⎕UCS i punti di codice
C # (.NET Core) , 25 byte
a=>a.All(x=>x%a.Length<1)
Provalo online!
Japt -e , 6 byte
c vNÎÊ
Provalo
MathGolf , 4 byte
$h÷╓
Inserimento come elenco di caratteri.
Provalo online.
Spiegazione:
$ # 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 byte
LḍOP
Provalo online! o Verifica tutti i casi!
Commmented: (Almeno penso che funzioni così)
P # product of ...
L # does the length
ḍ # ... divide ...
O # the char codes
R , 39 38 byte
Modifica: -1byte grazie alla nuova regola che possiamo produrre TRUE per FALSE e FALSE per TRUE
function(s)any(utf8ToInt(s)%%nchar(s))
Provalo online!
Oppure prova la versione originale a 39 byte che restituisce TRUE per TRUE ...
Clojure, 41 caratteri
(every? #(= 0 (mod (int %) (count x))) x)
Rimozione di spazi dopo il commento 37 caratteri
(every? #(= 0(mod(int %)(count x)))x)
MAWP , 34 33 24 23 byte
`|_=M0=A0/[M%{0:.}?`]1:
Provalo!
Grazie a @Razetime per aver salvato 9 byte!
Spiegazione:
` 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 byte
ạfᵐ∋ᵛ~l?
Provalo online!
ạfᵐ∋ᵛ~l?
ạ characters to integer
fᵐ find all factors
∋ᵛ every list of factors contain …
~l? the length of the input
Versione 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 byte
N=input();print(not sum([ord(i)%len(N) for i in N]))
Provalo online!
GolfScript , 20 byte
.,0@{(3$%@+\}3$*;!\;
Provalo online!
Questo restituisce 1 se la stringa è divisibile e 0 se non lo è. Sia S la stringa e L la sua lunghezza.
.,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
Carboncino , 8 byte
¬⊙θ﹪℅ιLθ
Provalo online! Il collegamento è alla versione dettagliata del codice. L'output è un booleano Charcoal, ovvero -vero, niente per falso. Spiegazione:
θ 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θ funziona anche ovviamente.
Fattore , 62 byte
: f ( s -- ? ) dup length [ mod ] curry [ + ] map-reduce 0 = ;
Provalo online!
C # (Visual C # Interactive Compiler) , 81 byte
(s)=>{var bs = ASCIIEncoding.ASCII.GetBytes(s);return bs.All(b=>b%s.Length==0);};
Provalo online!
C # (Visual C # Interactive Compiler) ,
27
26 byte
s=>s.All(c=>c%s.Length<1);
Provalo online!