23
\$\begingroup\$

A string is dot-heavy when its morse representation contains more dots than dashes. For example, the letter E is a single dot, which means it is Dot-heavy.

Input

  • The input string will only contain characters in the range of [a-z] or [A-Z]. You can decide if they should all be upper case, or all lower case. AAA is fine, aaa is fine, aAa is not.
  • The input string will always be at least 1 character in length.
  • You may assume that input strings will never have an equal amount of dots and dashes.

Output

You should return Truthy for inputs that contain more dot characters.
You should return Falsy for inputs that contain more dash characters.
Edit: I will allow a positive value for dot and a negative value for dash as well.

Test cases

| input | morse representation | result          |
|------------------------------------------------|
| S     | ...                  | Truthy          |
| k     | -.-                  | Falsy           |
| HELLO | .... . .-.. .-.. --- | Truthy          |
| code  | -.-. --- -.. .       | Falsy           |

Reference

International Morse Code

This is . Shortest code in bytes wins.

\$\endgroup\$
3
  • \$\begingroup\$ Related \$\endgroup\$ Mar 11, 2019 at 16:35
  • 4
    \$\begingroup\$ Can we return a value above 0 for dotheavy and a negative value for dash-heavy? \$\endgroup\$
    – Gymhgy
    Mar 11, 2019 at 20:20
  • \$\begingroup\$ @EmbodimentofIgnorance That works for me, as long as you specify it in your post. I don't think it usually passes the truthy falsy test but it feels like a good solution in this case so I will allow it \$\endgroup\$ Mar 12, 2019 at 8:38

30 Answers 30

9
\$\begingroup\$

x86-16 machine code, 42 bytes

00000000: fc32 e4bb 1c01 ac24 1fd0 e8d7 7206 51b1  .2.....$....r.Q.
00000010: 04d2 e859 240f 2c03 02e0 e2ea c353 4452  ...Y$.,......SDR
00000020: 7412 5130 3146 2452 3100                 t.Q01F$R1.

Listing:

FC          CLD                 ; LODS string functions forward
32 E4       XOR  AH, AH         ; running sum = 0
BB 0119     MOV  BX, OFFSET TBL-1 ; offset data by -1 byte for one-based index 
        LETTER_LOOP: 
AC          LODSB               ; load next char from DS:SI into AL, advance SI 
24 1F       AND  AL, 1FH        ; convert ASCII letter to one-based index 
D0 E8       SHR  AL, 1          ; divide index by 2, CF=1 if odd index 
D7          XLAT                ; look up letter in table 
72 06       JC   ODD            ; odd index use low nibble; even use high
51          PUSH CX             ; save loop counter 
B1 04       MOV  CL, 4          ; set up right shift for 4 bits 
D2 E8       SHR  AL, CL         ; bit shift right 
59          POP  CX             ; restore loop counter 
        ODD: 
24 0F       AND  AL, 0FH        ; isolate low nibble 
2C 03       SUB  AL, 3          ; dash/dot difference remove +3 positive bias 
02 E0       ADD  AH, AL         ; add difference to sum, set result flags 
E2 EA       LOOP LETTER_LOOP    ; loop until end of input 
C3          RET                 ; return to caller

        TBL:
            DB   53H, 44H, 52H, 74H, 12H, 51H
            DB   30H, 31H, 46H, 24H, 52H, 31H, 30H 

Explanation

Input: string at DS:SI (upper or lower case OK). Output: Truthy if SF == OF (use JG or JL to test).

The letter difference table values are stored as binary nibbles, so only takes 13 bytes in total. One byte can be saved by converting the input ASCII into a one-based index ('a' = 1, etc), however that would offset the table lookup index by one byte, not one nibble. Fortunately for us, the opcode of the previous RET instruction is C3, so that 3 is used for the 'a' = 1 nibble index.

Example Output:

enter image description here

