Skip to content

Instantly share code, notes, and snippets.

@himan94
Created December 23, 2018 05:19
Show Gist options
  • Save himan94/cab92332742308e8c0bea93a490d09db to your computer and use it in GitHub Desktop.
Save himan94/cab92332742308e8c0bea93a490d09db to your computer and use it in GitHub Desktop.
The bootcamp python file in ipnyb format
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Starting with the inputs"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the list of numbers as string:3 10 4 9 8 7 4\n"
]
}
],
"source": [
"a = input('Enter the list of numbers as string:')"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [],
"source": [
"a = a.split(' ') #spliting elements of the string"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[3, 10, 4, 9, 8, 7, 4]"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b = [] #storing elements in a new list\n",
"for strings in a:\n",
" b.append(int(strings))\n",
"b"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the required number as string:12\n"
]
}
],
"source": [
"required_number = input('Enter the required number as string:')"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [],
"source": [
"required_number = int(required_number)"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"int"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(required_number)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Starting with the code"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [],
"source": [
"b = list(set(b)) # eliminating duplicates"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[3, 4, 7, 8, 9, 10]"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Demonstrating how to make pairs( could be done by itertools)"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3 4\n",
"3 7\n",
"3 8\n",
"3 9\n",
"3 10\n",
"4 7\n",
"4 8\n",
"4 9\n",
"4 10\n",
"7 8\n",
"7 9\n",
"7 10\n",
"8 9\n",
"8 10\n",
"9 10\n"
]
}
],
"source": [
"for m in range(0,len(b)): \n",
" for n in range(m+1,len(b)):\n",
" print(b[m],b[n]) "
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(3, 4),\n",
" (3, 7),\n",
" (3, 8),\n",
" (3, 9),\n",
" (3, 10),\n",
" (4, 7),\n",
" (4, 8),\n",
" (4, 9),\n",
" (4, 10),\n",
" (7, 8),\n",
" (7, 9),\n",
" (7, 10),\n",
" (8, 9),\n",
" (8, 10),\n",
" (9, 10)]"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import itertools\n",
"\n",
"list(itertools.combinations(b, 2))"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3 9\n",
"4 8\n"
]
}
],
"source": [
"for m in range(0,len(b)):\n",
" for n in range(m+1,len(b)):\n",
" if (b[m] + b[n] == required_number):\n",
" print(b[m],b[n])"
]
},
{
"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.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment