Skip to main content
Prettify hint is "cpp" rather than the language name
Source Link
Toby Speight
  • 6.5k
  • 1
  • 24
  • 41

C++, 76 bytes

This assumes the input is in ASCII (or any encoding that's compatible for letters, e.g. UTF-8 or ISO-8859.1). Input may be in either case.

[](auto s,int&i){i=0;for(int c:s)i+="DDFDEEECHFBCFBDADBEGCEFCDBD"[c&31]-68;}
[](auto s,int&i){i=0;for(int c:s)i+="DDFDEEECHFBCFBDADBEGCEFCDBD"[c&31]-68;}

It's a function that accepts a string and and integer reference. The integer gets assigned a positive number (truthy) for dot-heavy input or a negative number (falsey) for dot-light input. Balanced input returns zero.

The magic string encodes the weight of each of A..Z, offset by D (i.e. D represents a Morse-balanced letter). That's the 68 we subtract from each value.

I did attempt encoding two Morse symbols per char in the magic string, but the cost of unpacking was too great.

Test program:

auto f = [](auto s,int&i){i=0;for(int c:s)i+="DDFDEEECHFBCFBDADBEGCEFCDBD"[c&31]-68;};

#include <iostream>
#include <string>

int main(int argc, char **argv)
{
    int i;
    while (*++argv) {
        std::string s{*argv};
        f(s, i);
        std::cout << s << ": " << i << '\n';
    }
}
auto f = [](auto s,int&i){i=0;for(int c:s)i+="DDFDEEECHFBCFBDADBEGCEFCDBD"[c&31]-68;};

#include <iostream>
#include <string>

int main(int argc, char **argv)
{
    int i;
    while (*++argv) {
        std::string s{*argv};
        f(s, i);
        std::cout << s << ": " << i << '\n';
    }
}

Output:

./181318 S k HELLO code
S: 3
k: -1
HELLO: 6
code: -1

C++, 76 bytes

This assumes the input is in ASCII (or any encoding that's compatible for letters, e.g. UTF-8 or ISO-8859.1). Input may be in either case.

[](auto s,int&i){i=0;for(int c:s)i+="DDFDEEECHFBCFBDADBEGCEFCDBD"[c&31]-68;}

It's a function that accepts a string and and integer reference. The integer gets assigned a positive number (truthy) for dot-heavy input or a negative number (falsey) for dot-light input. Balanced input returns zero.

The magic string encodes the weight of each of A..Z, offset by D (i.e. D represents a Morse-balanced letter). That's the 68 we subtract from each value.

I did attempt encoding two Morse symbols per char in the magic string, but the cost of unpacking was too great.

Test program:

auto f = [](auto s,int&i){i=0;for(int c:s)i+="DDFDEEECHFBCFBDADBEGCEFCDBD"[c&31]-68;};

#include <iostream>
#include <string>

int main(int argc, char **argv)
{
    int i;
    while (*++argv) {
        std::string s{*argv};
        f(s, i);
        std::cout << s << ": " << i << '\n';
    }
}

Output:

./181318 S k HELLO code
S: 3
k: -1
HELLO: 6
code: -1

C++, 76 bytes

This assumes the input is in ASCII (or any encoding that's compatible for letters, e.g. UTF-8 or ISO-8859.1). Input may be in either case.

[](auto s,int&i){i=0;for(int c:s)i+="DDFDEEECHFBCFBDADBEGCEFCDBD"[c&31]-68;}

It's a function that accepts a string and and integer reference. The integer gets assigned a positive number (truthy) for dot-heavy input or a negative number (falsey) for dot-light input. Balanced input returns zero.

The magic string encodes the weight of each of A..Z, offset by D (i.e. D represents a Morse-balanced letter). That's the 68 we subtract from each value.

I did attempt encoding two Morse symbols per char in the magic string, but the cost of unpacking was too great.

Test program:

auto f = [](auto s,int&i){i=0;for(int c:s)i+="DDFDEEECHFBCFBDADBEGCEFCDBD"[c&31]-68;};

#include <iostream>
#include <string>

int main(int argc, char **argv)
{
    int i;
    while (*++argv) {
        std::string s{*argv};
        f(s, i);
        std::cout << s << ": " << i << '\n';
    }
}

Output:

./181318 S k HELLO code
S: 3
k: -1
HELLO: 6
code: -1
Simplify the test program
Source Link
Toby Speight
  • 6.5k
  • 1
  • 24
  • 41

C++, 76 bytes

This assumes the input is in ASCII (or any encoding that's compatible for letters, e.g. UTF-8 or ISO-8859.1). Input may be in either case.

[](auto s,int&i){i=0;for(int c:s)i+="DDFDEEECHFBCFBDADBEGCEFCDBD"[c&31]-68;}

It's a function that accepts a string and and integer reference. The integer gets assigned a positive number (truthy) for dot-heavy input or a negative number (falsey) for dot-light input. Balanced input returns zero.

The magic string encodes the weight of each of A..Z, offset by D (i.e. D represents a Morse-balanced letter). That's the 68 we subtract from each value.

I did attempt encoding two Morse symbols per char in the magic string, but the cost of unpacking was too great.

Test program:

auto f = [](auto s,int&i){i=0;for(int c:s)i+="DDFDEEECHFBCFBDADBEGCEFCDBD"[c&31]-68;};

#include <iostream>
#include <string>

int main(int argc, char **argv)
{
    int i;
    while (*++argv) {
        std::string s{*argv};
        f(s, i);
        std::cout << s << ": " << (f(s,i),i) << '\n';
    }
}

Output:

./181318 S k HELLO code
S: 3
k: -1
HELLO: 6
code: -1

C++, 76 bytes

This assumes the input is in ASCII (or any encoding that's compatible for letters, e.g. UTF-8 or ISO-8859.1). Input may be in either case.

[](auto s,int&i){i=0;for(int c:s)i+="DDFDEEECHFBCFBDADBEGCEFCDBD"[c&31]-68;}

It's a function that accepts a string and and integer reference. The integer gets assigned a positive number (truthy) for dot-heavy input or a negative number (falsey) for dot-light input. Balanced input returns zero.

The magic string encodes the weight of each of A..Z, offset by D (i.e. D represents a Morse-balanced letter). That's the 68 we subtract from each value.

I did attempt encoding two Morse symbols per char in the magic string, but the cost of unpacking was too great.

Test program:

auto f = [](auto s,int&i){i=0;for(int c:s)i+="DDFDEEECHFBCFBDADBEGCEFCDBD"[c&31]-68;};

#include <iostream>
#include <string>

int main(int argc, char **argv)
{
    int i;
    while (*++argv) {
        std::string s{*argv};
        std::cout << s << ": " << (f(s,i),i) << '\n';
    }
}

Output:

./181318 S k HELLO code
S: 3
k: -1
HELLO: 6
code: -1

C++, 76 bytes

This assumes the input is in ASCII (or any encoding that's compatible for letters, e.g. UTF-8 or ISO-8859.1). Input may be in either case.

[](auto s,int&i){i=0;for(int c:s)i+="DDFDEEECHFBCFBDADBEGCEFCDBD"[c&31]-68;}

It's a function that accepts a string and and integer reference. The integer gets assigned a positive number (truthy) for dot-heavy input or a negative number (falsey) for dot-light input. Balanced input returns zero.

The magic string encodes the weight of each of A..Z, offset by D (i.e. D represents a Morse-balanced letter). That's the 68 we subtract from each value.

I did attempt encoding two Morse symbols per char in the magic string, but the cost of unpacking was too great.

Test program:

auto f = [](auto s,int&i){i=0;for(int c:s)i+="DDFDEEECHFBCFBDADBEGCEFCDBD"[c&31]-68;};

#include <iostream>
#include <string>

int main(int argc, char **argv)
{
    int i;
    while (*++argv) {
        std::string s{*argv};
        f(s, i);
        std::cout << s << ": " << i << '\n';
    }
}

Output:

./181318 S k HELLO code
S: 3
k: -1
HELLO: 6
code: -1
Source Link
Toby Speight
  • 6.5k
  • 1
  • 24
  • 41

C++, 76 bytes

This assumes the input is in ASCII (or any encoding that's compatible for letters, e.g. UTF-8 or ISO-8859.1). Input may be in either case.

[](auto s,int&i){i=0;for(int c:s)i+="DDFDEEECHFBCFBDADBEGCEFCDBD"[c&31]-68;}

It's a function that accepts a string and and integer reference. The integer gets assigned a positive number (truthy) for dot-heavy input or a negative number (falsey) for dot-light input. Balanced input returns zero.

The magic string encodes the weight of each of A..Z, offset by D (i.e. D represents a Morse-balanced letter). That's the 68 we subtract from each value.

I did attempt encoding two Morse symbols per char in the magic string, but the cost of unpacking was too great.

Test program:

auto f = [](auto s,int&i){i=0;for(int c:s)i+="DDFDEEECHFBCFBDADBEGCEFCDBD"[c&31]-68;};

#include <iostream>
#include <string>

int main(int argc, char **argv)
{
    int i;
    while (*++argv) {
        std::string s{*argv};
        std::cout << s << ": " << (f(s,i),i) << '\n';
    }
}

Output:

./181318 S k HELLO code
S: 3
k: -1
HELLO: 6
code: -1