26
\$\begingroup\$

Write a program or function which takes a string of text as input and outputs the number of non-alphabetical characters in it (standard I/O rules apply). A non-alphabetical character is any character not appearing in this string:

abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

In terms of ASCII character codes, that's anything not in ranges 65-90 inclusive or 97-122 inclusive.

Rules

Your program must be able to handle all of printable ASCII, as well as any characters that appear in your code. In other words, you may assume input will not contain any characters iff they are in neither printable ASCII nor your solution. Printable ASCII here means ASCII's character codes 32-126 inclusive as well as character code 10 (Meaning you will have to count spaces and newlines).

You may assume that input will not be empty.

Scoring

Your score is the number of non-alphabetical characters (not bytes!) in your code. Lowest score wins, with as a tie-breaker.

Examples

"input" => output
"example" => 0
"3xampl3" => 2
"Hello, World!" => 3
"abc.def-" => 2
"     " => 5
"IT'S NORMAL" => 2
"•¥¥€???!!!" => 10
"abc
def" => 1
\$\endgroup\$
2
  • 3
    \$\begingroup\$ I suggest renaming this to what the challenge actually is. At first I though this was some kind of complex number challenge. \$\endgroup\$
    – Seggan
    Jun 8, 2022 at 18:48
  • \$\begingroup\$ @Seggan I could switch the order of the regex flags :P but yea it's pretty vanilla regex, I dunno. I'll think about verbosifying it. \$\endgroup\$ Jun 8, 2022 at 19:23

40 Answers 40

1
2
2
\$\begingroup\$

Japt, score: 2, 5 bytes

;eF l
;     // Alternative initial variables, where F = "[A-Za-z]".
 eF   // Replace all occurrences of F in input with default value of empty string.
    l // Return the result's length.

Try it here.

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

APL (NARS2000) Score 19, 31 bytes

{≢⍵~⎕av[C,32+C←66..91]}

Examples

{≢⍵~⎕av[C,32+C←66..91]} 'Hello, World!' evaluates to 3

{≢⍵~⎕av[C,32+C←66..91]} '•¥¥€???!!!' evaluates to 10

How it works

⎕av[C,32+C←66..91] generates the alphabetic string from built-in ⎕av array. ~ performs set difference with right argument . counts number of element in the set difference.

Score of this expression could be determined by applying it to itself:

⎕fmt {≢⍵~⎕av[C,32+C←66..91]} '{≢⍵~⎕av[C,32+C←66..91]}' evaluates to 19.

\$\endgroup\$
4
  • 2
    \$\begingroup\$ Score 6, 8 bytes: ≢⍞∼⎕A,⎕a \$\endgroup\$
    – Adám
    Jun 11, 2022 at 22:01
  • \$\begingroup\$ I didn't know existence of ⎕A and ⎕a and my ⎕av[C,32+C←66..91] is so inelegant. Will definitely apply them in future solutions. \$\endgroup\$
    – jimfan
    Jun 11, 2022 at 23:29
  • \$\begingroup\$ @Adám I encourage you to put up a solution for Dyalog APL using ≢⍞∼⎕A,⎕a \$\endgroup\$
    – jimfan
    Jun 11, 2022 at 23:31
  • \$\begingroup\$ It doesn't work in Dyalog APL, but does in APLX. I can post that. \$\endgroup\$
    – Adám
    Jun 11, 2022 at 23:48
2
\$\begingroup\$

BQN, Score: 8, 60 bytes

+´¬∘∊⟜"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

Try it here!

Thanks to @thejonymyster for helping halve the score!... Of course at the cost of tripling the byte count ;D

Explanation

  • ¬∘∊⟜"..." replace each char in input with 0 if alphabetical, 1 otherwise
  • sum occurrences of 1
\$\endgroup\$
2
  • 2
    \$\begingroup\$ generating the alphabet seems to be giving you a lot of score that could be avoided :P \$\endgroup\$ Jun 17, 2022 at 11:42
  • 2
    \$\begingroup\$ @jonymyster good point, I was too stuck in the code-golf mindset haha \$\endgroup\$ Jun 17, 2022 at 12:33
2
\$\begingroup\$

Ruby, score 5, 67 bytes

p gets.count"^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

Attempt This Online!

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

Alice, 32 bytes, score 24 9

/qlqaq"q&qcqqqo
qIq"qzqrq-q/d\q@

Try it online!

Explanation

All the q are useless and replace spaces to lower the score, leaving us with the following:

/                 Switch to ordinal mode
 Il               Read the input, lowercase
   "az"r          Build the alphabet string
        &-        For each character of the alphabet, remove it from the input
          c       Split into characters
           /      Back to cardinal mode
            d     Get the depth of the stack (the remaining characters non a-z)
             \o@  Print the depth
\$\endgroup\$
1
\$\begingroup\$

Ruby, Score: 24, 42 bytes

->s{s.chars.map{_1=~/[A-Za-z]/?0:1}.sum-1}

Attempt This Online!

There is a -1 the end due to the \n. If you don't coun't it, it's 40 bytes and Score: 22

\$\endgroup\$
1
  • 1
    \$\begingroup\$ you can -2 your score if you verbosify your regex slightly :P \$\endgroup\$ Jun 17, 2022 at 12:59
1
\$\begingroup\$

Brev, score 13

((fn (string-length (strse x 'alpha ""))) "(fn(string-length(strse x 'alpha \"\")))")
\$\endgroup\$
1
\$\begingroup\$

Japt v2.0a0, Score 2 (3 bytes)

è\L

Try it

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

Fig, score 0, \$4\log_{256}(96)\approx\$ 3.292 bytes

LFcB

Try it online!

Exact port of Vyxal. Beats it too :P

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

Go, score 30, 85 bytes

import."unicode"
func f(s[]rune)(c int){for _,e:=range s{if!IsLetter(e){c++}}
return}

Attempt This Online!

\$\endgroup\$
1
2

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.