Skip to content

Instantly share code, notes, and snippets.

View antsmartian's full-sized avatar
💭
Hacking...

antsmartian antsmartian

💭
Hacking...
  • India
View GitHub Profile
@antsmartian
antsmartian / #bubbleSort.groovy
Created February 3, 2012 13:30
Bubble Sort alg in Groovy!
//change this array and play with Bubble Sort!
a = [1,43,5,67,8,912,3,465,89,89,82,34]
println a.size()
for(out in (a.size()-2)..2)
{
0.upto(out) { index ->
if(a[index] > a[index+1])
swap(index,index+1)
}
}
@antsmartian
antsmartian / #selectionSort.groovy
Created February 4, 2012 11:02
Alg for selection sort :)
//change this array and play with selection sort :)
a = [2,34,4,123,45,56,3,56,67,87,21,45,56,67,22,4234,4234,2342,1,34,546,67,677,343,4,41,32,456,1341,13,454,234,453,565,5,524,234,234,5456,567,673,245,567,78,78,324,56576,782,435,676,873,2,345,6767,7,3463,3,6767673,34534,2,345,656,3,5652,1,34345,3453545,35345356,66,7883345,676787,8988]
b = a
long startTime = System.currentTimeMillis();
(0..(a.size()-2)).each {
minimumValue = it
(it+1).upto(a.size()-1){ insideElement ->
if(a[insideElement] < a[minimumValue])
minimumValue = insideElement
}
@antsmartian
antsmartian / gist:1743346
Created February 5, 2012 06:00
Stack on Groovy ;)
input = args[0] as Integer
top = -1
stack = new long[input]
def push(long j)
{
stack.putAt(++top,j)
}
def pop()
{
stack.getAt(top--)
@antsmartian
antsmartian / #reverse.groovy
Created February 5, 2012 06:46
Reversing a string or a number using Stack Data Structure
/*
These two inputs input and input2 are passed via command line
*/
//input as a String for reversal!
input2 = args[1]
//input for the size of Stack!
input = args[0] as Integer
top = -1
stack = new String[input]
boolean full = false
@antsmartian
antsmartian / #delimter.groovy
Created February 5, 2012 14:49
Delimiter Parsing In groovy
/*
These two inputs input and input2 are passed via command line
*/
input2 = args[0]
//input for the size of Stack!
input = input2.size()
top = -1
stack = new String[input]
boolean full = false
def push(String j)
@antsmartian
antsmartian / #xray.html
Created February 6, 2012 14:32
Blog Article code
<html>
<head>
<title>Play With Pixel</title>
<style>
canvas { margin: 0px auto 0; display: block; }
</style>
</head>
<body>
<canvas id="canvas" height="900" width="800"></canvas>
<script>
@antsmartian
antsmartian / #spell.groovy
Created February 12, 2012 01:47
Spell Checker in Groovy :)
//a book keep!
def a = ["grails","griffon","gradle","groovy"]
//user sentence
def sentence = "gralis grfifon grovoy gralde"
def split = sentence.split(' ')
def bool = false
split.each {
i = 0
def temp = []
temp << it.split{it=""}
@antsmartian
antsmartian / #chain.groovy
Created February 12, 2012 15:01
Top coder problem solution
//http://cse0812.blogspot.in/2012/02/problem-from-top-coder.html
def a = [".15", "7..", "402", "..3"]
c = a.permutations() as List
ans = []
c.each {
ans << it.join().tokenize('.')*.toList().collect{ it*.toInteger().sum() }
}
println ans.flatten().max()
@antsmartian
antsmartian / Histogram.c
Created April 5, 2012 08:44
Count the word lengths in a file :)
#include <stdio.h>
#define INSIDEWORD 1
#define OUTSIDEWORD 0
#define MAXWORD 10
main() {
char c;
int i=0,j,state= OUTSIDEWORD,words =0, ndigits[10],length=0;
for(i=0;i<10;i++)
ndigits[i] = 0;
while((c = getchar()) != EOF)
@antsmartian
antsmartian / chicken.groovy
Created April 6, 2012 12:54
Description of Chicken in a Programming Language.
String.metaClass.nutritions = {
def pro = [Carbohydrates:0.0,Protein:24.68,Fat:12.56,
Water:63.93]
if(delegate.toLowerCase() == "chicken")
{
println "--------$delegate Properties is--------"
pro.each { k,v ->
print k + " found in chicken is "
println v