Skip to content

Instantly share code, notes, and snippets.

View rrmhearts's full-sized avatar

Ryan rrmhearts

View GitHub Profile
@rrmhearts
rrmhearts / tree_to_list_nodes.js
Last active July 20, 2020 15:29
Create a flat list of nodes from a nested tree structure.
// nestedTree is a tree structure containing subtrees under "nodes" property. d=1 is the default "depth of the tree"
function flatDeep(arr, d = 1) {
return d > 0 ? arr.reduce((acc, val) => acc.concat(Array.isArray(val.nodes) ? flatDeep(val.nodes, d - 1) : val), [])
: arr.slice();
};
let flat = flatDeep(nestedTree, Infinity);
// Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat
@rrmhearts
rrmhearts / list_nodes_to_tree.js
Last active July 20, 2020 15:30
Create Tree Structure from list of nodes with parent property
const idMapping = data.reduce((acc, el, i) => {
acc[el.id] = i;
return acc;
}, {});
let root = [];
data.forEach(el => {
// Handle the root element
if (el.parent === null) {
root.push(el);
@rrmhearts
rrmhearts / cert-renew.sh
Created June 15, 2020 19:17
Renew lighttpd server certs using letsencrypt
#!/bin/bash
sudo /usr/local/bin/certbot-auto renew
sudo cp /etc/letsencrypt/live/domain.com/* /etc/lighttpd/certs/
cd /etc/lighttpd/certs
sudo cat privkey.pem cert.pem | sudo tee merged.pem
sudo systemctl restart lighttpd.service
## Renew every two months
@rrmhearts
rrmhearts / install_vscode_rpm.sh
Last active April 22, 2020 20:34
Visual Studio Code on Clear Linux
#!/bin/bash
# delete previous version if needed
INSTALL_DIR=/usr/share/code
if [ -d "$INSTALL_DIR" ]; then
echo "deleting: $INSTALL_DIR"
sudo rm -rf $INSTALL_DIR
fi
# download the rpm from https://code.visualstudio.com/download
@rrmhearts
rrmhearts / tunnel.md
Created March 25, 2020 13:52
ssh tunnel

On the computer without public IP:

ssh -R 2222:localhost:22 loginOfServerWithPublicIP@publicIP

This connects to the server by SSH and builds a tunnel from the server with public IP on port 2222 to the computer without public IP on port 22 (SSH).

And then on the server:

ssh -p 2222 loginOfComputerWithoutPublicIP@locahost
@rrmhearts
rrmhearts / long.sh
Created January 9, 2020 16:08
Long bash
function long {
    START=$(date +%s.%N)
    $*
    EXIT_CODE=$?
    END=$(date +%s.%N)
    DIFF=$(echo "$END - $START" | bc)
    RES=$(python -c "diff = $DIFF; min = int(diff / 60); print('%s min' % min)")
    result="$1 completed in $RES, exit code $EXIT_CODE."
    echo -e "\n⏰  $result"
    ( say -r 250 $result 2>&1 > /dev/null & )
@rrmhearts
rrmhearts / variable_function.c
Created October 29, 2019 14:48
Setting a function to a variable in C
#include <stdio.h>
typedef void (*functionType)();
void doThis() {
printf("Hello World!\n");
}
int main() {
functionType doVar = &doThis;
@rrmhearts
rrmhearts / passing_function.c
Last active October 29, 2019 14:48
Passing a Function in C
#include <stdio.h>
void printNumber(int nbr)
{
printf("%d\n", nbr);
}
void myFunction(void (*f)(int))
{
for(int i = 0; i < 5; i++)
{
(*f)(i);
@rrmhearts
rrmhearts / python_swap.md
Created October 9, 2019 14:44
Python Switch Version

Change python version system-wide

To change python version system-wide we can use update-alternatives command. Logged in as a root user, first list all available python alternatives:

# update-alternatives --list python
update-alternatives: error: no alternatives for python

The above error message means that no python alternatives has been recognized by update-alternatives command. For this reason we need to update our alternatives table and include both python2.7 and python3.4:

# update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode
@rrmhearts
rrmhearts / flip_edges.txt
Created March 19, 2019 02:29
Flip last edges of Rubik's cube
Flip last two edges in place. Front-top edge and left-top edge.
For this example, an algorithm that is more simple to remember,
and can be altered for the other case (opposite edges), is this:
F' E F2 E2 F' U' F E2 F2 E' F and U.
Replace U and U' with U2 for the case of flipping two opposite edges on top face.
(E is the equatorial slice, viewed from face U)