54
\$\begingroup\$

Definition

A dollar word is a word where when each of its letters is given a cent value, from a = 1 to z = 26, and the letters are summed, the result is 100. Here is an example on CodeReview, and here is a list of dollar words I found online.

Input

Input will be alphabetical from a-z, in your one language's text datatypes (arrays are allowed). You do not need to account for any other input - there will be no spaces, apostrophes, or hyphens. You can take as lowercase, uppercase, or a combination. Trailing newlines are allowed.

Output

Output a truthy value if the input is a dollar word, and a falsey value if it is not.

Test Cases

Truthy:

buzzy
boycott
identifies
adiabatically
ttttt

Falsey:

zzz
zzzzzzz
abcdefghiljjjzz
tttt
basic

This is code-golf, so the shortest answer in bytes wins! Standard loopholes and rules apply. Tie goes to first poster.

\$\endgroup\$
1
  • 25
    \$\begingroup\$ Title has dollar words in it, sorry if it threw you off. \$\endgroup\$
    – Stephen
    Commented Apr 18, 2017 at 20:56

86 Answers 86

3
\$\begingroup\$

Ruby (2.4+), 38 bytes

Takes input in lowercase. Requires Ruby 2.4's Array#sum so it won't run on TIO.

->a{a.chars.map{|c|c.ord-96}.sum==100}
\$\endgroup\$
2
  • 2
    \$\begingroup\$ Use String#bytes instead of String#chars so you don't have to call c.ord. \$\endgroup\$
    – Value Ink
    Commented Apr 19, 2017 at 2:44
  • \$\begingroup\$ And use sum on the array instead of map \$\endgroup\$
    – G B
    Commented Apr 19, 2017 at 6:07
3
\$\begingroup\$

Bash + GNU utils, 47

od -An -td1 -vw1|sed 's/^/a+=-96+/;$a!a-100'|bc

Try it online.

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

Aceto, 49 bytes

