Skip to content
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
280 changes: 280 additions & 0 deletions Bianca1.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
{
"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"
],
"metadata": {
"id": "Z8iSVv-r_WkT"
},
"execution_count": 1,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Define the 4 matrices as A,B,C,D\n",
"A=np.arange(6).reshape(2,3)\n",
"B=np.arange(15).reshape(3,5)\n",
"C=np.arange(10).reshape(5,2)\n",
"D=np.arange(4).reshape(2,2)"
],
"metadata": {
"id": "r1pVl7LbA1_I"
},
"execution_count": 27,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Print the 4 matrices\n",
"print(A)\n",
"print(B)\n",
"print(C)\n",
"print(D)"
],
"metadata": {
"id": "uJHj_CbhA83b",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "fdd7efc0-5649-4200-e415-1a4d4d30231d"
},
"execution_count": 28,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[0 1 2]\n",
" [3 4 5]]\n",
"[[ 0 1 2 3 4]\n",
" [ 5 6 7 8 9]\n",
" [10 11 12 13 14]]\n",
"[[0 1]\n",
" [2 3]\n",
" [4 5]\n",
" [6 7]\n",
" [8 9]]\n",
"[[0 1]\n",
" [2 3]]\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"# Find the Dot Product of matrices\n",
"AB=np.dot(A,B)\n",
"ABC=np.dot(AB,C)\n",
"ABCD=np.dot(ABC,D)\n",
"print(ABCD)"
],
"metadata": {
"id": "MDcD7tOWBMzG",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "a26deabf-1d8b-4656-96e4-88ede12cc984"
},
"execution_count": 29,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[1670 3185]\n",
" [5180 9890]]\n"
]
}
]
},
{
"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": [
"\n",
"def is_compatible(a, b):\n",
" if a.shape[1]==b.shape[0]:\n",
" res=np.dot(a, b)\n",
" print(res)\n",
" return res\n",
" else:\n",
" print(\"Dimension Error!!\")\n",
""
],
"metadata": {
"id": "RmS0xS8tCjNC"
},
"execution_count": 30,
"outputs": []
},
{
"cell_type": "code",
"source": [
"AB=is_compatible(A,B)\n",
"ABC=is_compatible(AB,C)\n",
"ABCD=is_compatible(ABC,D)\n"
],
"metadata": {
"id": "ds0XYSZ6E9nl",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "5d299227-dd68-4a3e-9304-549292badaf1"
},
"execution_count": 31,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[ 25 28 31 34 37]\n",
" [ 70 82 94 106 118]]\n",
"[[ 680 835]\n",
" [2120 2590]]\n",
"[[1670 3185]\n",
" [5180 9890]]\n"
]
}
]
},
{
"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": [
"r= int(input(\"Enter number of rounds between 1 and 12: \"))\n",
"def up_down(rounds):\n",
" guess_comp=np.random.randint(1,13)\n",
" points=0\n",
" while (points<30 and points>-30 and rounds>0):\n",
" guess_play=int(input(\"Enter guess between 1 and 12: \"))\n",
" if (guess_comp<7 and guess_play<7):\n",
" points=points+guess_play\n",
" elif (guess_comp==7 and guess_play==7):\n",
" points=points+14\n",
" elif (guess_comp>7 and guess_play>7):\n",
" points=points+guess_play\n",
" else:\n",
" points=points-guess_play\n",
" rounds=rounds-1\n",
" print(\"Final points are: \")\n",
" print(points)\n",
"\n",
"if(r<1 or r>12):\n",
" print(\"Invalid rounds!!\")\n",
"else:\n",
" up_down(r)"
],
"metadata": {
"id": "0fxnUDma7aPx",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "d0e4a788-8757-4055-f62d-8ad906f918f4"
},
"execution_count": 52,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Enter number of rounds between 1 and 12: 3\n",
"Enter guess between 1 and 12: 4\n",
"Enter guess between 1 and 12: 8\n",
"Enter guess between 1 and 12: 7\n",
"Final points are: \n",
"-3\n"
]
}
]
}
]
}