Skip to content

Instantly share code, notes, and snippets.

View Coutlaw's full-sized avatar

Cass Outlaw Coutlaw

View GitHub Profile
@Coutlaw
Coutlaw / enums_solution.rs
Last active October 26, 2022 20:47
Rustlings: Enums 1, 2 & 3 solution
// Enums 1 problem (doesn't compile)
#[derive(Debug)]
enum Message {
// TODO: define a few types of messages as used below
}
fn main() {
println!("{:?}", Message::Quit);
println!("{:?}", Message::Echo);
println!("{:?}", Message::Move);
@Coutlaw
Coutlaw / two_sum.rs
Last active May 22, 2020 10:52
Leetcode: two_sum problem in Rust
// /*
// Given an array of integers, return indices of the two numbers such that they add up to a specific target.
// You may assume that each input would have exactly one solution, and you may not use the same element twice.
// Example:
// Given nums = [2, 7, 11, 15], target = 9,
// Because nums[0] + nums[1] = 2 + 7 = 9,
@Coutlaw
Coutlaw / roman_to_int.rs
Last active May 22, 2020 14:38
leetcode: roman numerals to int value
/*
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
@Coutlaw
Coutlaw / longest_substring.rs
Last active May 22, 2020 19:15
Longest substring from a vector of strings in rust
/*
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".
Example 1:
Input: ["flower","flow","flight"]
Output: "fl"
@Coutlaw
Coutlaw / longest_common_prefix.rs
Created May 23, 2020 11:14
leetcode: longest common prefix
/*
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".
Example 1:
Input: ["flower","flow","flight"]
Output: "fl"
@Coutlaw
Coutlaw / tree_data_structure.rs
Last active March 1, 2023 00:01
Non-cyclical reference tree in Rust
use std::cell::RefCell;
use std::rc::{Rc, Weak};
// Implementation of a basic tree with out a cyclical reference, using strong and weak references
/*
* Every node is going to own its children, but share them so we can access each node directly
* to accomplish this, we make every child a Vec<T> and T is an Rc<Node> to maintain a reference count for the smart pointers
* We also need to be able to modify nodes that are children of other nodes
* to accomplish this we wrap each child Vec in RefCell<T>
* We also need to track who is the parent of the node
@Coutlaw
Coutlaw / thread_spawn.rs
Created May 27, 2020 11:00
Thread Spawn in rust
use std::thread;
use std::time::Duration;
fn main() {
// this thread will die when the main thread has ended
// but this spawned thread will start printing outputs while the main thread continues on
thread::spawn(|| {
for i in 1..10 {
println!("hi number {} from the spawned thread!", i);
thread::sleep(Duration::from_millis(1));
@Coutlaw
Coutlaw / merge_two_lists.rs
Last active August 31, 2022 01:51
Leetcode: Merge two sorted lists (Rust)
/*
Merge two sorted linked lists and return it as a new sorted list. The new list should be made by splicing together the nodes of the first two lists.
Example:
Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
*/
@Coutlaw
Coutlaw / palindrome_i32.rs
Created June 6, 2020 11:43
leetcode: can find palindrome i32 (rust)
/*
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Example 1:
Input: 121
Output: true
Example 2:
@Coutlaw
Coutlaw / i32_reverser.rs
Last active June 7, 2020 11:41
leetcode: reverse i32 preventing overflow (rust)
/*
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321
Example 2: