Skip to content

Commit

Permalink
week 1
Browse files Browse the repository at this point in the history
  • Loading branch information
devangi2000 committed Mar 24, 2021
1 parent e72071e commit 8beffdf
Show file tree
Hide file tree
Showing 4 changed files with 454 additions and 0 deletions.
68 changes: 68 additions & 0 deletions week 1/Knapsack.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Knapsack.ipynb",
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"metadata": {
"id": "YJ12sG4P92gx"
},
"source": [
"def knapSack(W, wt, val, n): \n",
" \n",
" if n == 0 or W == 0 : \n",
" return 0\n",
" \n",
" \n",
" if (wt[n-1] > W): \n",
" return knapSack(W, wt, val, n-1) \n",
" \n",
" else: \n",
" return max(val[n-1] + knapSack(W-wt[n-1], wt, val, n-1), \n",
" knapSack(W, wt, val, n-1))"
],
"execution_count": 10,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "a71qi5KI9_k-",
"outputId": "18e57be7-d04f-4bfa-b0d6-ceef81faa06c"
},
"source": [
"val = [60, 100, 120] \n",
"wt = [10, 20, 30] \n",
"W = 50\n",
"n = len(val) \n",
"print(knapSack(W, wt, val, n))"
],
"execution_count": 11,
"outputs": [
{
"output_type": "stream",
"text": [
"220\n"
],
"name": "stdout"
}
]
}
]
}
200 changes: 200 additions & 0 deletions week 1/N_Queens.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "N-Queens.ipynb",
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "YJ12sG4P92gx",
"outputId": "beb2de20-a81e-4820-a072-05d04977f00f"
},
"source": [
"global N \n",
"N = int(input(\"Enter Number Of Queens: \"))\n",
" \n",
"def printSolution(board): \n",
" for i in range(N): \n",
" for j in range(N): \n",
" print (board[i][j], end = \" \") \n",
" print()\n",
" "
],
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": [
"Enter Number Of Queens: 5\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "a71qi5KI9_k-"
},
"source": [
"def isSafe(board, row, col): \n",
" for i in range(col): \n",
" if board[row][i] == 1: \n",
" return False \n",
" for i, j in zip(range(row, -1, -1), \n",
" range(col, -1, -1)): \n",
" if board[i][j] == 1: \n",
" return False\n",
" \n",
" for i, j in zip(range(row, N, 1), \n",
" range(col, -1, -1)): \n",
" if board[i][j] == 1: \n",
" return False \n",
" return True"
],
"execution_count": 4,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "7ustKOIP-CqJ"
},
"source": [
"def solveNQUtil(board, col): \n",
" \n",
" if col >= N: \n",
" return True\n",
" \n",
" for i in range(N): \n",
" \n",
" if isSafe(board, i, col): \n",
" board[i][col] = 1\n",
" \n",
" if solveNQUtil(board, col + 1) == True: \n",
" return True\n",
" \n",
" board[i][col] = 0\n",
"\n",
" return False\n"
],
"execution_count": 5,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "pbgh7w3R-W3K",
"outputId": "ebcb835f-0b61-466c-daeb-5abfb126e082"
},
"source": [
"board = [[0] * N for i in range(N)]\n",
"board"
],
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[[0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0]]"
]
},
"metadata": {
"tags": []
},
"execution_count": 6
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "W-flfPjm-ZND"
},
"source": [
"def solveNQ(): \n",
" if solveNQUtil(board, 0) == False: \n",
" print (\"Solution does not exist\") \n",
" return False\n",
" \n",
" printSolution(board) \n",
" return True "
],
"execution_count": 7,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "gmklAzcL-bpd",
"outputId": "30ce2fb8-56fc-4a06-e14e-cb6b959821b5"
},
"source": [
"solveNQ()"
],
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"text": [
"1 0 0 0 0 \n",
"0 0 0 1 0 \n",
"0 1 0 0 0 \n",
"0 0 0 0 1 \n",
"0 0 1 0 0 \n"
],
"name": "stdout"
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {
"tags": []
},
"execution_count": 8
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "Op_wZ4BP-dri"
},
"source": [
""
],
"execution_count": null,
"outputs": []
}
]
}
73 changes: 73 additions & 0 deletions week 1/camel_problem.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "camel-problem.ipynb",
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "YJ12sG4P92gx",
"outputId": "f35edf1f-d773-4936-f146-3dbf3a450940"
},
"source": [
"total=int(input('Enter no. of bananas at starting: '))\n",
"distance=int(input('Enter distance you want to cover: '))\n",
"load_capacity=int(input('Enter max load capacity of your camel: '))\n",
"lose=0\n",
"start=total\n",
"for i in range(distance):\n",
" while start>0:\n",
" start=start-load_capacity\n",
"\n",
" if start==1:\n",
" lose=lose-1\n",
" lose=lose+2\n",
"\n",
" lose=lose-1\n",
" start=total-lose\n",
" if start==0:\n",
" break\n",
"print(start)"
],
"execution_count": 12,
"outputs": [
{
"output_type": "stream",
"text": [
"Enter no. of bananas at starting: 3000\n",
"Enter distance you want to cover: 1000\n",
"Enter max load capacity of your camel: 1000\n",
"533\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "vK27DogE_EI-"
},
"source": [
""
],
"execution_count": null,
"outputs": []
}
]
}
Loading

0 comments on commit 8beffdf

Please sign in to comment.