Skip to content

Instantly share code, notes, and snippets.

Avatar

Mykhailo Krainik mikhail-krainik

View GitHub Profile
@mikhail-krainik
mikhail-krainik / top_search.rs
Last active Nov 4, 2022
Find 3 most popular keywords from user search history #hash #rust #search
View top_search.rs
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 {
@mikhail-krainik
mikhail-krainik / quick_sort.rs
Created Oct 19, 2022
Quicksort algorithm implemented on rust
View quick_sort.rs
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] };
@mikhail-krainik
mikhail-krainik / example.md
Created Nov 4, 2022
Given an array you need to rotate its elements K number of times in Rust
View example.md

For example, an array [10,20,30,40,50,60] rotate by 2 positions to [30,40,50,60,10,20]

@mikhail-krainik
mikhail-krainik / binary_search.rs
Created Nov 3, 2022
Given a sorted array of size N and an integer K, find the position at which K is present in the array using binary search in Rust
View binary_search.rs
struct Solution;
impl Solution {
fn new() -> Solution { Self }
fn binary_search(&self, arr: &[i32], n: usize, k: i32) -> i32 {
let mut low = 0;
let mut high = n - 1;
while low <= high {
let mid = low + (high - low) / 2;
@mikhail-krainik
mikhail-krainik / simple_assembler_interpreter.rs
Created Oct 19, 2022
Simple assembler interpreter on Rust
View simple_assembler_interpreter.rs
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(..) {
@mikhail-krainik
mikhail-krainik / parentheses.rs
Created Oct 19, 2022
Remove the parentheses
View parentheses.rs
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
@mikhail-krainik
mikhail-krainik / rgb4hex.rb
Created Oct 19, 2022
RGB To Hex Conversion
View rgb4hex.rb
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
@mikhail-krainik
mikhail-krainik / Longest-Palindromic-Substring.rs
Last active Oct 19, 2022
Given a string s, return the longest palindromic substring in s. O(n^2) brute force solution. This is a very slow solution instead use dynamic programming and constructed intersection tables. Here are examples of how to do it optimally https://leetcode.com/problems/longest-palindromic-substring/solutions/127837/longest-palindromic-substring/
View Longest-Palindromic-Substring.rs
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>();
@mikhail-krainik
mikhail-krainik / palindrome.rs
Last active Oct 19, 2022
Find the longest palindrome in a string. #Rust #Polindrome
View palindrome.rs
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);
}
View kata-5848565e273af816fb000449.rs
#![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();