pytorch-stuff/Untitled.ipynb

143 lines
8.6 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [],
"source": [
"# The main function that prints \n",
"# all combinations of size r in \n",
"# arr[] of size n. This function \n",
"# mainly uses combinationUtil() \n",
"def printCombination(arr, n, r): \n",
" \n",
" # A temporary array to \n",
" # store all combination \n",
" # one by one \n",
" data = [0]*r; \n",
" \n",
" # Print all combination \n",
" # using temprary array 'data[]' \n",
" combinationUtil(arr, data, 0, \n",
" n - 1, 0, r); \n",
" \n",
"# arr[] ---> Input Array \n",
"# data[] ---> Temporary array to \n",
"# store current combination \n",
"# start & end ---> Staring and Ending \n",
"# indexes in arr[] \n",
"# index ---> Current index in data[] \n",
"# r ---> Size of a combination \n",
"# to be printed \n",
"def combinationUtil(arr, data, start, \n",
" end, index, r): \n",
" \n",
" temp1 =[]\n",
" # Current combination is ready \n",
" # to be printed, print it \n",
" if (index == r): \n",
" temp2 = []\n",
" for j in range(r): \n",
" temp2.append(data[j])\n",
" #print(data[j], end = \" \"); \n",
" #print(); \n",
" #temp1.append(temp2)\n",
" #return temp1\n",
" \n",
" # replace index with all \n",
" # possible elements. The \n",
" # condition \"end-i+1 >= \n",
" # r-index\" makes sure that \n",
" # including one element at \n",
" # index will make a combination \n",
" # with remaining elements at \n",
" # remaining positions \n",
" i = start; \n",
" while(i <= end and end - i + 1 >= r - index): \n",
" data[index] = arr[i]; \n",
" combinationUtil(arr, data, i + 1, \n",
" end, index + 1, r); \n",
" i += 1; \n",
" \n",
" return temp1"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [
{
"ename": "IndexError",
"evalue": "list assignment index out of range",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-64-eede45e736f3>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mn\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0malg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mprintCombination\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marr\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m<ipython-input-63-85ad8d7a3f5f>\u001b[0m in \u001b[0;36mprintCombination\u001b[0;34m(arr, n, r)\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;31m# using temprary array 'data[]'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m combinationUtil(arr, data, 0, \n\u001b[0;32m---> 15\u001b[0;31m n - 1, 0, r); \n\u001b[0m\u001b[1;32m 16\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0;31m# arr[] ---> Input Array\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<ipython-input-63-85ad8d7a3f5f>\u001b[0m in \u001b[0;36mcombinationUtil\u001b[0;34m(arr, data, start, end, index, r)\u001b[0m\n\u001b[1;32m 50\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0marr\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m;\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 51\u001b[0m combinationUtil(arr, data, i + 1, \n\u001b[0;32m---> 52\u001b[0;31m end, index + 1, r); \n\u001b[0m\u001b[1;32m 53\u001b[0m \u001b[0mi\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m;\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 54\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<ipython-input-63-85ad8d7a3f5f>\u001b[0m in \u001b[0;36mcombinationUtil\u001b[0;34m(arr, data, start, end, index, r)\u001b[0m\n\u001b[1;32m 50\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0marr\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m;\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 51\u001b[0m combinationUtil(arr, data, i + 1, \n\u001b[0;32m---> 52\u001b[0;31m end, index + 1, r); \n\u001b[0m\u001b[1;32m 53\u001b[0m \u001b[0mi\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m;\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 54\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<ipython-input-63-85ad8d7a3f5f>\u001b[0m in \u001b[0;36mcombinationUtil\u001b[0;34m(arr, data, start, end, index, r)\u001b[0m\n\u001b[1;32m 50\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0marr\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m;\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 51\u001b[0m combinationUtil(arr, data, i + 1, \n\u001b[0;32m---> 52\u001b[0;31m end, index + 1, r); \n\u001b[0m\u001b[1;32m 53\u001b[0m \u001b[0mi\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m;\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 54\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<ipython-input-63-85ad8d7a3f5f>\u001b[0m in \u001b[0;36mcombinationUtil\u001b[0;34m(arr, data, start, end, index, r)\u001b[0m\n\u001b[1;32m 48\u001b[0m \u001b[0mi\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mstart\u001b[0m\u001b[0;34m;\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 49\u001b[0m \u001b[0;32mwhile\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m \u001b[0;34m<=\u001b[0m \u001b[0mend\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mend\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mi\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m1\u001b[0m \u001b[0;34m>=\u001b[0m \u001b[0mr\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mindex\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 50\u001b[0;31m \u001b[0mdata\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0marr\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m;\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 51\u001b[0m combinationUtil(arr, data, i + 1, \n\u001b[1;32m 52\u001b[0m end, index + 1, r); \n",
"\u001b[0;31mIndexError\u001b[0m: list assignment index out of range"
]
}
],
"source": [
"arr =[1,3,5,7,9,11,13,15]\n",
"\n",
"n = len(arr)\n",
"\n",
"alg = printCombination(arr,n,3)"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\n"
]
}
],
"source": [
"print(alg)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}