Skip to content

Instantly share code, notes, and snippets.

View mykhailokrainik's full-sized avatar

Mykhailo Krainik mykhailokrainik

View GitHub Profile
@mykhailokrainik
mykhailokrainik / uniq.rb
Created July 6, 2018 15:46
Implement the uniq method to remove all duplicates.
def uniq(arr)
r = [arr[0]]
arr.each do |a|
uniq = true
r.each do |n|
if a == n
uniq = false
end
end
r.push a if uniq
@mykhailokrainik
mykhailokrainik / main.rs
Created July 7, 2018 11:55 — forked from patshaughnessy/main.rs
Execute a simple SQL report using Rust and Diesel
#[macro_use]
extern crate diesel;
use diesel::prelude::*;
table! {
users (id) {
id -> Int4,
first_name -> Nullable<Varchar>,
last_name -> Nullable<Varchar>,
@mykhailokrainik
mykhailokrainik / uniq.rs
Created July 7, 2018 16:18
Delete duplicate in an Array
fn uniq(arr: &[i32]) -> Vec<i32> {
let mut res = vec![arr[0]];
for a in arr.iter().skip(1) {
let mut uniq = true;
for n in res.iter() {
if a == n {
uniq = false;
}
}
if uniq { res.push(*a) };
@mykhailokrainik
mykhailokrainik / remove_duplicate.rs
Created July 13, 2018 12:34
Remove duplicate words
// Remove duplicate words
// Your task is to remove all duplicate words from string, leaving only single words entries.
// Example:
// Input:
// 'alpha beta beta gamma gamma gamma delta alpha beta beta gamma gamma gamma delta'
// Output:
// 'alpha beta gamma delta'
fn remove_duplicate_words(s: &str) -> String {
let r = s.split(" ").fold(String::from(""), |mut r, a| {
@mykhailokrainik
mykhailokrainik / remove_duplicate.rb
Created July 13, 2018 12:45
Remove duplicate words
=begin
Your task is to remove all duplicate words from string, leaving only single words entries.
Example:
Input:
'alpha beta beta gamma gamma gamma delta alpha beta beta gamma gamma gamma delta'
Output:
@mykhailokrainik
mykhailokrainik / chess.rs
Last active July 16, 2018 14:00
Chess Board Cell Color
// Task
// Given two cells on the standard chess board, determine whether they have the same color or not.
//
// Example
// For cell1 = "A1" and cell2 = "C3", the output should be true.
const BOARD_SIZE: usize = 9;
fn chessboard_cell_color(cell1: &str, cell2: &str) -> bool {
let mut board = [false; BOARD_SIZE * BOARD_SIZE];
//! ```cargo
//! [dependencies]
//! dialoguer = "*"
//! indicatif = "*"
//! ```
extern crate dialoguer;
extern crate indicatif;
use dialoguer::Select;
@mykhailokrainik
mykhailokrainik / playground.rs
Last active September 13, 2018 07:46 — forked from rust-play/playground.rs
Rust shared structs.
trait Fruit {
fn who_am_i(&self) -> String;
}
struct Apple {
name: String,
}
struct Banana {
name: String,
@mykhailokrainik
mykhailokrainik / iterator.rs
Created September 14, 2018 09:10
impl Iterator<Item = T>
fn iterate<T>(data: Vec<T>) -> impl Iterator<Item = T> {
data.into_iter()
}
fn main() {
for x in iterate(vec!["1", "2", "3"]) {
println!("Hi! {}", x);
}
}
@mykhailokrainik
mykhailokrainik / swap.rs
Created October 2, 2018 08:24
swap two digits without use third
fn main() {
let mut x = 2;
let mut y = 1;
println!("before x {} y {}", x, y); // before x 2 y 1
if x != y {
x = x ^ y;
y = x ^ y;
x = x ^ y;