Print a singly linked list in reverse!

Today’s topic is pretty simple. Let’s print a linked list in reverse. And it is a singly linked list meaning you can only traverse in one direction and not come back. You are only given a pointer to the head. So without further ado, below is the c++ code. Only the important parts are shown. Rest is left as an exercise for the reader to create a fully working program.

/*
 Print elements of a linked list in reverse order as standard output
 head pointer could be NULL as well for empty list
 Node is defined as 
 struct Node
 {
 int data;
 struct Node *next;
 }
*/
#include < vector >

void ReversePrint(Node *head)
{
 // This is a "method-only" submission. 
 // You only need to complete this method. 
 vector<int> data{0};
 while(head) {
   data.push_back(head->data);
   if(head->next) {
     head = head->next;
   } else { break; }
 }
 for(int i = data.size() - 1; i > 0; i--) {
   cout << data[i] << endl;
 }
}

The only thing I have done here is to use vector. On the forward traversal, simply push the values in and reverse traverse the list to print it. There are tons of others ways to do it. What is the other simple way?

Of course yes, it is using a stack. Simply push into it. And then start popping out once the traversal is done till the stack is empty. ENJOY the code!

Backward Compatibility!

Backward Compatibility

The internet is littered with articles on backward compatibility and why it is important and rants of people who work/use 1970s systems on the same. Well the last sentence there was a pun but people do rant on not being able to use the PS2 games on PS4 and or not being able to use Lint with the new c++11 or c++14 features or sticking to an outdated kernel (2.6.36) and not being able to use the new virtual environment enhancements, etc. Finally, all of these rants boil down to, “Everything should be backward compatible and should not break existing systems”.

But hey, your existing system is 20 years old and it is soon going to break down. Will you then try to search for a similar existing system or beat the old donkey till you live? I mean, there is this infatuation with backward compatibility in system developers. Yep, I am talking about those old, pot bellied, bald guys who did great c99 coding in their hay days but will not use tuples because it is NOT what they know and they have no clue as to how they can keep up with new standards. Bjarne Stroustrup in one of his talks explains why people should move away from the old ways of working and embrace newer things. I think it is the below lecture that he nicely sums up (in a better and positive manner) than what anybody could ever do.

He also explains the reasons as to why we don’t really need to keep doing things in the same old ways. And use the newer features available even in old code (i.e. refactor it). My understanding of the essence of what he wants to say is to get rid of backward compatibility. Stop doing things the wrong way and give the reason as being backward compatible. In that sense, the full stack development people are really fast to adapt and change. Not even a single person have I heard who says that he wants to stick to WordPress version 1.0 and PHP 3.x because of blah blah blah (insert your favorite 1980s keywords here).

In our organization, we have a product X which is being precisely used by 2 components A and B. Both A and B are moving to the new c++11 standards (or least are compiling towards the newer ABIs). The people now maintaining this product X still wants to be backward compatible? WTF? Who the heck is using your product? The A and B who are your clients are moving on, they are never going to come back and use the old things (at least not till time machine is invented and somehow magically all the pot-bellied, bald developers become young, the year changes to 1980 and Linus Torvalds never ever takes birth and yes, we still have Richard Stallman running around and GNU Hurd is still being penned down). I can go on and on, on what should not change for A & B to come back. But that’s it. Everything changes and everything has changed.

Wake up you pot-bellied, old, bald, about to retire dinosaurs. Everything is in a continuous state of change. Your product is outdated. Components A & B are still using it because the developers of those components are busy implementing new features (or are just too lazy to move over) or are restricted by the management class (another of those pot-bellied, old, bald, about to retire dinosaurs) to do any changes to what’s working. In any case, who are you maintaining the backward compatibility for? Why don’t you want to use the new features? Do you think that your stupid old code would be so useful that someone in future would just start using it?

I mean common guys. The only time your backward compatibility will ever be useful is…. “NEVER”. So stop being backwards compatible. We have got airplanes. We will not use donkeys anymore. I think this should be the motto of each and every developer. STOP BEING A DONKEY (or an asshole if you will)! Backwards compatible is essentially the same thing as trying to hold on to your youth. Hey, its gonna wither. Embrace the baldness and wear a wig. OKAY. Enough of my rant. If I write a bit more, I am pretty sure my article will be censored by Google and only people above 200 years of age will be able to read!

Signing off with this rant. Hope you like it. Give me your comments & suggestions on what you think. But hey, if you are for backwards compatibility, you are just one of those pot-bellied, old, bald and about to retire developers! Naah, just joking. Send in your comments.

