Skip to content

Instantly share code, notes, and snippets.

@imvickykumar999
Created February 25, 2023 11:43
Show Gist options
  • Save imvickykumar999/deb9f73174c5ee7b3cc4a2ebe3c56fe4 to your computer and use it in GitHub Desktop.
Save imvickykumar999/deb9f73174c5ee7b3cc4a2ebe3c56fe4 to your computer and use it in GitHub Desktop.
2D Rubix Cube.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyNyTQEXVJOIi+PQNF/PUAOi",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/imvickykumar999/deb9f73174c5ee7b3cc4a2ebe3c56fe4/2d-rubix-cube.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Nn7FmrDfPAGw",
"outputId": "f840883a-8829-43b3-cbd6-81f940430279"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[2, 3, 4, 5]\n"
]
}
],
"source": [
"my_list = [1,2,3,4] \n",
"print([x+1 for x in my_list]) # add 1 to every element?"
]
},
{
"cell_type": "code",
"source": [
"import numpy as np\n",
"arr = np.array([1,2.4,'3',4])\n",
"print(arr)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ZPM9JaYMPBOy",
"outputId": "62e7fd57-bbc5-4761-ad7b-ac56d43bf55c"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"['1' '2.4' '3' '4']\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"arr.dtype"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "WQxi1qQ6SDxM",
"outputId": "7134abf6-8fcf-4d08-ec7d-068a1b0d3613"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"dtype('<U32')"
]
},
"metadata": {},
"execution_count": 6
}
]
},
{
"cell_type": "code",
"source": [
"np.zeros((2,5), dtype=int)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "y-oFuVTsSN2D",
"outputId": "e0cd52e2-c767-4892-85db-7af0b4850f42"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0]])"
]
},
"metadata": {},
"execution_count": 9
}
]
},
{
"cell_type": "code",
"source": [
"np.ones((3,5), dtype=int)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "E_PF1ExOUUi5",
"outputId": "d7b16405-1c4a-41a9-f6fb-5cb9802898da"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1],\n",
" [1, 1, 1, 1, 1]])"
]
},
"metadata": {},
"execution_count": 10
}
]
},
{
"cell_type": "code",
"source": [
"np.linspace(1.3546778,1.3546779,10)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "wGkblRDMUzE0",
"outputId": "b3a51dbc-7aa2-4b17-dec5-5087434c1ea7"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([1.3546778 , 1.35467781, 1.35467782, 1.35467783, 1.35467784,\n",
" 1.35467786, 1.35467787, 1.35467788, 1.35467789, 1.3546779 ])"
]
},
"metadata": {},
"execution_count": 17
}
]
},
{
"cell_type": "code",
"source": [
"np.eye(4,5, dtype=int)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "uGByIcbhU7Gf",
"outputId": "fbf73d4d-2855-4fb9-e86f-6564f3dfe9f2"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[1, 0, 0, 0, 0],\n",
" [0, 1, 0, 0, 0],\n",
" [0, 0, 1, 0, 0],\n",
" [0, 0, 0, 1, 0]])"
]
},
"metadata": {},
"execution_count": 21
}
]
},
{
"cell_type": "code",
"source": [
"arr = [[1,2,3,4], [11,21,31,41], [12,22,32,42], [13,23,33,43]]\n",
"arr = np.array(arr)\n",
"arr[:,1].reshape(4,1)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ptKJG41PVoQm",
"outputId": "8639b403-6b28-433e-90ca-d17ab6ed8835"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[ 2],\n",
" [21],\n",
" [22],\n",
" [23]])"
]
},
"metadata": {},
"execution_count": 34
}
]
},
{
"cell_type": "code",
"source": [
"arr"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "xuDYgxrYZehX",
"outputId": "8a93a139-2c73-4f21-b31f-54836ee8e6be"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[ 1, 2, 3, 4],\n",
" [11, 21, 31, 41],\n",
" [12, 22, 32, 42],\n",
" [13, 23, 33, 43]])"
]
},
"metadata": {},
"execution_count": 39
}
]
},
{
"cell_type": "code",
"source": [
"arr.sum(axis=0)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "QwiVoiBHXr3q",
"outputId": "622509b9-73d1-4dec-c542-76cff5742342"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([ 37, 68, 99, 130])"
]
},
"metadata": {},
"execution_count": 38
}
]
},
{
"cell_type": "code",
"source": [
"arr.sum(axis=1) # sum of [ 1, 2, 3, 4]"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Y4dxqL4pZi-9",
"outputId": "67fc01db-cc9d-4809-e17a-47eb293d5b31"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([ 10, 104, 108, 112])"
]
},
"metadata": {},
"execution_count": 48
}
]
},
{
"cell_type": "code",
"source": [
"arr.mean(axis=0)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "4PKFqgkpZVnj",
"outputId": "7c0433f8-bc67-4a6f-b1b1-80b7ce723c0e"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([ 9.25, 17. , 24.75, 32.5 ])"
]
},
"metadata": {},
"execution_count": 43
}
]
},
{
"cell_type": "code",
"source": [
"# excercise # get this array wwithout using np.array # create an array \n",
"\n",
"arr = [[ 1, 2, 3, 4, 5],\n",
"[ 6, 7, 8, 9, 10],\n",
"[11, 12, 13, 14, 15],\n",
"[16, 17, 18, 19, 20]]"
],
"metadata": {
"id": "rTQ8C8eWai6Q"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"np.arange(1,21).reshape(4,5)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "-qagEP6JbIxk",
"outputId": "6c250929-7d66-4df9-eb1e-c6ce0e114b79"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[ 1, 2, 3, 4, 5],\n",
" [ 6, 7, 8, 9, 10],\n",
" [11, 12, 13, 14, 15],\n",
" [16, 17, 18, 19, 20]])"
]
},
"metadata": {},
"execution_count": 56
}
]
},
{
"cell_type": "code",
"source": [
"[1,2]*2"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "bEeo6EcNb725",
"outputId": "14bb9ea7-1230-4bef-d1ad-b2985ea114ca"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[1, 2, 1, 2]"
]
},
"metadata": {},
"execution_count": 57
}
]
},
{
"cell_type": "code",
"source": [
"np.array([0,1])/0"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "bCCu7O_Cnlyk",
"outputId": "2f6de6d4-862e-416f-91c0-8da14a4b0838"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"<ipython-input-62-f918abecc96d>:1: RuntimeWarning: divide by zero encountered in true_divide\n",
" np.array([0,1])/0\n",
"<ipython-input-62-f918abecc96d>:1: RuntimeWarning: invalid value encountered in true_divide\n",
" np.array([0,1])/0\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([nan, inf])"
]
},
"metadata": {},
"execution_count": 62
}
]
},
{
"cell_type": "code",
"source": [
"np.array([1,2,3])**(.5)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "SPkgPK6xn2Jm",
"outputId": "a93edd05-6ca6-47da-b526-8f2d341c18c8"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([1. , 1.41421356, 1.73205081])"
]
},
"metadata": {},
"execution_count": 60
}
]
},
{
"cell_type": "code",
"source": [
"np.inf, np.nan"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "aaeubBGsoBzJ",
"outputId": "615bcdb6-e0e5-4a80-f80a-c3e350f87281"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(inf, nan)"
]
},
"metadata": {},
"execution_count": 76
}
]
},
{
"cell_type": "code",
"source": [
"arr = np.array([1,2,5,7,5,7,2,4,7,9,0,2,7,3])\n",
"arr"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "fa3ld5XQph4p",
"outputId": "705f7921-178b-4bd1-9a2a-878a0e5166de"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([1, 2, 5, 7, 5, 7, 2, 4, 7, 9, 0, 2, 7, 3])"
]
},
"metadata": {},
"execution_count": 77
}
]
},
{
"cell_type": "code",
"source": [
"for i,j in enumerate(arr):\n",
" if j == 7:\n",
" print(i)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "pVrbpr9Vq3qo",
"outputId": "e4923fa5-aff4-4952-e032-61064ae7e400"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"3\n",
"5\n",
"8\n",
"12\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"np.where(arr==7)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "1kKL3oPHq_qv",
"outputId": "5aa94718-4d1b-4ad5-9836-0e8e768b9761"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(array([ 3, 5, 8, 12]),)"
]
},
"metadata": {},
"execution_count": 81
}
]
},
{
"cell_type": "code",
"source": [
"arr2d = [[1,5,7,3], [4,7,2,4], [7,6,8,3]]\n",
"arr2d = np.array(arr2d)\n",
"arr2d"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "-6q_5I62rQ9U",
"outputId": "394700a8-34f2-4dbd-e4ca-a5b609abac56"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[1, 5, 7, 3],\n",
" [4, 7, 2, 4],\n",
" [7, 6, 8, 3]])"
]
},
"metadata": {},
"execution_count": 83
}
]
},
{
"cell_type": "code",
"source": [
"np.where(arr2d==7)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "l9cc95HProFd",
"outputId": "532820b1-2d4f-4496-a76f-d8e5956548d2"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(array([0, 1, 2]), array([2, 1, 0]))"
]
},
"metadata": {},
"execution_count": 84
}
]
},
{
"cell_type": "code",
"source": [
"\n",
"import numpy as np\n",
"\n",
"empty = np.chararray((3, 3))\n",
"red = np.chararray((3, 3))\n",
"white = np.chararray((3, 3))\n",
"green = np.chararray((3, 3))\n",
"yellow = np.chararray((3, 3))\n",
"blue = np.chararray((3, 3))\n",
"orange = np.chararray((3, 3))\n",
"\n",
"empty[:] = ' '\n",
"red[:] = 'R'\n",
"white[:] = 'W'\n",
"green[:] = 'G'\n",
"yellow[:] = 'Y'\n",
"blue[:] = 'B'\n",
"orange[:] = 'O'\n",
"\n",
"layer1 = np.hstack((empty, red, empty, empty))\n",
"layer2 = np.hstack((white, green, yellow, blue))\n",
"layer3 = np.hstack((empty, orange, empty, empty))\n",
"\n",
"board = np.vstack((layer1, layer2, layer3))\n",
"print(board)\n"
],
"metadata": {
"id": "tf-B7qk5ruRj",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "a462da53-3baf-4c23-da96-bfeb1a150796"
},
"execution_count": 49,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[b' ' b' ' b' ' b'R' b'R' b'R' b' ' b' ' b' ' b' ' b' ' b' ']\n",
" [b' ' b' ' b' ' b'R' b'R' b'R' b' ' b' ' b' ' b' ' b' ' b' ']\n",
" [b' ' b' ' b' ' b'R' b'R' b'R' b' ' b' ' b' ' b' ' b' ' b' ']\n",
" [b'W' b'W' b'W' b'G' b'G' b'G' b'Y' b'Y' b'Y' b'B' b'B' b'B']\n",
" [b'W' b'W' b'W' b'G' b'G' b'G' b'Y' b'Y' b'Y' b'B' b'B' b'B']\n",
" [b'W' b'W' b'W' b'G' b'G' b'G' b'Y' b'Y' b'Y' b'B' b'B' b'B']\n",
" [b' ' b' ' b' ' b'O' b'O' b'O' b' ' b' ' b' ' b' ' b' ' b' ']\n",
" [b' ' b' ' b' ' b'O' b'O' b'O' b' ' b' ' b' ' b' ' b' ' b' ']\n",
" [b' ' b' ' b' ' b'O' b'O' b'O' b' ' b' ' b' ' b' ' b' ' b' ']]\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"temp = np.copy(board[4, 0:3])\n",
"board[4, 0:3] = board[4, 3:6]\n",
"board[4, 3:6] = board[4, 6:9]\n",
"board[4, 6:9] = board[4, 9:12]\n",
"board[4, 9:12] = temp\n",
"print(board)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "XssgfbdZPBDg",
"outputId": "54603b57-7511-4a22-b07f-c3f73476060e"
},
"execution_count": 50,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[b' ' b' ' b' ' b'R' b'R' b'R' b' ' b' ' b' ' b' ' b' ' b' ']\n",
" [b' ' b' ' b' ' b'R' b'R' b'R' b' ' b' ' b' ' b' ' b' ' b' ']\n",
" [b' ' b' ' b' ' b'R' b'R' b'R' b' ' b' ' b' ' b' ' b' ' b' ']\n",
" [b'W' b'W' b'W' b'G' b'G' b'G' b'Y' b'Y' b'Y' b'B' b'B' b'B']\n",
" [b'G' b'G' b'G' b'Y' b'Y' b'Y' b'B' b'B' b'B' b'W' b'W' b'W']\n",
" [b'W' b'W' b'W' b'G' b'G' b'G' b'Y' b'Y' b'Y' b'B' b'B' b'B']\n",
" [b' ' b' ' b' ' b'O' b'O' b'O' b' ' b' ' b' ' b' ' b' ' b' ']\n",
" [b' ' b' ' b' ' b'O' b'O' b'O' b' ' b' ' b' ' b' ' b' ' b' ']\n",
" [b' ' b' ' b' ' b'O' b'O' b'O' b' ' b' ' b' ' b' ' b' ' b' ']]\n"
]
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "FlQbsudXRzYP"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "e90cQWp_R_Jj"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment