Skip to content

Instantly share code, notes, and snippets.

View pgebert's full-sized avatar
🎯
Currently working on timeseries forecasting / computer vision projects

Patrick Gebert pgebert

🎯
Currently working on timeseries forecasting / computer vision projects
View GitHub Profile
@pgebert
pgebert / transposeTableInKotlin.kt
Last active December 13, 2023 18:22
Transpose a list of lists in kotlin.
fun <T> List<List<T>>.transpose(): List<List<T>> {
var transposed = mutableListOf<List<T>>()
for (i in first().indices) {
val col: MutableList<T> = ArrayList()
forEach { row ->
col.add(row[i])
}
transposed.add(col)
}
return transposed
@pgebert
pgebert / reset_display.ps1
Created December 6, 2023 10:46
Reset your graphics driver on Windows 10 using powershell. This has to be run as administrator - your screen will flicker a few times.
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Start-Process powershell.exe "-WindowStyle Minimized -NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit
}
Get-PnpDevice -Class "Display" | Disable-PnpDevice -Confirm:$false
Get-PnpDevice -Class "Display" | Enable-PnpDevice -Confirm:$false
@pgebert
pgebert / base64.sh
Created August 3, 2023 09:55
Encodes and decodes a string on a Linux system.
/**
* Created by pgebert on 03/08/23.
*
* Encodes and decodes a string on a Linux system.
*/
// encode with base64 - Output: SGVsbG8=
echo -n 'Hello' | base64
@pgebert
pgebert / TreeMapsInKotlin.kt
Created April 5, 2023 19:58
Implementation of treeMapOf and toTreeMap equivalent to mapOf and toMap in kotlin.
import java.util.TreeMap
fun <K, V> treeMapOf(vararg pairs: Pair<K, V>): TreeMap<K, V> =
TreeMap<K, V>().apply { putAll(pairs) }
fun <K : Comparable<K>, V> Map<K, V>.toTreeMap(): TreeMap<K, V> =
TreeMap<K, V>().apply { putAll(this@toTreeMap) }
// Usage:
val mapA = treeMapOf(1 to "A")
CREATE FUNCTION jsonb_replace_by_key(obj jsonb, search text, substitute jsonb) RETURNS jsonb
STRICT LANGUAGE SQL AS $$
SELECT CASE jsonb_typeof(obj)
WHEN 'object' THEN
(SELECT coalesce(jsonb_object_agg(key, CASE WHEN key = search
THEN substitute
ELSE jsonb_replace_by_key(value, search, substitute)
END), '{}'::jsonb)
FROM jsonb_each(obj))
WHEN 'array' THEN
@pgebert
pgebert / digits.py
Created February 1, 2022 19:21
Get the first and last n digits of an integer number.
/**
* Created by pgebert on 01/02/22.
*
* Get the first and last n digits of an integer number
*/
import math
def first_n_digits(num: int, n: int):
return abs(num) // 10 ** (int(math.log(num, 10)) - n + 1)
@pgebert
pgebert / fibonacci.py
Created February 1, 2022 18:45
Python generator for fibonacci numbers.
/**
* Created by pgebert on 01/02/22.
*
* Iterator for fibonacci numbers.
*/
from typing import Iterator
def fibonacci() -> Iterator[int]:
a, b = 1, 1
@pgebert
pgebert / flask_test_app.py
Last active January 15, 2022 11:59
Pytest fixture to test requests for flask applications.
from typing import Generator
import pytest
from flask import Flask
from ..app import create_app
/**
* Created by pgebert on 10/01/22.
*
@pgebert
pgebert / is_prime.py
Last active January 14, 2022 08:34
Primality test for integers in python.
/**
* Created by pgebert on 14/01/22.
*
* Test whether a number is prime or not in python.
*/
def is_prime(n: int) -> bool:
"""Primality test for integers."""
if n <= 3:
@pgebert
pgebert / decode_jwt.py
Created December 6, 2021 21:22
Decryption of an example jwt token with python.
import base64
import json
/**
* Created by pgebert on 06/12/21.
*
* Decryption of an example jwt token with python.
*/
token = 'eyJuYW1lIjogInVzZXJfbmFtZSJ9'