Skip to main content
3 of 4
Removed unnecessary character from regex in Easy answer
Armand
  • 519
  • 2
  • 7

Groovy

Easy: 26 25 characters

c={(it=~/[\W^_]/).size()}

assert c("abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789")== 2
assert c("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")==0
assert c("a-b-7*NNuah ~#77%")==7
assert c('_') == 1

Hard: 47 characters

s={it.replaceAll(/[\w&&[^_]]/,'').bytes.size()}

assert s("abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789")== 2
assert s("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")==0
assert s("a-b-7*NNuah ~#77%")==7
assert s('_') == 1
Armand
  • 519
  • 2
  • 7