Skip to content

Instantly share code, notes, and snippets.

View kra3's full-sized avatar
🤠
I may be slow to respond.

Arun Karunagath kra3

🤠
I may be slow to respond.
View GitHub Profile
@kra3
kra3 / tictactoe.py
Created May 11, 2011 15:29
Retro "Tic Tac Toe" in python, wrote for fun
#! /usr/bin/env python
# All right reserved. Distributed Under BSD Lisence
__author__="Arun.K.R <the1.arun@gmail.com>"
__date__ ="$May 11, 2011 6:23:17 PM$"
import os, time
from random import choice
WIDTH = 3
@kra3
kra3 / first_sorted.py
Created September 19, 2011 19:11
First implementation
def compare(x, y):
return y < x
def cmp_to_key(mycmp, col, covert_to_proper_datatype):
class K(object):
class K(object):
def __init__(self, obj, *args):
self.obj = obj
def __lt__(self, other):
return mycmp(covert_to_proper_datatype(self.obj[col]), covert_to_proper_datatype(other.obj[col])) == False
@kra3
kra3 / second_sort.py
Created September 19, 2011 19:24
Seconed refinement
def cmp_to_key(col, covert_to_proper_datatype, cmpr = (lambda x, y: x < y)):
class K(object):
def __init__(self, obj, *args):
self.obj = obj
def __lt__(self, other):
return cpmr(covert_to_proper_datatype(self.obj[col]), covert_to_proper_datatype(other.obj[col])) == True
return K
@kra3
kra3 / third_sort.py
Created September 19, 2011 19:25
3rd refinement
def cmp_to_key(col, covert_to_proper_datatype):
#Discrad this approch, once we have an option to sort via webmethods, sorting by DB engine is more efficient than doing it ourselves.
class K(object):
def __init__(self, obj, *args):
self.obj = obj
def __lt__(self, other):
return covert_to_proper_datatype(self.obj[col]) < covert_to_proper_datatype(other.obj[col])
return K
@kra3
kra3 / dict_map_sort.py
Created September 19, 2011 19:30
mapped dict for sorting
mapping_tbl = []
mapping_tbl.insert(0, lambda x: int(m.parseString(x).childNodes[0].childNodes[0].nodeValue))
mapping_tbl.insert(1, lambda x: x.lower())
mapping_tbl.insert(2, lambda x: x.lower())
mapping_tbl.insert(3, lambda x: int(x))
mapping_tbl.insert(4, lambda x: datetime.strptime(x, "%m/%d/%Y"))
mapping_tbl.insert(5, lambda x: int(x.replace('-', '')))
mapping_tbl.insert(6, lambda x: datetime.strptime(x, "%m/%d/%Y"))
mapping_tbl.insert(7, lambda x: datetime.strptime(x, "%m/%d/%Y"))
mapping_tbl.insert(8, lambda x: x.lower())
@kra3
kra3 / refined_map_sort.py
Created September 19, 2011 19:36
Mapping is again refined
mapping_functions = {
'str_lower' : lambda x: x.lower(),
'date_mdY' : lambda x: datetime.strptime(x, "%m/%d/%Y"),
'ssn' : lambda x: int(x.replace('-', ''))
'int' : lambda x: int(x)
'dom' : lambda x: int(m.parseString(x).childNodes[0].childNodes[0].nodeValue)
}
mapping_tbl = []
mapping_tbl.insert(0, mapping_functions['dom'])
@kra3
kra3 / in_effective_sort.py
Created September 19, 2011 19:38
First Thought
sorted([[5, 'Man', 1], [2, 'Arun', 6], [4, 'Dummy', 6], [1, 'Naveen', 3], [3, 'Bajsd', 7]], key=lambda x: str(x[1]).lower())
@kra3
kra3 / in_effective_sort.py
Created September 19, 2011 19:38
First Thought
sorted([[5, 'Man', 1], [2, 'Arun', 6], [4, 'Dummy', 6], [1, 'Naveen', 3], [3, 'Bajsd', 7]], key=lambda x: str(x[1]).lower())
@kra3
kra3 / final_Sort.py
Created September 19, 2011 19:42
final solution
from datetime import datetime
import xml.dom.minidom as m
def cmp_to_key(col, covert_to_proper_datatype):
class K(object):
def __init__(self, obj, *args):
self.obj = obj
def __lt__(self, other):
return covert_to_proper_datatype(self.obj[col]) < covert_to_proper_datatype(other.obj[col])
return K
struct User{
char name[45];
int age;
float height;
}usr;
usr.name = "Someone";
usr.age = 31;
ur.height = 6.1;