Skip to content

Instantly share code, notes, and snippets.

@AnthonyMikh
AnthonyMikh / main.rs
Created October 19, 2020 20:25
(Ab)using custom Deref implementations
use std::ops::{Deref, DerefMut};
use std::cell::Cell;
#[derive(Default)]
struct Simple {
field: u32,
}
impl Simple {
fn new(field: u32) -> Self {
@AnthonyMikh
AnthonyMikh / main.rs
Created October 10, 2020 10:49
Text justification (solution for Leetcode problem). Copid from submission verbatim.
impl Solution {
pub fn full_justify(words: Vec<String>, max_width: i32) -> Vec<String> {
let mut ret = Vec::new();
let width = max_width as usize;
let mut words = &words[..];
while let Some((first, rest)) = split_by_width(&mut words, width) {
if words.is_empty() {
ret.push(pack_left_justified(width, first, rest));
} else{
ret.push(pack(width, first, rest));
@AnthonyMikh
AnthonyMikh / main.rs
Created October 2, 2020 20:27
Totally safe singly linked list in Rust
struct Node<T> {
item: T,
next: NextNode<T>,
}
type NextNode<T> = Option<Box<Node<T>>>;
impl<T> Node<T> {
fn replace_tail(&mut self, new_tail: NextNode<T>) -> NextNode<T> {
std::mem::replace(&mut self.next, new_tail)
#![feature(trace_macros)]
trace_macros!(true);
macro_rules! assert_ct_not_equal {
($a:tt, $b:tt) => {
#[deny(const_err)]
const _: () = {
macro_rules! is_a {
($a) => { true };
@AnthonyMikh
AnthonyMikh / main.rs
Created July 19, 2020 12:00
Solution for "Add binary" Leetcode problem
impl Solution {
pub fn add_binary(a: String, b: String) -> String {
enum These {
Both(u8, u8),
Single(u8),
}
use These::*;
let len = a.len().max(b.len()) + 1;
let mut a = a.as_bytes().iter().rev().copied();
#include <array>
#include <memory>
#include <functional>
#include <optional>
class TrieNode {
public:
std::array<std::unique_ptr<TrieNode>, 26> next = {};
bool has_word_end = false;
};
@AnthonyMikh
AnthonyMikh / main.rs
Last active June 28, 2020 09:14
Generation of vizualisation of problematic test case for "Cheapest Flights Within K Stops" Leetcode problem
fn main() {
let width = 9_usize;
let total = width * width + 2;
let len = width * width * (width - 1) + width * 2;
let mut out = Vec::with_capacity(len);
for i in 0..width {
out.push([0, i + 1, 100]);
}
for layer in 0..width - 1 {
let base = layer * width + 1;
@AnthonyMikh
AnthonyMikh / lib.rs
Created June 24, 2020 09:59
Solution to "Single Number II" Leetcode problem
impl Solution {
pub fn single_number(nums: Vec<i32>) -> i32 {
let compressed = nums.iter()
.map(|&n| evenly_space_bits(n as u32))
.fold(0, |acc, n| omit_threes(acc + n));
unspace_bits(compressed) as _
}
}
fn evenly_space_bits(n: u32) -> u64 {
@AnthonyMikh
AnthonyMikh / main.rs
Last active June 27, 2020 11:55
Generation of problematic test case for "Cheapest Flights Within K Stops" Leetcode problem
fn main() {
let width = 9_usize;
let total = width * width + 2;
let len = width * width * (width - 1) + width * 2;
let mut out = Vec::with_capacity(len);
for i in 0..width {
out.push([0, i + 1, 100]);
}
for layer in 0..width - 1 {
let base = layer * width + 1;
@AnthonyMikh
AnthonyMikh / testcase.txt
Last active June 27, 2020 11:55
Problematic test case for "Cheapest Flights Within K Stops" Leetcode problem
83
[[0, 1, 100], [0, 2, 100], [0, 3, 100], [0, 4, 100], [0, 5, 100], [0, 6, 100], [0, 7, 100], [0, 8, 100], [0, 9, 100], [1, 10, 100], [1, 11, 100], [1, 12, 100], [1, 13, 100], [1, 14, 100], [1, 15, 100], [1, 16, 100], [1, 17, 100], [1, 18, 100], [2, 10, 100], [2, 11, 100], [2, 12, 100], [2, 13, 100], [2, 14, 100], [2, 15, 100], [2, 16, 100], [2, 17, 100], [2, 18, 100], [3, 10, 100], [3, 11, 100], [3, 12, 100], [3, 13, 100], [3, 14, 100], [3, 15, 100], [3, 16, 100], [3, 17, 100], [3, 18, 100], [4, 10, 100], [4, 11, 100], [4, 12, 100], [4, 13, 100], [4, 14, 100], [4, 15, 100], [4, 16, 100], [4, 17, 100], [4, 18, 100], [5, 10, 100], [5, 11, 100], [5, 12, 100], [5, 13, 100], [5, 14, 100], [5, 15, 100], [5, 16, 100], [5, 17, 100], [5, 18, 100], [6, 10, 100], [6, 11, 100], [6, 12, 100], [6, 13, 100], [6, 14, 100], [6, 15, 100], [6, 16, 100], [6, 17, 100], [6, 18, 100], [7, 10, 100], [7, 11, 100], [7, 12, 100], [7, 13, 100], [7, 14, 100], [7, 15, 100], [7, 16, 100], [7, 17, 100], [7, 18, 100], [8, 10, 100], [8, 11,