From 5bb55306bb76d4105b216c21d78785a68b3d2108 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Fernando=20S=C3=A1nchez?= Date: Mon, 24 Feb 2014 03:39:51 -0800 Subject: [PATCH 1/6] --- array_combination.ipynb | 75 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 array_combination.ipynb diff --git a/array_combination.ipynb b/array_combination.ipynb new file mode 100644 index 0000000..33c6634 --- /dev/null +++ b/array_combination.ipynb @@ -0,0 +1,75 @@ +{ + "metadata": { + "name": "Untitled0" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": "a=set([1,2,3,4])\nb=set([4,5,6,7])\nc=set([7,8,9])", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 5 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "def get_nonrepeated(seta, setb):\n for i in seta:\n if i not in setb:\n yield i", + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 9 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "def shuffle(*args):\n combinations = [[i] for i in args[0]]\n for arg in args[1:]:\n tempcomb = []\n for i in arg:\n print i\n tempcomb += [c+[i] for c in combinations if i not in c ]\n combinations = tempcomb\n return combinations\nshuffle(a,b,c)", + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": "4\n5\n6\n7\n8\n9\n7\n" + } + ], + "prompt_number": 50 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "x= [2,4,3]\nx + 6", + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "can only concatenate list (not \"int\") to list", + "output_type": "pyerr", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mx\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m6\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m: can only concatenate list (not \"int\") to list" + ] + } + ], + "prompt_number": 27 + }, + { + "cell_type": "code", + "collapsed": false, + "input": "", + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file From edf3519032edfc58cf6885c1ca2e8ca0ba6e8a83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Fernando=20S=C3=A1nchez?= Date: Mon, 24 Feb 2014 03:40:29 -0800 Subject: [PATCH 2/6] --- array_combination.ipynb | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/array_combination.ipynb b/array_combination.ipynb index 33c6634..6f2d4c0 100644 --- a/array_combination.ipynb +++ b/array_combination.ipynb @@ -10,11 +10,11 @@ { "cell_type": "code", "collapsed": false, - "input": "a=set([1,2,3,4])\nb=set([4,5,6,7])\nc=set([7,8,9])", + "input": "a=set([3,4])\nb=set([3,7])", "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 5 + "prompt_number": 112 }, { "cell_type": "code", @@ -28,37 +28,26 @@ { "cell_type": "code", "collapsed": false, - "input": "def shuffle(*args):\n combinations = [[i] for i in args[0]]\n for arg in args[1:]:\n tempcomb = []\n for i in arg:\n print i\n tempcomb += [c+[i] for c in combinations if i not in c ]\n combinations = tempcomb\n return combinations\nshuffle(a,b,c)", + "input": "def array_combinations(*args):\n combinations = [[i] for i in set(args[0])]\n for arg in args[1:]:\n tempcomb = []\n for i in set(arg):\n tempcomb += [c+[i] for c in combinations if i not in c ]\n combinations = tempcomb\n return combinations\nres = array_combinations(a,b)\nprint res\nprint len(res)", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "4\n5\n6\n7\n8\n9\n7\n" + "text": "[[4, 3], [3, 7], [4, 7]]\n3\n" } ], - "prompt_number": 50 + "prompt_number": 115 }, { "cell_type": "code", "collapsed": false, - "input": "x= [2,4,3]\nx + 6", + "input": "", "language": "python", "metadata": {}, - "outputs": [ - { - "ename": "TypeError", - "evalue": "can only concatenate list (not \"int\") to list", - "output_type": "pyerr", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mx\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m6\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m: can only concatenate list (not \"int\") to list" - ] - } - ], - "prompt_number": 27 + "outputs": [], + "prompt_number": 56 }, { "cell_type": "code", From 9d35c53858b23915bbdefcd34105e2b4bcb45b8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Fernando=20S=C3=A1nchez?= Date: Mon, 24 Feb 2014 03:42:36 -0800 Subject: [PATCH 3/6] --- array_combination.ipynb | 28 +--------------------------- 1 file changed, 1 insertion(+), 27 deletions(-) diff --git a/array_combination.ipynb b/array_combination.ipynb index 6f2d4c0..6fe891f 100644 --- a/array_combination.ipynb +++ b/array_combination.ipynb @@ -16,15 +16,6 @@ "outputs": [], "prompt_number": 112 }, - { - "cell_type": "code", - "collapsed": false, - "input": "def get_nonrepeated(seta, setb):\n for i in seta:\n if i not in setb:\n yield i", - "language": "python", - "metadata": {}, - "outputs": [], - "prompt_number": 9 - }, { "cell_type": "code", "collapsed": false, @@ -38,24 +29,7 @@ "text": "[[4, 3], [3, 7], [4, 7]]\n3\n" } ], - "prompt_number": 115 - }, - { - "cell_type": "code", - "collapsed": false, - "input": "", - "language": "python", - "metadata": {}, - "outputs": [], - "prompt_number": 56 - }, - { - "cell_type": "code", - "collapsed": false, - "input": "", - "language": "python", - "metadata": {}, - "outputs": [] + "prompt_number": 116 } ], "metadata": {} From ab7fa8f286a06b91f12fe13795cc5f71f6b0bbda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Fernando=20S=C3=A1nchez?= Date: Tue, 25 Feb 2014 07:00:17 -0800 Subject: [PATCH 4/6] --- array_combination.ipynb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/array_combination.ipynb b/array_combination.ipynb index 6fe891f..5ece3dc 100644 --- a/array_combination.ipynb +++ b/array_combination.ipynb @@ -10,26 +10,26 @@ { "cell_type": "code", "collapsed": false, - "input": "a=set([3,4])\nb=set([3,7])", + "input": "a=set([3,4])\nb=set([3])\nc=set([4])\n", "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 112 + "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, - "input": "def array_combinations(*args):\n combinations = [[i] for i in set(args[0])]\n for arg in args[1:]:\n tempcomb = []\n for i in set(arg):\n tempcomb += [c+[i] for c in combinations if i not in c ]\n combinations = tempcomb\n return combinations\nres = array_combinations(a,b)\nprint res\nprint len(res)", + "input": "def array_combinations(*args):\n combinations = [[i] for i in set(args[0])]\n for arg in args[1:]:\n tempcomb = []\n for i in set(arg):\n tempcomb += [c+[i] for c in combinations if i not in c ]\n if tempcomb:\n combinations = tempcomb\n return combinations\nres = array_combinations(a,b,c)\nprint res\nprint len(res)", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "[[4, 3], [3, 7], [4, 7]]\n3\n" + "text": "[[4, 3]]\n1\n" } ], - "prompt_number": 116 + "prompt_number": 5 } ], "metadata": {} From 5691cd367ba2c6fccc36f78d40c1c4d757510ae9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Fernando=20S=C3=A1nchez?= Date: Tue, 25 Feb 2014 08:00:10 -0800 Subject: [PATCH 5/6] --- array_combination.ipynb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/array_combination.ipynb b/array_combination.ipynb index 5ece3dc..2907865 100644 --- a/array_combination.ipynb +++ b/array_combination.ipynb @@ -10,26 +10,26 @@ { "cell_type": "code", "collapsed": false, - "input": "a=set([3,4])\nb=set([3])\nc=set([4])\n", + "input": "a=set([3,4])\nb=set([3,7])\nc=set([4])\n", "language": "python", "metadata": {}, "outputs": [], - "prompt_number": 1 + "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, - "input": "def array_combinations(*args):\n combinations = [[i] for i in set(args[0])]\n for arg in args[1:]:\n tempcomb = []\n for i in set(arg):\n tempcomb += [c+[i] for c in combinations if i not in c ]\n if tempcomb:\n combinations = tempcomb\n return combinations\nres = array_combinations(a,b,c)\nprint res\nprint len(res)", + "input": "def array_combinations(*args):\n combinations = [set([i]) for i in set(args[0])]\n for arg in args[1:]:\n tempcomb = []\n for i in set(arg):\n for c in combinations:\n c.update([i])\n tempcomb.append(c)\n if tempcomb:\n combinations = tempcomb\n return combinations\nres = array_combinations(a,b,c)\nprint res\nprint len(res)", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", - "text": "[[4, 3]]\n1\n" + "text": "[set([3, 4, 7]), set([3, 4, 7]), set([3, 4, 7]), set([3, 4, 7])]\n4\n" } ], - "prompt_number": 5 + "prompt_number": 25 } ], "metadata": {} From 9cfc2da26524287f6166cc20ca88ad568f762143 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Fernando=20S=C3=A1nchez?= Date: Tue, 25 Feb 2014 08:00:51 -0800 Subject: [PATCH 6/6] --- frozen_combination.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 frozen_combination.py diff --git a/frozen_combination.py b/frozen_combination.py new file mode 100644 index 0000000..d8e193f --- /dev/null +++ b/frozen_combination.py @@ -0,0 +1,13 @@ +def frozen_combination(*args): + combinations = set([frozenset(),]) + for arg in args: + tempcomb = set(frozenset(),) + for i in set(arg): + tempcomb.update(set([frozenset(list(c)+[i]) for c in combinations if i not in c])) + if len(tempcomb) > 0: + combinations = tempcomb + return combinations +# return [set(comb) for comb in combinations] # for sets instead of frozensets +res = frozen_combination(a,b) +print res +print len(res) \ No newline at end of file