20
\$\begingroup\$

Task

Given a string s, output a truthy value if the ASCII code of each letter is divisible by the length of s, and a falsey otherwise.

Input/Output

Input is a nonempty string containing only ASCII [32-126]. Output is a standard truthy/falsey value. Note that you can switch the values, for example returning 0/False if divisible and vice versa

Test cases

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
\$\endgroup\$
8
  • \$\begingroup\$ Suggested truthies off and fir \$\endgroup\$
    – ZaMoC
    Commented Sep 9, 2020 at 8:40
  • \$\begingroup\$ @J42161217 I would rather add more test cases if they are either long truthy cases or very short falsey cases. We already have a 3-letter truthy. \$\endgroup\$
    – Dion
    Commented Sep 9, 2020 at 8:46
  • 1
    \$\begingroup\$ "UPZAP" (not sure if that's a real word, but could refer to changing TV channel to a higher-numbered one using a remote control...) \$\endgroup\$ Commented Sep 9, 2020 at 9:13
  • \$\begingroup\$ @Dion I just thought it would be nice to include a real word...good luck in finding bigger ones \$\endgroup\$
    – ZaMoC
    Commented Sep 9, 2020 at 9:19
  • 1
    \$\begingroup\$ Can we switch truthy/falsey return values (ie return a falsey value if the ASCII code of each letter is divisible by the length of s, and a truthy otherwise.) \$\endgroup\$
    – Noodle9
    Commented Sep 9, 2020 at 9:20

49 Answers 49

1
2
1
\$\begingroup\$

C# (Visual C# Interactive Compiler), 81 bytes

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

Try it online!

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

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

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You can use s.Length<1 instead of s.Length==0 like in my answer \$\endgroup\$
    – LiefdeWen
    Commented Sep 9, 2020 at 11:29
  • \$\begingroup\$ Thanks @LiefdeWen , I hadn't thought of that until I saw your answer. \$\endgroup\$
    – bit
    Commented Sep 9, 2020 at 11:32
1
\$\begingroup\$

Aceto, 21 bytes

&L
|%o 1
€l|!
rM@dp

Explanation

We read a string and xplode it, push the stack length and Memorize that.

Then, after setting the exception catch point (@), we always duplicate the top stack element, negate (!) it, and m|rror horizontally if we get a truthy value (string has ended; we popped a 0). Otherwise, we get the ordinal of the character, Load the memorized value and do modulo (%). If this is truthy, we m|rror again.

Finally, we raise an exception (&) to land back in front of the d, for our next character.

If we mirrored, then we eventually land on p, printing the top-most element of the stack. In one of the two cases of mirroring, we will have pushed a 1 before.


I don't see much potential to golf this down further; there's only one space character used, and 3 newlines. Perhaps one or two bytes could be saved by making it a 16x16 in two lines.

\$\endgroup\$
1
\$\begingroup\$

Clojure, 41 chars

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

Removing spaces after comment 37 chars

(every? #(= 0(mod(int %)(count x)))x) 
\$\endgroup\$
4
  • \$\begingroup\$ are some of the spaces not removable? \$\endgroup\$
    – Razetime
    Commented Sep 11, 2020 at 9:27
  • \$\begingroup\$ Welcome to the site! This is a code-golf challenge, so the aim is to minimise your code as much as possible. As Razetime mentioned, is it possible to remove some/all of the spaces in your code? Also, make sure to check out our Tips for golfing in Clojure page \$\endgroup\$ Commented Sep 11, 2020 at 9:43
  • 1
    \$\begingroup\$ Oh, good point. I thought the Clojure compiler would moan without the spaces. \$\endgroup\$
    – Stuart
    Commented Sep 11, 2020 at 9:44
  • \$\begingroup\$ You can generate Stack Exchange markdown answers from your working code at tio.run/#clojure . It's the general place we use to check each other's answers around here. \$\endgroup\$
    – Razetime
    Commented Sep 11, 2020 at 10:40
1
\$\begingroup\$

MAWP, 34 33 24 23 bytes

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

Try it!

Thanks to @Razetime for saving 9 bytes!

Explanation:

`        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
\$\endgroup\$
4
  • \$\begingroup\$ why not add the condition in the first loop itself? \$\endgroup\$
    – Razetime
    Commented Sep 11, 2020 at 9:47
  • \$\begingroup\$ Try it! (I think the link generator needs to point to MAWP 2.0.) \$\endgroup\$
    – Razetime
    Commented Sep 11, 2020 at 9:50
  • \$\begingroup\$ @Razetime yep, fixed link gen, thanks \$\endgroup\$
    – Dion
    Commented Sep 11, 2020 at 10:22
  • \$\begingroup\$ Small edit(again): change the <%> conditional to ?% or its equivalent in 2.0 \$\endgroup\$
    – Razetime
    Commented Sep 11, 2020 at 10:33
1
\$\begingroup\$

Brachylog, 8 bytes

ạfᵐ∋ᵛ~l?

Try it online!

ạ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
\$\endgroup\$
1
\$\begingroup\$

NARS2000 (12 characters)

(∧/0=⍴|⎕ucs)

Fork ⍴|⎕ucs finds residue of each ASCII int value when divided by length of string. ∧/0= checks if all residue are zero.

\$\endgroup\$
1
\$\begingroup\$

Python 3.8 (pre-release), 63 49 bytes

lambda h:print({0}=={*[ord(i)%len(h)for i in h]})

Try it online!

* in the {} constructs a set which I think is more byte-efficient than using set(), which I didn't know.

----

Python 3.8 (pre-release), 42? bytes

lambda h:{0}=={*[ord(i)%len(h)for i in h]}

I can't get TIO to output the True or False. This works with IDLE or Jupyter. Try it online.

\$\endgroup\$
1
\$\begingroup\$

PowerShell Core, 30 bytes

!(($args|?{(++$l)})|?{+$_%$l})

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Java, 41 bytes

s->s.chars().allMatch(i->i%s.length()==0)

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Husk, 6 bytes

Λo¦L¹c

Try it online!

Explanation

Λo¦L¹c   translates = Λo¦L⁰c⁰
Λ     ⁰  all of input:
 o   c   charcode
  ¦      divides
   L⁰    length of input
\$\endgroup\$
1
\$\begingroup\$

APOL, 23 bytes

!(x(ƒ(i %(↶(∋) l(⋒)))))

Explanation:

!(           Not
  x(         Any list item is true
    ƒ(       List-builder for
      i      Input
      %(     Modulo
        ↶(   ASCII codepoint
          ∋  Loop item
       )
       l(    Length of
         ⋒   Loop iterator (what the loop is looping through)
       )
     )
    )
  )
)
\$\endgroup\$
1
\$\begingroup\$

Thunno 2 B, 3 bytes

lḊp

Try it online!

Explanation

lḊp  # Implicit input
     # Convert to charcodes
l    # Length of input
 Ḋ   # Are they divisible
  p  # Product of the list
     # Implicit output
\$\endgroup\$
1
\$\begingroup\$

Vyxal 2.4.1 K, 3 bytes

LḊΠ

Try it Online!

I too can play the K flag game, especially given that I invented it.

\$\endgroup\$
0
\$\begingroup\$

Excel, 66 bytes

=SUM(MOD(CODE(MID(A1,ROW(OFFSET(A1,0,0,LEN(A1))),1))/LEN(A1),1))=0

Input is in the cell A1.

ROW(OFFSET(A1,0,0,LEN(A1))) gives us an array of values from 1 to the length of the input.
MID(A1,ROW(~),1) pulls out each character of the input, one at a time.
CODE(MID(~) converts those characters to their decimal ASCII equivalent.
MOD(CODE(~)/LEN(A1),1) returns just the decimal portion of those codes divided by the input length.
SUM(MOD(~))=0 adds up all those decimal portions and returns TRUE if there weren't any (SUM=0 means everything divided nicely) and FALSE if there were.


I thought this would have to be an array formula but it seems to work without that. Color me surprised.

\$\endgroup\$
0
\$\begingroup\$

T-SQL, 65 bytes

Returns 1 for true, 0 for false

SELECT-1/~max(ascii(substring(@,number,1))%len(@))FROM spt_values

Try it online

\$\endgroup\$
0
\$\begingroup\$

Go, 76 bytes

func f(a string)int{for _,v:=range a{if int(v)%len(a)>0{return 0}}
return 1}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

x86-16 machine code, 10 bytes

00000000: 880e 0601 acd4 0ae1 fbc3                 ..........

Listing:

88 0E 0106      MOV  BYTE PTR[CHR_LOOP+2], CL   ; move len to second byte of AAM opcode 
            CHR_LOOP: 
AC              LODSB                           ; AL = next char 
D4 0A           AAM  CL                         ; ZF = (AL % CL == 0) 
E1 FB           LOOPZ CHR_LOOP                  ; loop if CX > 0 AND ZF = 1 
C3              RET                             ; return to caller

As a callable function. Input: string in SI, length in CX. Output: ZF = 1 if Truthy, ZF = 0 if Falsey.

Output of DOS test program:

enter image description here

\$\endgroup\$
0
\$\begingroup\$

C (clang), 149 148 bytes

#include<stdio.h>
int main(){char s[100];scanf("%s",s);int j;int k=strlen(s);for(int i=0;i<k;i++){if(s[i]%k>0){printf("0");return 0;}};printf("1");}

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ I think you can leave out the #include <stdio.h> and it will still run fine (just giving a few more warnings)... \$\endgroup\$ Commented Sep 10, 2020 at 10:25
  • \$\begingroup\$ 78 bytes \$\endgroup\$
    – ceilingcat
    Commented Sep 10, 2020 at 22:55
0
\$\begingroup\$

Factor + math.unicode, 33 bytes

[ dup length '[ _ mod 0 = ] ∀ ]

Try it online!

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.