Skip to content

Instantly share code, notes, and snippets.

View johanastborg's full-sized avatar
🧭
Dreaming in Code

Johan Astborg johanastborg

🧭
Dreaming in Code
  • Sweden
View GitHub Profile
@johanastborg
johanastborg / gennetworks.py
Last active May 15, 2023 06:07
Generate IP ranges and gateways from CIDR range
from netaddr import IPNetwork
from ipaddress import IPv4Network
start = "10.0.0.0/16"
end = "10.9.0.0/16"
start_range = IPNetwork(start)
end_range = IPNetwork(end)
allowed_range = []
@johanastborg
johanastborg / genips.py
Created May 12, 2023 14:20
Generate IP-addresses from a given CIDR-range
import ipaddress
ips = []
for addr in ipaddress.IPv4Network('192.1.0.0/16'):
ips.append(addr)
for ip in ips[0:10]:
print(ip)
@johanastborg
johanastborg / uuid.c
Last active May 7, 2023 15:55
UUID in C
#include <ctime>
#include <cstdlib>
// For illustrative purpose only!
int main() {
// Seed the random number generator with the current time
srand(time(NULL));
// Generate 4 random 32-bit integers to form a UUID
@johanastborg
johanastborg / README.md
Created May 7, 2023 15:52
Implementation of the Black-Scholes model in Python

The input parameters for both functions are:

  • S: the current stock price
  • K: the strike price of the option
  • T: the time to expiration of the option (in years)
  • r: the risk-free interest rate
  • sigma: the volatility of the stock price

Example usage:

@johanastborg
johanastborg / README.md
Last active May 7, 2023 15:47
Calculate customer lifetime value (CLV) using the Pareto/NBD model in Python

The CSV file transaction_data.csv contains columns for customer id, transaction id, transaction value, recency (time since last transaction), and T (time since first transaction)

@johanastborg
johanastborg / README.md
Created May 7, 2023 15:41
Customer lifetime value (CLV) in Python
# Create a sample transaction data set
transactions = pd.DataFrame({
    'customer_id': [1, 2, 3, 1, 2, 1, 2, 3],
    'date': pd.to_datetime(['2020-01-01', '2020-01-01', '2020-01-01', '2020-02-01', '2020-02-01', '2020-03-01', '2020-04-01', '2020-04-01']),
    'amount': [100, 200, 150, 80, 120, 300, 250, 100]
})

# Calculate the customer lifetime value
clv = calculate_clv(transactions, discount_rate=0.1, retention_rate=0.8)
@johanastborg
johanastborg / README.md
Created May 7, 2023 15:37
Implementation of an inverted index in Python
index = InvertedIndex()

# add some documents
index.add_document(1, "The quick brown fox")
index.add_document(2, "The quick brown dog")
index.add_document(3, "The slow brown dog")

# search for documents containing "quick brown"
result = index.search("quick brown")
@johanastborg
johanastborg / avltree.py
Created May 7, 2023 15:34
AVL tree in Python
class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
self.height = 1
class AVLTree:
def __init__(self):
@johanastborg
johanastborg / permute.cc
Created May 7, 2023 15:27
All permutations of a string in C++ using STL
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
string str = "abc";
sort(str.begin(), str.end()); // Sort the string in lexicographically increasing order
do {
@johanastborg
johanastborg / maxheap.cc
Created May 7, 2023 15:25
Implementation of a max heap in C++
#include <iostream>
#include <vector>
using namespace std;
class MaxHeap {
private:
vector<int> heap;
int parent(int i) { return (i - 1) / 2; } // parent index of i-th element
int left(int i) { return 2 * i + 1; } // left child index of i-th element