Skip to main content


Showing posts from 2017

Heckerrank Problem: Birthday Cake Candles

This is a solution to Hackerrank's algorithm problem Implementation of the same problem in C++ is as following. #include #include #include #include #include using namespace std; int main() { int age_n; std::vector candles_n; int input; cin>>age_n; while(cin >> input) candles_n.push_back(input); int largest_height = *std::max_element(candles_n.begin(), candles_n.end()); int mycount = std::count (candles_n.begin(), candles_n.end(), largest_height); cout << mycount; return 0; }

Counting Inversion: Piggybacking on Merge Sort with Python and C++

This is a solution to Coursera's algorithm problem import os def main(): array = [] with open("IntegerArray.txt") as file: array = [line.strip() for line in file] mergesort(array) def merge(a,b): c = [] count = 0 while len(a) != 0 and len(b) != 0: if a[0] < b[0]: c.append(a[0]) a.remove(a[0]) count += 1 else: c.append(b[0]) b.remove(b[0]) count += 1 if len(a) == 0: c += b else: c += a # print("\n\n\n\n"+str(count)+"\n\n\n\n") return count def mergesort(x): if len(x) == 0 or len(x) == 1: return x else: middle = len(x)/2 middle = int(middle) a = mergesort(x[:middle]) b = mergesort(x[middle:]) return merge(a,b) if __name__ == '__main__': main() Implementation of the same problem in C++ is as follow