\$\endgroup\$
5
  • \$\begingroup\$ I might be missing something, but does that macro not assume AH = 0 upon entry? Granted, that assumption is valid when using the test program. \$\endgroup\$
    – gastropner
    Mar 12, 2019 at 18:07
  • 1
    \$\begingroup\$ Good eye! The assumption is based on DOS execution initial startup register values, which for almost all versions of DOS is 0000h for AX source: fysnet.net/yourhelp.htm \$\endgroup\$
    – 640KB
    Mar 12, 2019 at 18:24
  • \$\begingroup\$ From one assembly golfer to another: nice! Extra style points for using purely 8088-compatible instructions. That's a platform where code golfing is largely equivalent to optimization, and truly a lost art. Nice use of XLAT to do exactly what it is meant to do. If you were actually optimizing for speed over size, you'd want to do WORD-sized lookups. This is still a speed win even on the 8088 with its anemic 8-bit external bus, because you are doubling the throughput without increasing the code size, save for an XCHG instruction or two. \$\endgroup\$ Mar 12, 2019 at 23:31
  • \$\begingroup\$ @CodyGray thanks! It's always fun when a challenge lines up nicely with the platform and instruction set. Plus it is neat when you can accomplish something on the original PC's 8088 in 1 byte (such as XLAT), even though it takes 6 bytes to do a bitwise shift right 4 places (inside a LOOP). \$\endgroup\$
    – 640KB
    Mar 13, 2019 at 15:41
  • \$\begingroup\$ Yup. For performance, you would definitely want to do 4 separate shifts by 1, eliminating the push and pop. It's not even that many more bytes (+2), so overall a net win, but not good for golfing. The real fun comes when the challenge doesn't line up with the ISA, and you have to stretch your mind to find new, innovative ways of applying the existing building blocks. The 1-byte string instructions are really nice on 8088 for performance and also golfing. I use them in real code. XLAT is one I don't often find much use for, I guess because modern architectures have biased me against LUTs. \$\endgroup\$ Mar 13, 2019 at 22:25
7
\$\begingroup\$

APL (Dyalog Extended), 24 15 bytesSBCS

-9 thanks to Ven

Anonymous tacit prefix function taking uppercase as argument.

>/'.-'⍧∊∘⌂morse

Try it online!

⌂morse convert to list of Morse strings
 then
ϵnlist (flatten)
'.-'⍧ count the number of dots and dashes in that
>/ more dots than dashes? (lit. greater-than reduction)

\$\endgroup\$
2
  • \$\begingroup\$ why not have Extended preload dfns by default? \$\endgroup\$
    – ngn
    Mar 13, 2019 at 19:53
  • \$\begingroup\$ @ngn It is now built-in \$\endgroup\$
    – Adám
    Apr 11, 2019 at 12:02
7
\$\begingroup\$

Java (JDK), 131 124 110 84 64 bytes

Interestingly, "dot" is dash-heavy and "dash" is dot-heavy.

Takes input in all caps as an IntStream (scroll down for a version with an actual String for an extra 8 bytes). I've had quite a lot of help golfing this one: Thanks to Expired Data for golfing 20 bytes, to Neil for golfing 26 bytes, to Olivier Grégoire for golfing 18 bytes and to Kevin Cruijssen for golfing 2 bytes.

Contains 26 unprintable characters inside the double quotes.

c->c.map(a->"".charAt(a-65)-4).sum()>0

Try it online!

Ungolfed:

c -> // lambda taking input as an IntStream in upper case and returning a boolean
  c.map(a -> "" // map each character's ASCII value to its net dot impact (unprintable characters here)
    .charAt(a - 65) // translate the ASCII code into a zero-based index into the above string (65 is 'A')
    - 4) // unprintables are > 0, this restores the proper values
  .sum() > 0 // add up all the values, positive sum indicates a dot-heavy input string

Java (JDK), 131 124 110 84 72 bytes

For purists; takes input as a String. Thanks to Expired Data for golfing 20 bytes, to Neil for golfing 26 bytes and to Olivier Grégoire for golfing 10 bytes.

s->s.chars().map(a->"".charAt(a-65)-4).sum()>0

Try it online.

Ungolfed:

s -> // lambda taking input as a String in upper case and returning a boolean
  s.chars() // convert to a stream of characters
  .map(a -> "" // map each character's ASCII value to its net dot impact (unprintable characters here)
    .charAt(a - 65) // translate the ASCII code into a zero-based index into the above string (65 is 'A')
    - 4) // unprintables are > 0, this restores the proper values
  .sum() > 0 // add up all the values, positive sum indicates a dot-heavy input string
