Table of Contents

Let’s put C++17 in practice!

One of the good ways to do it is to take part in a coding challenge.
So together with Jonathan Boccara from Fluent C++ we invite you to participate in “The Expressive C++17 coding challenge”.

The Expressive C++17 coding challenge  

Jonathan made a few contests on his blog (for example this one), and I thought it might be fun to do something similar for C++17. Moreover, on his blog there’s a lot about expressive C++, which is a perfect match for the C++17 coding style. That’s why we joined our forces :)

Here’s Jonathan’s corresponding post!

The purpose of this challenge is to write a piece of code that contains as many features of C++17 as possible, and that is as clear as possible.

The case  

The task proposed in the challenge is to write a command line tool that takes in a CSV file, overwrites all the data of a given column by a given value, and outputs the results into a new CSV file.

More specifically, this command line tool should accept the following arguments:

  • the filename of a CSV file,
  • the name of the column to overwrite in that file,
  • the string that will be used as a replacement for that column,
  • the filename where the output will be written.

For instance, if the CSV file had a column “City” with various values for the entries in the file, calling the tool with the name of the input file, City, London and the name of output file would result in a copy of the initial file, but with all cities set equal to “London”:

Here are small input and output CSV files that can serve as a test case.

Here is how to deal with edge cases:

  • if the input file is empty, the program should write “input file missing” to the console.
  • if the input file does not contain the specified column, the program should write “column name doesn’t exist in the input file” to the console.

In both cases, there shouldn’t be any output file generated.

And if the program succeeds but there is already a file having the name specified for output, the program should overwrite this file.

Of course, we could go further with that idea. For example, the replacement would happen only when a text in a column matches some condition. But let’s focus on the core parts for now.

BTW: here’s almost the solution using Power Shell:

Import-Csv input_file.csv | ForEach-Object {
    $_."Column Name" = 'Replacement String'
    $_
} | Export-Csv .\output_file.csv -NoTypeInformation

The rules of the challenge  

To win the challenge, your code must have as many features of C++17 as possible as long as they are useful to solve the above case. Please write all those you’ve used in a comment section at the top of your .cpp file.

To win the challenge, your code must also be as clear as possible (you can write about what you did to achieve this too if you want).

To submit a solution, paste your code into this empty coliru link (it has the C++17 compilation flag) to make sure it compiles, click the “Share!” button and add the generated link to the comment section of this post, or on Jonathan ’s blog.

The gcc options: g++ -std=c++1z -O2 -Wall -pedantic -pthread main.cpp -lstdc++fs && ./a.out (so with threads and filesystem).

To be registered in the contest, add your email address at the beginning of your code so that we can get in touch with you. We’ll be sure to add you to the mailing lists of bfilipek.com and Fluent C++ (you can unsubscribe at any time, but you’ll get notified when the results come out).

If you don’t want to display your email address on your solution, you can send it to Jonathan or me privately. The same goes for your entire solution if you’d like.

The contest ends on the 15th of October at midnight GMT. So the 15th is ok, the 16th is too late. The results will be published on the 23rd of October. We’ll discuss the best solutions in the summary post(s).

If two people submit an equivalent solution, the first one wins.

Notes / Remarks  

  • Don’t use any third party libraries, the code should call only STD library.
  • We use GCC 7.2 so parallel algorithms aren’t supported.
  • You can assume input files won’t be super large and can fit fully into memory.
  • We use comma as a separator for entries in lines, but, for simplification, you don’t have to support commas inside… for example
    • Hello World, abc, xyz // valid as 3 entries
      “Hello, World”, abc, xyz // that is 4 entries even though quotation marks are used.

Experimentally you can code using tech.io basic playground:

But please save your code often. Be sure to copy the final code into Coliru and prepare a shared link. The Tech.io playground is here only for you to try.

Wrap-up  

Let’s have some fun with C++17!

To help you with the learning here are some resources:

Should you have any question or feedback, don’t hesitate to get in touch with either one of us.

We’re waiting for your submissions & happy coding! :)