나눌 수있는 문자열

Sep 09 2020

직무

문자열이 주어지면 s각 문자의 ASCII 코드가의 길이로 나눌 수 있으면 진실 값을 출력하고 그렇지 않으면 거짓 값을 출력하십시오 s.

입출력

입력은 ASCII 만 포함하는 비어 있지 않은 문자열 [32-126]입니다. 출력은 표준 진실 / 거짓 값입니다. 값을 전환 할 수 있습니다 (예 : 나눌 수있는 경우 0/ 반환 False및 그 반대로)

테스트 케이스

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

답변

3 LuisMendo Sep 09 2020 at 15:18

MATL , 4 바이트

tn\~
  • 나눌 수있는 문자열의 경우 출력은 1s 만 포함하는 벡터 이며, 이는 진실 입니다.
  • 그렇지 않으면, 출력은 여러 포함하는 벡터이고, 1S 및 적어도 하나 0이며, falsy이 .

온라인으로 시도하십시오! 또는 진실성 / 허위 테스트를 포함한 모든 테스트 케이스 를 확인하십시오 .

작동 원리

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

Befunge-98 (FBBI) , 31 바이트

출력, 종료 코드 경유 1, truthy에 대한 0falsey의 경우에.

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

온라인으로 시도하십시오!


입력 lol및 ab다음 과 함께 실행되는 코드 :

작은 숫자는 리터럴 바이트 값을 나타냅니다.

8 rak1507 Sep 09 2020 at 17:20

Haskell , 42 39 바이트

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

ovs 및 xnor 덕분에 3 바이트 감소!

온라인으로 시도하십시오!

7 ovs Sep 09 2020 at 15:06

05AB1E , 5 바이트

ÇsgÖP

온라인으로 시도하십시오!

댓글 작성

        # 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

록 스타 , 205 192 175 162 바이트

글쎄요, 재미 있었어요. Rockstar는 문자열의 길이를 직접 읽을 방법이없고 문자를 코드 포인트로 변환 할 수 없으며 모듈로 연산자도 없습니다. 이 짧은 결과가 놀랐습니다!

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

여기에서 시도해보세요 (코드를 붙여 넣어야합니다).

4 Mukundan314 Sep 09 2020 at 17:35

Pyth , 8 바이트

!sm%CdlQ

온라인으로 시도하십시오!

!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 바이트

Ouput이 반대입니다.

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

온라인으로 시도하십시오!

3 Kaddath Sep 10 2020 at 21:30

PHP , 56 52 바이트

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

온라인으로 시도하십시오!

출력이 반전 됨

f문자가 나눌 수없는 경우 실행이 중지되고 , 모두 나눌 수있는 경우 빈 문자열 (PHP에서는 거짓)이 실행됩니다.

편집 : @ 640KB 덕분에 4 바이트 절약

2 Dion Sep 09 2020 at 15:06

Python 2 , 41 39 바이트

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

온라인으로 시도하십시오!

@ovs 덕분에 -2 바이트

2 GalenIvanov Sep 09 2020 at 15:34

K (oK) , 11 바이트

{~+/(#x)!x}

온라인으로 시도하십시오!

2 madlaina Sep 09 2020 at 16:48

Rust , 36 바이트

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

온라인으로 시도하십시오!

입력을로 취하고 &[u8]출력을 bool.

2 Razetime Sep 09 2020 at 17:13

Pip , 12 바이트

!$+(A_Ma)%#a

온라인으로 시도하십시오!

설명

!$+(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 바이트

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

지도 만 문자열에 사용할 수 있다면 ..

ovs에서 -10 바이트

-Dingus에서 1 바이트.

온라인으로 시도하십시오!

2 NahuelFouilleul Sep 09 2020 at 21:05

Perl 5 -pF , 20 바이트

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

온라인으로 시도하십시오!

2 Noodle9 Sep 09 2020 at 16:36

C (gcc) , 54 53 바이트

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

온라인으로 시도하십시오!

각 문자의 ASCII 값이 입력 문자열의 길이로 나눌 수 있으면 false를 반환하고 그렇지 않으면 true를 반환합니다.

설명:

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 언어 (Mathematica) , 40 바이트

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

온라인으로 시도하십시오!

@att 덕분에 일부 바이트를 절약했습니다.

1 Adám Sep 09 2020 at 15:24

APL (Dyalog 확장) , 7 바이트 ( SBCS )

익명의 암묵적 접두사 기능

⍱≢|⎕UCS

온라인으로 시도하십시오!

⍱ 다음 중 어떤 것도 참 (0이 아님)이 아닙니까?

≢ 길이

| 분할 (나눌 때 밝은 분할 나머지)

⎕UCS 코드 포인트

1 LiefdeWen Sep 09 2020 at 15:56

C # (. NET Core) , 25 바이트

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

온라인으로 시도하십시오!

1 Shaggy Sep 09 2020 at 16:36

Japt -e , 6 바이트

c vNÎÊ

시도 해봐

1 KevinCruijssen Sep 09 2020 at 16:42

MathGolf , 4 바이트

$h÷╓

문자 목록으로 입력합니다.

온라인으로 시도하십시오.

설명:

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

젤리 , 4 바이트

LḍOP

온라인으로 시도하십시오! 또는 모든 사례를 확인하십시오!

칭찬 : (적어도 이렇게 작동한다고 생각합니다)

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

R , 39 38 바이트

편집 : FALSE의 경우 TRUE, TRUE의 경우 FALSE를 출력 할 수있는 새 규칙 덕분에 -1byte

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

온라인으로 시도하십시오!

또는 TRUE에 대해 TRUE를 출력 하는 원래 39 바이트 버전 을 사용해보십시오 .

1 Stuart Sep 11 2020 at 16:10

Clojure, 41 자

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

주석 37 자 뒤에 공백 제거

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

MAWP , 34 33 24 23 바이트

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

시도 해봐!

9 바이트를 절약 한 @Razetime에게 감사드립니다!

설명:

`        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 바이트

ạfᵐ∋ᵛ~l?

온라인으로 시도하십시오!

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

대체 버전,

⟨ạ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 바이트

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

온라인으로 시도하십시오!

1 2014MELO03 Sep 17 2020 at 03:26

GolfScript , 20 바이트

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

온라인으로 시도하십시오!

문자열이 나눌 수 있으면 1을 출력하고 그렇지 않으면 0을 출력합니다. S는 문자열이고 L은 길이입니다.

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

목탄 , 8 바이트

¬⊙θ﹪℅ιLθ

온라인으로 시도하십시오! 링크는 자세한 코드 버전입니다. 출력은 Charcoal 부울입니다. 즉 -, true이면 false이고 아무것도 아닌 경우입니다. 설명:

  θ         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θ 물론 작동합니다.

GalenIvanov Sep 09 2020 at 16:06

요소 , 62 바이트

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

온라인으로 시도하십시오!

BlackPanther Sep 09 2020 at 18:08

C # (Visual C # 대화 형 컴파일러) , 81 바이트

(s)=>{var bs = ASCIIEncoding.ASCII.GetBytes(s);return bs.All(b=>b%s.Length==0);};

온라인으로 시도하십시오!

C # (Visual C # 대화 형 컴파일러) , 27 26 바이트

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

온라인으로 시도하십시오!