\$\endgroup\$
9
  • 1
    \$\begingroup\$ 124 bytes \$\endgroup\$ Mar 11, 2019 at 17:32
  • 1
    \$\begingroup\$ Make that 111 bytes \$\endgroup\$ Mar 11, 2019 at 21:53
  • 2
    \$\begingroup\$ Why not use "35344527512513031462452313".charAt(a-65)-51? \$\endgroup\$
    – Neil
    Mar 11, 2019 at 22:13
  • 1
    \$\begingroup\$ 66 bytes \$\endgroup\$ Mar 12, 2019 at 9:50
  • 1
    \$\begingroup\$ @OlivierGrégoire Your 66-byte is actually 65, since you forgot to remove the trailing semi-colon. 1 more byte can be saved by using unprintable characters however: 64 bytes \$\endgroup\$ Mar 12, 2019 at 10:18
5
\$\begingroup\$

Jelly, 21 bytes

Oị“ÆġwıMƥ)ɠịṙ{’D¤Æm>4

Try it online!

How?

Oị“ÆġwıMƥ)ɠịṙ{’D¤Æm>4 - Link: list of characters ([A-Z]), S
                ¤     - nilad followed by link(s) as a nilad:
  “ÆġwıMƥ)ɠịṙ{’       -   base 250 integer = 14257356342446455638623624
               D      -   to decimal digits
                      -   -- that is the number of dots less the number of dashes plus 4
                      -      ... for each of OPQRSTUVWXYZABCDEFGHIJKLMN
O                     - ordinals of S   e.g. "ATHROUGHZ" -> [65,84,72,82,79,85,71,72,90]
 ị                    - index into (1-indexed & modular, so O gets the 79%26 = 1st item
                      -                                  or A gets the 65%26 = 13th item
                 Æm   - arithmetic mean
                   >4 - greater than 4?
\$\endgroup\$
4
\$\begingroup\$

05AB1E, 22 21 bytes

Saved a byte thanks to Kevin Cruijssen

SA•U(Õþć6Δ
»›I•‡3-O.±

Try it online!

Explanation

•U(Õþć6Δ
»›I•

is 35344527512513031462452313 compressed to base 255.

S              # split input into list of chars
       ‡       # transliterate
 A             # the lowercase alphabet
  •...•        # with the digits from the compressed string
        3-     # subtract 3 from each              
          O    # then sum the result
           .±  # and take the sign
\$\endgroup\$
5
  • \$\begingroup\$ You can save a byte by replacing the map with S. \$\endgroup\$ Mar 12, 2019 at 7:44
  • \$\begingroup\$ @KevinCruijssen: Thanks! I was sure I had tried that, but apparently not :) \$\endgroup\$
    – Emigna
    Mar 12, 2019 at 7:48
  • 3
    \$\begingroup\$ Just for fun: the string usdgpsahsoaboutlopezgbidol (concatenation of 8 words from the dictionary) can be used to get the values: for each character \$c\$ in there: v = ord(c)*3%83%8. \$\endgroup\$
    – Arnauld
    Mar 12, 2019 at 11:26
  • \$\begingroup\$ @Arnauld: Interesting! How did you find that out? Not by hand I hope :P \$\endgroup\$
    – Emigna
    Mar 12, 2019 at 11:38
  • 1
    \$\begingroup\$ I brute-forced all word pairs and the longest match was aboutlopez. I then looked for other matches with the same multiplier and modulo. (So it's absolutely not guaranteed to be optimal.) \$\endgroup\$
    – Arnauld
    Mar 12, 2019 at 11:45
4
\$\begingroup\$

C# (Visual C# Interactive Compiler), 47 bytes

n=>n.Sum(i=>("[E[LduRgmQSMK"[i%13]>>i%2*3)%8-3)

Uses Level River St's 'magic string'. Be sure to upvote their solution as well!

It's not everyday C# beats Ruby, Python, Javascript, C, Retina, and Perl!

Try it online!

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

Jelly, 23 bytes

9“¡ȷṡẓh)ėḂYF@’ḃ_4ị@OS0<

Try it online!

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

C (gcc), 84 82 81 79 75 bytes

Assumes all caps.

r;f(char*s){for(r=0;*s;r+="+-+,,-*/--*-)+(+),.*,-*+)+"[*s++%65]-43);s=r>0;}

Try it online!

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

Python 2, 73 70 69 bytes

lambda s:sum(int(`0x21427b563e90d7783540f`[ord(c)%25])-3for c in s)>0

Try it online!

Uppercase only

-3 bytes, thanks to Erik the Outgolfer


Both upper- and lowercase version:

Python 2, 73 71 bytes

lambda s:sum(int(oct(0x1d7255e954b0ccca54cb)[ord(c)%32])-3for c in s)>0

Try it online!

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

JavaScript (Node.js),  69 68  65 bytes

-3 bytes thanks to @l4m2

Expects the input string in uppercase. Returns \$0\$ or \$1\$.

s=>!Buffer(s).map(n=>s='30314624523133534452741251'[n%26]-4-~s)<s

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 65 \$\endgroup\$
    – l4m2
    Nov 20, 2023 at 6:59
2
\$\begingroup\$

Stax, 20 bytes

ÉBÜ◙ƒ╣<Hf6─òɼsäS╗◄↔

Run and debug it

Unpacked, ungolfed, and commented, it looks like this.

"45D.J57KBJa`"I"    string literal with code points [52 53 68 46 74 53 55 75 66 74 97 34 73]
$                   flatten to string "52536846745355756674973473"
;                   push input
@                   get string characters at indices 
                    (using input codepoints as indices; lookups wrap around)
:V                  arithmetic mean
53>                 is greater than 53

Run this one

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

Ruby, 64 bytes

->s{n=0;s.bytes{|i|n+=("[E[LduRgmQSMK"[i%13].ord>>i%2*3)%8-3};n}

Try it online!

Uses a 13-byte magic string, 2 numbers 0..7 encoded in each byte. Subtract 3 for a range -3..4.

The ASCII code for A (and also N) taken modulo 13 is by coincidence, zero.

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

GNU sed, 113 bytes

Accepts lower-case inputs, one per line. Output is 1 for dot-heavy or 0 for dash-heavy. Since the question didn't specify the output for exactly balanced lines, I produce an empty line in that case.

s/[acnpxz]//g
y/bdfgjklqruvwy/ieitmtimeeitm/
:e
s/h/ii/
s/s/ie/
s/i/ee/
s/o/mt/
s/m/tt/
s/et\|te//
te
/e/c1
/t/c0
  1. First, remove the letters which are themselves balanced.
  2. Replace unbalanced letters with their equivalents (e.g. Q is --·-, so its net contribution is --, i.e. M).
  3. Expand these equivalents into the corresponding count of E or T.
  4. Remove paired E and T
  5. If we have an E remaining, we're dot-heavy; if we have a T remaining, we're dash-heavy; otherwise (nothing remaining) evenly balanced.
\$\endgroup\$
1
\$\begingroup\$

Retina 0.8.2, 51 bytes

T`L`35344527412513031462452313
.
$*<>>>
+`<>|><

^<

Try it online! Link includes test cases. Only accepts upper case (+6 bytes for mixed case). Shamelessly stealing @Arnauld's string but I was going to use the same algorithm anyway. Explanation:

T`L`35344527412513031462452313
.

Change each letter into the difference in numbers of dots and dashes, plus three, so O=0 and H=7.

$*<>>>

Represent the difference as that number of <s and three >s. (Sadly I can't use dots because they're special in regex.)

+`<>|><

Remove matched pairs of <s and >s.

^<

Check whether there are still any dots left.

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

Bash+coreutils,  64 60 bytes

tr a-z 35344526512513031462452313|sed s/./\&z-+/g|dc -eIK?^p

Try it online!

Takes a string in lowercase, outputs zero for falsy, nonzero for truthy

Explanation

Uses tr and sed to create a dc program that looks like (for the example input 'hello'):

IK6z-+4z-+5z-+5z-+0z-+^p

IK     Push 10, then 0 to the stack
6z-+  Push 6 (three more than the dots minus dashes in 'h'), subtract 3, and accumulate
...    Do the same for all other letters, so the stack now has the total dots minus dashes
^      Raise 10 to this power - precision is zero so this turns negative/positive to falsy/truthy
p      Print result
\$\endgroup\$
1
  • \$\begingroup\$ Golfed two bytes by just putting the dc in the pipeline rather than use command substitution, then another byte by replacing <space>3 with z (conveniently, I have 3 items on the stack at that point!) and another byte by replacing the quotes around my sed program with a single backslash to escape the & \$\endgroup\$ Mar 11, 2019 at 19:05
1
\$\begingroup\$

R, 74 70 bytes

f=utf8ToInt;sum(f("42433250265364746315325464")[f(scan(,''))-96]-52)<0

input should be lower case, returns TRUE or FALSE

Try it online

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

TI-BASIC (TI-84), 111 bytes

:Ans→Str1:"ABCDEFGHIJKLMNOPQRSTUVWXYZ→Str2:"35344527512513031462452312→Str3:0<sum(seq(expr(sub(Str3,inString(Str2,sub(Str1,X,1)),1)),X,1,length(Str1))-3

I used the same string for determining dot-heaviness as some of the other answers.
Program returns truthy (1) if the input string is dot-heavy, falsy (0) if not.
Input string must be in all-caps.
Input is stored in Ans. Output is stored in Ans and is automatically printed out when the program completes.

Ungolfed:

:Ans→Str1
:"ABCDEFGHIJKLMNOPQRSTUVWXYZ→Str2 
:"35344527512513031462452312→Str3
:0<sum(seq(expr(sub(Str3,inString(Str2,sub(Str1,X,1)),1)),X,1,length(Str1))-3

Example:

"HELLO
HELLO
prgmCDGF3
           1
"CODE
CODE
prgmCDGF3
           0

Explanation:
(TI-BASIC doesn't have comments, assume that ; indicates a commment)

:Ans→Str1                          ;store the input into Str1
:"ABCDEFGHIJKLMNOPQRSTUVWXYZ→Str2  ;store the uppercase alphabet into Str2
:"35344527512513031462452312→Str3  ;store dot-dash+3 for each letter into Str3

:0<sum(seq(expr(sub(Str3,inString(Str2,sub(Str1,X,1)),1)),X,1,length(Str1))-3 ;full logic

   sum(                                                                       ;sum the elements of
       seq(                                                               )    ;the list evaluated by
                sub(                                    )                       ;the substring of
                    Str3,                                                        ;Str3
                         inString(                  ),                           ;at the index of
                                       sub(        )                              ;the substring of
                                           Str1,                                   ;Str1
                                                X,                                 ;starting at X
                                                  1                                ;of length 1
                                  Str2,                                           ;in Str2
                                                      1                          ;of length 1
           expr(                                        ),                       ;converted to an integer
                                                          X,                    ;using X as the increment variable
                                                            1,                  ;starting at 1
                                                              length(Str1)      ;ending at the length of Str1
                                                                           -3   ;then subtract 3 from all elements in the list
  0<                                                                           ;then check if the sum is greater than 0
                                                                               ;implicitly output the result

Note: The byte count of a program is evaluated using the value in [MEM]>[2]>[7] (124 bytes) then subtracting the length of the program's name, CDGF3, (5 bytes) and an extra 8 bytes used for storing the program:

124 - 5 - 8 = 111 bytes

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

Thunno 2, 21 bytes

Ạ»œfḅ“ƈ©OMṾȤØ»ṆdN3-Sʂ

Attempt This Online!

Port of Emigna's 05AB1E answer.

Explanation

Ạ»œfḅ“ƈ©OMṾȤØ»ṆdN3-Sʂ  # Implicit input
Ạ                      # Push the lowercase alphabet
 »œfḅ“ƈ©OMṾȤØ»         # Push compressed integer 35344527512513031462452313
              Ṇ        # Transliterate the input string (^^ --> ^)
               dN      # Convert to a list of digits
                 3-    # Subtract 3 from each
                   S   # Sum the list
                    ʂ  # Sign of the sum
                       # Implicit output
\$\endgroup\$
1
\$\begingroup\$

Morsecco 175 bytes

morsecco has a small advantage here: it knows morse code by nature. Do . - .-. to Read the stdin (special address -), -.- .- to Konvert from ·Text, -.- -- to Konvert to Morse and you already have the morse transcript. But then the work begins!

. - .-. -.- .- -.- -- . . - .
-- - -.-. - - - . . -... -.. --.. --.
- .- . . - . -.-. . - .. .- - . -- --.. --.
. - .- - .. .- - . - - .-.. --.. --.
- .- --.
- .- -.-. - ---

One funny golfing trick in the third line (handling of space or dash): Concatening a dot in front of the character turns the space into a null . , but the dash into a minus 1 (.-), so this value can simply be added to the total count.

The `Output is simply the first character of the count, which happens to be a dot for negative numbers and a dash for positive numbers, so the output is the character with less occurrences.

\$\endgroup\$
0
\$\begingroup\$

Perl 5 -pF, 53 bytes

$p+=y/a-z/35344526512513031462452313/r-3for@F;$_=$p>0

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Factor, 66 bytes

: f ( s -- ? ) >morse [ [ 45 = ] count ] [ [ 46 = ] count ] bi < ;

Try it online!

\$\endgroup\$
0
\$\begingroup\$

C++ (compiled with Visual Studio 2017) 171bytes

int f(string i){const char*c="1322131421130102123023121211210120032121323101112232";int j=0,h[2]={0};while(j<sizeof(i)/28)*h+=c[i[j]-97],h[1]+=c[i[j++]-71];return*h>h[1];}

if we take the main program that exists for test purposes into account as well its more.

this is the ungolfed "tidy" variant

#include "stdafx.h"
int main()
{
    const int dotCount[] = {1,3,2,2,1,3,1,4,2,1,1,3,0,1,0,2,1,2,3,0,2,3,1,2,1,2};
    const int dashCount[] = {1,1,2,1,0,1,2,0,0,3,2,1,2,1,3,2,3,1,0,1,1,1,2,2,3,2};
    std::cout << "Enter String:\n";
    std::string input;
    std::cin >> input;
    int inputsHeavyness[2] = { 0 };
    for(int i = 0;i < sizeof(input)/sizeof(std::string);i++)
    {
        inputsHeavyness[0] += dotCount[input[i] - 'a'];
        inputsHeavyness[1] += dashCount[input[i] - 'a'];
    }
    if (inputsHeavyness[0] > inputsHeavyness[1])
    {
        std::cout << "Dot Heavy\n";
    }
    else
    {
        std::cout << "Dash Heavy or Neutral\n";
    }
    return 0;
}

assumes all lowercase

\$\endgroup\$
9
  • 1
    \$\begingroup\$ You may want to add a TIO link. (Also, I think you have a typo in the ungolfed code: this 22 should be 2.) \$\endgroup\$
    – Arnauld
    Mar 12, 2019 at 1:11
  • \$\begingroup\$ yeah this may well be a typo. i guess i fixed that in the golfed version though. tio well i have no clue of that stuff (i think i looked at it once and it didnt feature the compiler i m using so results between vs2017 and tio would likely vary? no good at all) \$\endgroup\$
    – der bender
    Mar 12, 2019 at 1:15
  • 1
    \$\begingroup\$ 145 bytes. Results may indeed vary between VS and TIO. It sometimes varies for me too, and I am actually using GCC (albeit MinGW). \$\endgroup\$
    – gastropner
    Mar 12, 2019 at 23:58
  • 1
    \$\begingroup\$ Tweak of @ceilingcat for 131 bytes \$\endgroup\$
    – gastropner
    Apr 15, 2019 at 16:49
  • 1
    \$\begingroup\$ Building on @gastropner 111 bytes Combined both arrays into one; "132... and "112... become "353... and 51 is the ASCII value of 3 \$\endgroup\$
    – ceilingcat
    Apr 15, 2019 at 20:15
0
\$\begingroup\$

c (118 characters) returns a positive value for over-dot-ness and negative value for over-dash-ness

int n(char* c){int v=25124858,d=3541434,i=0,o=0;for(;c[i]!=0;i++)o=(1&(v>(c[i]-65)))>0?(1&(d>>(c[i]-65)))>0?o+1:o-1:o;return o;}

un-golfed

int n(char* c)
{
  // Bitwise alpha map: 
  // more dots = 1
  // more dashes or equal = 0
  int d=3541434;  
  // validation bit map.
  // dot/dash heavy = 1
  // even = 0
  int v=25124858;
  int i=0,o=0;
  for(;c[i]!=0;i++)   // iterate through all values
  {
    // There is no way to make this pretty
    // I did my best.
    // If the little endian validation bit corresponding
    // to the capitol letter ascii value - 65 = 0,
    // the output does not increment or decrement.
    // If the value is one it increases or decreases based
    // on the value of the d bitmap.
    o=(1& ( v > (c[I] - 65))) > 0 ?
      (1 & (d >> (c[I] - 65))) > 0 ?
        o + 1 :
        o - 1 :
      o;
  }
  return o;
}
\$\endgroup\$
1
  • \$\begingroup\$ I must confess I do not fully understand the comparison 1& ( v > (c[I] - 65)), which is the same as v > c[I] - 65, which I cannot imagine is ever false, so we could remove that whole thing while riffing on @ceilingcat for 56 bytes \$\endgroup\$
    – gastropner
    Apr 15, 2019 at 16:46
0
\$\begingroup\$

MathGolf, 22 bytes

{▄="Yⁿ∩┐↑rⁿ¼~<↔"$▒3-§+

Try it online!

Uses the same method as many other answers, where ⁿ∩┐↑rⁿ¼~<↔" represents the magic number 35344527512513031462452313.

\$\endgroup\$
0
\$\begingroup\$

Python 2, 90 86 bytes

import morse
s=''.join(morse.string_to_morse(input()))
print s.count('.')>s.count('-')

worked on my local with the morse library. -4 bytes. Thanks for the tip @JoKing!

Also, it's 1 byte more if it's in Python 3.

Python 3, 87 bytes

import morse
s=''.join(morse.string_to_morse(input()))
print(s.count('.')>s.count('-'))

Though the question assumes the number of '.'s and '-'s will not be equal; in case they are equal, this code will return True.

\$\endgroup\$
4
  • \$\begingroup\$ I mean, you can use input instead of raw_input if you want... \$\endgroup\$
    – Jo King
    Mar 14, 2019 at 11:34
  • \$\begingroup\$ @JoKing i tried. It was throwing an error and hence had to resort to raw_input \$\endgroup\$ Mar 15, 2019 at 6:50
  • \$\begingroup\$ you just have to put quotes around the string, since input evals STDIN before passing it to the program \$\endgroup\$
    – Jo King
    Mar 15, 2019 at 7:38
  • \$\begingroup\$ That is a very fair point. I feel stupid for missing that out! :3 \$\endgroup\$ Mar 16, 2019 at 14:20
0
\$\begingroup\$

Vyxal, 19 bytes

n»¨g±„ẇ⟇PNĖṡø»Ŀ3-∑±

Try it Online!

Saying the same joke but louder shorter is fun :p

Ports 05AB1E

Explained

n»¨g±„ẇ⟇PNĖṡø»Ŀ3-∑±
n                     # Lowercase alphabet
 »¨g±„ẇ⟇PNĖṡø»        # 35344527512513031462452313
              Ŀ       # transliterate
               3-     # - 3
                 ∑±   # sign of sum
\$\endgroup\$
0
\$\begingroup\$

Nibbles, 16.5 bytes (33 nibbles)

+.$-=$`D8 3 2954b0ccca54cb75c957

Attempt This Online!

Approach based on Emigna's O5AB1E answer.

Input is lowercase. Outputs the dot-dash sum: positive values are truthy in Nibbles, negative values (as well as zero) are falsy.
To directly compare to the Vyxal, Thunno, O5AB1E etc. answers, add 2 nibbles (+1 byte; prefix `$) to output the sign of the sum, although this isn't necessary here due to Nibbles' truthy/falsy convention.

      `D    2954b0ccca54cb75c957    # interpret hex 2954b0ccca54cb75c957
        8                           # in base 8
                                    # (digits: 51251303146245231335344527)
 .$                                 # now map over the input
    =$                              # getting the digit at the modular index
                                    # of each input codepoint 
   -      3                         # subtract 3 from each
+                                   # and sum the result
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

GNU sed -E, 112 bytes

An approach could not differ more from the existing sed solution, but beating it by only one byte! This one builds a map from +4 dots to +3 dashes then two s commands taking turns in moving one of their side down until one of them has no moves left:

s/^/H S IFLV EDRU :_GKWT_JQYM_O!/
:1
s/((\S)\S* (.).*)\2/\1\3/
T-
s/((.)_[^_]*([^_]).*!.*)\3/\1\2/
t1
c.
q
:-
c-

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Scala 3, 91 bytes

A port of @Level River St's Ruby answer in Scala.


Golfed version. Attempt This Online!

s=>{var n=0;s.getBytes.foreach{i=>n+=(("[E[LduRgmQSMK".apply(i%13).toInt>>(i%2*3))%8-3)};n}

Ungolfed version. Attempt This Online!

object Main {
  def main(args: Array[String]): Unit = {
    val f = (s: String) => {
      var n = 0
      s.getBytes.foreach { i =>
        val index = i % 13
        val shift = i % 2 * 3
        val char = "[E[LduRgmQSMK"(index)
        n += ((char.toInt >> shift) % 8 - 3)
      }
      n
    }

    List("S", "K", "HELLO", "CODE").foreach { j =>
      println(s"$j -> ${f(j)}")
    }

    ('A' to 'Z').foreach { j =>
      println(s"$j -> ${f(j.toString)}")
    }
  }
}
\$\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.