5
\$\begingroup\$

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?

\$\endgroup\$
11
  • \$\begingroup\$ (I know the answer, but wanted to try such a question to see if we consider this on-topic.) \$\endgroup\$
    – moinudin
    Jan 27 '11 at 22:19
  • \$\begingroup\$ Additionally should we include the counts of special compiler/interpreter flags in the character counts? \$\endgroup\$
    – JPvdMerwe
    Jan 27 '11 at 22:22
  • \$\begingroup\$ @JPvdMerwe That's a good question, but ask that on meta where we're fleshing out the description and usage of this site. \$\endgroup\$
    – moinudin
    Jan 27 '11 at 22:25
  • 1
    \$\begingroup\$ @Close voters: Since we're defining this site, can you please give reasons as to why you think should be off-topic. This post was experimental in that regard, but no feedback makes it a pointless exercise. \$\endgroup\$
    – moinudin
    Jan 27 '11 at 22:42
  • \$\begingroup\$ It's either meta.codegolf.stackexchange.com or superuser.com, because there is no puzzle. \$\endgroup\$
    – Nakilon
    Jan 27 '11 at 23:16
  • 1
    \$\begingroup\$ @Nakilon The area51 proposal had at least one meta-ish on-topic question: "Is code golfing the practice of elegance or obfuscation?" I disagree that this is for SU, as we've seen in answers there are specific details around newlines (and probably unicode too). \$\endgroup\$
    – moinudin
    Jan 27 '11 at 23:21
  • \$\begingroup\$ @Nakilon See meta.codegolf.stackexchange.com/questions/27/… \$\endgroup\$
    – moinudin
    Jan 27 '11 at 23:25
  • \$\begingroup\$ @marcog, if you asked this (about counting lines) question at proposal stage, I think, we could see mostly off-topic votes. \$\endgroup\$
    – Nakilon
    Jan 27 '11 at 23:46
  • \$\begingroup\$ @Nakilon Unfortunately I missed the definition stage, which is why I'm asking it now. \$\endgroup\$
    – moinudin
    Jan 27 '11 at 23:49
  • \$\begingroup\$ @Nakilon: The question is whether meta is "meta-puzzle/golf" or "meta-this-site" or both. I saw it more as "meta-this-site" so far and "meta-golf" questions would then be on scope. Just my opinion, though. \$\endgroup\$
    – Joey
    Jan 31 '11 at 12:40
  • \$\begingroup\$ @Joey Same opinion I voiced here. \$\endgroup\$
    – moinudin
    Jan 31 '11 at 13:00

10 Answers 10

4
\$\begingroup\$

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.

\$\endgroup\$
1
  • 4
    \$\begingroup\$ Any unix, not just linux. And you can ignore the -c if you're going to parse the output by Mk. I eyeball. \$\endgroup\$ Jan 27 '11 at 22:33
2
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ That's probably what I'll use for most of my solutions. If I ever do one, that is. \$\endgroup\$
    – Andy
    Feb 2 '11 at 4:42
1
\$\begingroup\$

I use this tool: http://www.javascriptkit.com/script/script2/charcount.shtml.

\$\endgroup\$
1
\$\begingroup\$

Usually I use one of these two ways:

  1. 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 used CR LF;
  2. If I'm too lazy to open Total, I go to IRC and give solution code to bot to count it's length.
\$\endgroup\$
1
  • \$\begingroup\$ Shift+F2, U, Enter in Far to save it with LF line endings. :-) \$\endgroup\$
    – Joey
    Jan 31 '11 at 12:31
1
\$\begingroup\$

I just look at the character count when I open it in vim to copy across.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Vim appends one unnecessary new line at the end of file. \$\endgroup\$
    – Alexandru
    Jan 28 '11 at 21:30
  • \$\begingroup\$ @Alexandru: :set binary noeol \$\endgroup\$
    – ninjalj
    Feb 4 '11 at 22:14
1
\$\begingroup\$

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
            $2ndlast = (Get-History)[-2].CommandLine.Length

            Write-Host -NoNewline -ForegroundColor DarkGray "Last command's length: $last "
            if ($2ndlast -eq $last) {
                Write-Host -NoNewline -ForegroundColor Gray "(?0)"
            } elseif ($2ndlast -lt $last) {
                Write-Host -NoNewline -ForegroundColor Red "(+$($last - $2ndlast))"
            } else {
                Write-Host -NoNewline -ForegroundColor Green "(-$($2ndlast - $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

\$\endgroup\$
1
\$\begingroup\$

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;).

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ And how do you count bytes for Mma programs? \$\endgroup\$ Oct 10 '14 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\$ Oct 10 '14 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\$ Oct 10 '14 at 8:59
0
\$\begingroup\$
$ python
>>> len("".join(open('filename').readlines()))

or, as hallvabo suggests:

$ python
>>> len(open('filename').read())
\$\endgroup\$
4
  • \$\begingroup\$ This would count the number of lines, not characters. \$\endgroup\$
    – hallvabo
    Jan 31 '11 at 21:50
  • \$\begingroup\$ @hallvabo, Good point. I've fixed it. \$\endgroup\$
    – Thomas O
    Feb 1 '11 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
    Feb 1 '11 at 12:44
  • \$\begingroup\$ @hallvabo, I didn't know you could do that. \$\endgroup\$
    – Thomas O
    Feb 1 '11 at 12:45
0
\$\begingroup\$

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 :))

\$\endgroup\$
1
  • \$\begingroup\$ on vim: g <Ctrl>-g \$\endgroup\$
    – ninjalj
    Feb 4 '11 at 22:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .