Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Word Count Tool

+7
−0

In Google Docs, the word count tool looks like this:

Word count

Pages would not make sense, but your goal is to implement every other count.

Definitions

Words are strings of text separated by any amount of whitespace (spaces, tabs, newlines). Characters are all printable bytes*. For the sake of this challenge, you may assume all inputs will consist of printable bytes. Note that Google Docs does not count newlines in its character count, so you must do the same.

*I define printable bytes as the variable string.printable in Python, which is shown below.

>>> string.printable
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'

Input

A string of text, or a list of strings instead of a multiline string if you wish.

Output

The 3 counts (words, characters, characters without spaces) in any order, and in an array if you like.

Examples

Input -> [words, characters, characters without spaces]
"Hello world" -> [2, 11, 10]
"H3ll0  w0r1d" -> [2, 12, 10]
"Hello \t\nworld" -> [2, 12, 11]
"\n\n\n" -> [0, 0, 0]
"   " -> [0, 3, 0]

This is code golf, so shortest code wins.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

General comments (6 comments)

9 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+1
−0

Japt v2.0a0, 19 18 17 bytes

I need more caffeine!

Takes input as an array of lines. Output is a reversed array.

[Uc¸¬U¬Ucq\s f]ml

Try it - includes all test cases, header splits string on newlines for ease of input. footer reverse the output for easier verification.

[Uc¸¬U¬Ucq\s f]ml     :Implicit input of array U
[                     :Construct an array containing
 Uc                   :1. Map U then flatten
   ¸                  :     Join with spaces
    ¬                 :   Join
     U¬               :2. Join U
       Uc             :3. Map U then flatten
         q            :     Split on
          \s          :     Regex /\s/g
             f        :   Filter, to remove empty strings
              ]       :End array
               m      :Map
                l     :  Length
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+2
−0

Ruby, 55 bytes

->a{[a.split,a.tr($/,''),a.gsub(/[ 
]/,'')].map &:size}

Try it online!

split removes all whitespace, but lines doesn't for some reason. Would've been useful in place of the tr.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+2
−0

APL (Dyalog Unicode), 27 bytes

Anonymous tacit prefix function

'\w+' '.' '[^ ]'{≢⍺⎕S⍬⊢⍵}¨⊂

Try it online!

'\w+' '.' '[^ ]'{}¨⊂ apply the following anonymous lambda on each of the PCRE patterns () and the entire string ():

⊢⍵ on the string

⍺⎕S⍬ find matches for the pattern

 tally (count) them

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

General comments (1 comment)
+0
−0

Haskell, 47 bytes

l=length
f s=(l(words s),l s,l$filter(/= ' ')s)

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

JavaScript (Node.js), 60 bytes

s=>[/\w+/g,/./g,/[\S\t]/g].map(p=>(v=s.match(p))?v.length:0)

Attempt This Online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

Python 3.8, 73 bytes

lambda x:[len(x.split()),len(x.replace('\n','')),len(''.join(x.split()))]

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

Lua 5.4, 62 bytes

_,w=s:gsub('%w+',0)_,c=s:gsub('[^\n]',0)_,n=s:gsub('[%g\t]',0)

Attempt This Online!


Another version, 65 bytes:

p={'%w+','[^\n]','[%g\t]'}for i=1,#p do _,r[i]=s:gsub(p[i],'')end

Attempt This Online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

Ruby, 48 bytes

->a{[a.split,b=a.chars-[$/],b-[' ']].map &:size}

Try this online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

Wolfram Language (Mathematica), 70 bytes

{Tr[1^StringSplit@#],StringLength@#,StringLength@StringDelete[#," "]}&

Try it online!

What made me decide to use Mathematica for string processing? I don't know. But at least it has the required builtins.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »