Skip to content

Instantly share code, notes, and snippets.

View syphh's full-sized avatar

Syphax Ait oubelli syphh

View GitHub Profile
@syphh
syphh / bus_park_optimization.py
Created December 5, 2023 12:52
Minimize total distance between drivers and their assigned bus station given capacity constraints
import pulp
import pandas as pd
import numpy as np
"""Example drivers.csv file:
driver_id,lat,lon
dr_1,10,20
dr_2,30,50
dr_3,50,40
dr_4,80,20
# 1st approach:
def kth_largest(arr, k):
for i in range(k-1):
arr.remove(max(arr))
return max(arr)
# 2nd approach:
def kth_largest(arr, k):
n = len(arr)
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
def update_annot(ind):
pos = sc.get_offsets()[ind["ind"][0]]
annot.xy = pos
attributes_to_show = [0, 1, 2, 7]
text = df.iloc[ind["ind"][0], attributes_to_show].to_string()
from typing import List, Any
class Node:
def __init__(self, key, value, nxt=None):
self.key: str = key
self.value: Any = value
self.nxt: Node = nxt
class LinkedList:
import numpy as np
def approximate_pi(n):
points = np.random.uniform(-1, 1, (n, 2))
inside = np.sum(points[:,0]**2+points[:,1]**2 <= 1)
k = inside/n
return 4*k
n = 1000000
import numpy as np
import matplotlib.pyplot as plt
def midpoint(p1, p2):
x1, y1 = p1
x2, y2 = p2
return (x1+x2)/2, (y1+y2)/2
def is_valid(grid, r, c, k):
not_in_row = k not in grid[r]
not_in_column = k not in [grid[i][c] for i in range(9)]
not_in_box = k not in [grid[i][j] for i in range(r//3*3, r//3*3+3) for j in range(c//3*3, c//3*3+3)]
return not_in_row and not_in_column and not_in_box
def solve(grid, r=0, c=0):
if r == 9:
return True
def is_safe(board, i, j):
n = len(board)
j_left = j
j_right = j
while i >= 0:
if (j_left >= 0 and board[i][j_left] == 1) or board[i][j] == 1 or (j_right < n and board[i][j_right] == 1):
return False
i -= 1
j_left -= 1
j_right += 1
def knapsack(values, weights, k, i=0, lookup=None):
lookup = {} if lookup is None else lookup
if (i, k) in lookup:
return lookup[(i, k)]
if i == len(values):
return 0
elif k < 0:
return float('-inf')
else:
lookup[(i, k)] = max(values[i]+knapsack(values, weights, k-weights[i], i+1, lookup),
import math
def dist(p1, p2):
x1, y1, x2, y2 = *p1, *p2
return math.sqrt((y2-y1)**2 + (x2-x1)**2)
def polar_angle(p1, p2):
if p1[1] == p2[1]:
return -math.pi