17
\$\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
    Sep 9 '20 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
    Sep 9 '20 at 8:46
  • \$\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\$ Sep 9 '20 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
    Sep 9 '20 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
    Sep 9 '20 at 9:20

43 Answers 43

9
\$\begingroup\$

Befunge-98 (FBBI), 31 bytes

Output is via exit code, 1 for truthy, 0 for falsey cases.

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

Try it online!


Code running with inputs lol and ab:

Code with input lol Code with input ab

small numbers represent literal byte values

\$\endgroup\$
1
  • 5
    \$\begingroup\$ Wow! The animation is amazing! Can we have this for every Befunge answer, please? \$\endgroup\$ Sep 9 '20 at 10:01
8
\$\begingroup\$

05AB1E, 5 bytes

ÇsgÖP

Try it online!

Commented

        # 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
\$\endgroup\$
8
\$\begingroup\$

Haskell, 42 39 bytes

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

3 fewer bytes thanks to ovs and xnor!

Try it online!

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Welcome! Here's a trick to get the code working on TIO: Try it online! \$\endgroup\$
    – xnor
    Sep 9 '20 at 10:30
  • \$\begingroup\$ pointfree is a little bit longer in this case: Try it online! \$\endgroup\$
    – ovs
    Sep 9 '20 at 10:33
  • \$\begingroup\$ @ovs You can even infix the mod to cut parens: Try it online! \$\endgroup\$
    – xnor
    Sep 9 '20 at 10:35
  • 1
    \$\begingroup\$ Another 39 with some partial pointfreeing: Try it online! \$\endgroup\$
    – xnor
    Sep 9 '20 at 10:38
5
\$\begingroup\$

MATL, 4 bytes

tn\~
  • For divisible strings the output is a vector containing only 1s, which is truthy.
  • Otherwise the output is a vector containing several 1s and at least one 0, which is falsy.

Try it online! Or verify all test cases including truthiness/falsihood test.

How it works

t   % Implicit input. Duplicate
n   % Number of elements
\   % Modulo
~   % Negate. Implicit display
\$\endgroup\$
5
\$\begingroup\$

Rockstar, 205 192 175 162 bytes

Well, this was fun. Rockstar has no way of reading the length of a string directly, can't convert characters to codepoints and has no modulo operator. Surprised it worked out this short!

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

Try it here (Code will need to be pasted in)

\$\endgroup\$
5
\$\begingroup\$

Pyth, 8 7 bytes

-1 byte thanks to @isaag

!%#lQCM

Try it online!

!%#lQCM
!        - logical negation of
  #      - filtering
     CM  - ascii values of input with
 % lQ    - func(x): return x % len(input) 
\$\endgroup\$
3
  • \$\begingroup\$ Alternate solution for 8 bytes: !s%RlQCM \$\endgroup\$
    – Sok
    Sep 9 '20 at 12:28
  • \$\begingroup\$ Yet another: !f%CTlQQ Filter string with ascii(letter) % length \$\endgroup\$
    – Scott
    Sep 14 '20 at 23:06
  • \$\begingroup\$ Combining Sok and Scott's solutions gives a 7 byte solution: !%#lQCM \$\endgroup\$
    – isaacg
    Feb 22 at 21:51
4
\$\begingroup\$

JavaScript, 32 bytes

Ouput is reversed.

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

Try it online!

\$\endgroup\$
4
\$\begingroup\$

PHP, 56 52 bytes

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

Try it online!

Output is reversed

Execution stops with f if any char is not divisible, or empty string (falsy in PHP) if all are divisible

EDIT: saved 4 bytes thanks to @640KB

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Nice one. Alternate take 53 bytes or 52 bytes if you are willing to output f for Falsey and empty for Truthy. \$\endgroup\$
    – 640KB
    Sep 11 '20 at 17:14
  • \$\begingroup\$ Or a bit of a wierdo one if you can return 0 for Falsey and non-zero for Truthy \$\endgroup\$
    – 640KB
    Sep 11 '20 at 17:35
  • \$\begingroup\$ @640KB great suggestion, never thought that ord(NULL) would be 0, should have tried.. and should have thought of moving the exit inside the for, it's usually something that I test :) I like the weird one too \$\endgroup\$
    – Kaddath
    Sep 15 '20 at 11:25
3
\$\begingroup\$

Python 2, 41 39 bytes

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

Try it online!

-2 bytes thanks to @ovs

\$\endgroup\$
5
  • \$\begingroup\$ 39 bytes with all: lambda s:all(ord(i)%len(s)<1for i in s). \$\endgroup\$
    – ovs
    Sep 9 '20 at 8:09
  • 2
    \$\begingroup\$ Don't delete your answer since the cat is already out of the bag but usually you don't answer your own question for a few days to give other people a shot at it - as OP you have an unfair time advantage. \$\endgroup\$
    – Noodle9
    Sep 9 '20 at 9:55
  • \$\begingroup\$ If you accept "truthy" to be False and "falsey" to be True, then you can spare two bytes by removing <1 and substituting any for all. \$\endgroup\$
    – Stef
    Sep 9 '20 at 12:58
  • \$\begingroup\$ @Stef I was thinking about that. I would rather go with standard truthy-falsey values, and im not sure how well that goes \$\endgroup\$
    – Dion
    Sep 9 '20 at 13:04
  • 1
    \$\begingroup\$ Also a valid Python 3 solution. \$\endgroup\$
    – L3viathan
    Sep 10 '20 at 7:08
3
\$\begingroup\$

