Skip to content

Instantly share code, notes, and snippets.

View ericwen229's full-sized avatar

ericwen229 ericwen229

  • ByteDance Inc.
  • Shenzhen, China
View GitHub Profile
@ericwen229
ericwen229 / maze.rs
Created February 10, 2024 15:50
DFS based maze generating algorithm implemented in Rust
use rand::rngs::StdRng;
use rand::seq::SliceRandom;
use rand::SeedableRng;
use std::collections::HashSet;
pub fn gen(rows: usize, cols: usize, seed: u64) -> Maze {
MazeGenerator::new(rows, cols, seed).gen()
}
pub struct Maze {
@ericwen229
ericwen229 / sudoku.rs
Last active February 10, 2024 15:50
DFS based sudoku solving algorithm implemented in Rust
use std::collections::HashSet;
use std::fmt::{Debug, Formatter};
#[derive(Clone)]
pub struct SudokuBoard {
cells: Vec<SudokuCell>,
}
#[derive(Debug, Clone)]
pub enum SudokuCell {
@ericwen229
ericwen229 / run.sh
Created November 7, 2023 07:12
Shell script to run commands at first launch in day
# run at first launch in day
LAST_RUN="$HOME/.last_run"
if [ ! -f "$LAST_RUN" ] || [ "$(cat $LAST_RUN)" != "$(date +"%Y%m%d")" ]; then
# TODO: do sth here
date +"%Y%m%d" > "$LAST_RUN"
fi
@ericwen229
ericwen229 / kth.rs
Created August 22, 2023 14:10
Distributed kth selection algorithm implemented in Rust
use rand::Rng;
use std::fmt::{Display, Formatter, Result};
pub struct Worker<T> {
arr: Vec<T>,
begin: usize,
end: usize,
}
impl<T> Display for Worker<T>
@ericwen229
ericwen229 / concurrent_run.go
Created January 16, 2023 02:33
Go implementation of concurrently running multiple processes with timeout
package main
import (
"context"
"fmt"
"sync"
"sync/atomic"
"time"
)
@ericwen229
ericwen229 / reverse_singly_linked_list.c
Last active January 16, 2023 02:33
Recursive implementation of reversing singly linked list
#include <stdio.h>
#include <stdlib.h>
typedef struct SinglyLinkedListNode {
int value;
struct SinglyLinkedListNode* next;
} SinglyLinkedListNode;
SinglyLinkedListNode* reverseSinglyLinkedList(SinglyLinkedListNode* list);
SinglyLinkedListNode* reverseSinglyLinkedListHelper(SinglyLinkedListNode* listToReverse, SinglyLinkedListNode* listToAppend);
@ericwen229
ericwen229 / pipe.c
Created March 4, 2019 03:53
A small sample program emulating several workers hitting a nail alternately using fork() and pipe()
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
void performTask(int id, int readfd, int writefd) {
FILE* readFile = fdopen(readfd, "r");
FILE* writeFile = fdopen(writefd, "w");
char requestBuffer[100];
while (fscanf(readFile, "%s", requestBuffer) != EOF) {
@ericwen229
ericwen229 / KMP.java
Created October 21, 2018 02:40
Java implementation of KMP algorithm
public class KMP {
private final String pattern;
private final int[] next;
public KMP(String _pattern) {
if (_pattern == null) {
throw new IllegalArgumentException("pattern cannot be null");
}
else if (_pattern.isEmpty()) {
@ericwen229
ericwen229 / ShortestSubsequenceInterval.java
Created August 31, 2018 14:31
Java implementation of finding the shortest interval containing given subsequence
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
public class ShortestSubsequenceInterval {
public static void main(String[] args) {
String[] words = {"*", "*", "*", "-", "*", "*", "*", "-", "*", "*", "*", "*"};
String[] keys = {"*", "*", "*", "*"};
@ericwen229
ericwen229 / BSTTraverser.java
Created August 26, 2018 14:57
Java implementation of binary search tree traversal algorithm (constant extra space)
import java.util.function.Consumer;
public class BSTTraverser {
public enum TraverseOrder {
PreOrder, InOrder,
}
public static void main(String[] args) {
BST bst = new BST();