Skip to content

Instantly share code, notes, and snippets.

View patelpreet422's full-sized avatar
๐Ÿš€
Changing the world for better

Preet Patel patelpreet422

๐Ÿš€
Changing the world for better
View GitHub Profile

Haskell, Stack and Intellij IDEA IDE setup tutorial how to get started

Upon completion you will have a sane, productive Haskell environment adhering to best practices.

Basics

  • Haskell is a programming language.
  • Stack is tool for Haskell projects. (similar tools for other languages include Maven, Gradle, npm, RubyGems etc)
  • Intellij IDEA IDE is a popular IDE.

Install required libraries

sudo apt-get install libtinfo-dev libghc-zlib-dev libghc-zlib-bindings-dev

@patelpreet422
patelpreet422 / producer_consumer.cpp
Last active April 26, 2018 11:45
Producer consumer problem implementation in C++.
//run following program by using following command
//compile: g++ -std=c++17 -pthread producer_consumer.cpp
//run: ./a.out (on linux machine)
//run: a.exe (on windows machine)
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <queue>
#include <chrono>
@patelpreet422
patelpreet422 / rpn.cpp
Last active May 5, 2018 20:08
Reverse Polish Notation (RPN) using C++
/*
* This code was compiled using g++-7 on Ubuntu 16.04LTS platform using following command
* g++-7 -std=c++17 rpn.cpp
* ./a.out
*/
#include <iostream>
#include <vector>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <map>
#include <iostream>
#include <algorithm>
unsigned long long moveto(const std::pair<int, int> &p, std::map< std::pair<int, int>, unsigned long long> &memo)
{
if(memo.find(p) != memo.end()) return memo[p];
/*
(0, 0) -> (0, 0) => We are already at our destiny so, no of ways we can reach our destiny is '0'
(0, 0) -> (a, 0) => Only '1' way to reach any point on X-axis from origin
#include <iostream>
void toh(char from, char to, char intermediate, int noofdisk)
{
if(noofdisk == 1)
std::cout << "Move " << noofdisk << " from " << from << " to " << to << '\n';
else {
toh(from, intermediate, to, noofdisk-1);
std::cout << "Move " << noofdisk << " from " << from << " to " << to << '\n';
toh(intermediate, to, from, noofdisk-1);
}
@patelpreet422
patelpreet422 / recursive_palindrome.cpp
Last active June 7, 2018 21:12
Palindrome using recursion
#include <iostream>
#include <string>
using namespace std;
bool isPalindrome(const string &str, int low, int high)
{
if (low >= high)return true;
if (str[low] != str[high])return false;
return isPalindrome(str, ++low, --high);
}
@patelpreet422
patelpreet422 / recursive_bsearch.cpp
Last active June 6, 2018 12:26
Recursive binary search
#include <iostream>
#include <algorithm>
using namespace std;
bool binarySearchHelper(int key, int *array, int low, int high){
if(low == high) return array[low] == key;
if(low < high) {
int mid = (low + high)/2;
if(array[mid] == key) return true;
else if(array[mid] > key) return binarySearchHelper(key, array, low, mid);
@patelpreet422
patelpreet422 / kadanes_algorithm.cpp
Last active June 8, 2018 03:44
Maximum sum sub-array
#include <iostream>
#include <algorithm>
#include <climits>
#include <tuple>
using namespace std;
tuple<int, int, int> maxsumsubarray(int *arr, int size)
{
int start = 0, end = size-1, max_sum=INT_MIN, sum = 0;
for(int i = 0; i < size; ++i)
{
@patelpreet422
patelpreet422 / permutation.cpp
Last active June 7, 2018 21:03
Permutation in C++
#include <iostream>
#include <string>
#include <algorithm>
void permuteHelper(std::string s, std::string chosen)
{
if (s.empty())
{
std::cout << chosen << '\n';
return;
}
@patelpreet422
patelpreet422 / factorial.cpp
Last active June 9, 2018 19:31
Factorial of large numbers
#include <iostream>
#include <algorithm>
#include <string>
#include <chrono>
using namespace std::chrono;
const int limit = 200;
const std::string factorial(int n)
{
std::string res(limit, '0');
res[0] = '1';