Skip to content

Instantly share code, notes, and snippets.

View adeishs's full-sized avatar

ade ishs adeishs

View GitHub Profile
@adeishs
adeishs / pg-convert-week-number-to-monday-iso-8601.sql
Created September 30, 2022 00:57
Convert week number to Monday (ISO 8601)
WITH week AS (
-- change week number and year as you need
SELECT CAST(2 AS INTEGER) AS number,
CAST(2022 AS INTEGER) AS year
),
first_date AS (
SELECT CAST(year || '-01-01' AS DATE) AS d
FROM week
),
@adeishs
adeishs / content.md
Last active November 7, 2022 12:06
Multilevel Dictionaries in Python

As an experienced Perl programmer, I have been dealing with nested hashrefs of hashrefs. (Those not familiar with Perl: hashref is short for hash reference. Hash is also known as associative array.)

In Perl, we can just arbitrarily create hashrefs of hashrefs on the fly, e.g. hashref-hashref.pl

In Python, not so easy. Something like multilevel-dict-broken.py will give you a KeyError exception.

My friend Kamal Advani (thanks, man!) suggested something like multilevel-dict-solution.py to me.

Use at your own risk. I do not provide any support for this. You have my permission to copy it.

@adeishs
adeishs / content.md
Created November 7, 2022 11:23
ANSI/ISO C89/C90 Tips

I have used C for my own software development tasks. I have also taught a university-level C course (well, this isn’t really relevant, but some think otherwise. Oh, well). Here are my tips based on my observation of common mistakes (including those I’ve done myself). I hope the following tips can reduce the occurrences of those mistakes.

  1. Be sure to fclose() after a successful fopen(). Similarly for *alloc() and free().

  2. An easy way to malloc():

    ptr = malloc(NUM * sizeof *ptr);
    

    This is good as we don’t need to care about the type of ptr. Note that ptr won’t be dereferenced since it’s used as an operand to sizeof, hence you don’t need to check whether its value is NULL.

@adeishs
adeishs / gj-elim.rb
Created December 24, 2023 20:29
Gauss–Jordan Elimination
require 'bigdecimal'
def gj_elim(els)
h = 0
k = 0
m = els.size
n = els.first.size
# form row-echelon matrix
while h < m && k < n