Teilbare Zeichenfolgen

Sep 09 2020

Aufgabe

sGeben Sie bei einer gegebenen Zeichenfolge einen Wahrheitswert aus, wenn der ASCII-Code jedes Buchstabens durch die Länge von teilbar ist s, andernfalls einen Falsey.

Input-Output

Die Eingabe ist eine nicht leere Zeichenfolge, die nur ASCII enthält [32-126]. Die Ausgabe ist ein Standardwert für Wahrheit / Falschheit. Beachten Sie, dass Sie die Werte ändern können, z. B. Rückgabe 0/ Falsewenn teilbar und umgekehrt

Testfälle

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

Antworten

3 LuisMendo Sep 09 2020 at 15:18

MATL , 4 Bytes

tn\~
  • Für teilbare Zeichenfolgen ist die Ausgabe ein Vektor, der nur 1s enthält, was wahr ist .
  • Andernfalls ist die Ausgabe ein Vektor, der mehrere 1s und mindestens einen enthält 0, was falsch ist .

Probieren Sie es online aus! Oder überprüfen Sie alle Testfälle einschließlich Wahrheits- / Falschheitsprüfung.

Wie es funktioniert

t   % Implicit input. Duplicate
n   % Number of elements
\   % Modulo
~   % Negate. Implicit display
8 ovs Sep 09 2020 at 15:57

Befunge-98 (FBBI) , 31 Bytes

Die Ausgabe erfolgt über den Exit-Code, 1um ehrlich zu sein, 0für Falsey-Fälle.

#v~\1+
v>53p
>:#v_1q
^  >' %#@_

Probieren Sie es online aus!


Code läuft mit Eingaben lolund ab:

kleine Zahlen stehen für wörtliche Bytewerte

8 rak1507 Sep 09 2020 at 17:20

Haskell , 42 39 Bytes

(<1).sum.(map=<<flip(mod.fromEnum).length)
f s=sum[fromEnum c`mod`length s|c<-s]<1

3 weniger Bytes dank ovs und xnor!

Probieren Sie es online aus!

7 ovs Sep 09 2020 at 15:06

05AB1E , 5 Bytes

ÇsgÖP

Probieren Sie es online aus!

Kommentiert

        # 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 Bytes

Nun, das hat Spaß gemacht. Rockstar hat keine Möglichkeit, die Länge eines Strings direkt zu lesen, kann keine Zeichen in Codepunkte konvertieren und hat keinen Modulo-Operator. Überrascht hat es so kurz geklappt!

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

Probieren Sie es hier aus (Code muss eingefügt werden)

4 Mukundan314 Sep 09 2020 at 17:35

Pyth , 8 Bytes

!sm%CdlQ

Probieren Sie es online aus!

!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 Bytes

Ausgabe ist umgekehrt.

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

Probieren Sie es online aus!

3 Kaddath Sep 10 2020 at 21:30

PHP , 56 52 Bytes

for(;$c=ord($argn[$i++]);$c%strlen($argn)?die(f):1);

Probieren Sie es online aus!

Die Ausgabe ist umgekehrt

Die Ausführung stoppt mit, fwenn ein Zeichen nicht teilbar ist, oder mit einer leeren Zeichenfolge (in PHP falsch), wenn alle teilbar sind

BEARBEITEN: 4 Bytes dank @ 640KB gespeichert

2 Dion Sep 09 2020 at 15:06

Python 2 , 41 39 Bytes

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

Probieren Sie es online aus!

-2 Bytes dank @ovs

2 GalenIvanov Sep 09 2020 at 15:34

K (oK) , 11 Bytes

