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 / #King_Of_Kings.groovy
Created January 15, 2012 06:11
Problem solved on Top Coders.(14.2.500)
//map for managing the conversion from roman to no's
def map =[I:1,II:2,III:3,IV:4,V:5,VI:6,VII:7,VIII:8,IX:9,X:10,XI:11]
//map for managing the conversion from no to romans
def map1 = ['1':"I",'2':"II",'3':"III",'4':"IV",'5':"V",'6':"VI",'7':"VII",'8':"VIII",'9':"IX",'10':"X",'11':"XI"]
//temp variables
ans = []
res = []
//final result
res1 = []
//for managing the flow of algorithm
@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 / #insertionSort.groovy
Created February 4, 2012 15:46
Insertion Sort In Groovy
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();
(1..(a.size()-2)).each { out ->
def temp = a.getAt(out)
def index = out
while(index>0 && a.getAt(index-1) >= temp)
{
value = a.getAt(index-1)
a.putAt(index,value)
@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()