30
\$\begingroup\$

Between the kitten question and seeing this question at U&L about some sed magic, how about implementing tac?


Objective

Implement a program that will reverse and print the lines in a file.


Input

A file, provided as a name or via standard input


Output

The lines, reversed, to standard out.


Scoring

Bytes of source code.

\$\endgroup\$
8
  • 10
    \$\begingroup\$ tac is a bit strange when it comes to trailing linefeeds. It transforms a\nb\n (trailing linefeed) into b\na\n and a\nb (no trailing linefeed) into ba\n. Is this how our code it supposed to behave? \$\endgroup\$
    – Dennis
    Oct 1, 2015 at 6:34
  • \$\begingroup\$ Related \$\endgroup\$ Oct 1, 2015 at 6:35
  • 11
    \$\begingroup\$ Also, if we have to replicate tac's behavior, a 3 byte Bash answers that executes tac is only a matter of time... \$\endgroup\$
    – Dennis
    Oct 1, 2015 at 6:50
  • 1
    \$\begingroup\$ @Dennis at this point probably best to leave undefined. \$\endgroup\$
    – Nick T
    Oct 1, 2015 at 15:36
  • 1
    \$\begingroup\$ @Dennis Makes sense to me. Visualize the lines of a file as horizontal rows, all ending with \n. tac reverses the order of these rows. If an \n is removed from the middle of the file, the row it terminated is joined to the next row, but in the case of the last line, there is no next row to join to. \$\endgroup\$ Oct 1, 2015 at 19:43

43 Answers 43

1
2
2
\$\begingroup\$

><>, 55 bytes

0i:1+?!v:a=?v}1+!
0.20r}~<    >~}00
oa]~~.?)2l2dor[;!?l

It first counts the length of each line so it can isolate them in a separate stack when it browses back the lines to display them.

It can probably get shorter, I may work on it later.

\$\endgroup\$
2
\$\begingroup\$

Addict, 67 bytes

a g
 t _[_]
 p
 o
a p
 i _
 g
 d
a o
 d _
 s
 d
a s
 c _[_]
 o
 d
g

Copy and paste the code here to try it.

\$\endgroup\$
2
\$\begingroup\$

Jelly, 10 9 bytes

Ḋ;ƈ$ÐLỴṚY

Try it online!

Thanks to Dennis for -1.

\$\endgroup\$
1
  • \$\begingroup\$ Note: I assume that "provided as a name or via standard input" means that we can choose whether our program will take input from a file or STDIN (as other answers do), so, in this case, my program takes input from STDIN. \$\endgroup\$ Mar 6, 2017 at 13:10
1
\$\begingroup\$

STATA, 46 bytes

inf str99 v using a.b
g a=-_n
so a
l v,noo noh

Expects input as a file called a.b, with each line no more than 99 characters. Reads in a file and labels the variable v. Makes a new variable called a with the negative index of it. Sort by the negative index (i.e. reverse order) and then list every thing in sorted (i.e. reverse) order.

For a longer length of lines, put quotes around each line and use (42 bytes):

insheet using a.b
g a=-_n
so a
l v,noo noh
\$\endgroup\$
1
\$\begingroup\$

q, 35 bytes

-1 each reverse read0 hsym`$.z.x 0;
\$\endgroup\$
1
\$\begingroup\$

Python, 80 72 bytes

It isn't the best solution, but I guess it's something.

a=lambda x: f=open(x,'r');l=f.readlines();for o in reversed(l): print(o)

Any tips on golfing it further?

\$\endgroup\$
3
  • \$\begingroup\$ return outside of a def function is a syntax error. If you use a lambda, the return value is the result of the (single) expression. \$\endgroup\$
    – Nick T
    Oct 2, 2015 at 23:42
  • \$\begingroup\$ @NickT Thanks, I'll (try to) fix it. \$\endgroup\$
    – m654
    Oct 3, 2015 at 16:10
  • \$\begingroup\$ would l[::-1] instead of reversed(l) work? Also, you could remove the spaces after the : \$\endgroup\$
    – Simon
    Sep 14, 2017 at 12:25
1
\$\begingroup\$

Scala, 53 52 bytes

(io.Source.stdin.getLines():\0){(a,b)=>println(a);0}

Bastardizing/side-effecting foldRight (:\) to deal with iterators (as returned by getLines) not having reverse

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

C#, 145 bytes

Not as short as I hoped.. But I tried!

Golfed

using System.Linq;class c{static void Main(string[]a){foreach(var s in System.IO.File.ReadAllLines(a[0]).Reverse())System.Console.WriteLine(s);}}

Ungolfed

using System.Linq;
class c
{
    static void Main(string[]a)
    {
        foreach(var s in System.IO.File.ReadAllLines(a[0]).Reverse())
            System.Console.WriteLine(s);
    }
}
\$\endgroup\$
1
\$\begingroup\$

Vim, 6 bytes:

!Grev<cr>

Filters every line through the external command: rev. Must be run on linux.

Try it online!

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

tcl, 52

File is named F

puts [join [lreverse [split [read [open F]] \n]] \n]

demo

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

GNU command line environment, 41 40 37 bytes

cat -n|sort -nr|sed 's/^[^ ]* //'
\$\endgroup\$
0
\$\begingroup\$

Pyth, 4 bytes

j_.z

Try it!

Takes input from STDIN

explanation

j_.z
  .z    # Input as list of lines
 _      # reverse
j       # join on newline
\$\endgroup\$
0
\$\begingroup\$

Thunno 2 N, 2 bytes

¤r

Try it online!

  • ¤ - lines of input
  • r - reverse
  • N - join on newlines
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.