Skip to content

Instantly share code, notes, and snippets.

View adeishs's full-sized avatar

ade ishs adeishs

View GitHub Profile
@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
@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 / 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 / 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 / pg-get-first-monday.sql
Created September 30, 2022 00:11
Get the first Monday of a year
-- get the first Monday of a year
WITH year AS (
-- change the year as you need
SELECT CAST(2022 AS INTEGER) y
),
first_date AS (
SELECT CAST(y || '-01-01' AS DATE) AS d
FROM year
)
@adeishs
adeishs / uniq-sort.pl
Created March 3, 2021 00:42
Not uniquely sorted
#!/usr/bin/env perl
use List::MoreUtils;
use Data::Dumper::Concise;
my @els = qw(e a p c x e e x);
my @not_uns = sort List::MoreUtils::uniq @els;
my @uns = sort { $a cmp $b } List::MoreUtils::uniq @els;
@adeishs
adeishs / list-slurp-fix.pl
Last active August 4, 2020 00:22
Perl list slurp
#!/usr/bin/env perl
use strict;
use List::Util qw(any);
use Data::Dumper::Concise;
my %h = (
k1 => {
s1 => 0,
s2 => 0,

Keybase proof

I hereby claim:

  • I am adeishs on github.
  • I am adeishs (https://keybase.io/adeishs) on keybase.
  • I have a public key ASDQlVBSXewGedj1Ivd9i-shtY31suJQLNx4RrpyVqvNcgo

To claim this, I am signing this object:

@adeishs
adeishs / .bash_profile
Created November 29, 2018 00:11
My .bash_profile
#!/bin/bash
# https://github.com/git/git/blob/master/contrib/completion/git-completion.bash
source ~/git-completion.bash
# https://perlbrew.pl/
source ~/perl5/perlbrew/etc/bashrc
@adeishs
adeishs / .vimrc
Last active December 1, 2018 11:00
My .vimrc
execute pathogen#infect()
colorscheme borland
set nu
set title
set guifont=Source\ Code\ Pro:h13
set termencoding=utf-8
set encoding=utf-8
set fileencoding=utf-8
set shiftwidth=4 softtabstop=4 smarttab expandtab
set colorcolumn=80