| Bytes | Lang | Time | Link |
|---|---|---|---|
| 087 | AWK | 250228T182718Z | xrs |
| 048 | Zsh | 241215T062809Z | roblogic |
| 248 | Pascal | 230306T010009Z | Kai Burg |
| 079 | Ruby p0 | 221020T205526Z | Jordan |
| 014 | Jelly | 181228T155232Z | Erik the |
| 115 | Powershell | 180926T151303Z | mazzy |
| 025 | Pip s | 180925T184528Z | DLosc |
| 023 | 05AB1E | 180925T132259Z | Kevin Cr |
| 178 | C | 160227T191319Z | user4677 |
| 140 | Haskell | 160227T061226Z | arjanen |
| 065 | Retina | 160226T180849Z | FryAmThe |
| 066 | Lua | 160226T153514Z | Jesse Pa |
| 039 | Seriously | 160226T034654Z | user4594 |
| 061 | Perl | 160226T131731Z | Kenney |
| 049 | Perl | 160226T134152Z | Ton Hosp |
| 140 | PowerShell | 160226T142021Z | AdmBorkB |
| 065 | POSIX awk | 160226T093056Z | muru |
| 115 | JavaScript ES6 | 160226T114815Z | Neil |
| 038 | MATL | 160226T051107Z | David |
| 081 | Julia | 160226T013047Z | Alex A. |
| 021 | Pyth | 160226T055411Z | isaacg |
| 108 | Ruby | 160226T035410Z | afuous |
| 077 | Python 2 | 160226T025536Z | Logic Kn |
| 026 | CJam | 160226T014542Z | Dennis |
| 253 | AppleScript | 160226T015818Z | Addison |
AWK, 87 bytes
{x=gsub(/\\n/,FS);j+=length;i+=NF}END{k=split(j,y,_)+1;printf"%*i%*i%*i",k-1,x,k,i,k,j}
{x=gsub(/\\n/,FS); # replace \n with space and get count
j+=length; # number of chars
i+=NF} # number of words
END{ # at end of records
k=split(j,y,_)+1; # get length of #chars for padding
printf"%*i%*i%*i", # print with variable spacing
k-1,x,k,i,k,j} # padded printout
Zsh, 48 bytes
a=$[$#S+1];printf %$#a\d\ ${(wf)#S} ${(w)#S} $a
$#S counts the bytes in string S, but omits the terminating null byte so I set a=$#S+1 to match the result from wc.
Using Zsh expansion flags, (f) splits a string on newlines. (w) counts 'words' in an array (or #string); usually space delimited, but with (s) or (f) flags the 'words' can be delimited by other separators.
The printf directive %5d (for example) will right-align a number so it's 5 digits wide. Here we align $#a digits (the length of a), followed by a space \ . This matches the spacing of wc.
Pascal, 248 bytes
This complete program requires a processor supporting “Extended Pascal” as defined by ISO standard 10206.
- The variable
inputpossesses the built-in data typetext. As such any newline character (sometimes it’s CR, sometimes LF, sometimes CR+LF, or something else) is automagically converted into a single space character (' '). In order to distinguish from a space character as payload of a line, and a space character that resulted from this conversion, you will need to query the functionEOLn. - A
textvariable is conceptually similar to afile of char. Thisprogramin fact counts characters, i. e. actually behaves likewc ‑m. However, since the task states that characters strictly draw from the ASCII character set, there is no difference.
program c(input,output);var l,w,c,z:integer value 0;p:char;begin p:=' ';while not EOF do begin c:=c+1;l:=l+ord(EOLn);w:=w+card([p]*[' ',' ']-[input^]);p:=input^;get(input)end;z:=trunc(ln(card([0..l,0..w,0..c]))/ln(10))+2;writeLn(l:z-1,w:z,c:z)end.
Ungolfed and annotated version:
{ By listing a `program` parameter of the spelling `input`,
there’s an implicit `reset(input)` which implies a `get(input)`. }
program wordCount(input, output);
var
{ `line`, `word` and `character` are singular, because they kind
of enumerate these units as if input was a zero-based `array`. }
line, word, character, width: integer value 0;
previousCharacter: char;
begin
{ This program counts “word beginnings” as words. This is the
_change_ from “space character” _to_ “non-space character”. }
previousCharacter := ' ';
while not EOF(input) do
begin
character := character + 1;
{ As per POSIX specification a line is payload _plus_ one newline. }
line := line + ord(EOLn(input));
{ The second character literal is an HT (horizontal tabulator). }
word := word + card([previousCharacter] * [' ', ' '] - [input^]);
{ Prepare `previousCharacter` for _next_ iteration. }
previousCharacter := input^;
{ Advance “reading cursor”. }
get(input);
end;
{ To produce POSIX-compliant output write:
writeLn(line:1, ' ', word:1, ' ', character:1);
As per task specification we will determine a common width though.
We use ⌊ln(N) / ln(10)⌋ + 1 as the base formula, where N needs to be
the largest number from `line`, `word` and `character`. This can be
found by the expression |[0, line] ∪ [0, word] ∪ [0, character] ∩ ℤ|.
We add another +1 for the space _between_ numbers. }
width := trunc(ln(card([0..line, 0..word, 0..character])) / ln(10)) + 2;
writeLn(line:width-1, word:width, character:width);
end.
Ruby -p0, 79 bytes
Includes a trailing space in the output.
a=[/
/,/\S+/,/./m].map{$_.scan(_1).size}
$_="%#{a.map{_1.to_s.size}.max}d "*3%a
Ruby -p0, 84 bytes
No trailing space.
a=[/
/,/\S+/,/./m].map{$_.scan(_1).size}
$_=["%#{a.map{_1.to_s.size}.max}d"]*3*" "%a
Powershell, 123 115 bytes
switch -r($args|% t*y){'\s'{$a=0}'\S'{$w+=!$a;$a=1}'(?s).'{$b++}'
'{$l++}}$c="$b".Length
"{0,$c} {1,$c} $b"-f$l,+$w
Test script:
$f = {
switch -r($args|% t*y){ # evaluate all matched cases
'\s' {$a=0} # any whitespace (newline not included)
'\S' {$w+=!$a;$a=1} # any not-whitespace (newline not included)
'(?s).'{$b++} # any char (newline included!)
'`n' {$l++} # new line char
}
$c="$b".Length
"{0,$c} {1,$c} $b"-f$l,+$w
}
@(
, ("a b c d`n", "1 4 8")
, ("a b c d e f`n", " 1 6 12")
, (" a b c d e f `n", " 1 6 16")
, ("a`nb`nc`nd`n", "4 4 8")
, ("a`n`n`nb`nc`nd`n", " 6 4 10")
, ("abc123{}[]()...`n", " 1 1 16")
, ("`n", "1 0 1")
, (" `n", "1 0 4")
, ("`n`n`n`n`n", "5 0 5")
, ("`n`n`na`nb`n", "5 2 7")
) | % {
$s,$e = $_
$r = &$f $s
"$($e-eq$r): $r"
}
Output:
True: 1 4 8
True: 1 6 12
True: 1 6 16
True: 4 4 8
True: 6 4 10
True: 1 1 16
True: 1 0 1
True: 1 0 4
True: 5 0 5
True: 5 2 7
Explanation:
$args|% t*ysplits arument strings into charsswitch -r($args|% t*y)evaluate all matched cases'\s'case for any whitespace'\S'case for any non-whitespace'(?s).'case for any char (newline included)'\n'case for newline char (newline represent itself)
$c="$b".Lengthcalculate a length of bytes number. $b is always max($l,$w,$b) by design"{0,$c} {1,$c} $b"-f$l,+$wformat numbers with same length. The variable $w converts to int. It need for strings without words. Other variables formats 'as is' because 'The input will always contain a trailing newline' and $l and $b cannot be 0.
Pip -s, 25 bytes
sX##a-#_._M[nNa`\S+`Na#a]
Takes the multiline string as a command-line argument. Try it online!
Thanks to Dennis's CJam answer for making me realize that the longest number is always the character count.
Explanation
s is space; n is newline; a is 1st cmdline arg (implicit)
[ ] Construct a list of three elements:
nNa Number of newlines in a
`\S+`Na Regex search: number of runs of non-whitespace characters in a
#a Length of a (i.e. number of characters in a)
M To each element of that list, map this function:
#a Number of characters in a
# Length of that number
-#_ Subtract length of each element
sX Construct a string of that many spaces
._ Prepend it to the element
The resulting list is autoprinted, space-separated (-s flag)
Here's a 29-byte solution with flags -rs that takes input from stdin:
[#g`\S+`NST:gY#g+1]MsX#y-#_._
05AB1E, 24 23 bytes
¨¶¡¹…
S¡õK¹)€g§Zg>jJ¦
j is currently bugged, so could have been 21 bytes without the § and J..
Try it online or verify all test cases.
Explanation:
¨ # Remove the trailing newline of the (implicit) input
¶¡ # And split it on newlines
¹…
S¡ # Take the first input again, and split it on [" \n\t"]
õK # Then remove all empty string items
¹ # And take the first input again as is
) # Wrap all three value of the stack to a single list
€g # Take the length of each of the items
§ # Cast the integers to strings (should have been implicit, but `j` is bugged)
Z # Take the max (always the last / amount of bytes) (without popping the list)
g> # Take the length + 1 of this max
j # Append leading spaces so all items or of this length
J # Join them together (should have been done by the `j` already, but it's bugged)
¦ # Remove the leading space (and output implicitly to STDOUT)
C, 180 178 bytes
#include <stdio.h>
#include <ctype.h>
main(b,w,l,c,d){d=' ';b=w=l=0;while((c=fgetc(stdin))!=EOF){if(!isspace(c)&&isspace(d))w++;b++;d=c;if(c==10)l++;}printf("%d %d %d\n",l,w,b);}
Haskell, 140 bytes
import Text.Printf
w h=let{l=length;s=show.l;c=s h;m=s.words$h;n=s.lines$h;f=maximum$map l[c, m, n];p=printf"%*s"f}in p n++' ':p m++' ':p c
The ungolfed version is hereunder, with expanded variable and function names:
import Text.Printf
wc str =
let charcount = show.length $ str
wordcount = show.length.words $ str
linecount = show.length.lines $ str
fieldwidth = maximum $ map length [charcount, wordcount, linecount]
printer = printf "%*s" fieldwidth
in printer linecount ++ (' ' : printer wordcount ++ (' ' : printer charcount))
This is a function that accepts a string and returns a string. It just uses the Prelude functions words (resp. lines) to get the number of words (resp. lines) given that they seem to use the same definition as wc, then gets the longest value (as a string) amongst the counts and use the printf format taking the width amongst its arguments for formatting.
Retina, 65
^((\S+)|(¶)|.)*
$#3 $#2 $.0
+`(\b(.)+ )(?!.*\b(?<-2>.)+$)
a$1
a
<space>
The first stage is the actual wc program, the rest of it is for padding. The a placeholder thing is probably unnecessary, and some of the groups can probably be simplified a bit.
Lua, 74 66 bytes
Golfed:
t=arg[1]_,l=t:gsub('\n','')_,w=t:gsub('%S+','')print(l,w,t:len())
Ungolfed:
text = arg[1]
_,lines = text:gsub('\n','')
_,words = text:gsub('%S+','')
print(lines, words, text:len())
Receives input through command line arguments.
We rename the first argument (arg[1]) to save bytes. string.gsub returns the number of replacements as well as the modified string, so we're using that to count first '\n' (newlines), then '%S+' (instances of one or more non-whitespace characters, as many as possible, i.e. words). We can use anything we want for the replacement string, so we use the empty string ('') to save bytes. Then we just use string.len to find the length of the string, i.e. the number of bytes. Then, finally, we print it all.
Seriously, 39 bytes
"
"╩╜l;$l╝@╜sl'
╜ck`#╛#"{:>%d}"%f`M' j
Explanation (newlines are replaced with \n):
"\n "╩╜l;$l╝@╜sl'\n╜ck`#╛#"{:>%d}"%f`M' j
"\n " push a string containing a newline and a space
╩ push input to register 0 (we'll call it s)
╜l; push two copies of len(s) (byte count)
$l╝ push len(str(len(s))) to register 1
(this will serve as the field width in the output)
@╜sl push word count by getting the length of the list formed by
splitting s on spaces and newlines
'\n╜c count newlines in input
k push stack to list
`#╛#"{:>%d}"%f`M map:
# listify
╛# push reg 1 (field width), listify
"{:>%d}" push that string
% do old-style string formatting for field width
f do new-style string formatting to pad the field appropriately
' j join on spaces
Perl, 71 62 61 bytes
includes +1 for -n
$;=length($b+=y///c);$w+=split$"}{printf"%$;d %$;d $b",$.,$w
Commented:
while (<>) { # implicit because of -n
$; = length( # printf formatting: width
$b += y///c # count characters
);
$w += split $" # count words
}{ # explicit: end while, begin END block
printf "%$;d %$;d $b", $., $w # $. = $INPUT_LINE_NUMBER
} # implicit because of -n
- Save another byte, again thanks to @TonHospel.
- Save 9 bytes thanks to @TonHospel showing me a few tricks of the trade!
Perl, 49 bytes
Added +3 for -an0
Input on STDIN or 1 or more filenames as arguments.
Run as perl -an0 wc.pl
wc.pl:
/\z/g;pos=~//;printf"%@+d %@+d $`
",y/
//,~~@F
Explanation:
-n0 slurps the whole input into $_ and says we will do our own printing
-a tells perl to split the input on whitespace into array @F
/\z/g Matches the absolute end of the input. g modifier so the position
is remembered in pos which will now contain the input length
pos=~// An empy regex repeats the last succesful match, so /\z/ again.
After that $` will contain the the number of input characters and
the array @+ will contain the length of this number
printf All preparation is complete, we can go print the result
"%@+d" will become e.g. %6d if the number of characters is a number of
length 6, so lines and words will get printed right aligned
in a field of length 6.
$` $` we can directly interpolate since it won't contain a %
y/\n// Count the number of newlines in $_
~~@F The array of words @F in scalar context gives the number of words
PowerShell, 140 bytes
param($a)$c="$((($l=($a-split"`n").Count-1),($w=($a-split"\S+").Count-1),($b=$a.length)|sort)[-1])".Length;
"{0,$c} {1,$c} {2,$c}"-f$l,$w,$b
(newline left in for clarity :D)
The first line takes input $a, and then the next part is all one statement. We're setting $c equal to some-string's .length. This will form our requisite padding. Inside the string is an immediate code block $(...), so that code will be executed before evaluated into the string.
In the code block, we're sending three items through the |sort command, and then taking the biggest one (...)[-1]. This is where we're ensuring to get the columns to the correct width. The three items are $l the line count, where we -split on newlines, the $w word count, where we -split on whitespace, and $b the length.
The second line is our output using the -f operator (which is a pseudo-shorthand for String.Format()). It's another way of inserting expanded variables into strings. Here, we're saying that we want all of the output to be padded to the left so that each column is $c wide. The padding is done via spaces. The 0, 1, and 2 correspond to the $l, $w, and $b that are arguments to the format operator, so the line count, word count, and byte count are padded and output appropriately.
Note that this either requires the string to have already-expanded newlines (e.g., doing a Get-Content on a text file or something, and then either piping or saving that to a variable, then calling this code on that input), or use the PowerShell-styled escape characters with backticks (meaning `n instead of \n).
Example
PS C:\Tools\Scripts\golfing> .\reimplement-wc.ps1 "This line`nis broken`ninto three lines.`n"
3 7 38
POSIX awk, 79 75 67 65 bytes
{w+=NF;c+=length+1}END{d=length(c)"d %";printf"%"d d"d\n",NR,w,c}
Edit: saved 4 bytes since POSIX allows a bare length, saved 7 bytes by discounting the invocation part, and saved two bytes thanks to Doorknob's tip for adding d % to d.
This was originally for GNU awk, but best I can tell, it uses only POSIX awk functionality.
Better formatted:
gawk '{
w += NF
c += length($0) + 1 # length($0) misses the newline
}
END {
d = length(c) # GNU awk's length returns the length of string representation of number
printf "%"d"d %"d"d %d\n", NR, w, c
}'
JavaScript (ES6), 115 bytes
s=>[/\n\/g,/\S+/g,/[^]/g].map(r=>l=(s.match(r)||[]).length).map(n=>(' '.repeat(99)+n).slice(-`${l}`.length)).join` `
Does not require any input. Formatting was painful. If there was an upper limit on the amount of padding I could reduce (' '.repeat(99)+n) to something shorter e.g. ` ${n}`.
MATL, 38 bytes
'\n'32cZtttnGnw-wPZvPYbnqbnvvV!3Z"vX:!
You can try it online! This shouldn't be so long though...
Explanation, for the calculation,
'\n'32cZt %// Takes implicit input and replaces any \n with a space
tt %// Duplicate that string twice
nGnw-w %// Length of the string with \n's minus length with spaces to give number of \n's
PZvPYbnq %// Take string with spaces, flip it, remove leading spaces, flip it again,
%// split on spaces, find length and decrement for number of words
bn %// get length of string with spaces, the number of characters
The last part does the output formatting
vvV! %// concatenate the 3 numbers to a column vector, convert to string and transpose
3Z"v %// make string ' ' and concatenate on the bottom of previous string
X:! %// linearise and transpose to get correct output (impicitly printed)
Julia, 112 81 bytes
f(s,n=endof,l="$(n(s))",g=r->lpad(n(split(s,r))-1,n(l)))=g(r"\n")" "g(r"\S+")" "l
This is a function that accepts a string and returns a string.
We save the following as function arguments:
n = endoffunction, which gets the last index of an indexable collection (in this case is the length of the string)l = "$(n(s)), the length of the input converted to a string using interpolation- A lambda function
gthat accepts a regular expression and returns the length - 1 of the input split on that regex, left padded with spaces to match the length ofl.
We get the number of lines using g(r"\n") and the number of words using g(r"\S+"), then we join those together with l delimited by spaces.
Saved 31 bytes thanks to Dennis!
Pyth, 21 bytes
jdm.[;l`lQ`ld[@bQcQ)Q
Pyth has some very nice built-ins here. We start by making a list ([) of the newlines in the string (@bQ), the words in the string (cQ)) and the string itself (Q). Then, we pad (.[) the length of each string (ld) with spaces (; in this context) out to the length of the number of characters (l`lQ). Finally, join on spaces (jd).
Ruby, 108 bytes
f=->s{a=[s.count($/),s.split(/\S+/).size-1,s.size].map(&:to_s)
a.map{|b|" "*(a.map(&:size).max-b.size)+b}*" "}
Python 2, 100 77 bytes
This solution is a Python function that accepts a multi-line string and prints the required counts to stdout. Note that I use a format string to build a format string (which requires a %% to escape the first format placeholder).
Edit: Saved 23 bytes due to print optimisations by Dennis.
def d(b):c=len(b);a='%%%us'%len(`c`);print a%b.count('\n'),a%len(b.split()),c
Before the minifier, it looks like this:
def wc(text) :
size = len(text);
numfmt = '%%%us' % len(`size`);
print numfmt % text.count('\n'), numfmt % len(text.split()), size
CJam, 31 26 bytes
q_)/_S*S%@_]:,:s),f{Se[}S*
How it works
q_ e# Read all input from STDIN and push two copies.
) e# Pop the last character (linefeed) of the second copy.
/ e# Split the remaining string at linefeeds.
_ e# Push a copy.
S* e# Join the copy, separating by spaces.
S% e# Split at runs of spaces.
@_ e# Rotate the original input on top and push a copy.
] e# Wrap all four items in an array.
:, e# Get the length of each item.
:s e# Cast the lengths (integers) to strings.
) e# Pop the last length (byte count).
, e# Get the number of digits.
f{Se[} e# Left-pad all three length with spaces to that length.
S* e# Join, separating by spaces.
AppleScript, 253 bytes
This assumes that AppleScript's text item delimiters are set to space (if I need to count the stuff to force that assumption, I'll add it).
set w to(display dialog""default answer"")'s text returned
set x to b(w)
set y to w's text item's number
set z to w's paragraph's number
a(x,z)&z&a(x,y)&y&" "&x
on a(x,n)
set o to" "
repeat b(x)-b(n)
set o to o&" "
end
o
end
on b(n)
count(n as text)
end