Skip to content

Instantly share code, notes, and snippets.

@ahmedahamid
ahmedahamid / C#Example.cs
Last active May 10, 2016 02:58
Why I started to feel differently about C# | Using Dictionaries
//
// (1) C#: Defining an initializing a dictionary [key type=string, value type=int]
//
Dictionary<string, int> dict = new Dictionary<string, int>()
{
{"Eve", 101},
{"George", 150},
{"Emma", 200}
};
//
@ahmedahamid
ahmedahamid / C++Example.cpp
Last active May 10, 2016 02:57
Why I started to feel differently about C# | Using STL map
//
// (1) C++: - Defining a dictionary [key type=string, value type=int]
// - No easy way to initialize.
//
map<string, int> dict;
//
// (1`) C++11: Defining and initializing a dictionary [key type=string, value type=int]
//
map<string, int> dict
{
@ahmedahamid
ahmedahamid / binary_search_recursive.cpp
Last active September 20, 2015 05:10
Tricky pointer basics explained | Recursive implementation of binary search
tree_node* binary_search(tree_node* n, int value)
{
if (n == NULL)
{
return NULL;
}
if (value == n->data)
{
return n;
@ahmedahamid
ahmedahamid / binary_search_iterative.cpp
Last active September 20, 2015 05:35
Tricky pointer basics explained | Iterative implementation of binary search
tree_node* binary_search(tree_node* n, int value)
{
if (n == NULL)
{
return NULL;
}
tree_node* x = n;
while (x != NULL)
@ahmedahamid
ahmedahamid / insert.cpp
Last active September 20, 2015 06:21
Tricky pointer basics explained | Inserting an element into a binary search tree
tree_node* insert(tree_node* n, int value)
{
if (n == NULL)
{
return NULL;
}
tree_node** x = &n;
while ((*x) != NULL)
tree_node* insert(tree_node* n, int value)
{
if (n == NULL)
{
return NULL;
}
tree_node* x_parent = NULL;
tree_node* x = n;
@ahmedahamid
ahmedahamid / insert_recursive.cpp
Last active September 20, 2015 06:20
Tricky pointer basics explained | Recursive implementation of insertion into binary search tree
tree_node* insert(tree_node* n, int value)
{
if (n == NULL)
{
tree_node* new_node = new tree_node();
new_node->data = value;
new_node->left = new_node->right = NULL;
return new_node;
}
#include <string>
#include <fstream>
int main() {
std::ifstream input_file("/etc/passwd");
if (input_file)
{
std::string line;
@ahmedahamid
ahmedahamid / binary-search-JDK-6-b27.java
Created August 19, 2017 09:43
Useful insights into Binary Search problems
private static int binarySearch0(int[] a, int fromIndex, int toIndex, int key) {
int low = fromIndex;
int high = toIndex - 1;
while (low <= high) {
int mid = (low + high) >>> 1;
int midVal = a[mid];
if (midVal < key)
low = mid + 1;
@ahmedahamid
ahmedahamid / search-cyclically-sorted-array-EPI.java
Created August 19, 2017 09:55
Useful insights into Binary Search problems
//
// Finds the position of the smallest element in a cyclically sorted array.
// Copied verbatim from: http://elementsofprogramminginterviews.com/solutions/java/BinarySearchCircularArray.java
//
public static int searchSmallest(List<Integer> A) {
int left = 0, right = A.size() - 1;
while (left < right) {
int mid = left + ((right - left) / 2);
if (A.get(mid) > A.get(right)) {
// Minimum must be in A.subList(mid + 1, right + 1).