Skip to content

Instantly share code, notes, and snippets.

/* Here is your chance to take over Socrates!
Spend 10 minutes on each of the following hacks to the socrates website.
Enter them in the console to make sure it works and then save
your results here.
Choose a new pair for each. Add your names to the section you complete.
*/
@avanishgiri
avanishgiri / gist:5853867
Created June 24, 2013 21:42
Intro to Rails Routing

In Sinatra, we have

'''ruby get "/posts/:id" do @post = ... erb :post end '''

class Integer
def printMySquare
print self * self
print "\t"
return self
end
end
puts 4.printMySquare
#include <iomanip>
#include <iostream>
using namespace std;
void bottomLeft(int** array, int x1, int y1, int x2, int y2);
void topRight(int **array, int x1, int y1, int x2, int y2){
for(int i = x1; i<= x2; i++){
cout << array[y1][i] << " ";
}
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.string :title
t.integer :seller_id
t.timestamps
end
end
end
@avanishgiri
avanishgiri / find_pivot.rb
Last active December 24, 2015 13:39
Solution to find pivot in linear time
def find_pivot(array)
return -1 if array.empty?
left = 0
right = array[1..-1].inject(0,:+)
for i in 0...array.length
return i if left == right
left += array[i]
right -= array[i+1] || 0
public class NQueens
{
final static int N = 15;
private static boolean possibleQueen(boolean[][] board, int row, int column){
for(int i = 0; i < N; i++)
if(board[row][i] && i != column)
return false;
for(int i = 0; i < N; i++)
@avanishgiri
avanishgiri / NQueens.java
Last active December 25, 2015 20:49
This program prints out every solution the N-Queens puzzle. ------- http://en.wikipedia.org/wiki/Eight_queens_puzzle
public class NQueens
{
final static int N = 12;
private static boolean possibleQueen(boolean[][] board, int row, int column){
for(int i = 0; i < N; i++)
if(board[row][i] && i != column)
return false;
for(int i = 0; i < N; i++)
@avanishgiri
avanishgiri / como.cpp
Created November 8, 2013 01:15
combo.cpp
#include <iostream>
using namespace std;
void combos(string soFar, string rest, string full){
if(rest == ""){
int len = full.length();
for(int i = 0, j = 0; i < len; i++)
{
if(i == (int)(soFar[j] - '0')){
#include <iostream>
using namespace std;
void print_combos(string s){
int len = s.length();
int iterations = 1 << len; //this makes iterations 2 to the power of length
for(int i = 0; i < iterations; i++){