Skip to main content

Posts

Find time from time ranges (including overlapping ranges)

 This is a very simple problem. In this trying to find a given time from ranges of times. So, I have a collection which stores different time ranges (including overlapping time ranges). The job is to find or search if a time provided by user exists in time ranges collection. Example: Let's assume I have following collections of time ranges: 1. 07:00 - 09:00  2. 13:45 - 15:15 3. 16:25 - 18:10 4. 08:30 - 10:00 Now, need to find if time 09:30 is present in that collection or not. If it finds it shall print true, otherwise false. Similarly it can be use to check other times too. Solution: To store time ranges, what I did is converted start and end time to decimal numbers respectively and stored them in STL's set (multiset) used to address overlapping ranges. As we know that set data structure in STL is a tree (Red Black Tree / Balanced Binary tree).  Time Complexity:  1. set::insert - If N elements are inserted, Nlog(size+N). Implementations may optimize if the range is ...

Network Shell Utility (netsh, An useful usage)

 In Windows OS, there is a command known as netsh or network shell. It is a command line utility included in Microsoft Windows NT lines of the operating system, beginning with Windows 2000. It allows local/remote network configuration as well as it can be used to display network configuration. Today, we can see how 'netsh' can be leveraged to display wireless passwords. It happens we may forget the wireless password(s) after connecting a device to many wireless networks since it gets stored as a part of the network profile. That said, we enter password for a wireless network for the first time and it gets stored, and then the next time onwards the device gets connected to the wireless network once it is available. We don't need to key in a password again. As a result, we may forget passwords and may get challenged to remember and connect to any other device.  In this post, we will see how can we leverage netsh command to get/retrieve wireless network passwords from a device...

Conversion to std::string and return from function

 Many times I have come across code something like the below: class AClass { public:     std::string getName(); }; std::string AClass::getName() {     return "I am returning"; } Here the member function converts the string constant to std::string while returning. This is in my opinion not necessary. This cast away some flexibility. I mean this way of converting to a std::string while returning to function does not make much sense.  Reason:  If we need in any place to get the name as const char* I need to reverse by calling string::c_str() const char* pName = instanceOfClassA.getName().c_str(); However, keeping the return type as const char* has some benefits: const char* AClass::getName() const {     return "I am returning"; } 1. const char* pName =  instanceOfClassA.getName().getName(); // No Conversion will take place 2. std::string sName = instanceOfClassA.getName().getName(); // Conversion happens to std::string  So, only based ...

A simple echo program

The following small program accepts streams from the console and prints out as soon as we hit enter. #include template < typename In, typename Out > void Echo(In& in, Out& out) {     out << in.rdbuf(); } int main() {     Echo(std::cin, std::cout);     return 0; } So, the rdbuf() returns a pointer to the underlying  basic_streambuf  for the input stream and prints it out again on the console. The program runs in a loop until we break it by Ctrl+C.

Pancake Sorting using C++(STL)

This is a variation of selection sort, it bring the largest pancake not yet sorted to the top with one flip; take it down to its final position with one more flip; and repeat this process for the remaining pancakes. The algorithm has been discussed in following wiki link: Pancake Sorting The algorithm is visually represented in following link: Pancake Sorting(Visualization) The idea of this sorting is similar to Selection Sort, here taking maximum items one by one and started placing those from last and at the same time reducing the vector size one by one. To place bigger items in order from last using flips two move to their respective positions. Here is the implementation using std::rotate() and std::reverse(). The output: 

Insertion Sort (C++ Way) Using Standard Template Library

This is an effort to implement insertion sort in C++ way using Standard Template Library. The below implementation is for vector of any type. Two STL libraries algorithm has been used. 1. std::lower_bound 2. std::rotate The implementation cross compiler compatible, tested on GNU C++ (5.4.0) and Microsoft C++ (2010). //g++  5.4.0 #include < iostream > #include < algorithm > #include < vector > template< typename T > void insertionSort(std::vector< T > &vec) { typedef typename std::vector< T >::value_type vt; typename std::vector< vt >::iterator itBegin = vec.begin(); typename std::vector< vt >::iterator itEnd = vec.end(); for(typename std::vector< vt >::iterator it = itBegin; it != itEnd; ++it) { typename std::vector ::iterator ins = std::lower_bound(itBegin, it, *it); std::rotate(ins, it, std::next(it)); } } int main() {     int arr[] = {34, 20, 99, 10, 23}; std::vector...

Selection Sort (C++ way) for vector of any type

In my last article have seen how we can leverage C++ STL to do selection sort on the vector of integers only. However, the selection sort wasn't very generic to accept vectors of any basic type and sort accordingly. The below program was an effort to make the same selection sort (ascending order) implementation for vectors of any type like vectors of integers, doubles, chars, etc. #include < iostream > #include < algorithm > #include < vector > template < class ForwardIterator1, class ForwardIterator2 > void iters_swap (ForwardIterator1 a, ForwardIterator2 b) {     std::swap (*a, *b); } template < typename T > void selectionSort(std::vector< T > &v) { // Selection sort typedef typename std::vector ::value_type vt; typename std::vector ::iterator it = v.begin(); while(it != v.end()) {     typename std::vector ::iterator i_Min = std::min_element (it, v.end()); iters_swap(i_Min, it); ++it; } } ...