16
\$\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\$
  • \$\begingroup\$ Suggested truthies off and fir \$\endgroup\$ – J42161217 Sep 9 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 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\$ – Dominic van Essen Sep 9 at 9:13
  • \$\begingroup\$ @Dion I just thought it would be nice to include a real word...good luck in finding bigger ones \$\endgroup\$ – J42161217 Sep 9 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 at 9:20

37 Answers 37

3
\$\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
| improve this answer | |
\$\endgroup\$
8
\$\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

| improve this answer | |
\$\endgroup\$
  • 5
    \$\begingroup\$ Wow! The animation is amazing! Can we have this for every Befunge answer, please? \$\endgroup\$ – Dominic van Essen Sep 9 at 10:01
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!

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

| improve this answer | |
\$\endgroup\$
4
\$\begingroup\$

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
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Alternate solution for 8 bytes: !s%RlQCM \$\endgroup\$ – Sok Sep 9 at 12:28
  • \$\begingroup\$ Yet another: !f%CTlQQ Filter string with ascii(letter) % length \$\endgroup\$ – Scott Sep 14 at 23:06
3
\$\begingroup\$

JavaScript, 32 bytes

Ouput is reversed.

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

Try it online!

| improve this answer | |
\$\endgroup\$
3
\$\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

| improve this answer | |
\$\endgroup\$
  • 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 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 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 at 11:25
2
\$\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

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 39 bytes with all: lambda s:all(ord(i)%len(s)<1for i in s). \$\endgroup\$ – ovs Sep 9 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 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 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 at 13:04
  • 1
    \$\begingroup\$ Also a valid Python 3 solution. \$\endgroup\$ – L3viathan Sep 10 at 7:08
2
\$\begingroup\$

K (oK), 11 bytes

{~+/(#x)!x}

Try it online!

| improve this answer | |
\$\endgroup\$
2
\$\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.

| improve this answer | |
\$\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)
| improve this answer | |
\$\endgroup\$
2
\$\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!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ You don't need the brackets around &:zero?: tio.run/… \$\endgroup\$ – ovs Sep 9 at 10:47
  • \$\begingroup\$ Yep, missed that. Havent golfed ruby in around a month lol \$\endgroup\$ – Razetime Sep 9 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 at 10:50
  • \$\begingroup\$ yes, that shortens it a lot. \$\endgroup\$ – Razetime Sep 9 at 10:53
  • 2
    \$\begingroup\$ You can save one more byte with <1 instead of ==0. \$\endgroup\$ – Dingus Sep 9 at 10:54
2
\$\begingroup\$

Perl 5 -pF, 20 bytes

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

Try it online!

| improve this answer | |
\$\endgroup\$
2
\$\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
                                                r=l;  // Return r (r will be 0
                                                      //   iff every character was
                                                      //   divisible by l)
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Wolfram Language (Mathematica), 40 bytes

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

Try it online!

thanks to @att for saving some bytes

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 37 bytes taking a list of characters instead of a string \$\endgroup\$ – att Sep 12 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\$ – J42161217 Sep 12 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 at 18:26
1
\$\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

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

C# (.NET Core), 25 bytes

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

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Japt -e, 6 bytes

c vNÎÊ

Try it

| improve this answer | |
\$\endgroup\$
1
\$\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)
| improve this answer | |
\$\endgroup\$
1
\$\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
| improve this answer | |
\$\endgroup\$
1
\$\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...

| improve this answer | |
\$\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) 
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ are some of the spaces not removable? \$\endgroup\$ – Razetime Sep 11 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\$ – caird coinheringaahing Sep 11 at 9:43
  • 1
    \$\begingroup\$ Oh, good point. I thought the Clojure compiler would moan without the spaces. \$\endgroup\$ – Stuart Sep 11 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 Sep 11 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
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ why not add the condition in the first loop itself? \$\endgroup\$ – Razetime Sep 11 at 9:47
  • \$\begingroup\$ Try it! (I think the link generator needs to point to MAWP 2.0.) \$\endgroup\$ – Razetime Sep 11 at 9:50
  • \$\begingroup\$ @Razetime yep, fixed link gen, thanks \$\endgroup\$ – Dion Sep 11 at 10:22
  • \$\begingroup\$ Small edit(again): change the <%> conditional to ?% or its equivalent in 2.0 \$\endgroup\$ – Razetime Sep 11 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
| improve this answer | |
\$\endgroup\$
1
\$\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
| improve this answer | |
\$\endgroup\$
0
\$\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.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Factor, 62 bytes

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

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\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!

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ You can use s.Length<1 instead of s.Length==0 like in my answer \$\endgroup\$ – LiefdeWen Sep 9 at 11:29
  • \$\begingroup\$ Thanks @LiefdeWen , I hadn't thought of that until I saw your answer. \$\endgroup\$ – Black Panther Sep 9 at 11:32
0
\$\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.

| improve this answer | |
\$\endgroup\$

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.