K (oK), 11 bytes

{~+/(#x)!x}

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Rust, 36 bytes

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

Try it online!

Takes the input as a &[u8], outputs a bool.

\$\endgroup\$
3
\$\begingroup\$

Ruby, 43 37 36 32 bytes

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

if only map could be used on strings..

-10 bytes from ovs.

-1 byte from Dingus.

Try it online!

\$\endgroup\$
8
  • \$\begingroup\$ You don't need the brackets around &:zero?: tio.run/… \$\endgroup\$
    – ovs
    Sep 9 '20 at 10:47
  • \$\begingroup\$ Yep, missed that. Havent golfed ruby in around a month lol \$\endgroup\$
    – Razetime
    Sep 9 '20 at 10:48
  • \$\begingroup\$ And putting the ==0 in the map shortens this a little more: ->a{a.bytes.all?{|n|n%a.size==0}} \$\endgroup\$
    – ovs
    Sep 9 '20 at 10:50
  • \$\begingroup\$ yes, that shortens it a lot. \$\endgroup\$
    – Razetime
    Sep 9 '20 at 10:53
  • 2
    \$\begingroup\$ You can save one more byte with <1 instead of ==0. \$\endgroup\$
    – Dingus
    Sep 9 '20 at 10:54
3
\$\begingroup\$

Perl 5 -pF, 20 bytes

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

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Wolfram Language (Mathematica), 40 bytes

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

Try it online!

thanks to @att for saving some bytes

\$\endgroup\$
3
  • \$\begingroup\$ 37 bytes taking a list of characters instead of a string \$\endgroup\$
    – att
    Sep 12 '20 at 3:25
  • \$\begingroup\$ @att your code checks only the first character. That is why it fails for lov because Tr@{{0}, {0}, {1}}=0 . I made some changes and now it works \$\endgroup\$
    – ZaMoC
    Sep 12 '20 at 8:12
  • \$\begingroup\$ Ah, right, forgot that ToCharacterCode returns a list for single characters as well. Looks like your modification is Totaling the character codes before taking the Mod, though. 40 bytes \$\endgroup\$
    – att
    Sep 12 '20 at 18:26
3
\$\begingroup\$

C (gcc), 54 53 bytes

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

Try it online!

Returns falsey if the ASCII value of each character is divisible by the length of the input string or truthy otherwise.

Explanation:

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
                                                l=r;  // Return r (r will be 0
                                                      //   iff every character was
                                                      //   divisible by the length of the   
                                                      //   input string)
\$\endgroup\$
3
+50
\$\begingroup\$

Vyxal, 8 5 bytes

₌CLœΠ

Thanks @Lyxal for saving 3 bytes Try it Online!

\$\endgroup\$
2
3
\$\begingroup\$

PowerShell Core, 40 33 bytes

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

7 bytes saved thanks to mazzy

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ nice. you can save some bytes Try it online! \$\endgroup\$
    – mazzy
    Feb 23 at 5:25
2
\$\begingroup\$

APL (Dyalog Extended), 7 bytes (SBCS)

Anonymous tacit prefix function

⍱≢|⎕UCS

Try it online!

 are not any of the following true (non-zero)?

 the length

| divides (lit. division remainder when dividing)

⎕UCS the code points

\$\endgroup\$
2
\$\begingroup\$

C# (.NET Core), 25 bytes

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

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Japt -e, 6 bytes

c vNÎÊ

Try it

\$\endgroup\$
2
\$\begingroup\$

MathGolf, 4 bytes

$h÷╓

Input as a list of characters.

Try it online.

Explanation:

$     # 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)
\$\endgroup\$
2
\$\begingroup\$

Pip, 12 bytes

!$+(A_Ma)%#a

Try it online!

Explanation

!$+(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)
\$\endgroup\$
2
\$\begingroup\$

Jelly, 4 bytes

LḍOP

Try it online! or Verify all cases!

Commmented: (At least I think it works like this)

   P  # product of ...
L     #   does the length 
 ḍ    #   ... divide ...
  O   #   the char codes
\$\endgroup\$
2
\$\begingroup\$

R, 39 38 bytes

Edit: -1byte thanks to the new rule that we can output TRUE for FALSE and FALSE for TRUE

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

Try it online!

Or try the original 39-byte version that outputs TRUE for TRUE...

\$\endgroup\$
2
\$\begingroup\$

Python 3, 55 52 bytes

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

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I think your map would be shorter as a list comprehension. \$\endgroup\$
    – Grain Ghost
    Sep 13 '20 at 8:20
2
\$\begingroup\$

Zsh, 31 bytes

for c (${(s::)1})((r||=#c%$#1))

Try it online!

If we accept a "list of characters" as arguments, then we don't have to split the string for 19 bytes:

for c;((r||=#c%$#))

Try it online!

\$\endgroup\$
2
\$\begingroup\$

GolfScript, 20 bytes

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

Try it online!

This outputs 1 if the string is divisible and 0 if it isn't. Let S be the string and L its length.

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

Charcoal, 8 bytes

¬⊙θ﹪℅ιLθ

Try it online! Link is to verbose version of code. Output is a Charcoal boolean, i.e. - for true, nothing for false. Explanation:

  θ         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θ also works of course.

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

Factor, 62 bytes

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

Try it online!

\$\endgroup\$
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
    Sep 9 '20 at 11:29
  • \$\begingroup\$ Thanks @LiefdeWen , I hadn't thought of that until I saw your answer. \$\endgroup\$
    – bit
    Sep 9 '20 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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