Skip to content

Instantly share code, notes, and snippets.

View misha-krainik's full-sized avatar

Misha Krainik misha-krainik

View GitHub Profile
@misha-krainik
misha-krainik / muttononmut.rs
Created December 20, 2023 17:36
&mut Struct to Struct (Rust)
impl From<(PartialUser, UserEntry, &str)> for AuthorizedUser {
fn from((user, entry, jwt_token): (PartialUser, UserEntry, &str)) -> Self {
let join_names_fn =
|first_name: Option<String>, middle_name: Option<String>, last_name: Option<String>| {
let names = [first_name, middle_name, last_name];
names
.iter()
.filter_map(|name| name.as_ref())
.map(|name| name.to_string())
.collect::<Vec<String>>()
@misha-krainik
misha-krainik / rust-box-pin-dyn-future.rs
Created May 1, 2023 18:46
Example a future that resolves to return function value using Pin<Box<dyn Future<Output = u32>>>.
use std::pin::Pin;
use std::future::Future;
async fn async_fn() -> u32 {
42
}
fn main() {
let future = Box::pin(async_fn());
let pinned_future: Pin<Box<dyn Future<Output = u32>>> = future;
@misha-krainik
misha-krainik / quicksort.rs
Last active March 28, 2023 14:57
More faster implementation of quick sorting
fn quicksort(arr: &mut [i32]) {
if arr.len() <= 1 {
return;
}
let pivot = arr[arr.len() - 1];
let mut i = 0;
for j in 0..arr.len() - 1 {
if arr[j] < pivot {
arr.swap(i, j);
i += 1;
enum Error {
recordNotFound,
notValidId,
}
class Result<T, E> {
T? value;
E? error;
Result.Ok(T value) {
@misha-krainik
misha-krainik / binary_search.rs
Last active February 1, 2023 21:38
Implementation of the binary search algorithm (logarithmic search)
use std::cmp::Ordering;
pub fn binary_search(arr: &[i32], value: i32) -> Option<usize> {
let mut mid;
let mut low = 0;
let high = arr.len();
while low <= high {
mid = low + (high - low) / 2;
match arr.get(mid) {
Some(arr_value) => match value.cmp(&arr_value) {
def multiplication(a,b)
m = {}
while a >= 1
m[a] = b
a = a / 2
b = b * 2
end
m.select { |k, _| k % 2 != 0 }.values.sum
end
@misha-krainik
misha-krainik / example.md
Created November 4, 2022 00:42
Given an array you need to rotate its elements K number of times in Rust

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

@misha-krainik
misha-krainik / binary_search.rs
Created November 3, 2022 22:09
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
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;
@misha-krainik
misha-krainik / simple_assembler_interpreter.rs
Created October 19, 2022 18:32
Simple assembler interpreter on Rust
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(..) {
@misha-krainik
misha-krainik / parentheses.rs
Created October 19, 2022 18:29
Remove the parentheses
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