Skip to content

Instantly share code, notes, and snippets.

View dimitrilw's full-sized avatar

Dimitri dimitrilw

  • Colorado
  • 16:42 (UTC -07:00)
View GitHub Profile
@dimitrilw
dimitrilw / vec_to_list.rs
Last active March 15, 2024 23:24
convert vector of i32s into a LeetCode linked list
impl Solution {
// ...stuff...
// Returns a LeetCode linked list from the given vec.
fn vec_to_list(vec: Vec<i32>) -> Option<Box<ListNode>> {
let mut root: Option<Box<ListNode>> = None;
let mut node = &mut root;
for v in vec {
node.replace(Box::new(ListNode::new(v)));
if let Some(n) = node {
@dimitrilw
dimitrilw / enum.go
Created November 26, 2023 16:09
Go (golang) enum
package main
import "fmt" // for demo, below
type Weekday int
const (
Sunday Weekday = iota
Monday
Tuesday
@dimitrilw
dimitrilw / builder-rs.rs
Last active September 21, 2023 18:43
playing with Struct construction patterns via derive-Default & the typed_builder crate
/* Cargo.toml
[package]
name = "builder-rs"
version = "0.1.0"
edition = "2021"
[dependencies]
typed-builder = "0.16.1"
@dimitrilw
dimitrilw / multi-line.rs
Last active September 21, 2023 01:12
Rust multiline string abuse
fn main() {
println!(
"{}",
" this is a test of multi-line string, with indents removed\n\
AND also of using an invisible char (\\u{200b}) to anchor\n\
the start of text, which allows an indented bullet-list\n\
\u{200b} - this is a zero-width space\n\
\u{200b} - this should be indented 4\n\
".trim(),
);
@dimitrilw
dimitrilw / map-sum-vs-fold.rs
Created September 15, 2023 17:12
Rust map-sum vs fold
/* map-sum vs fold
This code uses map-sum instead of fold. I find map-sum more readable.
In addition, they compile to the same instructions, thus performance
should also be identical.
Ref: https://godbolt.org/z/v73rW3rdd
*/
/// Determine whether the given number is an Armstrong number.
///
/// An Armstrong number is a number that is the sum of its own digits,
@dimitrilw
dimitrilw / dsu.rs
Created September 15, 2023 15:19
Rust Disjoint Set Union (DSU)
mod dsu {
pub struct dsu {
parents: Vec<usize>,
ranks: Vec<usize>,
}
impl dsu {
pub fn new(size: usize) -> Self {
Self {
parents: (0..size).collect(),
@dimitrilw
dimitrilw / github-emoji.md
Last active September 6, 2023 00:51 — forked from rxaviers/gist:7360908
Complete list of github markdown emoji markup

People

:bowtie: :bowtie: 😄 :smile: 😆 :laughing:
😊 :blush: 😃 :smiley: ☺️ :relaxed:
😏 :smirk: 😍 :heart_eyes: 😘 :kissing_heart:
😚 :kissing_closed_eyes: 😳 :flushed: 😌 :relieved:
😆 :satisfied: 😁 :grin: 😉 :wink:
😜 :stuck_out_tongue_winking_eye: 😝 :stuck_out_tongue_closed_eyes: 😀 :grinning:
😗 :kissing: 😙 :kissing_smiling_eyes: 😛 :stuck_out_tongue:
@dimitrilw
dimitrilw / bitcount.go
Created August 18, 2023 15:31
Go (golang) int to bitcount
func bitCount(n int) (res int) {
for n > 0 {
if n & 1 == 1 { res++ }
n >>= 1
}
return
}
@dimitrilw
dimitrilw / primeFactors.go
Created August 18, 2023 15:11
Go (golang) prime factors
func PrimeFactors(n int) (res []int) {
// keep dividing by 2 until we arrive at n is odd
for n%2 == 0 {
res = append(res, 2)
n = n / 2
}
// n is now odd & cannot be even
for i := 3; i*i <= n; i = i + 2 {
for n%i == 0 {
@dimitrilw
dimitrilw / memo.go
Last active August 11, 2023 15:01
Go (golang) modular function cache
/* memoization outside of the function being memoized
The value of this pattern is the modularity of the memoization.
This means that if we change it from a Go map to an external
DB store, then it's just the memo function that needs editing.
Does NOT work with recursive functions.
*/
type FnInt_Int func(int) int