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

Comments on Word Count Tool

Parent

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 moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

General comments (6 comments)
Post
+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 moderator attention?
You might want to add some details to your flag.

1 comment thread

General comments (1 comment)
General comments
Shaggy‭ wrote almost 3 years ago

Won't \w+ fail if any of the words contain anything other than [0-9a-zA-Z_]?