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

Post

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)
General comments
hyper-neutrino‭ wrote over 3 years ago · edited over 3 years ago

Can you define "printable"? IIRC tabs and newlines aren't part of printable ASCII. Also just to be sure - whitespace is just space, newline, and tab, right? Or am I missing something...

Adám‭ wrote over 3 years ago

"you may assume all inputs will consist of printable bytes" conflicts with "Hello \t\nworld"

Quintec‭ wrote over 3 years ago

@hyper-neutrino @Adam sorry for being unclear, I meant to go with the definition that is string.printable in python, I didn't realize there were others

Adám‭ wrote over 3 years ago

Can we take multi-line text as a list of strings?

Quintec‭ wrote over 3 years ago

@Adam yeah that sounds reasonable

General Sebast1an‭ wrote about 3 years ago

So which of the characters formatted with \ won't be in the count on characters?