{~+/(#x)!x}

Probieren Sie es online aus!

2 madlaina Sep 09 2020 at 16:48

Rost , 36 Bytes

|s|s.iter().all(|x|1>x%s.len()as u8)

Probieren Sie es online aus!

Nimmt die Eingabe als a &[u8], gibt a aus bool.

2 Razetime Sep 09 2020 at 17:13

Pip , 12 Bytes

!$+(A_Ma)%#a

Probieren Sie es online aus!

Erläuterung

!$+(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 Bytes

->a{a.bytes.all?{|n|n%a.size<1}}

Wenn nur Map für Strings verwendet werden könnte.

-10 Bytes von Ovs.

-1 Byte von Dingus.

Probieren Sie es online aus!

2 NahuelFouilleul Sep 09 2020 at 21:05

Perl 5 -pF , 20 Bytes

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

Probieren Sie es online aus!

2 Noodle9 Sep 09 2020 at 16:36

C (gcc) , 54 53 Bytes

l;r;f(char*s){l=strlen(s);for(r=0;*s;)r|=*s++%l;l=r;}

Probieren Sie es online aus!

Gibt falsey zurück, wenn der ASCII-Wert jedes Zeichens durch die Länge der Eingabezeichenfolge teilbar ist oder auf andere Weise wahr ist.

Erläuterung:

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 Bytes

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

Probieren Sie es online aus!

Vielen Dank an @att für das Speichern einiger Bytes

1 Adám Sep 09 2020 at 15:24

APL (Dyalog Extended) , 7 Bytes ( SBCS )

Anonyme stillschweigende Präfixfunktion

⍱≢|⎕UCS

Probieren Sie es online aus!

⍱ Sind keine der folgenden Aussagen wahr (nicht Null)?

≢ die Länge

| teilt (lit. Teilungsrest beim Teilen)

⎕UCS der Code zeigt

1 LiefdeWen Sep 09 2020 at 15:56

C # (.NET Core) , 25 Byte

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

Probieren Sie es online aus!

1 Shaggy Sep 09 2020 at 16:36

Japt -e , 6 Bytes

c vNÎÊ

Versuch es

1 KevinCruijssen Sep 09 2020 at 16:42

MathGolf , 4 Bytes

$h÷╓

Eingabe als Liste von Zeichen.

Probieren Sie es online aus.

Erläuterung:

$     # 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

Gelee , 4 Bytes

LḍOP

Probieren Sie es online aus! oder Überprüfen Sie alle Fälle!

Kommentar: (Zumindest denke ich, dass es so funktioniert)

   P  # product of ...
L     #   does the length 
 ḍ    #   ... divide ...
  O   #   the char codes
1 DominicvanEssen Sep 09 2020 at 16:20

R , 39 38 Bytes

Bearbeiten: -1 Byte dank der neuen Regel, dass wir TRUE für FALSE und FALSE für TRUE ausgeben können

function(s)any(utf8ToInt(s)%%nchar(s))

Probieren Sie es online aus!

Oder probieren Sie die ursprüngliche 39-Byte-Version aus , die TRUE für TRUE ausgibt ...

1 Stuart Sep 11 2020 at 16:10

Clojure, 41 Zeichen

(every? #(= 0 (mod (int %) (count x))) x)

Leerzeichen nach Kommentar 37 Zeichen entfernen

(every? #(= 0(mod(int %)(count x)))x) 
1 Dion Sep 10 2020 at 20:07

MAWP , 34 33 24 23 Bytes

`|_=M0=A0/[M%{0:.}?`]1:

Versuch es!

Vielen Dank an @Razetime für das Speichern von 9 Bytes!

Erläuterung:

`        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 Bytes

ạfᵐ∋ᵛ~l?

Probieren Sie es online aus!

ạfᵐ∋ᵛ~l?
ạ        characters to integer
 fᵐ      find all factors
   ∋ᵛ    every list of factors contain …
     ~l? the length of the input

Alternative Version,

⟨ạ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 Bytes

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

Probieren Sie es online aus!

1 2014MELO03 Sep 17 2020 at 03:26

GolfScript , 20 Bytes

.,0@{(3$%@+\}3$*;!\;

Probieren Sie es online aus!

Dies gibt 1 aus, wenn die Zeichenfolge teilbar ist, und 0, wenn dies nicht der Fall ist. Sei S die Saite und L ihre Länge.

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

Holzkohle , 8 Bytes

¬⊙θ﹪℅ιLθ

Probieren Sie es online aus! Der Link führt zur ausführlichen Version des Codes. Die Ausgabe ist ein Boolescher Wert -für Holzkohle, dh für wahr, nichts für falsch. Erläuterung:

  θ         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θ funktioniert natürlich auch.

GalenIvanov Sep 09 2020 at 16:06

Faktor 62 Bytes

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

Probieren Sie es online aus!

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);};

Probieren Sie es online aus!

C # (Visual C # Interactive Compiler) , 27 26 Byte

s=>s.All(c=>c%s.Length<1);

Probieren Sie es online aus!