Basic use of Tuples C++11

Basic use of Tuples in C++11

Tuples are an amazing feature in C++. Tuples are specified in C++11 standard (ISO/IEC 14882:2011), i.e. they are a new introduction in c++11. So if you are more used to traditional c++, tuples will come as a surprise for you. It happened to me as well and I am not that old (quite yet)! They are a great addition to generic programming and brings C++ to a whole new level of usability. Of course we have had ways to do exactly the same things that tuples does but now that it is a part of the standard language, it makes sense to use this feature instead of cooking something of our own. I see tuples as an important feature like dictionary in python (more or less). Tuples are amazing and you can feel the power when you start using them. Auto types and tuples make a great combination for very nice cool features previously rarely thought of (multiple return values from a function anyone). Anyways, I don’t see people really appreciating the flexibility being provided by our beloved language. Stefan Lavavej (@StefanTLavavej) maintaining the STL implementation, working at @microsoft, has a very nice talk in CPPCon 2016 that I recommend you watch. Check it out below. This is the one that got me interested in tuples.

So, now that you are back after watching the video, lets start using Tuples! Below is the code on ideone.com for you to play with. It is very simple. All it does is ask for an input number, calls a function which returns a tuples with 4 string elements. The output is formatted from the data returned from the function call. Tuples is templated so one can use anything inside, even custom classes.

std::make_tuple() is used to create the tuple and std::get<n>(tuple_var) to get the individual values out of it. You can also use std::tie() to define varaible names and can access them instead of accessing by index. @StefanTLavavej video shows more details and go through it if you haven’t done that already. Happy coding using tuples.

Send in your comments & Suggestions. Follow me on Twitter @wolverine2k and share the love by clicking the buttons below and posting this on your social networks.

Ranged Switch Case C++

Ranges in C++ Switch Case block

Today, we will do something really stupid! Yes, I am talking about the ranged switch case support that is available in C++. In all the commercial/hobby source code that I have seen, implemented and used, rarely have I come across the ranged switch case use. Most of the code when in need of ranges inside a switch case start using multiple case cascading or (the more niche programmers) resorting to nested if..else blocks!
So much so that when I told one of my co-workers that we can use ranges in C++ switch case, he started laughing (hysterically if I may add). Taken aback, I wanted to see if I have learned stuff wrong. So here it is, just to brush up your basic cpp knowledge and fall in love with it again! Below is the code in ideone ready for you to take for a spin.

Just to make it plain simple, you can use ranges in your cpp switch case block. And you can do it using ” … ” without the block quotes as shown in the code snippet above. This is quite useful when you don’t want to pollute your code using nested if..else blocks. Switch case is simple, readable and maintainable. Of course, don’t forget your “breaks” :).

Send in your comments & Suggestions. Follow me on Twitter @wolverine2k and share the love by clicking the buttons below and posting this on your social networks.

Simple Downloader!

I come across this very nice site http://www.dwarkadheeshvastu.com/ which has a collection of excellent MP3s (Devotional Music). I congratulate the maintainer of that site for the excellent collection of texts/mp3s in multiple languages. So the site is more or less static with mp3s arranged inside folders starting from 000.mp3 to the <lastsection>.mp3. And you want to download them all.

Layman way is to keep right clicking on each link and do save as. But we are lazy programmers. We will just give our job to the computer to increment the URL and download the file for us. This can very simply be done in a bash script. Below is a snippet. I will not go into explaining all the details, just that the script takes in 3 parameters (start of the count, end of count and the absolute URL of the folder). Run the script and get all the mp3s that you need. One can extend the script to pass in the kind of file we are interested in (like mp3, pdf, etc.). But that is left as a user exercise. The folder path can be got by looking at the HTML code (in case you are wondering how I got it).

Example invocation to get the whole of Ram-Charit-Manas in Avdhi language:

./getRamayan.sh 0 195 http://parthtechsolutions.com/001-MData/Ram-Charit-Manas-Avdhi

Enjoy the code below. PS: Of course it is bash and it runs on Linux. Create a windows batch file and post it in comments.

 #!/bin/bash

i=$1
while [ $i -lt $2 ];
 do
 if [ $i -gt 99 ]; then
   wget -O "$i.mp3" "$3/$i.mp3"
 elif [ $i -lt 10 ]; then
   wget -O "00$i.mp3" "$3/00$i.mp3"
 elif [ $i -gt 9 ]; then
   wget -O "0$i.mp3" "$3/0$i.mp3"
 fi
 ((i+=1))
done

echo "Enjoy!"