02 Conditions and Loop Problems Solns - Ipynb
AI-enhanced title
"cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# <center>Conditions and Loops</center>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Table of Contents\n", "\n", "- **[2 dimensional array](#2-dimensional-array)** (*See Again*)\n", "- **[check triangle](#check-triangle)**\n", "- **[construct number pattern](#construct-number-pattern)**\n", "- **[construct pattern](#construct-pattern)**\n", "- **[count digit letter](#count-digit-letter)**\n", "- **[count even odd](#count-even-odd)**\n", "- **[fibonacci generator](#fibonacci-generator)** (*See Again*)\n", "- **[find number](#find-number)**\n", "- **[guess game](#guess-game)** (*See Again*)\n", "- **[password validation](#password-validation)** (*See Again*)\n", "- **[triangle types](#triangle-types)**\n", "- **[vowel and consonent](#vowel-and-consonent)**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2 dimensional array" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Problem Statement:\n", "# =================\n", "\n", "# Write a Python program which takes two digits m (row) and n (column) asinput and generates a two-dimensional array. \n", "\n", "# The element value in the i-th row and j-th column of the array should bei*j.\n", "\n", "# Note :\n", "# i = 0,1.., m-1 \n", "# j = 0,1, n-1.\n", "\n", "# Input\n", "# Input number of rows: 3\n", "# Input number of columns: 4 \n", "\n", "# Output\n", "# [[0, 0, 0, 0], [0, 1, 2, 3], [0, 2, 4, 6]] " ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter number of rows:3\n", "Enter number of columns:4\n", "[[0, 0, 0, 0], [0, 1, 2, 3], [0, 2, 4, 6]]\n" ] } ], "source": [ "# Solution:\n", "\n", "row = int(input('Enter number of rows:'))\n", "col = int(input('Enter number of columns:'))\n", "\n", "twoD_list = [[0 for c in range(col)] for r in range(row)] # created shapeof the required format output\n", "\n", "for i in range(row):\n", " for j in range(col):\n", " twoD_list[i][j] = i * j # assigning valueinto created format as per condition\n", "print(twoD_list)\n", "\n", "\n", "\n", "# Explanation: [[0 for c in range(col)] for r in range(row)]\n", "# ===========\n", "\n", "# [0 for c in range(col)] --> Output: [0, 0, 0, 0]\n", "# [[0 for c in range(col)] for r in range(row)] --> Output: [[0, 0, 0, 0],[0, 0, 0, 0], [0, 0, 0, 0]]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## check triangle" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a Python program to check a triangle is valid or not." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter first side:1\n", "Enter second side:2\n", "Enter third side:5\n", "No, a triangle will not be formed with these sides\n" ] } ], "source": [ "# If x , y , z are sides of a triangle, then in ascending order, if itsatisfies condition x+y <=z , \n", "# then its a degenerate triangle. (https://www.quora.com/What-is-a-degenerate-triangle)\n", "\n", "def check_triangle(x, y, z):\n", " if (x + y < z) or (y + z < x) or (z + x < y):\n", " print('No, a triangle will not be formed with these sides')\n", " elif (x + y == z) or (y + z == x) or (z + x == y):\n", " print('Yes, these sides will form a degenerate triangle.')\n", " else:\n", " print('Yes, these sides will form a triangle.')\n", " \n", "side1 = int(input('Enter first side:'))\n", "side2 = int(input('Enter second side:'))\n", "side3 = int(input('Enter third side:'))\n", "\n", "check_triangle(side1, side2, side3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## construct number pattern" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Write a Python program to construct the following pattern, using a nestedloop number.\n", "\n", "# 1\n", "# 22\n", "# 333\n", "# 4444\n", "# 55555\n", "# 666666\n", "# 7777777\n", "# 88888888\n", "# 999999999 " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter a number:10\n", "\n", "1\n", "22\n", "333\n", "4444\n", "55555\n", "666666\n", "7777777\n", "88888888\n", "999999999\n" ] } ], "source": [ "n = int(input('Enter a number:'))\n", "\n", "for i in range(n):\n", " print(str(i) * i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## construct pattern" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Write a Python program to construct the following pattern, using a nestedfor loop.\n", "\n", "# * \n", "# * * \n", "# * * * \n", "# * * * * \n", "# * * * * * \n", "# * * * * \n", "# * * * \n", "# * * \n", "# *" ]},{ "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter a number:5\n", "*\n", "**\n", "***\n", "****\n", "*****\n", "*****\n", "****\n", "***\n", "**\n" ] } ], "source": [ "n = int(input('Enter a number:'))\n", "\n", "for i in range(1, n):\n", " print('*'*i)\n", "print('*'*n)\n", "for i in range(n, 1, -1):\n", " print('*'*i)" ]},{ "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter a number:5\n", "\n", "* \n", "* * \n", "* * * \n", "* * * * \n", "* * * * * \n", "* * * * \n", "* * * \n", "* * \n", "* \n" ] } ], "source": [ "n = int(input('Enter a number:'))\n", "\n", "for i in range(n):\n", " for j in range(i):\n", " print('*', end = ' ')\n", " print('')\n", "for i in range(n, 0, -1):\n", " for j in range(i):\n", " print('*', end = ' ')\n", " print('')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## count digit letter" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Write a Python program that accepts a string and calculate the number ofdigits and letters\n", "\n", "# Sample Data : \"Python 3.2\"\n", "# Expected Output :\n", "# Letters 6 \n", "# Digits 2" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter a string: Python 3.2\n", "Letters: 6\n", "Digit: 2\n" ] } ], "source": [ "s = input('Enter a string: ')\n", "count_digit = 0\n", "count_letter = 0\n", "\n", "for i in s:\n", " if i.isdigit():\n", " count_digit += 1\n", " elif i.isalpha():\n", " count_letter += 1\n", " else:\n", " pass\n", "\n", "print('Letters:', count_letter)\n", "print('Digit:', count_digit)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## count even odd" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Count the number of even and odd numbers from a series of numbers\n", "\n", "# Input \n", "# numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9) # Declaring the tuple\n", "# Output\n", "# Number of even numbers : 4\n", "# Number of odd numbers : 5 " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Number of even numbers: 4\n", "Number of odd numbers: 5\n" ] } ], "source": [ "numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9)\n", "e_count = 0\n", "o_count = 0\n", "\n", "for i in numbers:\n", " if i % 2 == 0:\n", " e_count += 1\n", " else:\n", " o_count += 1\n", "\n", "print('Number of even numbers:', e_count)\n", "print('Number of odd numbers:', o_count)" ]},{ "cell_type": "markdown", "metadata": {}, "source": [ "## fibonacci generator" ]},{ "cell_type": "markdown", "metadata": {}, "source": [ "Write a Python program to get the Fibonacci series between 0 to 50." ]},{ "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter a number to start: 0\n", "Enter a number to end: 50\n", "1 1 2 3 5 8 13 21 34 " ] } ], "source": [ "a = int(input('Enter a number to start: '))\n", "b = int(input('Enter a number to end: '))\n", "\n", "first = a\n", "second = a + 1\n", "\n", "for i in range(a, b):\n", " print(second, end = ' ')\n", " first, second = second, first + second\n", " if second > end:\n", " break" ]},{ "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "1\n", "2\n", "3\n", "5\n", "8\n", "13\n", "21\n", "34\n" ] } ], "source": [ "# Another Solution:\n", "\n", "x, y=0, 1\n", "\n", "while y < 50:\n", " print(y)\n", " x, y = y, x+y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## find number" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a Python program to find those numbers which are divisible by 7 andmultiple of 5, between 1500 and 2700." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1505 1540 1575 1610 1645 1680 1715 1750 1785 1820 1855 1890 1925 1960 19952030 2065 2100 2135 2170 2205 2240 2275 2310 2345 2380 2415 2450 2485 2520 25552590 2625 2660 2695 " ] } ], "source": [ "for i in range(1500, 2701):\n", " if i % 7 == 0 and i % 5 == 0:\n", " print(i, end = ' ')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## guess game" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Generate a random number between 1 and 9 (including 1 and 9). Ask the user toguess the number, then tell them whether they guessed too low, too high, or exactlyright. " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Guess and input the auto generated number:1\n", "Too low\n", "Guess the number again:6\n", "Too low\n", "Guess the number again:7\n", "\n", "Exactly right! Auto generated number: 7 & User guessed number: 7\n", "You guessed correct number in 3 tries\n" ] } ], "source": [ "import random\n", "\n", "auto_num = random.randint(1, 9)\n", "user_num = int(input('Guess and input the auto generated number:'))\n", "count = 0\n", "\n", "while True:\n", " count += 1\n", " if user_num < auto_num:\n", " print('Too low')\n", " user_num = int(input('Guess the number again:'))\n", " elif user_num > auto_num:\n", " print('Too high')\n", " user_num = int(input('Guess the number again:')) \n", " else:\n", " print('\\nExactly right! Auto generated number: {} & User guessednumber: {}'.format(auto_num, user_num))\n", " print('You guessed correct number in {} tries'.format(count))\n", " break" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## password validation" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Write a Python program to check the validity of a password (input fromusers).\n", "\n", "# Validation :\n", "# At least 1 letter between [a-z] and 1 letter between [A-Z].\n", "# At least 1 number between [0-9].\n", "# At least 1 character from [$#@].\n", "# Minimum length 6 characters.\n", "# Maximum length 16 characters.\n", "\n", "# Input\n", "# W3r@100a\n", "# Output\n", "# Valid password" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter your password:W3r@100a\n", "Valid Password\n" ] } ], "source": [ "import re\n", "\n", "def check_password(p):\n", " if len(p) >= 6 and len(p) <= 16:\n", " condition = bool(re.search('[a-z]', p)) and bool(re.search('[A-Z]',p)) and bool(re.search('[0-9]', p)) and bool(re.search('[$#@]', p))\n", " if condition:\n", " print('Valid Password')\n", " else:\n", " print('Invalid Password')\n", " else:\n", " print('Invalid Password')\n", " \n", "user_pass = input('Enter your password:')\n", "check_password(user_pass)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## triangle types" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Write a Python program to check a triangle is equilateral, isosceles orscalene.\n", "\n", "# Note :\n", "# An equilateral triangle is a triangle in which all three sides are equal.\n", "# A scalene triangle is a triangle that has three unequal sides.\n", "# An isosceles triangle is a triangle with (at least) two equal sides." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "First Side:11\n", "Second Side:11\n", "Third Side:20\n", "Isosceles\n" ] } ], "source": [ "def triangle_type(x,y,z):\n", " if x == y == z:\n", " print('Equilateral')\n", " elif x!= y != z:\n", " print('Scalene')\n", " else:\n", " print('Isosceles')\n", "\n", "x = int(input('First Side:'))\n", "y = int(input('Second Side:'))\n", "z = int(input('Third Side:'))\n", "\n", "triangle_type(x, y, z)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## vowel and consonent" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Write a Python program to check whether an alphabet is a vowel orconsonant." ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter an alphabate:k\n", "k - is consonant\n" ] } ], "source": [ "alphabate = input('Enter an alphabate:')\n", "\n", "if alphabate in ('aeiou'):\n", " print('{} - is vowel'.format(alphabate))\n", "else:\n", " print('{} - is consonant'.format(alphabate))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## <a href = \"#Table-of-Contents\" style=\"text-align:right\">Move toTop</a>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# <center> The End </center>" ] } ], "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.3" } }, "nbformat": 4, "nbformat_minor": 2}