Skip to content

Instantly share code, notes, and snippets.

View HirbodBehnam's full-sized avatar

Hirbod Behnam HirbodBehnam

View GitHub Profile
@HirbodBehnam
HirbodBehnam / disjoint_set.cpp
Created January 13, 2022 11:00
Simple disjoint set which I don't know if it works or not.
// from https://github.com/spakin/disjoint
class disjoint_set {
private:
typedef struct {
int rank;
int parent;
} element;
vector<element> set_list;
public:
explicit disjoint_set(int size) {
@HirbodBehnam
HirbodBehnam / unicode-decompose.py
Last active January 10, 2022 15:48
This script decomposes the Unicode characters in a text file based on UnicodeData.txt and then creates a json file which can be used in other programming languages for decomposing. I used this script to fix the Persian characters extracted from PDF files.
import re
import json
fixset = {}
# Get this file from http://ftp.unicode.org/Public/UNIDATA/UnicodeData.txt
with open('UnicodeData.txt', 'r') as data:
for line in data:
lineData = line.split(';')
if lineData[5] != '':
g = re.search(r'^<.+> (.+)$', lineData[5])
if g is not None and g.group(1) is not None:
@HirbodBehnam
HirbodBehnam / prim.cpp
Created December 26, 2021 14:23
Simple and inefficient prim algorithm implementation in C++
#include <iostream>
#include <unordered_set>
#include <vector>
using namespace std;
class graph {
private:
typedef struct {
int to;
@HirbodBehnam
HirbodBehnam / bitset.cpp
Created November 25, 2021 07:41
Ultra simple implementation of bit set in C++
class bit_set {
private:
uint8_t *data;
public:
explicit bit_set(int size) {
// (size + 7) / 8 is ceiling of size / 8
data = new uint8_t[(size + 7) / 8]();
}
void set(int index) {
@HirbodBehnam
HirbodBehnam / ffmpeg-scripts.sh
Last active December 12, 2025 16:38
FFMpeg scripts I use to convert videos
# Simple FFMPEG scripts I use to convert videos or audios
# The ones with h264_nvenc codec only work with NVidia graphic cards. You also need to install the drives to use them
# The hevc_nvenc codec is pretty fast but the output file size is terrible. So I won't include that in. (Special thanks to Erfan Mojibi)
# The libx265 provides better quality to file size. So we can increase the crf in it (source: https://trac.ffmpeg.org/wiki/Encode/H.265#ConstantRateFactorCRF)
# The tag is provided by @alirezahabib
# Simple convert from webm to mp4. ACC audio codec at 128kb/s. Change 24 to change the quality (higher is worse)
ffmpeg -i in.webm -r 10 -vf "scale=-2:720" -c:v h264_nvenc -cq:v 24 -profile:v high -c:a aac -b:a 128k -strict experimental out.mp4
ffmpeg -i in.webm -r 10 -vf "scale=-2:720" -c:v libx264 -crf 24 -c:a aac -b:a 128k -strict experimental out.mp4
ffmpeg -i in.webm -r 10 -vf "scale=-2:720" -c:v libx265 -crf 28 -c:a aac -b:a 128k -tag:v hvc1 -strict experimental out.mp4
@HirbodBehnam
HirbodBehnam / csv.go
Created June 10, 2021 08:54
A function to convert an array into csv file in go.
package util
import (
"encoding/csv"
"fmt"
"io"
"reflect"
)
// StreamCsv converts an array of an struct to csv
@HirbodBehnam
HirbodBehnam / ipify.rs
Created May 11, 2021 10:40
A function to get your IP address from ipify.org. It doesn't use any libraries and writes raw input in connection.
use std::net::{TcpStream};
use std::io::{Write, BufReader, BufRead};
/// Connects to api.ipify.org and returns your IP address
/// Note that this method does not use any proxies
fn get_ip() -> Result<String, String> {
// at first, try to to connect to ipify
match TcpStream::connect("api.ipify.org:80") {
Ok(mut stream) => {
// create the request header; This is a const value
@HirbodBehnam
HirbodBehnam / c-testcase-generator.sh
Last active March 31, 2021 17:21
A simple script to generate C testcases based on inputs for Quera
#!/bin/bash
# A simple script to generate C testcases based on inputs for Quera
# Place your input files in a folder called "raw-input" with the format of *.txt
# Then run the program with the code filename as the argument and then this script will generate
# the answers based on your program
# Compile the program
gcc -O3 -o program "$1" -lm # I include the math library as well
# Create the output folders if needed
mkdir -p "in" "out"
# Loop all test cases
@HirbodBehnam
HirbodBehnam / maze-viewer.cpp
Created March 3, 2021 17:09
View mazes generated by our maze generator
/**
Maze example:
1e11111111111
1*0*0*0*0*0*1
1110111111101
1*1*0*1*0*1*1
1011111010101
1*0*0*0*1*0*1
11111111111e1
*/
@HirbodBehnam
HirbodBehnam / checker.bat
Created February 28, 2021 13:31
Some scripts for creating test cases for java applications.
@echo off
setlocal EnableDelayedExpansion
javac %1
set /A Counter=1
set ClassName=%1
set ClassName=%ClassName:~0,-5%
:loop
set "i=in%Counter%.txt"
set "o=out%Counter%.txt"