Coding Challenge #4
Photo by Kenny Eliason on Unsplash

Coding Challenge #4

This weeks challenge is to build your own version of the Unix command line tool cut!

The Unix command line tools are a great metaphor for good software engineering and they follow the Unix Philosophies of:

  • Writing simple parts connected by clean interfaces - each tool does just one thing and provides a simple CLI that handles text input from either files or file streams.
  • Design programs to be connected to other programs - each tool can be easily connected to other tools, via files and streams, to create incredibly powerful compositions.

Following these philosophies has made the simple unix command line tools some of the most widely used software engineering tools which can be chained together to create far more complex and powerful set of tools that you’d expect.

You can read more about the Unix Philosophy in the excellent book?The Art of Unix Programming.

The Challenge - Building cut

The functional requirements for cut are concisely described by it’s man page - give it a go in your local terminal now:

man cut        

The TL/DR version is:?cut - cuts out the selected portions from each line in a file.

Step Zero

In this introductory step you’re going to set your environment up ready to begin developing and testing your solution.

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 text from my Dropbox.

Step 1

In this step your goal is to implement a simple version of cut that will open the provided tab separated file and print out the second field (-f2) from each line. The result should look something like this:

cut -f2 sample.tsv
f1
1
6
11
16
21        

Step 2

In this step your goal is to extend your code to support the -d option to allow the user to specify what character to use as the delimiter between fields. If no delimiter is provided then tab should still be used, we can test this first with a comma as the delimiter:

cut -f1 -d, fourchords.csv | head -n5
Song title
"10000 Reasons (Bless the Lord)"
"20 Good Reasons"
"Adore You"
"Africa"        

Here we again seeing how the Unix command line tools can be chained (piped) together to create more powerful data processing pipelines. With the head command allowing us to limit the output to the first five lines.

Then check we still default to a tab:

cut -f1 sample.tsv
f0
0
5
10
15
20        

Step 3

In this step your goal is to add support for the -f option. This option specifies a list of fields to be printed out. Output fields are separated by a single occurrence of the field delimiter character.

The field list is a comma or whitespace separated list of fields, i.e. -f1,2 or -f “1 2”. The first field is field number 1.

Here’s a couple of tests on this:

cut -f1,2 sample.tsv
f0      f1
0       1
5       6
10      11
15      16
20      21        

and

cut -d, -f"1 2" fourchords.csv | head -n5
Song title,Artist
"10000 Reasons (Bless the Lord)",Matt Redman and Jonas Myrin
"20 Good Reasons",Thirsty Merc
"Adore You",Harry Styles
"Africa",Toto        

Step 4

In this step your goal is to support reading from the standard input stream if no filename is provided or if the single dash is provided ‘-’.

tail -n5 fourchords.csv | cut -d, -f"1 2"
"Young Volcanoes",Fall Out Boy
"You Found Me",The Fray
"You'll Think Of Me",Keith Urban
"You're Not Sorry",Taylor Swift
"Zombie",The Cranberries        

or

tail -n5 fourchords.csv| cut -d, -f"1 2" -
"Young Volcanoes",Fall Out Boy
"You Found Me",The Fray
"You'll Think Of Me",Keith Urban
"You're Not Sorry",Taylor Swift
"Zombie",The Cranberries        

Step 5

In this step we’re going to revisit the first coding challenge and use our wc tool, combining it with our new cut tool to build a simple command line data engineering pipeline. We’ll test that by seeing how many unique artists are in the data set, again piping together Unix command line tools.

cut -f2 -d, fourchords.csv | uniq | wc -l
155        

Share Your Solutions!

Even though this is only the first coding challenge I’ve already been asked to share model solutions in as many programming languages as possible - which is an awesome idea, but I just don’t have the time and don’t know even half the programming languages out there so I’d like your help!

If you think your solution is an example of the developers can learn from please share it, put it on GitHub, GitLab or elsewhere. Then let me know - ping me a message via Twitter or LinkedIn or just post about it there and tag me.

I’ll take a look at every solution and I’ll mention the best solution in the follow week’s newsletter.

Request for Feedback

I’m writing these challenges to help you develop your skills as a software engineer based on how I’ve approached my own personal learning and development. What works for me, might not be the best way for you - so if you have suggestions for how I can make these challenges more useful to you and others, please get in touch and let me know. All feedback greatly appreciated.

