สตริงหาร

Sep 09 2020

งาน

กำหนดสตริงsให้ส่งออกค่าที่แท้จริงหากรหัส ASCII ของแต่ละตัวอักษรหารด้วยความยาวsและเป็นเท็จ

อินพุต / เอาต์พุต

อินพุตเป็นสตริงที่ไม่ว่าง[32-126]เปล่าที่มีเฉพาะ ASCII เอาต์พุตเป็นค่าความจริง / เท็จมาตรฐาน โปรดทราบว่าคุณสามารถเปลี่ยนค่าได้เช่นส่งกลับ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 ซึ่งเป็นtruthy
  • มิฉะนั้นออกเป็นเวกเตอร์ที่มีหลาย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สำหรับจริง0สำหรับกรณีเท็จ

#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

3 ไบต์น้อยลงด้วย ovs และ xnor!

ลองออนไลน์!

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

Rockstar , 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ถ้า char ใด ๆ ไม่สามารถหารได้หรือสตริงว่างเปล่า (เป็นเท็จใน PHP) หากทั้งหมดหารลงตัว

แก้ไข: บันทึก 4 ไบต์ด้วย @ 640KB

2 Dion Sep 09 2020 at 15:06

Python 2 , 41 39 ไบต์

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

ลองออนไลน์!

-2 ไบต์ขอบคุณ @ovs

2 GalenIvanov Sep 09 2020 at 15:34

K (oK) , 11 ไบต์

{~+/(#x)!x}

ลองออนไลน์!

2 madlaina Sep 09 2020 at 16:48

สนิม 36 ไบต์

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

ลองออนไลน์!

รับอินพุตเป็น a &[u8], เอาต์พุต a 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}}

ถ้าใช้ได้เฉพาะแผนที่กับสตริง ..

-10 ไบต์จาก ovs

-1 ไบต์จาก Dingus

ลองออนไลน์!

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 ของแต่ละอักขระหารด้วยความยาวของสตริงอินพุตหรือความจริงเป็นอย่างอื่น

คำอธิบาย:

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 Extended) , 7 ไบต์ ( SBCS )

ฟังก์ชันคำนำหน้าโดยปริยายแบบไม่ระบุชื่อ

⍱≢|⎕UCS

ลองออนไลน์!

 ไม่ใช่ค่าความจริงใด ๆ ต่อไปนี้ (ไม่ใช่ศูนย์)?

 ความยาว

| หาร (lit. ส่วนที่เหลือเมื่อหาร)

⎕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 ไบต์

แก้ไข: -1 ไบต์ด้วยกฎใหม่ที่เราสามารถส่งออก TRUE สำหรับ FALSE และ FALSE สำหรับ TRUE

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

ลองออนไลน์!

หรือลองใช้เวอร์ชัน 39 ไบต์ดั้งเดิมที่แสดงผล TRUE สำหรับ TRUE ...

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:

ลองมัน!

ขอบคุณ @Razetime สำหรับการประหยัด 9 ไบต์!

คำอธิบาย:

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

ลองออนไลน์! ลิงก์คือรหัสเวอร์ชันที่ละเอียด เอาต์พุตเป็นบูลีนของถ่านเช่น-จริงไม่มีอะไรเป็นเท็จ คำอธิบาย:

  θ         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 # Interactive Compiler) , 81 ไบต์

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

ลองออนไลน์!

C # (Visual C # Interactive Compiler) , 27 26 ไบต์

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

ลองออนไลน์!