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

Nathan Dsouza #31

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
292 changes: 227 additions & 65 deletions Tasks.ipynb
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "6LkPygla_OXh"
},
"source": [
"# **Task 1**\n",
"\n",
Expand All @@ -29,57 +18,126 @@
"5. Print the result.\n",
"\n",
"(Print the shape of matrix always with the matrix)\n"
],
"metadata": {
"id": "6LkPygla_OXh"
}
]
},
{
"cell_type": "code",
"source": [
"# Import Numpy"
],
"execution_count": 1,
"metadata": {
"id": "Z8iSVv-r_WkT"
},
"execution_count": null,
"outputs": []
"outputs": [],
"source": [
"# Import Numpy\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"source": [
"# Define the 4 matrices as A,B,C,D"
],
"execution_count": 20,
"metadata": {
"id": "r1pVl7LbA1_I"
},
"execution_count": null,
"outputs": []
"outputs": [],
"source": [
"# Define the 4 matrices as A,B,C,D\n",
"A = np.array([[1,2,3], [4,5,6]])\n",
"B = np.array([[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]])\n",
"C = np.array([[1,2], [3,4],[5,6],[7,8],[9,10]])\n",
"D = np.array([[1,2,3], [4,5,6], [7,8,9]])"
]
},
{
"cell_type": "code",
"source": [
"# Print the 4 matrices"
],
"execution_count": 24,
"metadata": {
"id": "uJHj_CbhA83b"
},
"execution_count": null,
"outputs": []
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Matrix A\n",
" [[1 2 3]\n",
" [4 5 6]]\n",
"Matrix B\n",
" [[ 1 2 3 4 5]\n",
" [ 6 7 8 9 10]\n",
" [11 12 13 14 15]]\n",
"Matrix C\n",
" [[ 1 2]\n",
" [ 3 4]\n",
" [ 5 6]\n",
" [ 7 8]\n",
" [ 9 10]]\n",
"Matrix D\n",
" [[1 2 3]\n",
" [4 5 6]\n",
" [7 8 9]]\n"
]
}
],
"source": [
"# Print the 4 matrices\n",
"print(\"Matrix A\\n\",A)\n",
"print(\"Matrix B\\n\",B)\n",
"print(\"Matrix C\\n\",C)\n",
"print(\"Matrix D\\n\",D)"
]
},
{
"cell_type": "code",
"source": [
"# Find the Dot Product of matrices\n"
],
"execution_count": 14,
"metadata": {
"id": "MDcD7tOWBMzG"
},
"execution_count": null,
"outputs": []
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Dot Product of A and B:\n",
" [[ 46 52 58 64 70]\n",
" [100 115 130 145 160]]\n",
"\n",
"Dot Product of B and C:\n",
" [[ 95 110]\n",
" [220 260]\n",
" [345 410]]\n",
"\n",
"Dot Product of C and A:\n",
" [[ 9 12 15]\n",
" [19 26 33]\n",
" [29 40 51]\n",
" [39 54 69]\n",
" [49 68 87]]\n",
"\n",
"Dot Product of D with D:\n",
" [[ 30 36 42]\n",
" [ 66 81 96]\n",
" [102 126 150]]\n"
]
}
],
"source": [
"# Find the Dot Product of matrices\n",
"dot_product = np.dot(A, B)\n",
"print(\"\\nDot Product of A and B:\\n\", dot_product)\n",
"dot_product = np.dot(B, C)\n",
"print(\"\\nDot Product of B and C:\\n\", dot_product)\n",
"dot_product = np.dot(C, A)\n",
"print(\"\\nDot Product of C and A:\\n\", dot_product)\n",
"dot_product = np.dot(D, D)\n",
"print(\"\\nDot Product of D with D:\\n\", dot_product)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Rv1D09dyB0eP"
},
"source": [
"# ValueError: shapes (2,2) and (3,3) not aligned: 2 (dim 1) != 3 (dim 0)\n",
"\n",
Expand All @@ -89,36 +147,67 @@
"Else print \"Dimension Error!!\"\n",
"\n",
"\n"
],
"metadata": {
"id": "Rv1D09dyB0eP"
}
]
},
{
"cell_type": "code",
"source": [
"def is_compatible(A, B):\n",
" pass"
],
"execution_count": 29,
"metadata": {
"id": "RmS0xS8tCjNC"
},
"execution_count": null,
"outputs": []
"outputs": [],
"source": [
"def is_compatible(A, B):\n",
" numcol = len(A[0])\n",
" numrow = len(B)\n",
" if numcol != numrow:\n",
" return False\n",
" else:\n",
" return True"
]
},
{
"cell_type": "code",
"source": [
"# Find Dot product only if the matrices are compatible"
],
"execution_count": 31,
"metadata": {
"id": "ds0XYSZ6E9nl"
},
"execution_count": null,
"outputs": []
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 46 52 58 64 70]\n",
" [100 115 130 145 160]]\n",
"\n",
"[[ 95 110]\n",
" [220 260]\n",
" [345 410]]\n",
"\n",
"Dimension Error\n",
"\n"
]
}
],
"source": [
"# Find Dot product only if the matrices are compatible\n",
"def get_dot(A,B):\n",
" if(is_compatible(A,B)):\n",
" print(np.dot(A,B))\n",
" else:\n",
" print(\"Dimension Error\")\n",
" print()\n",
"\n",
"get_dot(A,B)\n",
"get_dot(B,C)\n",
"get_dot(D, A)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "SR_JeHeEdWOX"
},
"source": [
"# **Task 2**\n",
"\n",
Expand Down Expand Up @@ -147,22 +236,95 @@
"At the end of the game, the function should print the final score.\n",
"\n",
"Your task is to implement this function in Python using the given rules."
],
"metadata": {
"id": "SR_JeHeEdWOX"
}
]
},
{
"cell_type": "code",
"source": [
"def up_down(rounds):\n",
" pass"
],
"execution_count": null,
"metadata": {
"id": "0fxnUDma7aPx"
},
"execution_count": null,
"outputs": []
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"You guessed 11 and computer guessed 10\n",
"You guessed 4 and computer guessed 10\n",
"You guessed 8 and computer guessed 11\n",
"You guessed 1 and computer guessed 7\n",
"You guessed 9 and computer guessed 10\n",
"You guessed 3 and computer guessed 2\n",
"You guessed 6 and computer guessed 9\n",
"You guessed 7 and computer guessed 9\n",
"Final score: 13\n"
]
}
],
"source": [
"def up_down(rounds):\n",
" user_score = 0\n",
" while int(rounds) > 0 :\n",
" \n",
" user_guess = int(input(\"Enter a number to guess: \"))\n",
" guess = int((np.random.rand()*12)+1)\n",
"\n",
" print(f'You guessed {user_guess} and computer guessed {guess}')\n",
" if (guess < 7):\n",
" if user_guess < 7:\n",
" user_score = user_score + user_guess\n",
" else:\n",
" user_score = user_score - user_guess\n",
"\n",
"\n",
" if (guess == 7):\n",
" if user_guess == 7:\n",
" user_score = user_score + 14\n",
" else:\n",
" user_score = user_score - user_guess\n",
"\n",
"\n",
" if(guess > 7):\n",
" if user_guess > 7:\n",
" user_score = user_score + user_guess\n",
" else:\n",
" user_score = user_score - user_guess\n",
" \n",
" if user_score > 30:\n",
" print(f\"You win! Score: {user_score}\")\n",
" elif user_score < -30:\n",
" print(\"You lose....\")\n",
"\n",
" rounds = rounds - 1\n",
" \n",
" print(f\"Final score: {user_score}\")\n",
" \n",
" \n",
"up_down(int(input(\"Welcome to Guessing Game! Enter the number of rounds: \")))\n"
]
}
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"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.13.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}