Skip to content

Instantly share code, notes, and snippets.

#Newbie programmer
def factorial(x):
if x == 0:
return 1
else:
return x * factorial(x - 1)
print factorial(6)
#First year programmer, studied Pascal
@gennad
gennad / UrlShortenerPos.java
Created December 2, 2010 12:38
Generates the number of position for your url in url shortener
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Formatter;
import java.util.List;
import java.util.Locale;
import java.util.Scanner;
import java.util.Stack;
import java.util.regex.Matcher;
public class Secret {
private String secretCode = "It's a secret";
private String getSecretCode(){
return secretCode;
}
}
/**
* Connects to the database
*
* Connects to the database and some other
* discription....
* @param $type some variable
*/
function connect($type = '') {
//some code
}
@gennad
gennad / GoldenNumber.cpp
Created December 22, 2010 15:59
GoldenNumber.cpp
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <string>
#include <map>
int getIntVal(std::string strConvert) {
int intReturn;
@gennad
gennad / Debug output
Created January 9, 2011 18:42
Debug output
Initial:
x.x.x.x
x.x.x
x.x...x
x.x.x
x.x.x.x
1.
xOx.x.x
xOx.x
@gennad
gennad / BFSDFS.java
Created January 23, 2011 09:14
Breadth-first search and depth-first search Java implementation
Class Main {
public void bfs()
{
// BFS uses Queue data structure
Queue queue = new LinkedList();
queue.add(this.rootNode);
printNode(this.rootNode);
rootNode.visited = true;
while(!queue.isEmpty()) {
Node node = (Node)queue.remove();
@gennad
gennad / Dijkstra.java
Created January 23, 2011 09:36
Dijkstra algorithm java implementation
import java.util.*;
public class Dijkstra {
// assumes Nodes are numbered 0, 1, ... n and that the source Node is 0
ArrayList<Node> findShortestPath(Node[] nodes, Edge[] edges, Node target) {
int[][] Weight = initializeWeight(nodes, edges);
int[] D = new int[nodes.length];
Node[] P = new Node[nodes.length];
ArrayList<Node> C = new ArrayList<Node>();
int partition(int arr[], int left, int right)
{
int i = left, j = right;
int tmp;
int pivot = arr[(left + right) / 2];
while (i <= j) {
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
/**
* Mergesort algorithm.
* @param a an array of Comparable items.
*/
public static void mergeSort( Comparable [ ] a ) {
Comparable [ ] tmpArray = new Comparable[ a.length ];
mergeSort( a, tmpArray, 0, a.length - 1 );
}
/**