# Divisible strings

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

• Suggested truthies off and fir – J42161217 Sep 9 at 8:40
• @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. – Dion Sep 9 at 8:46
• "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...) – Dominic van Essen Sep 9 at 9:13
• @Dion I just thought it would be nice to include a real word...good luck in finding bigger ones – J42161217 Sep 9 at 9:19
• 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.) – Noodle9 Sep 9 at 9:20

# 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


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

small numbers represent literal byte values

• Wow! The animation is amazing! Can we have this for every Befunge answer, please? – Dominic van Essen Sep 9 at 10:01

# Haskell, 42 39 bytes

(<1).sum.(map=<<flip(mod.fromEnum).length)

f s=sum[fromEnum cmodlength s|c<-s]<1


3 fewer bytes thanks to ovs and xnor!

Try it online!

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

# 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


# Rockstar, 205192175 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)

# Pyth, 8 bytes

!sm%CdlQ


Try it online!

!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

• Alternate solution for 8 bytes: !s%RlQCM – Sok Sep 9 at 12:28
• Yet another: !f%CTlQQ Filter string with ascii(letter) % length – Scott Sep 14 at 23:06

# JavaScript, 32 bytes

Ouput is reversed.

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


Try it online!

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 • Nice one. Alternate take 53 bytes or 52 bytes if you are willing to output f for Falsey and empty for Truthy. – 640KB Sep 11 at 17:14 • Or a bit of a wierdo one if you can return 0 for Falsey and non-zero for Truthy – 640KB Sep 11 at 17:35 • @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 – Kaddath Sep 15 at 11:25 # Python 2, 41 39 bytes lambda s:all(ord(i)%len(s)<1for i in s)  Try it online! -2 bytes thanks to @ovs • 39 bytes with all: lambda s:all(ord(i)%len(s)<1for i in s). – ovs Sep 9 at 8:09 • 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. – Noodle9 Sep 9 at 9:55 • 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. – Stef Sep 9 at 12:58 • @Stef I was thinking about that. I would rather go with standard truthy-falsey values, and im not sure how well that goes – Dion Sep 9 at 13:04 • Also a valid Python 3 solution. – L3viathan Sep 10 at 7:08 # K (oK), 11 bytes {~+/(#x)!x}  Try it online! # 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. # 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)


# Ruby, 433736 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!

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

# Perl 5-pF, 20 bytes

$_=!grep ord()%@F,@F  Try it online! # 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 r=l; // Return r (r will be 0 // iff every character was // divisible by l)  # Wolfram Language (Mathematica), 40 bytes {0}==##&@@ToCharacterCode@#~Mod~Tr[1^#]&  Try it online! thanks to @att for saving some bytes • 37 bytes taking a list of characters instead of a string – att Sep 12 at 3:25 • @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 – J42161217 Sep 12 at 8:12 • 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 – att Sep 12 at 18:26 # 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 # C# (.NET Core), 25 bytes a=>a.All(x=>x%a.Length<1)  Try it online! # Japt-e, 6 bytes c vNÎÊ  Try it # 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)  # Jelly, 4 bytes LḍOP  Commmented: (At least I think it works like this)  P # product of ... L # does the length ḍ # ... divide ... O # the char codes  # 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... # Clojure, 41 chars (every? #(= 0 (mod (int %) (count x))) x)  Removing spaces after comment 37 chars (every? #(= 0(mod(int %)(count x)))x)  • are some of the spaces not removable? – Razetime Sep 11 at 9:27 • 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 – caird coinheringaahing Sep 11 at 9:43 • Oh, good point. I thought the Clojure compiler would moan without the spaces. – Stuart Sep 11 at 9:44 • 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. – Razetime Sep 11 at 10:40 # MAWP, 343324 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  • why not add the condition in the first loop itself? – Razetime Sep 11 at 9:47 • Try it! (I think the link generator needs to point to MAWP 2.0.) – Razetime Sep 11 at 9:50 • @Razetime yep, fixed link gen, thanks – Dion Sep 11 at 10:22 • Small edit(again): change the <%> conditional to ?% or its equivalent in 2.0 – Razetime Sep 11 at 10:33 # 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  # 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


# Charcoal, 8 bytes

¬⊙θ﹪℅ιＬθ


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
Ｌ     Length
¬           Boolean NOT
Implicitly print


⬤θ¬﹪℅ιＬθ also works of course.

# Factor, 62 bytes

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


Try it online!

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

• You can use s.Length<1 instead of s.Length==0 like in my answer – LiefdeWen Sep 9 at 11:29
• Thanks @LiefdeWen , I hadn't thought of that until I saw your answer. – Black Panther Sep 9 at 11:32

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