This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashMap; | |
fn simple_assembler(program: Vec<&str>) -> HashMap<String, i64> { | |
let mut registers = HashMap::new(); | |
let mut shift = 0i64; | |
'begin: while shift < program.len().try_into().unwrap_or(0) { | |
let instruction = program[shift as usize]; | |
shift = shift + 1; | |
match instruction.split(" ").collect::<Vec<&str>>().get(..) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::LinkedList; | |
use std::iter::FromIterator; | |
fn remove_parentheses(s: &str) -> String { | |
let w: LinkedList<char> = s.chars().collect(); | |
let mut skip_count = 0; | |
String::from_iter(w.into_iter().filter_map(|l| { | |
if l == '(' { skip_count += 1 } | |
let r = if skip_count != 0 { | |
None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def rgb(r, g, b) | |
a1, b1 = split_num(r); p a1,b1 | |
a2, b2 = split_num(g); p a2,b2 | |
a3, b3 = split_num(b); p a3,b3 | |
"#{dec_to_hex(a1)}#{dec_to_hex(b1)}#{dec_to_hex(a2)}#{dec_to_hex(b2)}#{dec_to_hex(a3)}#{dec_to_hex(b3)}" | |
end | |
def split_num(num) | |
if num > 255 then num = 255 end | |
if num < 0 then num = 0 end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub fn quick_sort<T: Clone + PartialOrd>(arr: &[T]) -> Vec<T> { | |
if arr.len() <= 1 { | |
return arr.to_vec(); | |
} | |
if arr.len() == 2 { | |
let a = arr[0].clone(); | |
let b = arr[1].clone(); | |
return if a < b { vec![a, b] } else { vec![b, a] }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashMap; | |
pub fn longest_palindrome(s: String) -> String { | |
let mut poly: HashMap<String, usize> = Default::default(); | |
for i in 0..=s.len() { | |
for j in i..=s.len() { | |
let sc = &s[i..j]; | |
let len = sc.len(); | |
let mid = (len / 2) as usize; | |
let rev = sc[mid..len].chars().rev().collect::<String>(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashMap; | |
pub fn longest_palindrome(s: String) -> String { | |
let mut poly: HashMap<&str, usize> = Default::default(); | |
for substr in s.split(" ") { | |
let len = substr.len(); | |
let mid = (len / 2) as usize; | |
if substr[0..mid] == substr[mid..len].chars().rev().collect::<String>() { | |
poly.insert(substr, len); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashMap; | |
const TOP_COUNT: usize = 3; | |
type CountMap<'a> = HashMap<&'a str, usize>; | |
fn worlds_count<'a>(query: &'a str, keys: &mut CountMap<'a>) { | |
let words = query.split(" ").map(|c| c.trim()).collect::<Vec<_>>(); | |
for w in words { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(assoc_char_funcs)] | |
use std::collections::VecDeque; | |
use std::iter::FromIterator; | |
fn main() -> Result<(), failure::Error> { | |
let a = "Hello World"; | |
dbg!(a | |
.split(" ") | |
.fold(String::with_capacity(a.len()), |mut acc, w| { | |
let mut s: VecDeque<char> = w.chars().collect(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use time; | |
use std::time::{SystemTime, UNIX_EPOCH}; | |
fn main() { | |
let a = time::get_time().sec; | |
dbg!(&a); | |
let since_the_epoch = SystemTime::now().duration_since(UNIX_EPOCH) | |
.expect("Time went backwards").as_secs(); | |
println!("{:?}", since_the_epoch); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type CustomError = &'static str; | |
type Result<T> = std::result::Result<T, CustomError>; | |
fn main() -> Result<()> { | |
let val = vec![Ok(1), Ok(2),Err("Some error")]; | |
let res: Vec<i32> = val.into_iter().collect::<Result<_>>()?; // Error: "Some error" | |
println!("{:?}", res); | |
Ok(()) |