Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python Assignment added #21

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
276 changes: 276 additions & 0 deletions Python-Session/Submissions/LeanIn_ML_Python_assignment.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "LeanIn ML Python assignment.ipynb",
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "1z5anc6RU522"
},
"source": [
"Name: Aayushi Bansal\r\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "yo5YKxiwVQ0i"
},
"source": [
"Q1. Write a function that returns the maximum of two numbers."
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "1oXandtlLear",
"outputId": "ddf3a22f-28fc-44ef-d5c4-ff183641c2aa"
},
"source": [
"a=int(input(\"Enter first number\"))\r\n",
"b=int(input(\"Enter second number\"))\r\n",
"def maxtwo(a,b):\r\n",
" if a>b:\r\n",
" return a\r\n",
" else:\r\n",
" return b\r\n",
"print(maxtwo(a,b))"
],
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": [
"Enter first number4\n",
"Enter second number8\n",
"8\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "McjOIGSPVTMY"
},
"source": [
"Q2. Write a function called fizz_buzz that takes a number.\r\n",
"\r\n",
"• If the number is divisible by 3, it should return “Fizz”.\r\n",
"\r\n",
"• If it is divisible by 5, it should return “Buzz”.\r\n",
"\r\n",
"• If it is divisible by both 3 and 5, it should return “FizzBuzz”.\r\n",
"\r\n",
"• Otherwise, it should return the same number."
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "sDZs6GIWL_Pu",
"outputId": "70f2146f-a1ae-4daa-8d28-d22126a3e749"
},
"source": [
"def fizz_buzz(n):\r\n",
" if n%3==0 and n%5==0:\r\n",
" return \"FizzBuzz\"\r\n",
" elif n%3==0:\r\n",
" return \"Fizz\"\r\n",
" elif n%5==0:\r\n",
" return \"Buzz\"\r\n",
" else:\r\n",
" return n\r\n",
"\r\n",
"n=int(input())\r\n",
"print(fizz_buzz(n))\r\n"
],
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": [
"7\n",
"7\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "d8ryWtGTVddq"
},
"source": [
"Q3. Write a function for checking the speed of drivers. This function should have one parameter: speed.\r\n",
"\r\n",
"• If speed is less than 70, it should print “Ok”.\r\n",
"\r\n",
"• Otherwise, for every 5km above the speed limit (70), it should give the \r\n",
"driver one demerit point and print the total number of demerit points. For\r\n",
"example, if the speed is 80, it should print: “Points: 2”.\r\n",
"\r\n",
"• If the driver gets more than 12 points, the function should print: “License \r\n",
"suspended”"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "UnS0lQK6Mt6D",
"outputId": "48769ae3-3c02-4adf-c020-137c40e63912"
},
"source": [
"def checkSpeed(speed):\r\n",
" if speed<70:\r\n",
" print(\"OK\")\r\n",
" else:\r\n",
" count=0\r\n",
" while speed-5>=70:\r\n",
" count+=1\r\n",
" speed-=5\r\n",
" print(\"Points: \",count)\r\n",
"\r\n",
" if count>12:\r\n",
" print(\"License Suspended\")\r\n",
"\r\n",
"s=int(input())\r\n",
"checkSpeed(s)"
],
"execution_count": 11,
"outputs": [
{
"output_type": "stream",
"text": [
"200\n",
"Points: 26\n",
"License Suspended\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "IIEWpylIVnLY"
},
"source": [
"Q4. Write a function called showNumbers that takes a parameter called limit. It \r\n",
"should print all the numbers between 0 and limit with a label to identify the even \r\n",
"and odd numbers. For example, if the limit is 3, it should print:\r\n",
"\r\n",
"• 0 EVEN\r\n",
"\r\n",
"• 1 ODD\r\n",
"\r\n",
"• 2 EVEN\r\n",
"\r\n",
"• 3 ODD"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "lXYs1f7sOZRJ",
"outputId": "8c80bd7e-6811-4535-d9bb-1d36400887a5"
},
"source": [
"def showNumbers(limit):\r\n",
" for i in range(limit):\r\n",
" if i%2==0:\r\n",
" print(i,\"EVEN\")\r\n",
" else:\r\n",
" print(i, \"ODD\")\r\n",
"\r\n",
"limit=int(input())\r\n",
"showNumbers(limit)"
],
"execution_count": 14,
"outputs": [
{
"output_type": "stream",
"text": [
"7\n",
"0 EVEN\n",
"1 ODD\n",
"2 EVEN\n",
"3 ODD\n",
"4 EVEN\n",
"5 ODD\n",
"6 EVEN\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "pz9250u3Vyik"
},
"source": [
"Q5. Write a function that returns the sum of multiples of 3 and 5 between 0 \r\n",
"and limit (parameter). For example, if limit is 20, it should return the sum of 3, 5, \r\n",
"6, 9, 10, 12, 15, 18, 20."
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "chnJM9RZQ7Hr",
"outputId": "c839a331-30b3-4242-d7ce-b1347c42a21b"
},
"source": [
"def sumMultiples(limit):\r\n",
" sum=0\r\n",
" if limit<3:\r\n",
" return 0\r\n",
" for i in range(3,limit+1):\r\n",
" if i%3==0 or i%5==0:\r\n",
" sum+=i\r\n",
" return sum\r\n",
"limit=int(input())\r\n",
"print(sumMultiples(limit))"
],
"execution_count": 27,
"outputs": [
{
"output_type": "stream",
"text": [
"20\n",
"98\n"
],
"name": "stdout"
}
]
}
]
}
Loading