Skip to content

Instantly share code, notes, and snippets.

View ishahid's full-sized avatar

Imran Shahid ishahid

View GitHub Profile
@ishahid
ishahid / sort_json_array.js
Created February 26, 2014 03:26
Generic way of sorting JSON array by attribute
function sort_by(attr) {
return function(a, b) {
if( a[attr] > b[attr]) {
return 1;
} else if( a[attr] < b[attr] ) {
return -1;
}
return 0;
}
}
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
Write tags to files
Usage:
tag.py "TagName" FileName1 FileName2
You can use wildcards for the file name. Use quotes if spaces in tags.
To check if it worked, use xattr -l FileName
@ishahid
ishahid / dropbox_poc.py
Last active August 29, 2015 13:56
Dropbox File Transfer Comparison - Queue vs Parallel
#!/usr/bin/python
"""
Dropbox File Transfer Comparison - POC
This program compares the speed of uploading multiple files to dropbox using
both queued and parallel connections per file.
"""
import time
@ishahid
ishahid / anagram.py
Last active January 3, 2016 07:29
Find all anagrams for a given word from a list of words.
def signature(word):
""" Returns the sorted word """
return ''.join(sorted(word))
def anagrams(word):
""" Returns a list of all anagrams for a given word """
words = [w.strip().lower() for w in open('words.txt')]
@ishahid
ishahid / ugly_number.py
Created January 15, 2014 02:44
O(N) solution to find Nth ugly number.
def ugly_number(n):
""" Returns Nth ugly number """
assert n > 0, 'The argument must be greater than zero.'
list = [0]*n
i2, i3, i5 = 0, 0, 0
w2, w3, w5 = 2, 3, 5
w, list[0] = 1, 1
@ishahid
ishahid / num2str.py
Created January 15, 2014 02:41
Convert numbers to their string representation.
import re
units = {
0: 'Zero', 1: 'One', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five', 6: 'Six', 7: 'Seven', 8: 'Eight', 9: 'Nine'
}
tens = {
10: 'Ten', 11: 'Eleven', 12: 'Twelve', 13: 'Thirteen', 14: 'Fourteen', 15: 'Fifteen', 16: 'Sixteen',
17: 'Seventeen', 18: 'Eighteen', 19: 'Nineteen', 20: 'Twenty', 30: 'Thirty', 40: 'Forty', 50: 'Fifty',
60: 'Sixty', 70: 'Seventy', 80: 'Eighty', 90: 'Ninety'
}
@ishahid
ishahid / recursive_sum.py
Created January 15, 2014 02:39
Recursive sum of all elements of a given list.
def sum(args):
""" Returns sum of all elements of a given list, recursively """
if len(args) == 0:
return 0
return args[0] + sum(args[1:])
if __name__ == "__main__":
@ishahid
ishahid / soap.php
Created January 15, 2014 02:31
SOAP service call in PHP.
<?php
try {
$options = array(
'soap_version'=>SOAP_1_2,
'exceptions'=>true,
'trace'=>1,
'cache_wsdl'=>WSDL_CACHE_NONE
);
$client = new SoapClient('http://soap.service-provider.com/endpoint.asmx?WSDL', $options);
}
@ishahid
ishahid / hash.py
Created January 15, 2014 02:28
Hash calculation tool.
#!/usr/bin/env python
import os, sys
import hashlib
def usage(full=False):
msg = ''
if full:
msg += 'Computes hash of a string.' + os.linesep
@ishahid
ishahid / http_headers.php
Created January 15, 2014 02:19
Display HTTP headers in PHP.
<?php
$http_client_ip = '';
$http_x_forwarded_for = '';
$remote_addr = $_SERVER['REMOTE_ADDR'];
$remote_host = '';
$remote_port = $_SERVER['REMOTE_PORT'];
$remote_user = '';
$redirect_remote_user = '';
$http_user_agent = $_SERVER['HTTP_USER_AGENT'];