{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "WndTMk8X9C2D",
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# JamCoders Week 2 Day 2 Lecture 1\n",
"\n",
"## Gunjan Mansingh\n",
"\n",
"### RECURSION \n",
"When a function calls itself"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"#### Question 1\n",
"Write an iterative function that multiplies two given numbers without using the * operator. You can use the addition operator. (Use **for** or **while**)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"#Iterative Using while loop\n",
"def mult(x,y):\n",
" result = 0\n",
" ctr = 0\n",
" while (ctr < y ):\n",
" result = result + x\n",
" ctr = ctr + 1\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"12"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mult(4,3)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"#### Question 2\n",
"Write a recursive function that multiplies two given numbers. "
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"id": "G5ddOZg39C2J",
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"# Recursive function for multiplying numbers\n",
"def mult(x,y):\n",
" if y == 0:\n",
" return 0 #Base Case\n",
" else:\n",
" return x + mult(x, y-1) #Recursive Case"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"12"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mult(4,3)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "wfKwcWAk9C2J",
"outputId": "8c3ed039-a2d9-49f2-8661-f625a1388939",
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"#### Question 3\n",
"Write a recursive function power that takes two parameters and raises the first to the second. \n",
"For example, power (4,3) is 4*4*4"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"_move to powerpoint_"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "roWzCHFD9C2J",
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"# Recursive function for finding power\n",
"def power(x,y):\n",
" if y == 0:\n",
" return 1 #BASE CASE\n",
" else:\n",
" return x * power(x, y-1) #RECURSIVE CASE\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"64"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"power(4,3)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"#Write a recursive power using mult\n",
"def power_m(x,y):\n",
" if y == 0:\n",
" return 1 #Base Case\n",
" else:\n",
" return mult(x, power_m(x, y-1)) #Recursive Case"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"125"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"power_m(5,3)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"source": [
"#### Questions???"
]
}
],
"metadata": {
"celltoolbar": "Slideshow",
"colab": {
"name": "jamcoders_day2_lec1.ipynb",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}