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

Dashboard
Notifications
Mark all as read
Challenges

Word Count Tool

+3
−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!"#$%&\'()*+,-./:;<=>[email protected][\\]^_`{|}~ \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.

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 (5 comments)

3 answers

+1
−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

Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

General comments (1 comment)
+0
−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.

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+0
−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
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »

This community is part of the Codidact network. We have other communities too — take a look!

You can also join us in chat!

Want to advertise this community? Use our templates!