Update to Functions

master
mehulnagpurkar 2018-06-18 00:49:00 +10:00
parent 1796a65c16
commit 063ed5cad2
2 changed files with 374 additions and 116 deletions

View File

@ -315,31 +315,33 @@
},
{
"cell_type": "code",
"execution_count": 86,
"execution_count": 168,
"metadata": {},
"outputs": [],
"source": [
"def master_yoda(text):\n",
" mylist = text.split()\n",
" yodalist = []\n",
" for word in mylist[::-1]:\n",
" yodalist += word+\" \"\n",
" \n",
" return ''.join(yodalist) "
" return ' '.join(mylist[::-1])\n",
"\n",
"# yodalist = []\n",
"# for word in mylist[::-1]:\n",
"# yodalist += word+\" \"\n",
"# \n",
"# return ''.join(yodalist)"
]
},
{
"cell_type": "code",
"execution_count": 87,
"execution_count": 169,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'home am I '"
"'home am I'"
]
},
"execution_count": 87,
"execution_count": 169,
"metadata": {},
"output_type": "execute_result"
}
@ -351,16 +353,16 @@
},
{
"cell_type": "code",
"execution_count": 83,
"execution_count": 170,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'ready are We '"
"'ready are We'"
]
},
"execution_count": 83,
"execution_count": 170,
"metadata": {},
"output_type": "execute_result"
}
@ -635,14 +637,14 @@
},
{
"cell_type": "code",
"execution_count": 177,
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"def blackjack(a,b,c):\n",
" if sum((a,b,c)) <= 21:\n",
" return sum((a,b,c))\n",
" elif (a or b or c) == 11:\n",
" elif (a == 11 or b == 11 or c == 11):\n",
" return sum((a,b,c)) - 10\n",
" else:\n",
" return 'BUST'"
@ -650,27 +652,7 @@
},
{
"cell_type": "code",
"execution_count": 178,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 178,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(9 or 11) <= 60"
]
},
{
"cell_type": "code",
"execution_count": 179,
"execution_count": 19,
"metadata": {},
"outputs": [
{
@ -679,7 +661,7 @@
"18"
]
},
"execution_count": 179,
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
@ -691,20 +673,18 @@
},
{
"cell_type": "code",
"execution_count": 180,
"execution_count": 20,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "argument of type 'int' is not iterable",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-180-ef261d118a84>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# Check\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mblackjack\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m9\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m9\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m9\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;32m<ipython-input-177-73895faa56ca>\u001b[0m in \u001b[0;36mblackjack\u001b[1;34m(a, b, c)\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0msum\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m<=\u001b[0m \u001b[1;36m21\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0msum\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[1;32melif\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mb\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mc\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32min\u001b[0m \u001b[1;36m11\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 5\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0msum\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m-\u001b[0m \u001b[1;36m10\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mTypeError\u001b[0m: argument of type 'int' is not iterable"
]
"data": {
"text/plain": [
"'BUST'"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
@ -714,16 +694,16 @@
},
{
"cell_type": "code",
"execution_count": 173,
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'BUST'"
"19"
]
},
"execution_count": 173,
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
@ -746,23 +726,41 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
"def summer_69(arr):\n",
" pass"
" result = subtract = sixth_index = ninth_index = 0\n",
" for index,value in enumerate(arr):\n",
" if value == 6:\n",
" sixth_index = index\n",
" elif value == 9:\n",
" ninth_index = index\n",
" break\n",
" result = sum(arr[0:])\n",
" if sixth_index:\n",
" subtract = sum(arr[sixth_index:ninth_index+1])\n",
" return result - subtract\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"execution_count": 47,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"9"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check\n",
"summer_69([1, 3, 5])"
@ -770,11 +768,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"9"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check\n",
"summer_69([4, 5, 6, 7, 8, 9])"
@ -782,11 +789,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"execution_count": 49,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"14"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check\n",
"summer_69([2, 1, 6, 9, 11])"
@ -812,23 +828,37 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"execution_count": 119,
"metadata": {},
"outputs": [],
"source": [
"def spy_game(nums):\n",
" pass"
" mylist = []\n",
" for num in nums:\n",
" if num == 0 or num == 7:\n",
" mylist.append(num)\n",
" if mylist[0] == 0 and mylist[1] == 0 and mylist[2] == 7:\n",
" return True\n",
" else:\n",
" return False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"execution_count": 120,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 120,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check\n",
"spy_game([1,2,4,0,0,7,5])"
@ -836,11 +866,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"execution_count": 121,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 121,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check\n",
"spy_game([1,0,2,4,0,5,7])"
@ -848,11 +887,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"execution_count": 122,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 122,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check\n",
"spy_game([1,7,2,0,4,5,0])"
@ -870,24 +918,43 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"execution_count": 136,
"metadata": {},
"outputs": [],
"source": [
"def count_primes(num):\n",
" pass\n",
" "
" count = 0\n",
" prime = 0\n",
" for i in range(2,num):\n",
" j = 2\n",
" while j < i:\n",
" #print (f'j is {j}')\n",
" if i%j == 0:\n",
" count += 1\n",
" #print (f'count {count}')\n",
" j += 1\n",
" if count == 1:\n",
" prime += 1\n",
" count = 0 \n",
" return prime"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"execution_count": 137,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"276"
]
},
"execution_count": 137,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check\n",
"count_primes(100)"
@ -911,25 +978,37 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"execution_count": 154,
"metadata": {},
"outputs": [],
"source": [
"def print_big(letter):\n",
" pass"
" patterns = {1:' * ',2:' * * ',3:'* *',4:'*****',5:'**** ',6:' * ',7:' * ',8:'* * ',9:'* '}\n",
" alphabet = {'A':[1,2,4,3,3],'B':[5,3,5,3,5],'C':[4,9,9,9,4],'D':[5,3,3,3,5],'E':[4,9,4,9,4]}\n",
" for pattern in alphabet[letter.upper()]:\n",
" print(patterns[pattern])\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"execution_count": 162,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"*****\n",
"* \n",
"*****\n",
"* \n",
"*****\n"
]
}
],
"source": [
"print_big('a')"
"print_big('e')"
]
},
{

View File

@ -405,7 +405,7 @@
},
{
"cell_type": "code",
"execution_count": 39,
"execution_count": 2,
"metadata": {},
"outputs": [
{
@ -414,7 +414,7 @@
"[(1, 'Fra'), (2, 'Bra'), (3, 'Ger'), (4, 'Esp')]"
]
},
"execution_count": 39,
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
@ -428,6 +428,27 @@
"list(zip(list1,list2))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The 1 position is goes to Fra \n",
"The 2 position is goes to Bra \n",
"The 3 position is goes to Ger \n",
"The 4 position is goes to Esp \n"
]
}
],
"source": [
"for index,value in list(zip(list1,list2)):\n",
" print ('The {} position is goes to {} '.format(index,value))"
]
},
{
"cell_type": "code",
"execution_count": 43,
@ -605,12 +626,170 @@
"print ('type is {}'.format(type(result)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### list comprehensions"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": []
"source": [
"mystring = 'hello'"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['h', 'e', 'l', 'l', 'o']"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mylist1 = [letter for letter in mystring]\n",
"mylist1"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"mylist1 = [num for num in range(0,11)]\n",
"# getting square of numbers\n",
"mylist2 = [num**2 for num in range(0,11)]\n",
"# getting even numbers\n",
"mylist3 = [num for num in range(0,11) if num%2==0]"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mylist1"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mylist2"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 2, 4, 6, 8, 10]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mylist3"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[32.0, 50.0, 93.2, 72.14]"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# To find the temperature in farenheit\n",
"celcius = [0,10,34,22.3]\n",
"\n",
"farenheit = [((9/5)*temp + 32) for temp in celcius]\n",
"farenheit"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"x = 10\n",
"\n",
"even = x if x%2 == 0 else (x+1)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"even"
]
}
],
"metadata": {