47
\$\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\$
  • 20
    \$\begingroup\$ Title has dollar words in it, sorry if it threw you off. \$\endgroup\$ – Stephen Apr 18 '17 at 20:56

70 Answers 70

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\$
  • \$\begingroup\$ You can save a few bytes by using sum(utf8ToInt(scan(,""))-96)==100 instead of defining a function using function(x). \$\endgroup\$ – JAD Apr 20 '17 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 Apr 20 '17 at 18:19
  • \$\begingroup\$ is that new? Generally the implicit print when something is not assigned was considered 'output'. \$\endgroup\$ – JAD Apr 21 '17 at 7:12
  • \$\begingroup\$ @JarkoDubbeldam Not that new I think, I've been pinged on it before \$\endgroup\$ – MickyT Apr 23 '17 at 22:38
  • \$\begingroup\$ Odd, that never was mentioned on any of my submissions :x \$\endgroup\$ – JAD Apr 24 '17 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\$
  • \$\begingroup\$ If it makes your program shorter and it doesn't really affect look, that's why it's in the rules :) \$\endgroup\$ – Stephen Apr 20 '17 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\$
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\$
  • \$\begingroup\$ Could counting down from 100 save bytes? Also, &31 might work to make your code case insensitive. \$\endgroup\$ – Neil Apr 19 '17 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\$ – Kevin Cruijssen Apr 19 '17 at 7:55
  • 1
    \$\begingroup\$ As input is a-z, maybe you can use getBytes ? \$\endgroup\$ – Arnaud Apr 19 '17 at 8:27
  • \$\begingroup\$ Ah, of course, this is Java, it takes 3 bytes to compare to zero... \$\endgroup\$ – Neil Apr 19 '17 at 8:33
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\$

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\$
  • 1
    \$\begingroup\$ for(;$s+=ord($argv[++$i])%32?:die($s==100);); saves 3 bytes. \$\endgroup\$ – user63956 Apr 19 '17 at 6:07
  • \$\begingroup\$ @user63956 nice trick thank you \$\endgroup\$ – Jörg Hülsermann Apr 19 '17 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 Apr 20 '17 at 13:22
  • \$\begingroup\$ @Titus Done If you find other improvements you can edit it by yourself \$\endgroup\$ – Jörg Hülsermann Apr 20 '17 at 13:45
2
\$\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\$
  • 2
    \$\begingroup\$ You can save 3 bytes like this: ạ-₉₆ᵐ+100 \$\endgroup\$ – Fatalize Mar 4 '18 at 7:57
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\$
  • \$\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\$ – Sriotchilism O'Zaic Mar 21 '18 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\$ – Sriotchilism O'Zaic Mar 21 '18 at 3:46
  • \$\begingroup\$ Thanks for the clarification. I've edited my answer \$\endgroup\$ – dragonite44 Mar 21 '18 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\$
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\$
  • \$\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 Apr 19 '17 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\$ – Richard Donovan Apr 19 '17 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\$ – Richard Donovan Apr 19 '17 at 3:53
  • \$\begingroup\$ Hadn't seen the APL answer; left a comment there as well. \$\endgroup\$ – Dennis Apr 19 '17 at 7:28
  • 1
    \$\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 Apr 19 '17 at 7:29
1
\$\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\$
1
\$\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\$
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\$
1
\$\begingroup\$

Röda, 25 bytes

{[ord(_)-96]|sum|[_=100]}

Try it online!

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

Batch, 189 bytes

@set/p,=
@set z=1
@for %%a in (a b c d e f g h i j k l m n o p q r s t u v w z y)do @set/a%%a=z,z+=1
@set .=100
:l
@set/a.-=%,:~,1%
@set ,=%,:~1%
@if not "%,%"=="" goto l
@exit/b%.%

Takes lowercase input on STDIN and ouputs via ERRORLEVEL (0 is truthy). Batch has no character code operator so I set the variables a-z to their value in a loop, then I individually evaluate all the characters in the input word and subtract them all from 100.

By using non-alphabetic variable names the code is readily extended to handle uppercase input.

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

Pyth, 9 bytes

q100smhxG

Explanation:

q100smhxGdQ autofill variables
       xGd  [index of d in "abcdefghijklmnopqrstuvwxyz"
      h      + 1
     m    Q  for d in eval(input())]
    s       sum(^)
q100        == 100

Test suite.

\$\endgroup\$
  • \$\begingroup\$ Came to the exact same program, but you need to count the d for 10 bytes, no? \$\endgroup\$ – Dave Aug 18 '17 at 15:56
  • \$\begingroup\$ Nope! d automatically gets filled in, since it's inside m. Variables inside lambdas autofill to the lambda's parameter (the first input in the case of two parameters). Look at what Python code results from this Pyth. \$\endgroup\$ – Steven H. Aug 22 '17 at 15:35
  • \$\begingroup\$ Fixed arity continues to be the greatest gift to the code golf in history, dang! \$\endgroup\$ – Dave Aug 22 '17 at 16:23
1
\$\begingroup\$

Excel, 52 + 2 bytes

{=SUM(IFERROR(CODE(MID(A1,ROW(A1:A100),1))-64,0))=100}

Input is in cell A1 in all uppercase.
Must be entered as an array formula with Ctrl+Shift+Enter (Adds the curly brackets { }).
Returns TRUE for dollar words and FALSE for others.

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

Rip, 27 bytes

10gDiW[8DmsagDi]P9iDmsI[d]O

Works only for uppercase input, which it reads from stdin. Please do not end the input with a newline. :)

Explanation:

1                            0I[Push a 1 for later]
 0                           0I[Sum is 0 now]
  gDiW[                      0I[Getchar, duplicate, increment, while: while not EOF]
       8Dms                  0I[8 8 dup mul subtract: subtract 65]
           a                 0I[Add to the current total]
            gDi]             0I[Getchar, dup, increment, end while: while not EOF]
                P            0I[Pop the last -1 (=EOF) read]
                 9iDms       0I[Subtract 100 (9 incr dup mul subtract)]
                      I[d]   0I[If not zero, decrement the first 1 (see first line) to a 0]
                          O  0I[Output the result (1 if ==100, 0 else) as a number]
\$\endgroup\$
1
\$\begingroup\$

C# / Linq, 42 bytes

Is Linq cheating?

(string w)=>{return w.Sum(x=>x-96)==100;};
\$\endgroup\$
  • \$\begingroup\$ Given Linq is a default .NET library it might be cheating a little, but you should be fine given you specified it's C# with Linq. Also you don't need string before w, or parentheses around it, you can just do w=>{...}; \$\endgroup\$ – Skidsdev Jun 2 '17 at 15:34
  • \$\begingroup\$ Based on other Java/C# answers, this format seems OK w=>w.Sum(x=>x-96)==100. I can see how the compiler having to infer the type of w from somewhere being a little odd though. \$\endgroup\$ – dana Dec 28 '18 at 2:21
1
\$\begingroup\$

Braingolf, 13 bytes [non.competing]

{#`-}&+#de1:0

Try it online!

Requires all lowercase input, outputs 1 for dollar word, 0 otherwise

Explanation

{#`-}&+#de1:0  Implicit input of string as char values to stack
{...}          Foreach item on stack..
 #`            ..Push char value of ` (96)
   -           ..Subtract from input char value
     &+        Sum entire stack
       #d      Push char value of d (100)
         e     If last 2 items are equal..
          1    ..Push 1
           :   else
            0  ..Push 0
               Implicit output of last item on stack
\$\endgroup\$
1
\$\begingroup\$

Proton, 32 bytes

map(ord+((-)&96))+sum+((==)&100)

Try it online!

Waiting for pull from TIO.

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

Perl 5, 28 + 1 (-p) = 29 bytes

s/./96-ord$&/ge;$_=-100^eval

Try it online!

Returns 0 for truthy, anything else for falsey. Assumes lowercase input.

How?

Replace each character with the additive inverse of its position in the alphabet. Then, evaluate that equation and XOR it with -100 to determine if this is a dollar word.

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

Common Lisp, 56 bytes

(=(loop as x across(read-line)sum(-(char-code x)96))100)

Try it online!

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

SmileBASIC, 48 bytes

INPUT W$WHILE""<W$D=D+ASC(POP(W$))-64WEND?D==100

The classic string eating loop.

\$\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.