Skip to content

Instantly share code, notes, and snippets.

@270ajay
270ajay / n_queens.py
Created December 21, 2023 05:12
Constraint Programming solver module 1
# Course: https://www.edx.org/learn/computer-programming/universite-catholique-de-louvain-constraint-programming
# Backtracking search algorithm for solving n-queens problem
import time
from typing import Callable
class NQueensFilter:
def __init__(self, number_of_queens: int):
self._number_of_queens: int = number_of_queens
@270ajay
270ajay / knapsack_solver.py
Created December 21, 2023 04:59
Knapsack solver library from ortools in python
# Knapsack library by ortools
# Translated from c++ code:
# https://github.com/google/or-tools/blob/stable/ortools/algorithms/knapsack_solver.h
# https://github.com/google/or-tools/blob/stable/ortools/algorithms/knapsack_solver.cc
import bisect
import heapq
import math
from ortools.linear_solver import pywraplp
@270ajay
270ajay / abstract_factory_pattern.py
Created December 3, 2023 09:35
Design Patterns: Taken from Derek Banas's Design Patterns Playlist on YouTube
from abc import ABC, abstractmethod
class EnemyShipBuilding(ABC):
@abstractmethod
def make_enemy_ship(self, type_of_ship: str) -> "EnemyShip":
pass
def order_the_ship(self, type_of_ship: str) -> "EnemyShip":
the_enemy_ship = self.make_enemy_ship(type_of_ship)
@270ajay
270ajay / refactoring1.py
Created November 29, 2023 12:23
Code Refactoring: Taken from Derek Banas's Refactoring Playlist on YouTube
class _FootballPlayer:
def __init__(
self,
passer_rating: int,
rushing_yard: int,
receiving_yard: int,
total_tackles: int,
interceptions: int,
avg_punt: int,
avg_kick_off_return: int,
@270ajay
270ajay / python-formatter.bat
Created November 23, 2023 13:00
Batch file to format python files
@ECHO OFF
:: This batch file helps in automating tasks on cmd.
:: First, we sort inports using isort package.
:: Then, we remove unused imports using autoflake package.
:: Finally, we format using black package.
TITLE Python Files Formatter
ECHO /!\ Please make sure you have the following packages installed:
@270ajay
270ajay / utils.py
Last active April 3, 2022 09:41
logging, json, excel functions, py to exe
# Example of license header:
# Unauthorized copying of this file, via any medium is strictly prohibited
# Proprietary and confidential
"""This file contains utility classes and functions."""
import json
import logging
import openpyxl
@270ajay
270ajay / create_lp_sol_file.py
Created February 15, 2022 10:04
Lp sol file generator for debugging mathematical models
"""Creates a file which contains info from lp and sol file.
----------------
Sample sol file:
----------------
# Solution for model Mathematical Model
# Objective value = 140
x1 10
x2 20
x3 30
@270ajay
270ajay / basic_song_recommender_system.py
Created September 30, 2021 06:12
Basic machine learning - Recommender systems
"""https://www.coursera.org/learn/ml-foundations?specialization=machine-learning#about
https://songxia-sophia.medium.com/collaborative-filtering-recommendation-with-co-occurrence-algorithm-dea583e12e2a
https://towardsdatascience.com/recommend-using-scikit-learn-and-tensorflow-recommender-bc659d91301a
"""
import numpy
import pandas
from scipy import sparse
from sklearn import metrics
@270ajay
270ajay / basic_document_retrieval_clustering.py
Created September 27, 2021 09:32
Basic machine learning - clustering
"""https://www.coursera.org/learn/ml-foundations?specialization=machine-learning#about"""
import pandas
from sklearn import neighbors
from sklearn import metrics
from sklearn.feature_extraction import text
if __name__ == "__main__":
people_df = pandas.read_csv("people_wiki.csv")
@270ajay
270ajay / basic_sentiment_classification.py
Created September 23, 2021 07:10
Basic machine learning - classification
"""https://www.coursera.org/learn/ml-foundations?specialization=machine-learning#about"""
from matplotlib import pyplot
import pandas
import seaborn
from sklearn import linear_model
from sklearn import model_selection
from sklearn import metrics
from sklearn.feature_extraction import text