Counting characters of a program manually is tedious, and we do this a lot in Code Golf. How do you do a more effective character count using utilities provided by your shell or ide?
10 Answers 10
On Linux, use wc -c file in your favourite shell. Note that you might be counting a trailing new-line, which you might want to ignore.
-
4\$\begingroup\$ Any unix, not just linux. And you can ignore the
-cif you're going to parse the output by Mk. I eyeball. \$\endgroup\$dmckee --- ex-moderator kitten– dmckee --- ex-moderator kitten2011年01月27日 22:33:37 +00:00Commented Jan 27, 2011 at 22:33 -
\$\begingroup\$
‐ccounts bytes.This stems from the sometimes erroneous historical view that bytes and characters are the same size.(source: POSIXTM) This question, however, is asking for character count. The flag for that is‐m:wc ‐m file...is the way to go. \$\endgroup\$Kai Burghardt– Kai Burghardt2023年09月05日 10:17:46 +00:00Commented Sep 5, 2023 at 10:17
Notepad++ includes a convenient count of both characters (not including newlines) and bytes (be sure to first remove any trailing newlines and switch to Unix newlines) at the bottom of the window. Separate counts exist for the current selection and the entire file.
-
\$\begingroup\$ That's probably what I'll use for most of my solutions. If I ever do one, that is. \$\endgroup\$Andy– Andy2011年02月02日 04:42:13 +00:00Commented Feb 2, 2011 at 4:42
I use this tool: http://www.javascriptkit.com/script/script2/charcount.shtml.
Usually I use one of these two ways:
- I always have my Total Commander (FAR, MC, what do you wish) opened at current working directory, so I just look on file size in bytes and substract
(number_of_lines - 1)if I usedCR LF; - If I'm too lazy to open Total, I go to IRC and give solution code to bot to count it's length.
-
\$\begingroup\$ Shift+F2, U, Enter in Far to save it with LF line endings. :-) \$\endgroup\$Joey– Joey2011年01月31日 12:31:05 +00:00Commented Jan 31, 2011 at 12:31
I just look at the character count when I open it in vim to copy across.
-
1\$\begingroup\$ Vim appends one unnecessary new line at the end of file. \$\endgroup\$Alexandru– Alexandru2011年01月28日 21:30:02 +00:00Commented Jan 28, 2011 at 21:30
-
\$\begingroup\$ @Alexandru:
:set binary noeol\$\endgroup\$ninjalj– ninjalj2011年02月04日 22:14:14 +00:00Commented Feb 4, 2011 at 22:14
For most golfings I just use Far, i.e. an orthodox file manager, save the file with LF line endings and just look at the file size (I need to remember svn ps svn:eolstyle LF * though because SVN tends to think too much). Since I'm on Windows there's luckily no trailing LF to be accounted for.
However, for one-liners in PowerShell, I have a small utility (slightly buggy, but never got around fixing them):
function Set-GolfPrompt {
function global:prompt {
# Info about how much shorter/longer the previous command was can only
# be given if the history is long enough, so don't do that if not.
$history = Get-History
if ($history.Length -ge 2) {
$last = (Get-History)[-1].CommandLine.Length
2ドルndlast = (Get-History)[-2].CommandLine.Length
Write-Host -NoNewline -ForegroundColor DarkGray "Last command's length: $last "
if (2ドルndlast -eq $last) {
Write-Host -NoNewline -ForegroundColor Gray "(?0)"
} elseif (2ドルndlast -lt $last) {
Write-Host -NoNewline -ForegroundColor Red "(+$($last - 2ドルndlast))"
} else {
Write-Host -NoNewline -ForegroundColor Green "(-$(2ドルndlast - $last))"
}
}
[Console]::Write("`n ")
0..10 |
ForEach-Object {
[Console]::ForegroundColor = ($_+1) % 2 * 2 + 12
[Console]::Write("$_ ")
}
[Console]::Write("`n ")
1..11 |
ForEach-Object {
[Console]::ForegroundColor = $_ % 2 * 2 + 12
[Console]::Write('1234567890')
}
[Console]::ForegroundColor = 'White'
[Console]::WriteLine()
"> "
}
Which then looks like this:
enter image description here
I like to paste the code into charactercountonline.com . I especially enjoy that I don't need to include the whole file in the count, because I often include useless information that doesn't need to be counted (e.g. in Java: package codegolf;).
I usually use Sublime for counting (even if I'm not writing the code in it). I paste the code I want to count into a new file. Then, if I'm sure that my code still works without any whitespace whatsoever, I prepare it with
- Ctrl +H (open Find & Replace)
- (optionally) Alt + R (activate regex search)
- \ s (search for whitespace)
- (optionally) Tab Del (clear replacement string)
- Ctrl + Alt + Enter (replace all)
- Ctrl + A (select all)
Now in the status bar at the bottom you'll see "x characters selected". This sequence of key presses becomes really mechanical after a while, and when you're doing it repeatedly, because you've been golfing more in the meantime, you can actually skip steps 2 to 4. Of course, some of those combinations may be configured differently on your machine, but I think they are the default key bindings in Windows.
This is particularly useful for Mathematica answers, because if you paste stuff out of Mathematica, you get tons of unwanted whitespace.
-
\$\begingroup\$ And how do you count bytes for Mma programs? \$\endgroup\$Dr. belisarius– Dr. belisarius2014年10月10日 03:57:55 +00:00Commented Oct 10, 2014 at 3:57
-
\$\begingroup\$ @belisarius If I've got Unicode (in any language), after the above steps, I'll paste it into this online byte counter. Of course, in Mathematica in particular, you sometimes need to insert the Unicode characters by hand, because when copying out of Mathematica, it will replace stuff like
⋂with\[Intersection]. \$\endgroup\$Martin Ender– Martin Ender2014年10月10日 08:42:26 +00:00Commented Oct 10, 2014 at 8:42 -
\$\begingroup\$ Yeah, thanks a lot. That char replacement is what sometimes drives me nuts. BTW, I count chars with Sublime too :) \$\endgroup\$Dr. belisarius– Dr. belisarius2014年10月10日 08:59:36 +00:00Commented Oct 10, 2014 at 8:59
$ python
>>> len("".join(open('filename').readlines()))
or, as hallvabo suggests:
$ python
>>> len(open('filename').read())
-
\$\begingroup\$ This would count the number of lines, not characters. \$\endgroup\$hallvabo– hallvabo2011年01月31日 21:50:38 +00:00Commented Jan 31, 2011 at 21:50
-
\$\begingroup\$ @hallvabo, Good point. I've fixed it. \$\endgroup\$Thomas O– Thomas O2011年02月01日 10:15:40 +00:00Commented Feb 1, 2011 at 10:15
-
\$\begingroup\$ Why not just open('filename').read()? And to avoid any behind-the-scenes automagial line terminator translation, use 'rb' as mode flag. \$\endgroup\$hallvabo– hallvabo2011年02月01日 12:44:34 +00:00Commented Feb 1, 2011 at 12:44
-
\$\begingroup\$ @hallvabo, I didn't know you could do that. \$\endgroup\$Thomas O– Thomas O2011年02月01日 12:45:37 +00:00Commented Feb 1, 2011 at 12:45
Usually I am using vim, so for one liners I position the cursor on the last character and read off that position
For multiple lines, I save the file and subtract 1 from the reported size (because of the extra newline)
If I solve a problem in one line in the python interpreter, I'll just paste it into
>>> len("""<solution>""")
(I don't think I've ever had an answer with """ in it :))
-
1\$\begingroup\$ on vim:
g <Ctrl>-g\$\endgroup\$ninjalj– ninjalj2011年02月04日 22:15:14 +00:00Commented Feb 4, 2011 at 22:15
off-topicvotes. \$\endgroup\$