Skip to content

Instantly share code, notes, and snippets.

View Per48edjes's full-sized avatar
👉
This is a good point.

Ravi Dayabhai Per48edjes

👉
This is a good point.
View GitHub Profile
@Per48edjes
Per48edjes / lint_json.sh
Created December 27, 2020 02:30 — forked from nstielau/lint_json.sh
A one-liner for validating json
# Lint JSON with python (the exit 255 stops xargs after the first failed command)
find . -name "*.json" -print | xargs -Ixx bash -c "echo JSON linting xx 1>&2; cat xx | python -mjson.tool > /dev/null || exit 255"
@Per48edjes
Per48edjes / disaggregate_count.sql
Created April 6, 2021 03:53
Disaggregate a COUNT / "expand" a frequencies of a value
with recursive
cte_expand as (
select Number, Frequency as i from histogram
union all
select Number, i-1 as i from cte_expand where i > 1
)
select Number from cte_expand
order by 1
@Per48edjes
Per48edjes / pivot_cases.sql
Created April 10, 2021 22:10
Pivot entries in MySQL using variables and CASE statements
-- Set counter variables
set @r1=0, @r2=0, @r3=0, @r4=0;
with
cte_pivot as (
select
case
when Occupation='Doctor' then (@r1:=@r1+1)
when Occupation='Professor' then (@r2:=@r2+1)
@Per48edjes
Per48edjes / longest_runs.py
Last active May 1, 2021 19:39
Counting longest runs of {0, 1} in binary NumPy array
## Source: https://stackoverflow.com/questions/24342047/count-consecutive-occurences-of-values-varying-in-length-in-a-numpy-array/24343375#24343375
def longest_runs_sim(n: int = 50) -> int:
# Random sequence where each n_i is element of {0, 1}
seq = np.random.randint(0, 2, n)
longest_runs = {1: None, 0: None}
for i, outcome in enumerate([~seq, seq]):
runs = \
np.diff( # Diff of indices counts runs
@Per48edjes
Per48edjes / game.py
Created May 26, 2021 19:48
Python OOP implementation of Conway's Game of Life
import os
from itertools import product
from random import randint
from time import sleep
class Cell:
def __init__(self, state):
self._state = state
self._future_state = state
@Per48edjes
Per48edjes / git_gym__Lesson_1.md
Created September 1, 2021 21:48
[Git Gym] Lesson 1: The Basics

Lesson 1: The Basics

xkcd git

The goal for this lesson is to bolster your confidence using git. We won't attempt a comprehensive review, but we will try to establish a more holistic understanding of what it is, what it isn't, and how you can begin to make it work for you.

This is not:

@Per48edjes
Per48edjes / git_gym__Lesson_2.md
Last active February 27, 2023 19:40
[Git Gym] Lesson 2: The 'Undo'

Lesson 2: The 'Undo'

Git doesn't have a CTRL Z equivalent -- it does have a souped up set of commands that give you finer grained control of how you want to backtrack.

Git: reset vs. checkout vs. restore vs. switch vs. revert

It's certainly understandable if distinguishing these commands is just out of reach or requires a few Google/Stackoverflow searches each time you reach for

@Per48edjes
Per48edjes / tictactoe.py
Last active March 20, 2022 08:47
Tic-Tac-Toe game for Recurse Center code interview
"""
Recurse Center Programming Task: Tic-Tac-Toe
Author: Ravi Dayabhai
"""
from __future__ import annotations
import copy
import math
import os
@Per48edjes
Per48edjes / name_strings_as_integers.c
Last active May 28, 2022 01:35
Encoding strings as integers, vice versa in C
#include <stdio.h>
#include <string.h>
/*
* Extra credit: If an array of characters is 4 bytes long, and an integer is 4
* bytes long, then can you treat the whole in_name array like it’s just an
* integer?
*/
@Per48edjes
Per48edjes / selection_sort.c
Last active May 26, 2022 20:21
Implementation of selection sort on array of integers in C
#include <stdio.h>
void swap(int *a, int *b);
void print_array(int *arr, int len_arr);
int main(void)
{
int items[] = { 5, 2, 7, 4, 1, 6, 3, 0 };