Skip to content

Instantly share code, notes, and snippets.

@gabhi
gabhi / gist:4c38e392e10e3ab04efd
Created June 28, 2014 06:08
custom eventmanager in javascript
var EventManager = function() {
this.initialize();
};
// place properties here
// Constructor
EventManager.prototype = {
initialize: function() {
//declare listeners as an object
this.listeners = {};
},
@gabhi
gabhi / gist:4f6d5d1758d9ddb04baf
Created June 28, 2014 19:42
leaf nodes in binary tree
int getLeafCount(struct Node node)
{
if(node == null)
return 0;
if(node.left == null && node.right==null)
return 1;
else
return getLeafCount(node.left)+
getLeafCount(node.right);
}
@gabhi
gabhi / gist:8c0dc1f7249f09b389b2
Last active August 29, 2015 14:03
Search in rotated Array
public class SearchInRotatedArray {
public static void main(String[] args) {
System.out.println("Search in rotated Array");
int[] a = { 5, 6, 7, 1, 2, 3, 4 };
System.out.println(rotated_binary_search(a, 6));
}
private static int rotated_binary_search(int A[], int key) {
@gabhi
gabhi / gist:dedae4bcee1d499b17cf
Created July 3, 2014 04:47
Find number of occurrences of an element in an array
import java.util.*;
import java.lang.*;
import java.io.*;
class OccurenceOfElementInSortedArray
{
static int findfirst(int arr[],int start,int end,int n,int no)
@gabhi
gabhi / gist:87c6289dd64d56e0ec8d
Created July 3, 2014 05:00
Diameter of binary Tree
public static int getDiameter(BinaryTreeNode root) {
if (root == null)
return 0;
int rootDiameter = getHeight(root.getLeft()) + getHeight(root.getRight()) + 1;
int leftDiameter = getDiameter(root.getLeft());
int rightDiameter = getDiameter(root.getRight());
return Math.max(rootDiameter, Math.max(leftDiameter, rightDiameter));
}
@gabhi
gabhi / gist:90eae2c95858aa81f18a
Created July 3, 2014 05:24
Merge 2 sorted arrays
public static int[] merge(int[] a, int[] b) {
int[] answer = new int[a.length + b.length];
int i = 0, j = 0, k = 0;
while (i < a.length && j < b.length)
{
if (a[i] < b[j])
answer[k++] = a[i++];
@gabhi
gabhi / gist:40acd4b113f754ed935b
Last active August 29, 2015 14:03
reverse linked list
public SingleLinkedList reverseRecursive(SingleLinkedList head) {
ListNode current = head;
if(current == null || current.next ==null) return current;
ListNode next= current.next;
current.next = null;
ListNode r = reverseList(next);
@gabhi
gabhi / gist:02edbb32d972176d851d
Created July 23, 2014 23:35
wordpress create post using xmlrpc
<?php
require("class-IXR.php");
$client = new IXR_Client('http://domain.com/xmlrpc.php');
$USER = 'rrrr';
$PASS = 'rrrr';
// if (!$client->query('wp.getCategories','', $USER,$PASS))
// {
// echo('Error occured during category request.' . $client->getErrorCode().":".$client->getErrorMessage());
@gabhi
gabhi / gist:9614609de7a3edcdefbe
Created July 24, 2014 01:02
Wordpress xmlrpc bulk post creator using csv
<?php
require("class-IXR.php");
function create($client, $USER,$PASS,$title,$desc){
$content['title'] = $title;
$content['description'] = $desc;
if (!$client->query('metaWeblog.newPost','', $USER,$PASS, $content, false))
{
<?php
/**
* IXR - The Incutio XML-RPC Library
*
* Copyright (c) 2010, Incutio Ltd.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*