Coding Challenge #10 - uniq
John Crickett
Helping you become a better software engineer by building real-world applications.
This weeks challenge is to build your own version of the Unix command line tool uniq.
I’m a big fan of the Unix Philosophy and the command line tools it has spawned. By chaining them together you can create complex and powerful software from simple building blocks. That’s a great model to follow for the functions and modules in the software you write and the components you use in the systems you build as a software developer.
The Challenge - Building uniq
Let’s check out the man page on uniq to find out what it does:
The uniq utility reads the specified input_file comparing adjacent lines, and
writes a copy of each unique input line to the output_file. If input_file is a
single dash (‘-’) or absent, the standard input is read. If output_file is
absent, standard output is used for output. The second and succeeding copies
of identical adjacent input lines are not written. Repeated lines in the input
will not be detected if they are not adjacent, so it may be necessary to sort
the files first.
Step Zero
Like most programming languages we’re zero indexed!
For this step, I’ll leave you to setup your IDE / editor of choice and programming language of choice. After that here’s what I’d like you to do to be ready to test your solution.
Download the test data from my?dropbox here?and unzip it.
Step 1
In this step your goal is to read an input file and and handle the default behaviour, which is to remove duplicate adjacent lines.
领英推荐
For example the input:
line1
line2
line2
line3
line4
would become:
% uniq test.txt
line1
line2
line3
line4
You can test your program using the above or on the countries list:
% uniq countries.txt | wc -l
246
Continued....
You can find Step 2 and beyond on the?Coding Challenges?website as?Write You Own uniq.
Or if you'd rather get the whole challenge delivered to you inbox every week, you can subscribe on the?Coding Challenges Substack.
Software Tech Lead @Zemetric | ex Co-Founder @Evy Energy (Acquired)
1 年This was a short and sweet tool to build. Thanks, John Crickett, for the challenges. Here's my solution in TypeScript: https://github.com/jainmohit2001/coding-challenges/tree/master/src/10