TL;DR: 3rd-order Hilbert curve. Uses character input, looping until a newline is given. Sums up the values (96 (5*3+9*9) minus them), and in the end adds 100 (5*5*4) and checks whether it's zero. Uses the fairly new catch marks.

}){(
(o-+
d{))
|(&
=,d@*45(
{d""*+5(
3*)+=0
599*p

Outputs True if the given word is a dollar word, False otherwise.

Explanation:

We first construct a 96 by calculating 5*3+9*9: 53*99*+. Next, we move a stack to the right and push an empty string: )"". Because we're about to enter a repeating part, we set the catching mark using @. This is where we will return if an error occurs.

We duplicate the empty string, read a single character (,), duplicate that character, and move it to the original stack ({). Still on the right stack, we compare the top two values (the empty string and our character) and use the conditional mirror to jump to the right edge if they are equal: =|.

Assuming they're not, we now move on the left stack, push the character once more one stack to the left, duplicate the 96, go to the left stack, push the character back on the middle stack and go there again: ({d(}).

Now we convert the character to the value of its unicode codepoint and subtract it from 96, resulting in it's negated dollar value: o-. We do it this way, because we'd have to swap the values otherwise. We push the value to the left stack, go there, and add the two values there (initially 0 and our negative character value): {(+. To prepare for reading another character, we go to the middle stack again ), and then raise an error: &.

Once a newline is read and the character is therefore empty and the equality truthful, we got mirrored somewhere on the top-left quadrant; the exact position doesn't matter in our case. The next operation that does something is moving back on the stack of our final value: ((. On it, we should now have the sum of all character values, negative. That means, if it's a dollar word, it should be -100. To test whether it is, we add 100 (554**+), push 0, test for equality and print the result: =p.

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

R, 36 39 bytes

Using a different method than the other R answer

function(x)sum(utf8ToInt(x)-96)==100

This is an unnamed function for lowercase characters. utf8ToInt(x) converts the string into a vector of ascii values. 96 is taken away from all items and sum checked against 100.

In use

> f=
+ function(x)sum(utf8ToInt(x)-96)==100
> f('buzzy')
[1] TRUE
> f('boycott')
[1] TRUE
> f('identifies')
[1] TRUE
> f('adiabatically')
[1] TRUE
> f('ttttt')
[1] TRUE
> f('zzz')
[1] FALSE
> f('zzzzzzz')
[1] FALSE
> f('abcdefghiljjjzz')
[1] FALSE
> f('tttt')
[1] FALSE
> f('basic')
[1] FALSE
\$\endgroup\$
5
  • \$\begingroup\$ You can save a few bytes by using sum(utf8ToInt(scan(,""))-96)==100 instead of defining a function using function(x). \$\endgroup\$
    – JAD
    Commented Apr 20, 2017 at 8:17
  • \$\begingroup\$ @JarkoDubbeldam Generally that would be considered a snippet as it requires a REPL to output the result. I would have to wrap a cat around it to make it a full program \$\endgroup\$
    – MickyT
    Commented Apr 20, 2017 at 18:19
  • \$\begingroup\$ is that new? Generally the implicit print when something is not assigned was considered 'output'. \$\endgroup\$
    – JAD
    Commented Apr 21, 2017 at 7:12
  • \$\begingroup\$ @JarkoDubbeldam Not that new I think, I've been pinged on it before \$\endgroup\$
    – MickyT
    Commented Apr 23, 2017 at 22:38
  • \$\begingroup\$ Odd, that never was mentioned on any of my submissions :x \$\endgroup\$
    – JAD
    Commented Apr 24, 2017 at 6:54
3
\$\begingroup\$

CJam, 9 bytes

q'`f-:+E=

Input should be in lowercase with a trailing newline. This is a tad cheaty as there's no reason to append a newline to the input, but it follows the rules as written.

Try it online!

How it works

q         e# Read all input from STDIN and push the resulting string on the stack.
 '`       e# Push the backtick character.
   f-     e# Subtract the backtick character from each input character. The
          e# difference of two characters is the difference of their code points,
          e# so this maps a..z to 1..26 and the newline to -86.
     :+   e# Take the sum.
       E  e# Push 14.
        = e# Test the sum and 14 for equality.
\$\endgroup\$
1
  • \$\begingroup\$ If it makes your program shorter and it doesn't really affect look, that's why it's in the rules :) \$\endgroup\$
    – Stephen
    Commented Apr 20, 2017 at 19:19
3
\$\begingroup\$

C++ (gcc), 45 bytes

Unnamed generic lambda, accepting char[], std::string or any other container of char and returning via reference parameter.

  • 0 means dollar word
  • everything else is no dollar word

Even works for capital letters ;>

[](auto&s,int&r){r=100;for(auto x:s)r-=x&31;}

Try it online!

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

Brachylog, 12 9 bytes

-3 bytes thanks to Fatalize

ạ-₉₆ᵐ+100

Takes input as a string of lowercase letters. Try it online!

Explanation

ạ          Convert input string to a list of ASCII codes
    ᵐ      To that list, map this predicate:
 -₉₆        Subtract 96 from each charcode
     +     Sum the resulting list of numbers 1-26
      100  Succeed if the sum is 100, fail otherwise
\$\endgroup\$
1
  • 2
    \$\begingroup\$ You can save 3 bytes like this: ạ-₉₆ᵐ+100 \$\endgroup\$
    – Fatalize
    Commented Mar 4, 2018 at 7:57
3
\$\begingroup\$

BQN, 13 12 bytes

-1 byte thanks to Razetime

100=0+´-⟜'`'

Anonymous tacit function that takes a lowercase string as its argument. Try it!

Explanation

       -⟜'`'  Subtract ` from each character of the argument; gives list of numbers 1-26
    0+´       Fold on addition, starting at 0
100=          1 if the result equals 100, 0 otherwise
\$\endgroup\$
4
  • \$\begingroup\$ with ·(nothing you can make the tacit version 12: 100=·+´-⟜'`' \$\endgroup\$
    – Razetime
    Commented Dec 10, 2021 at 10:10
  • \$\begingroup\$ Useful usescript: github.com/razetime/userscripts/blob/main/bqncgcc.user.js \$\endgroup\$
    – Razetime
    Commented Dec 10, 2021 at 10:10
  • 1
    \$\begingroup\$ Ah, thanks. I didn't realize fold had a dyadic version. I opted to use an explicit left argument of 0 instead because it was easier to explain. \$\endgroup\$
    – DLosc
    Commented Dec 10, 2021 at 16:28
  • \$\begingroup\$ nothing doesn't exactly count as a special case of fold, but rather a train function separator, so effectively BQN reads it as though it were your original solution: mlochbaum.github.io/BQN/try.html#code=MTAwPcK3K8K0LeKfnCdgJw== \$\endgroup\$
    – Razetime
    Commented Dec 10, 2021 at 17:15
3
\$\begingroup\$

R, 32 bytes

sum(strtoi(scan(,""),36)-9)==100

Try it online!

Since a word can be imported as a character array (==vector in R), here is a solution which converts the individual letters into the corresponding base36 number, subtracts 9 from each and sums up.

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

Go, 69 bytes

func(s string)bool{i:=0
for _,r:=range s{i+=int(r)-96}
return i==100}

Attempt This Online!

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

Java 7, 77 74 bytes

boolean c(String s){int r=0;for(int c:s.getBytes())r+=c&31;return r==100;}

Case-insensitive thanks to @Neil by changing c-96 to c&31.
-3 bytes thanks to @SuperChafouin.

Explanation:

Try it here.

boolean c(String s){          // Method with String parameter and boolean return-type
  int r=0;                    //  Resulting sum-integer
  for(int c:s.getBytes())     //  For each character in the String (as integer value)
    r+=c&31;                  //   Sum its alphabetic 1-indexed index (case-insensitive)
                              //  End of loop (implicit / single-line body)
  return r==100;              //  return if the sum equals 100
}                             // End of method
\$\endgroup\$
4
  • \$\begingroup\$ Could counting down from 100 save bytes? Also, &31 might work to make your code case insensitive. \$\endgroup\$
    – Neil
    Commented Apr 19, 2017 at 7:48
  • \$\begingroup\$ @Neil Thanks for the tip of making it case-insensitive. But counting down from 100 is the same byte-count: boolean c(String s){int r=100;for(int c:s.toCharArray())r-=c-96;return r==0;} \$\endgroup\$ Commented Apr 19, 2017 at 7:55
  • 1
    \$\begingroup\$ As input is a-z, maybe you can use getBytes ? \$\endgroup\$
    – Arnaud
    Commented Apr 19, 2017 at 8:27
  • \$\begingroup\$ Ah, of course, this is Java, it takes 3 bytes to compare to zero... \$\endgroup\$
    – Neil
    Commented Apr 19, 2017 at 8:33
2
\$\begingroup\$

jq, 28 characters

(25 characters code + 3 characters command line option)

[explode[]|.-96]|add==100

Sample run:

bash-4.3$ jq -R '[explode[]|.-96]|add==100' <<< 'bernardino'
true

On-line test

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

C#, 72 64 bytes


Golfed

(string w)=>{int s=0;foreach(var c in w)s+=c-96;return s==100;};

Ungolfed

( string w ) => {
   int s = 0;
   
   foreach( var c in w )
      s += c - 96;
   
   return s == 100;
};

Ungolfed readable

( string w ) => {
   // Initialize the var to store the sum of the chars
   //    of the given word
   int s = 0;
   
   // Cycle through each char
   foreach( var c in w )
   
      // Add it to the sum minus 96
      //    'a' == 97
      s += c - 96;
   
   // Return if the sum is equal, or not, to 100
   return s == 100;
};

Full code

using System;
using System.Collections.Generic;

namespace Namespace {
   class Program {
      static void Main( String[] args ) {
         Func<String, Boolean> f = ( string w ) => {
            int s = 0;
            
            foreach( var c in w )
               s += c - 96;
            
            return s == 100;
         };
         List<String>
            testCases = new List<String>() {
               "buzzy",
               "boycott",
               "identifies",
               "adiabatically",
               "ttttt",
               "zzz",
               "zzzzzzz",
               "abcdefghiljjjzz",
               "tttt",
               "basic",
            };

         foreach( String testCase in testCases ) {
            Console.WriteLine( $" Input: {testCase}\nOutput:{f( testCase )}\n" );
         }

         Console.ReadLine();
      }
   }
}

Releases

  • v1.1 - -8 bytes - Swapped the for loop with a foreach one
  • v1.0 - 72 bytes - Initial solution.

Notes

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

Groovy, 32 characters

{s->s*.value.sum{it[0]-96}==100}

Sample run:

groovy:000> ({s->s*.value.sum{it[0]-96}==100}("bernardino"))
===> true
\$\endgroup\$
2
\$\begingroup\$

braingasm, 20 bytes

Assumes input without trailing newlines, e.g echo -n "unaltered" | braingasm dollarwords.bg

,[96-[->+<],]>100-z:

Could easily been done in plain brainfuck, if I had the patience for it.

,                       Read a byte to the current cell.
 [                      While there's something in the current cell:
  96-                     subtract 96 from it,
     [->+<]               move the remainder to the next cell,
           ,              read another byte.
            ]           
             >100-      Move to next cell and subtract 100 from it,
                  z:    print 1 if remainder is zero, 0 if not.
\$\endgroup\$
2
\$\begingroup\$

Brain-Flak, 92 bytes

({{}[(((((()()()){}){}){}){}){}]})((((((()()()){}){}){}()){}){})({}[{}]<(())>){((<{}{}>))}{}

Try it online!

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

PHP, 45 Bytes

combination 3 Bytes saved by @user63956

input as argument list

for(;$s+=ord($argv[++$i])%32?:die($s==100););

input as string using the -R option

for(;$s+=ord($argn[$i++])%32?:die($s==100););

PHP, 48 Bytes

combination

for(;$c=$argv[++$i];)$s+=ord($c)%32;echo$s==100;

PHP, 50 Bytes

Running PHP from the commandline without a file $argv[0] is "-"

prints 1 for true and nothing for false

lowercase

echo array_sum(array_map(ord,$argv))-96*$argc==49;

uppercase

echo array_sum(array_map(ord,$argv))-64*$argc==81;
\$\endgroup\$
4
  • 1
    \$\begingroup\$ for(;$s+=ord($argv[++$i])%32?:die($s==100);); saves 3 bytes. \$\endgroup\$
    – user63956
    Commented Apr 19, 2017 at 6:07
  • \$\begingroup\$ @user63956 nice trick thank you \$\endgroup\$ Commented Apr 19, 2017 at 10:29
  • 1
    \$\begingroup\$ You should either mention that you take the letters as distinct command line arguments or use -R and $argn[$i++]. \$\endgroup\$
    – Titus
    Commented Apr 20, 2017 at 13:22
  • \$\begingroup\$ @Titus Done If you find other improvements you can edit it by yourself \$\endgroup\$ Commented Apr 20, 2017 at 13:45
2
\$\begingroup\$

Rust, 158 bytes

fn main(){let(i,mut c)=(&mut"".into(),0);std::io::stdin().read_line(i);let l:Vec<u8>=i.trim().bytes().collect();for b in&l{c+=*b as u8-96}print!("{}",c==100)}

Ungolfed

fn main() {
    let (input_string, mut count) = (&mut "".into(), 0);
    std::io::stdin().read_line(input_string);
    let bytes: Vec<u8> = input_string.trim().bytes().collect();
    for byte in &bytes {
        count += *byte as u8 - 96;
    }
    print!("{}", count == 100);
}

Prints true if the input is a dollar word, and false if it is not. Only works on lower case letters.

\$\endgroup\$
3
  • \$\begingroup\$ I don't believe that zero is considered a truthy value in rust. Here is how we generally define truthy. It is not the clearest, which is why we have been moving away from the terminology lately but I don't think it includes 0. \$\endgroup\$
    – Wheat Wizard
    Commented Mar 21, 2018 at 3:44
  • \$\begingroup\$ You can also likely save a byte by using *b as u8 instead of u64. Unless I am missing something. \$\endgroup\$
    – Wheat Wizard
    Commented Mar 21, 2018 at 3:46
  • \$\begingroup\$ Thanks for the clarification. I've edited my answer \$\endgroup\$ Commented Mar 21, 2018 at 4:20
2
\$\begingroup\$

Stax, 9 bytes

ü☺ïΦΘ╬╟₧╘

I love the ☺ in the code! Potential Stax/Emojicode polyglot in the future...?

Run and debug it at staxlang.xyz!

Unpacked (10 bytes):

{64-m|+AJ=

Explanation:

{   m         Form a block and map it over input.
 64-            Push 64 and subtract it from the character code.
     |+       Sum the resulting array.
       AJ     Push 10 and square it.
         =    Check for equality!

I'm quite new to Stax (and golfing languages); this can almost certainly be shortened. I think the { can probably be dropped with proper placement of the m...

Another approach (imperative, 10 bytes):

ú☼7Tí:£_▓δ

12 unpacked:

c|+AJ-s%64*=
c               Copy the top item on the stack.
 |+             Sum it. For a string, this sums code points.
   AJ           Push 10 and square, yielding 100.
     -          Subtract this 100 from the earlier sum.
      s         Swap the top two items, putting the input back on top.
       %        Push the input's length.
        64*     Push 64 and multiply.
           =    Check for equality!

Input for both is capital letters. For lowercase, replace 64 with 96.

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

R, 45 bytes

sum(as.numeric(charToRaw(scan(,"")))-96)==100

Try it online!

Returns TRUE or FALSE.

As stated in the question, expects user input in a-z, converts the characters to numbers, brings them to the 1–26 range, and checks if the sum is equal to 100.

Even if one converts it to a function, it still beats Giuseppe’s answer in terms of byte count (it would be 48):

function(x)sum(as.numeric(charToRaw(x))-96)==100
\$\endgroup\$
2
\$\begingroup\$

TI-Basic, 53 bytes

ᴇ2=sum(seq(inString("ABCDEFGHIJKLMNOPQRSTUVWXYZ",sub(Ans,I,1)),I,1,length(Ans

Takes input in uppercase in Ans. Output is stored in Ans and displayed.

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

MathGolf, 6 bytes

$♥%Σ♀=

Input as a list of characters. Input casing is irrelevant; can be lower-, upper-, or even mixed cased.

Try it online.

Explanation:

$      # Convert each character in the (implicit) input-list to a codepoint-integer
 ♥%    # Modulo-32 on each
   Σ   # Sum the list together
    ♀= # And check if it's equal to 100
       # (after which the entire stack is output implicitly as result)
\$\endgroup\$
2
\$\begingroup\$

Whitespace, 105 104 bytes

[S S S N
_Push_0][N
S S N
_Create_Label_LOOP][S N
S _Duplicate_sum][S N
S _Duplicate_sum][S N
S _Duplicate_sum][T N
T   S _Read_STDIN_as_Character][T   T   T   _Retrieve_input][S N
S _Duplicate_input][S S S T S T S N
_Push_10][T S S T   _Subtract][N
T   S S N
_If_0_Jump_to_Label_PRINT][S S S T  S S S S S N
_Push_32][T S T T   _Modulo][T  S S S _Add][N
S N
N
_Jump_to_Label_LOOP][N
S S S N
_Create_Label_PRINT][S N
S _Duplicate_10][T  S S N
_Multiply][T    S S T   _Subtract][S N
S _Duplicate][N
T   S T N
_If_0_Jump_to_Label_TRUTHY][S S T   T   N
_Push_-1][N
S S T   N
_Create_Label_TRUTHY][T N
S T _Print_as_integer]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Input casing is irrelevant; can be lower-, upper-, or even mixed cased.
Since Whitespace can only read input one character at a time, the input must contain a trailing newline so we'll know when we're done reading characters.
Whitespace also lacks truthy/falsey values, so this program will output 0 for truthy and -1 for falsey (could be 5 bytes less with 0 as truthy and any other integer as falsey).

Try it online (with raw spaces, tabs and new-lines only).

Explanation in pseudo-code:

Integer sum = 0
LOOP:
  Integer c = STDIN as character-codepoint
  If(c == '\n'):
    Jump to PRINT
  c = c modulo-32
  sum = sum + c
  Jump to LOOP

PRINT:
  If(sum == c*c):   // Note: c=10 at this point, which is the codepoint for '\n'
    Integer result = 0
    Jump to TRUTHY
  result = -1
  TRUTHY:
    Print result as integer to STDOUT
\$\endgroup\$
2
\$\begingroup\$

APOL, 33 bytes

v(0);f(i ∆(0 +(⌕(ⓛ ∋) 1)));=(⁰ ô)

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

Julia 1.0, 19 bytes

s->sum(s.-'`')==100

Try it online!

Takes input as an array of characters (since the question explicitly allows that).

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

Uiua SBCS, 9 bytes

=100/+-@`

Try it!

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

J 14 bytes or 10 bytes if rules allow!!!

100=+/_96+a.i.

        100=+/_96+a.i. 'identifies'
    1
        100=+/_96+a.i. 'ttttt'
    1
        100=+/_96+a.i. 'wrong'
    0

    Because:
                        a.i. gives position in code table 0-255
                    _96+ subtracts 96 ('a') from each value
                +/   adds them all together
            100= compares with 100  

IDEA: Can a "truthy" value be 100, and a "falsey value be <> 100?

If so...

+/_96+a.i.
    
    +/_96+a.i. 'boycott'
    100
    +/_96+a.i. 'boycot'
    80

:O)

\$\endgroup\$
5
  • \$\begingroup\$ We have a rather specific definition of truthy/falsy, so I'm afraid calling 100 truthy and non-100 falsy is not allowed. 100=+/_96+a.i. is also not valid, albeit just because of a technicality: by default, submissions have to be full programs or functions. Tacit verbs are usually the shortest option. \$\endgroup\$
    – Dennis
    Commented Apr 19, 2017 at 0:57
  • \$\begingroup\$ I don't understand your second point. What is the difference between my attempt and the Dialog APL solution directly above it??? \$\endgroup\$ Commented Apr 19, 2017 at 3:50
  • \$\begingroup\$ I also don't understand your first point. In the discussion, it was proposed: if (x) { print "x is truthy"; } else { print "x is falsy"; }. With my idea, substitute "answer is 100" for x and all is well. \$\endgroup\$ Commented Apr 19, 2017 at 3:53
  • \$\begingroup\$ Hadn't seen the APL answer; left a comment there as well. \$\endgroup\$
    – Dennis
    Commented Apr 19, 2017 at 7:28
  • 2
    \$\begingroup\$ You don't get to define which check to perform to determine truthiness; your language does. For J, non-zero integers are truthy and zero is falsy. tio.run/nexus/… \$\endgroup\$
    – Dennis
    Commented Apr 19, 2017 at 7:29
1
\$\begingroup\$

Ruby 2.4, 30 + 1 = 31 bytes

One extra byte for the n flag: $ ruby -n dollar_words.rb

p chop.bytes.sum{|c|c-96}==100

Prints true or false for each given line of input:

$ ruby -n dollar_words.rb 
zzzzz
false
unaltered
true
dollar
false
\$\endgroup\$
1
\$\begingroup\$

QBIC, 34 bytes

;[_lA||p=p+asc(_sA,a,1|)%32]?p=100

Explanation:

;               Get A$ from the cmd-line
[_lA||          FOR a = 0; a <= a$.Length(); a++
p=p+asc(        Increment P with the ASCII value of
_sA,a,1|)%32]     A$.Substring(a,1) mod 32 (thank you @JonathanAllan, that's a neat trick!)
?p=100          When done, print -1 if p is 100, 0 otherwise
\$\endgroup\$
1
\$\begingroup\$

Pyth, 16

!-100+Fm+1xGdcQ1

Try online.

\$\endgroup\$

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.