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

task 1 #32

Open
wants to merge 2 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
1 change: 1 addition & 0 deletions task/Xplore
Submodule Xplore added at 834625
264 changes: 264 additions & 0 deletions task/sarayu.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"source": [
"# **Task 1**\n",
"\n",
"Your goal is to find the answer of the dot product of the matrices of sizes (2,3), (3,5), (5,2) and (3,3).\n",
"\n",
"## **Workflow:**\n",
"1. Define the matrices of given sizes.\n",
"2. Print the matrices.\n",
"3. Use the np.dot function.\n",
"4. Print the output at each step.\n",
"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\n",
"import numpy as np\n",
"A = np.random.randint(0,11, (2,3))\n",
"B = np.random.randint(0,11, (3,5))\n",
"C = np.random.randint(0,11, (5,2))\n",
"D = np.random.randint(0,11, (3,3))\n",
"\n",
"print(f\"A = {A} shape = {A.shape}\")\n",
"print(f\"B = {B} shape = {B.shape}\")\n",
"print(f\"C = {C} shape = {C.shape}\")\n",
"print(f\"D = {D} shape = {D.shape}\")\n",
"\n",
"P = np.dot(A,B)\n",
"print(P)\n",
"Q = np.dot(P,C)\n",
"print(Q)\n",
"R = np.dot(Q,D)\n",
"print(Q)\n",
"\n"
],
"metadata": {
"id": "Z8iSVv-r_WkT",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 506
},
"outputId": "277a86c2-f5b0-4ef9-8703-a05421fdf75d"
},
"execution_count": 13,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"A = [[8 4 4]\n",
" [9 0 3]] shape = (2, 3)\n",
"B = [[ 5 1 3 6 2]\n",
" [ 3 6 4 4 10]\n",
" [ 1 1 1 0 1]] shape = (3, 5)\n",
"C = [[ 1 9]\n",
" [10 1]\n",
" [ 1 10]\n",
" [ 0 4]\n",
" [10 7]] shape = (5, 2)\n",
"D = [[ 5 2 5]\n",
" [ 6 7 10]\n",
" [ 9 1 7]] shape = (3, 3)\n",
"[[56 36 44 64 60]\n",
" [48 12 30 54 21]]\n",
"[[1060 1656]\n",
" [ 408 1107]]\n"
]
},
{
"output_type": "error",
"ename": "ValueError",
"evalue": "shapes (2,2) and (3,3) not aligned: 2 (dim 1) != 3 (dim 0)",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-13-619d529acb04>\u001b[0m in \u001b[0;36m<cell line: 0>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0mQ\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mP\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mC\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mQ\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 17\u001b[0;31m \u001b[0mR\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mQ\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mD\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 18\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mQ\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mValueError\u001b[0m: shapes (2,2) and (3,3) not aligned: 2 (dim 1) != 3 (dim 0)"
]
}
]
},
{
"cell_type": "code",
"source": [
"# Define the 4 matrices as A,B,C,D"
],
"metadata": {
"id": "r1pVl7LbA1_I"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Print the 4 matrices"
],
"metadata": {
"id": "uJHj_CbhA83b"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Find the Dot Product of matrices\n"
],
"metadata": {
"id": "MDcD7tOWBMzG"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# ValueError: shapes (2,2) and (3,3) not aligned: 2 (dim 1) != 3 (dim 0)\n",
"\n",
"Now the task is to define a function named is_compatible() that takes 2 matrices as input and only allows the dot product if the condition number of **columns** in the first matrix (A) matches the number of **rows** in the second matrix (B).\n",
"\n",
"Print the ouput if condition is satisfied\n",
"Else print \"Dimension Error!!\"\n",
"\n",
"\n"
],
"metadata": {
"id": "Rv1D09dyB0eP"
}
},
{
"cell_type": "code",
"source": [
"import numpy as np\n",
"def is_compatible(A, B):\n",
" if A.shape[1] == B.shape[0]:\n",
" print(np.dot(A,B))\n",
" else:\n",
" print(\"Dimensional Error!!\")"
],
"metadata": {
"id": "RmS0xS8tCjNC"
},
"execution_count": 12,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Find Dot product only if the matrices are compatible"
],
"metadata": {
"id": "ds0XYSZ6E9nl"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# **Task 2**\n",
"\n",
"Your goal is to define a function that simulates a number-guessing game called \"Up-Down.\" The function, named up_down, should take an integer rounds as input, representing the number of turns the player gets.\n",
"\n",
"Game Rules:\n",
"\n",
"1. The player inputs a number between 1 and 12 for each round.\n",
"2. The computer randomly selects a number between 1 and 12.\n",
"3. The scoring system is as follows:\n",
" \n",
" a. If computer's guess is less than 7 and the player gains points equivalent to their guess if their guess is also less than 7, otherwise, they lose points equivalent to their guess.\n",
" \n",
" b. If computer's guess is exactly 7, the player gains 14 points if thier guess is 7, otherwise, they lose points equivalent to their guess.\n",
" \n",
" c. If computer's guess is greater than 7 and the player gains points equivalent to their guess if their guess is also greater than 7, otherwise, they lose points equivalent to their guess.\n",
"\n",
"4. The game continues until:\n",
"\n",
" a. The player reaches more than 30 points, they win.\n",
"\n",
" b. The player reaches less than -30 points, they lose.\n",
"\n",
" c. The maximum number of rounds is reached.\n",
" \n",
"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": [
"import numpy as np\n",
"\n",
"def up_down(rounds):\n",
" score = 0\n",
"\n",
" for i in range(rounds):\n",
" player_guess = int(input(\"Enter a number from 1 to 12\"))\n",
" computer_guess = np.random.randint(1, 13)\n",
" print(f\"Computer's guess: {computer_guess}\")\n",
"\n",
" if computer_guess < 7:\n",
" if player_guess < 7:\n",
" score += player_guess\n",
" else:\n",
" score -= player_guess\n",
" elif computer_guess == 7:\n",
" if player_guess == 7:\n",
" score += 14\n",
" else:\n",
" score -= player_guess\n",
" else:\n",
" if player_guess > 7:\n",
" score += player_guess\n",
" else:\n",
" score -= player_guess\n",
"\n",
" print(f\"Current score: {score}\")\n",
"\n",
" if score > 30:\n",
" print(\"You win\")\n",
" return\n",
" if score < -30:\n",
" print(\"You lose.\")\n",
" return\n",
"\n",
" print(f\"Final score: {score}\")\n"
],
"metadata": {
"id": "0fxnUDma7aPx"
},
"execution_count": 6,
"outputs": []
}
]
}