Created
March 20, 2015 08:34
-
-
Save manujeevanprakash/d4b0be17ab3b3af0a314 to your computer and use it in GitHub Desktop.
Dn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "", | |
"signature": "sha256:552d265ed92325f3db5e7769376a58a5afc8450de717c631584ea956f9fff1a5" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"##Problem 1" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import collections\n", | |
" \n", | |
"def function_generateWordsHash(listofwords):\n", | |
"\n", | |
" listofwords.sort();\n", | |
"\n", | |
" # convert to a set to remove dupilcate\n", | |
" my_set = set(listofwords)\n", | |
"\n", | |
" # convert back to list to sort and maintain order\n", | |
" word_list = list(my_set)\n", | |
"\n", | |
" word_list.sort();\n", | |
"\n", | |
" listofalpha = []\n", | |
" for i in range(26):\n", | |
" listofalpha.append(set()) \n", | |
"\n", | |
"\n", | |
"\n", | |
" for item in my_set:\n", | |
" for char in item:\n", | |
" listofalpha[ord(char) - 97].add(item)\n", | |
" \n", | |
"\n", | |
" hashmap = {} \n", | |
"\n", | |
" for item in word_list:\n", | |
" mylist = set()\n", | |
" for char in item:\n", | |
" for m in listofalpha[ord(char) - 97]:\n", | |
" if m != item:\n", | |
" mylist.add(m);\n", | |
" temp = list(mylist)\n", | |
" temp.sort(); \n", | |
" hashmap.update({item:temp })\n", | |
" od = collections.OrderedDict(sorted(hashmap.items()))\n", | |
" for k, v in od.iteritems(): print k, v\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 11 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"listofwords = ['cat', 'dog', 'mad', 'mud', 'bat', 'tree', 'mad', 'but']\n", | |
"function_generateWordsHash(listofwords)\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"bat ['but', 'cat', 'mad', 'tree']\n", | |
"but ['bat', 'cat', 'mud', 'tree']\n", | |
"cat ['bat', 'but', 'mad', 'tree']\n", | |
"dog ['mad', 'mud']\n", | |
"mad ['bat', 'cat', 'dog', 'mud']\n", | |
"mud ['but', 'dog', 'mad']\n", | |
"tree ['bat', 'but', 'cat']\n" | |
] | |
} | |
], | |
"prompt_number": 12 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#Problem 2" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"from itertools import product\n", | |
"\n", | |
"def generateUniqueCombinations(lista):\n", | |
" \n", | |
"\n", | |
" newlists = []\n", | |
"\n", | |
"\n", | |
" for s in lista:\n", | |
" # string partition() method splits a string into 3 parts: \n", | |
" #the substring before the separator, the separator itself, and the substring after the separator\n", | |
"\n", | |
" head, _, tail = s.partition('=')\n", | |
" newlists.append(['%s=%s' % (head, u) for u in tail.split(',')])\n", | |
"\n", | |
" listb = ['&'.join(t) for t in product(*newlists)]\n", | |
"\n", | |
" for row in listb:\n", | |
" print row" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 13 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"lista = ['tX=txVal1,txVal2','tY=tyVal1,tyVal2,tyVal3','tZ=tzVal1,tZVal2']\n", | |
"\n", | |
"generateUniqueCombinations(lista)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"tX=txVal1&tY=tyVal1&tZ=tzVal1\n", | |
"tX=txVal1&tY=tyVal1&tZ=tZVal2\n", | |
"tX=txVal1&tY=tyVal2&tZ=tzVal1\n", | |
"tX=txVal1&tY=tyVal2&tZ=tZVal2\n", | |
"tX=txVal1&tY=tyVal3&tZ=tzVal1\n", | |
"tX=txVal1&tY=tyVal3&tZ=tZVal2\n", | |
"tX=txVal2&tY=tyVal1&tZ=tzVal1\n", | |
"tX=txVal2&tY=tyVal1&tZ=tZVal2\n", | |
"tX=txVal2&tY=tyVal2&tZ=tzVal1\n", | |
"tX=txVal2&tY=tyVal2&tZ=tZVal2\n", | |
"tX=txVal2&tY=tyVal3&tZ=tzVal1\n", | |
"tX=txVal2&tY=tyVal3&tZ=tZVal2\n" | |
] | |
} | |
], | |
"prompt_number": 14 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# this is how iter tools.product works. \n", | |
"import itertools\n", | |
"a = ['aVal1', 'aVal2']\n", | |
"b = ['bVal1', 'bVal2', 'bVal3']\n", | |
"c = ['cVal1', 'cVal2']\n", | |
"\n", | |
"for x in itertools.product(a, b, c):\n", | |
" print x" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"('aVal1', 'bVal1', 'cVal1')\n", | |
"('aVal1', 'bVal1', 'cVal2')\n", | |
"('aVal1', 'bVal2', 'cVal1')\n", | |
"('aVal1', 'bVal2', 'cVal2')\n", | |
"('aVal1', 'bVal3', 'cVal1')\n", | |
"('aVal1', 'bVal3', 'cVal2')\n", | |
"('aVal2', 'bVal1', 'cVal1')\n", | |
"('aVal2', 'bVal1', 'cVal2')\n", | |
"('aVal2', 'bVal2', 'cVal1')\n", | |
"('aVal2', 'bVal2', 'cVal2')\n", | |
"('aVal2', 'bVal3', 'cVal1')\n", | |
"('aVal2', 'bVal3', 'cVal2')\n" | |
] | |
} | |
], | |
"prompt_number": 15 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment