Interactive Diff

July 12, 2019

[ There will be no exercises next week, as my daughter is visiting from out-of-town and I will be spending time with her. ]

One of my favorite books is The Unix Programming Environment by Brian Kernighan and Rob Pike, which I have recently been re-reading; the spine of my copy broke long ago, so I must be careful turning the pages, and I wrap the book in a rubber band every time I put it back on my shelf. It is an excellent introduction to Unix, still relevant today even though it was published in 1984. The recent exercise Remind was inspired by a program in Section 4.4, and today’s exercise is a rewrite of the program in Section 6.8.

NAME

    idiff -- interactive diff

USAGE

    idiff file1 file2 -- interactively merge file differences

DESCRIPTION

    idiff takes two files file1 and file2, diffs them, and presents
    the difference to the user interactively. At each difference,
    the user may accept the text from file1 by responding <, or     accept the text from file2 by responding >, or edit the difference
    by responding e (in which case whatever the user saves from the
    editor is entered into the output file), or execute a command
    by typing !cmd, in which case the user must then respond when
    the prompt is repeated. The assembled output with the selected
    differences is placed in file idiff.out.

EXAMPLE

    $ cat file1
    This is
    a test
    of
    your
    skill
    and comprehension.
    $ cat file2
    This is
    not a test
    of
    our
    ability.
    $ diff file1 file2
    2c2
    < a test     ---     > not a test
    4,6d4,5
    < your
    < skill
    < and comprehension.     ---     > our
    > ability.
    $ idiff file1 file2
    2c2
    < a test     ---     > not a test
    ? >
    4,6c4,5
    < your
    < skill
    < and comprehension.     ---     > our
    > ability.
    ? <
    $ cat idiff.out
    This is
    not a test
    of
    your
    skill
    and comprehension.

[ WordPress is determined not to render less-than and greater-than signs properly. Look at https://ideone.com/nI9CNB if you can’t make sense of what you see. ]

Your task is to write idiff. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Advertisements

Pages: 1 2

Doubled Letters

July 9, 2019

We have a fun little exercise on a lazy summer Tuesday:

Given a list of words, remove from the list those words that have two adjacent identical letters. For instance, given “Now is the time for all good men to come to the aid of their country” the program should remove the words “all” and “good”.

Your task is to write a program to remove words with doubled letters from a list of words. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

Remind

July 2, 2019

NAME

    remind -- print reminders of upcoming events

USAGE

    remind -- show reminders for next seven days
    remind [year] month day message -- add reminder to database

DESCRIPTION

    Remind maintains a database of reminders in the .reminders file,
    in the user's home directory, each a single line of the form

        [year] month day message

    Year is optional, and must be an integer greater than 99; if no
    year is given, the reminder applies to all years (for instance,
    birthdays).

    If remind is called with no arguments, it writes to standard
    output all reminders that occur within the next seven days. If
    remind is called with arguments giving a date and message, a
    reminder is added to the database. Any time remind is called,
    all past reminders are deleted from the database.

EXAMPLE

    $ date
    Sun Jun 30 19:45:38 CDT 2019
    $ remind 4 2 Anne birthday
    $ remind 10 13 Kate birthday
    $ remind 7 4 Independence Day
    $ remind 2019 7 2 lunch with Pat
    $ remind 2019 5 13 dentist 2:00pm
    $ remind
    7 4 Independence Day
    2019 7 2 lunch with Pat
    $ cat ./reminders
    4 2 Anne birthday
    10 13 Kate birthday
    7 4 Independence Day
    2019 7 2 lunch with Pat

Your task is to implement remind. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2