Stringhe divisibili

Sep 09 2020

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

3 LuisMendo Sep 09 2020 at 15:18

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 uno 0, 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
8 ovs Sep 09 2020 at 15:57

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

8 rak1507 Sep 09 2020 at 17:20

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!

7 ovs Sep 09 2020 at 15:06

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
5 Shaggy Sep 10 2020 at 16:17

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)

4 Mukundan314 Sep 09 2020 at 17:35

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
3 Shaggy Sep 09 2020 at 15:57

JavaScript, 32 byte

L'uscita è invertita.

s=>Buffer(s).some(c=>c%s.length)

Provalo online!

3 Kaddath Sep 10 2020 at 21:30

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

2 Dion Sep 09 2020 at 15:06

Python 2 , 41 39 byte

lambda s:all(ord(i)%len(s)<1for i in s)

Provalo online!

-2 byte grazie a @ovs

2 GalenIvanov Sep 09 2020 at 15:34

K (oK) , 11 byte

{~+/(#x)!x}

Provalo online!

2 madlaina Sep 09 2020 at 16:48

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.

2 Razetime Sep 09 2020 at 17:13

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)
2 Razetime Sep 09 2020 at 17:39

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!

2 NahuelFouilleul Sep 09 2020 at 21:05

Perl 5 -pF , 20 byte

$_=!grep ord()%@F,@F

Provalo online!

2 Noodle9 Sep 09 2020 at 16:36

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)
2 J42161217 Sep 09 2020 at 15:11

Wolfram Language (Mathematica) , 40 byte

{0}==##&@@ToCharacterCode@#~Mod~Tr[1^#]&

Provalo online!

grazie a @att per aver salvato alcuni byte

1 Adám Sep 09 2020 at 15:24

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

1 LiefdeWen Sep 09 2020 at 15:56

C # (.NET Core) , 25 byte

a=>a.All(x=>x%a.Length<1)

Provalo online!

1 Shaggy Sep 09 2020 at 16:36

Japt -e , 6 byte

c vNÎÊ

Provalo

1 KevinCruijssen Sep 09 2020 at 16:42

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)
1 ovs Sep 09 2020 at 15:18

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
1 DominicvanEssen Sep 09 2020 at 16:20

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 ...

1 Stuart Sep 11 2020 at 16:10

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) 
1 Dion Sep 10 2020 at 20:07

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
1 xash Sep 12 2020 at 15:48

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
1 Steve28 Sep 13 2020 at 08:06

Python 3 , 55 52 byte

N=input();print(not sum([ord(i)%len(N) for i in N]))

Provalo online!

1 2014MELO03 Sep 17 2020 at 03:26

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
Neil Sep 09 2020 at 16:06

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.

GalenIvanov Sep 09 2020 at 16:06

Fattore , 62 byte

: f ( s -- ? ) dup length [ mod ] curry [ + ] map-reduce 0 = ;

Provalo online!

BlackPanther Sep 09 2020 at 18:08

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!