You can reach me on Twitter, LinkedIn or through SubStack

Thanks and happy coding!

John

P.S. This is on the Coding Challenges website as: Build You Own cut.

Ahtisham Ilyas

Top Software Testing Voice@2023 | Software Test Engineer | Automation Specialist: Web, Mobile, API | Scrum Fundamental Certified

9 个月

My Solution in java https://github.com/0kakarot0/BuildingCutChallenge.git it was little tricky for me to handle the command itself and then split it according to the model, as I solved the steps, I have to modify those part. but at the end able to solve all the steps Had fun playing with these concepts in java 1. Sets 2. List 3. String [] 4. String manipulation 5. Conditional Statments

Ahtisham Ilyas

Top Software Testing Voice@2023 | Software Test Engineer | Automation Specialist: Web, Mobile, API | Scrum Fundamental Certified

9 个月

John CrickettI had a point to raise, on the step 4, I think there is mistake as you have mentioned that if no file name is provided but example below file name is given

  • 该图片无替代文字
回复
Mohit Jain

Software Tech Lead @Zemetric | ex Co-Founder @Evy Energy (Acquired)

1 年

Having completed the previous 3 challenges, this was much easier to think and code. Here's my solution in TypeScript - https://github.com/jainmohit2001/coding-challenges/tree/master/src/4

Mikhail Golubitsky

Senior Software Engineer

1 年

I was just recently discussing with a coworker how the microservices pattern is essentially a repackaging of the Unix philosophy to 'Make each program do one thing well. To do a new job, build afresh rather than complicate old programs by adding new "features".' Looking forward to this one John Crickett!

James Galyen

??Application Developer at Press Ganey LLC

1 年

I still want to build a gps navigation that helps you with the weather. Routing you through the worst parts. I also want it to find all the round abouts and have you make at least two passes through them.

回复

要查看或添加评论,请登录

John Crickett的更多文章

  • From The Challenges - Memcached Server

    From The Challenges - Memcached Server

    ?? ?? Happy Birthday Coding Challenges - Two Years Old! ?? ?? Coding Challenges is turning two this week! To celebrate…

    13 条评论
  • Coding Challenge #85 - Time Zone Converter

    Coding Challenge #85 - Time Zone Converter

    Coding Challenge #85 - Time Zone Converter This challenge is to build your own Time Zone Converter. That is a tool to…

    12 条评论
  • From The Challenges - IRC Client

    From The Challenges - IRC Client

    Welcome To Coding Challenges - From The Challenges! In this Coding Challenges “from the challenges” newsletter I’m…

    5 条评论
  • Coding Challenge #84 - Mandelbrot Set Explorer

    Coding Challenge #84 - Mandelbrot Set Explorer

    This challenge is to build your own Mandelbrot set explorer. The Mandelbrot set is a set of fractals that exhibit great…

    4 条评论
  • From The Challenges - Cat

    From The Challenges - Cat

    Welcome To Coding Challenges - From The Challenges! In this Coding Challenges “from the challenges” newsletter I’m…

    7 条评论
  • Coding Challenge #83 - Markdown Presentation Tool

    Coding Challenge #83 - Markdown Presentation Tool

    Coding Challenge #83 - Markdown Presentation Tool This challenge is to build your own version of Go’s Present or…

    3 条评论
  • From The Challenges - Shell

    From The Challenges - Shell

    Welcome To Coding Challenges - From The Challenges! In this Coding Challenges “from the challenges” newsletter I’m…

    3 条评论
  • Coding Challenge #82 - Markdown To PDF Editor

    Coding Challenge #82 - Markdown To PDF Editor

    Coding Challenge #82 - Markdown To PDF Editor This challenge is to build your own tool to convert markdown to PDF. It…

    14 条评论
  • From The Challenges - Diff

    From The Challenges - Diff

    Welcome To Coding Challenges - From The Challenges! In this Coding Challenges “from the challenges” newsletter I’m…

    7 条评论
  • Coding Challenge #81 - Brainf*ck Interpreter

    Coding Challenge #81 - Brainf*ck Interpreter

    This challenge is to build your own Brainf*ck Interpreter. Just in case you’re wondering, yes the * should be a u, but…

    34 条评论

社区洞察

其他会员也浏览了