Skip to content

Instantly share code, notes, and snippets.

@binhngoc17
binhngoc17 / gist:10311749
Created April 9, 2014 20:34
Generate arff file for analysis in Weka (Prudential Challenge)
from datetime import datetime
import csv
label = """
@relation whatever
@attribute AGE numeric
@attribute GENDER {M,F}
@attribute HOSPITAL string
@attribute DIAGNOSISCODE string
@attribute DATEOFADM date "yyyy-MM-dd HH:mm:ss"
@binhngoc17
binhngoc17 / gist:10612847
Created April 14, 2014 03:02
Generate aggregate data
from datetime import datetime
import csv
cpi = {}
cpi['2012'] = 1.09
cpi['2011'] = 1.043
cpi['2010'] = 1.019
cpi['2009'] = 1
cpi['2008'] = 0.98
cpi['2007'] = 0.929
@binhngoc17
binhngoc17 / gist:fd5dee3984ab81b04465
Created September 22, 2014 10:54
Hack to Nosetest
import unittest
class SampleTestCase(unittest.TestCase):
def __init__(self, *args, **kwargs):
self.account_id = kwargs['account_id']
del kwargs['account_id']
super(SampleTestCase, self).__init__(*args, **kwargs)
def test_a(self):
@binhngoc17
binhngoc17 / gist:d7dc8ef028850ba92d8a
Created November 12, 2014 14:01
Find second largest element of a list - Python
def find_second_largest(arr):
if len(arr) < 2:
return None
largest = max(arr[0], arr[1])
sec_largest = min(arr[0], arr[1])
for item in arr[2:]:
if item > largest:
sec_largest = largest
largest = item
elif item <= largest and item > sec_largest:
@binhngoc17
binhngoc17 / gist:5a74374d80b93c19b1b2
Created November 12, 2014 14:15
Diagonal Matrix
'''
Notes about this problem:
- Try to analyze the formation of the result & a way to construct it
'''
M = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
def mat_diagonal(M):
@binhngoc17
binhngoc17 / anagram_checker.py
Created November 19, 2014 07:13
anagram checker
# Enter your code here. Read input from STDIN. Print output to STDOUT
import sys
import fileinput
input = fileinput.input()
s1 = input[0].replace('\n', '')
s2 = input[1].replace('\n', '')
dict_s1 = {}
dict_s2 = {}
# Gather character dictionary of the two strings
@binhngoc17
binhngoc17 / common_ancestor.py
Created November 19, 2014 07:54
Find Common Ancestor of two elements in a BST
import sys
test_cases = open(sys.argv[1], 'r')
bst = {
'datum': 30,
'left': {
'datum': 8,
'left': {
'datum': 3,
'left': None,
@binhngoc17
binhngoc17 / LCS.py
Created November 19, 2014 13:28
Longest Common Subsequence
import sys
test_cases = open(sys.argv[1], 'r')
def generate_char_dict(str):
char_dict = {}
for c in str:
char_dict[c] = char_dict.get(c, 0) + 1
return char_dict
def LCS(strA, strB):
@binhngoc17
binhngoc17 / postfix_eval.py
Created November 19, 2014 13:57
Evaluation of Postfix Expression
import sys
test_cases = open(sys.argv[1], 'r')
def eval_postfix(exp):
stack = []
for i in exp:
try:
val = int(i)
stack.append(val)
except:
@binhngoc17
binhngoc17 / count_ending.py
Created November 19, 2014 14:42
Count ending number
import sys
import pprint
test_cases = open(sys.argv[1], 'r')
for test in test_cases:
str_val = test.replace('\n', '')
n,p = [int(k) for k in str_val.split(' ')]
res = {}