Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"source": [
"#Brick Pyramid Construction\n",
"def min_rows_to_build_pyramid(n):\n",
" # Initialize variables to keep track of rows and total bricks\n",
" rows = 0\n",
" bricks_used = 0\n",
"\n",
" while bricks_used < n:\n",
" rows += 1\n",
" bricks_used += rows # Calculate the number of bricks in the current row\n",
"\n",
"\n",
" if(n==bricks_used):\n",
" return rows\n",
" return rows - 1\n",
"\n",
"# Example usage:\n",
"result1 = min_rows_to_build_pyramid(7)\n",
"print(result1) # Output: 3\n",
"\n",
"result2 = min_rows_to_build_pyramid(15)\n",
"print(result2) # Output: 5\n",
"\n",
"result3 = min_rows_to_build_pyramid(1)\n",
"print(result3) # Output: 1\n",
"\n",
"result4 = min_rows_to_build_pyramid(20)\n",
"print(result4) # Output: 5\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "LqiHEhftfVkA",
"outputId": "22f55000-4828-4195-bcc4-2855e5888a11"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"3\n",
"5\n",
"1\n",
"5\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"#Course Registration Optimization\n",
"\n",
"def stable_matching(student_prefs, course_prefs, course_capacity):\n",
" num_students = len(student_prefs)\n",
" num_courses = len(course_prefs)\n",
"\n",
" # Initialize all students and courses as free and unassigned\n",
" student_free = [True] * num_students\n",
" course_assignment = [-1] * num_courses\n",
"\n",
" while True:\n",
" # Find an unassigned student\n",
" student = -1\n",
" for i in range(num_students):\n",
" if student_free[i]:\n",
" student = i\n",
" break\n",
"\n",
" # If all students are assigned, we have a stable assignment\n",
" if student == -1:\n",
" break\n",
"\n",
" # Iterate through student's preferences\n",
" for i in range(len(student_prefs[student])):\n",
" course = student_prefs[student][i]\n",
"\n",
" # Check if the course is at capacity\n",
" if course_capacity[course] > 0:\n",
" course_capacity[course] -= 1\n",
" course_assignment[course] = student\n",
" student_free[student] = False\n",
" break\n",
"\n",
" # If the course is full, check if the current student prefers it over the current assignment\n",
" for j in range(num_students):\n",
" if course_assignment[course] == student_prefs[course][j]:\n",
" student_prefs[course][j] = -1\n",
" student_free[course_assignment[course]] = True\n",
" course_assignment[course] = student\n",
" student_free[student] = False\n",
" break\n",
"\n",
" return course_assignment\n",
"\n",
"# Example usage\n",
"student_preferences = [\n",
" [0, 1, 2],\n",
" [1, 0, 2],\n",
" [0, 1, 2],\n",
"]\n",
"\n",
"course_preferences = [\n",
" [0, 1, 2],\n",
" [1, 0, 2],\n",
" [0, 1, 2],\n",
"]\n",
"\n",
"course_capacity = [2, 2, 2]\n",
"\n",
"result = stable_matching(student_preferences, course_preferences, course_capacity)\n",
"print(result)\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Y6q25uHkH84S",
"outputId": "6746b275-bdfe-46a9-8c10-d6edf8a44a65"
},
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[2, 1, -1]\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"#Array Reordering Algorithm\n",
"\n",
"def reorder_array(arr):\n",
" even_nums = []\n",
" odd_nums = []\n",
"\n",
" for num in arr:\n",
" if num % 2 == 0:\n",
" even_nums.append(num)\n",
" else:\n",
" odd_nums.append(num)\n",
"\n",
" even_nums.sort()\n",
" odd_nums.sort()\n",
"\n",
" reordered_arr = even_nums + odd_nums\n",
" return reordered_arr\n",
"\n",
"# Example usage:\n",
"input_arr = [5, 2, 9, 4, 3, 6, 8, 1, 7]\n",
"output_arr = reorder_array(input_arr)\n",
"print(output_arr) # Output: [2, 4, 6, 8, 5, 9, 3, 1, 7]\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "9hJdXl8VNEf3",
"outputId": "535684a4-ef45-4180-b428-07b0e82e1b1b"
},
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[2, 4, 6, 8, 1, 3, 5, 7, 9]\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"#Shortest Path in a Weighted Directed Graph\n",
"\n",
"import heapq\n",
"\n",
"def shortest_path(graph, source, target):\n",
" num_nodes = len(graph)\n",
" dist = [float('inf')] * num_nodes\n",
" dist[source] = 0\n",
" prev = [None] * num_nodes\n",
" visited = set()\n",
" pq = [(0, source)]\n",
"\n",
" while pq:\n",
" _, u = heapq.heappop(pq)\n",
"\n",
" if u == target:\n",
" path = []\n",
" while u is not None:\n",
" path.append(u)\n",
" u = prev[u]\n",
" return path[::-1]\n",
"\n",
" if u not in visited:\n",
" visited.add(u)\n",
" for v, weight in enumerate(graph[u]):\n",
" if weight > 0 and dist[u] + weight < dist[v]:\n",
" dist[v] = dist[u] + weight\n",
" prev[v] = u\n",
" heapq.heappush(pq, (dist[v], v))\n",
"\n",
" return []\n",
"\n",
"# Example usage:\n",
"graph = [\n",
" [0, 4, 0, 0, 0, 0],\n",
" [0, 0, 8, 0, 0, 0],\n",
" [0, 0, 0, 7, 0, 2],\n",
" [0, 2, 0, 0, 0, 0],\n",
" [0, 0, 0, 5, 0, 3],\n",
" [0, 0, 0, 0, 0, 0]\n",
"]\n",
"\n",
"source_node = 0\n",
"target_node = 5\n",
"shortest_path = shortest_path(graph, source_node, target_node)\n",
"print(shortest_path) # Output: [0, 1, 2, 5]\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "SbQb-oaDP8rF",
"outputId": "0e65f72d-9ddc-40c7-c3aa-1b06f58bd378"
},
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[0, 1, 2, 5]\n"
]
}
]
}
]
}
Loading