Skip to content

Commit

Permalink
-- practice refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
djnzx committed Oct 24, 2024
1 parent b1c41db commit 701d48a
Show file tree
Hide file tree
Showing 64 changed files with 669 additions and 101 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
itertools = "0.13.0"
colored = "2.1.0"
rand = "^0.8.5" # 0.8.5+, <0.9.0
time = "0.3.36"
Expand Down
12 changes: 12 additions & 0 deletions exam/0assesment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
максимальна оцінка на екзамені - 100 балів

5+ = 95-100
5 = 90-95
4 = 75-89
3 = 60-74

1. теоретичне питання - макс 20 балів
2. практичне питання №1 - макс 30 балів
3. практичне питання №2 - макс 50 балів
4. наявність GitHub репозиторію з 16 практичними завданнями - макс 20 балів
5. наявність GitHub репозиторію з 20 задачами hackerrank - макс 20 балів
219 changes: 188 additions & 31 deletions exam/q1theory.md

Large diffs are not rendered by default.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions src/lectures/lec04/a4case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,16 @@ fn change_case() {
println!("{}", upper); // ПРИВІТ
println!("{}", lower); // привіт
}

#[test]
fn test1() {
// in some cases when letter becomes lowercase
// it has more than 1 symbol
let s = "ΣΣ";
let c = 'Æ';

let cl = c.to_lowercase();
let c2 = cl.collect::<String>();
println!("{}", s.to_lowercase());
println!("{}", c2);
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod homeworks;
mod lectures;
mod practice;
mod usecases;

fn main() {
println!("Hello from \u{211D}u\x73\x74😀!");
Expand Down
18 changes: 18 additions & 0 deletions src/practice/list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
### practical lessons

1. github
2. project structure
3. TODO
4. envelope - console
5. apples_and_oranges
6. chess_board - console
7. rhombus - console
8. brackets_is_valid_simple - string/array
9. brackets_max_level - string/array
10. shoes_groups - string/array
11. counting_valleys - string/array
12. parking_dilemma - array/array
13. brackets_is_valid_full - string/array
14. char_indexes - data manipulation
15. pack-unpack - data manipulation
16. 5317^1000000000
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions src/practice/p04/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod envelope1;
mod envelope2;
Binary file added src/practice/p04/slides1.pdf
Binary file not shown.
Binary file added src/practice/p04/slides2.pdf
Binary file not shown.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/practice/p05/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
mod envelope;
mod apple_and_oranges;
Binary file modified src/practice/p05/slides.pdf
Binary file not shown.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/practice/p06/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
mod envelope;
mod chessboard;
Binary file modified src/practice/p06/slides.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion src/practice/p07/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
mod apple_and_oranges;
mod rhombus;
File renamed without changes.
Binary file modified src/practice/p07/slides.pdf
Binary file not shown.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/practice/p08/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
mod chessboard;
mod brackets_is_valid;
Binary file removed src/practice/p08/slides.pdf
Binary file not shown.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/practice/p09/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
mod rhombus;
mod brackets_max_level;
Binary file removed src/practice/p09/slides.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion src/practice/p10/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
mod brackets_is_valid;
mod shoes_groups;
File renamed without changes.
File renamed without changes.
File renamed without changes.
65 changes: 65 additions & 0 deletions src/practice/p11/counting_valleys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/// https://www.hackerrank.com/challenges/counting-valleys/problem
fn countingValleys(steps: i32, path: &str) -> i32 {
let mut n = 0;
let mut p = 0;

for c in path.chars() {
let l = match c {
'U' => p + 1,
'D' => p - 1,
_ => continue,
};
if l == 0 && p < 0 {
n += 1;
}
p = l;
}

n
}

fn countingValleys2(steps: i32, path: &str) -> i32 {
struct State {
level: i32,
count: i32,
}

fn is_crossed(prev_level: i32, curr_level: i32) -> bool {
prev_level < 0 && curr_level == 0
}

impl State {
fn new(level: i32, count: i32) -> State {
State { level, count }
}
fn fresh() -> State {
State::new(0, 0)
}
fn next(&self, cur_level: i32, crossed: bool) -> State {
let delta = if crossed { 1 } else { 0 };
State::new(cur_level, self.count + delta)
}
fn up(&self) -> State {
let cur_level = self.level + 1;
self.next(cur_level, is_crossed(self.level, cur_level))
}
fn down(&self) -> State {
let cur_level = self.level - 1;
self.next(cur_level, is_crossed(self.level, cur_level))
}
}

fn logic(st: State, c: char) -> State {
match c {
'U' => st.up(),
'D' => st.down(),
_ => st,
}
}

let s = path
.chars()
.fold(State::fresh(), logic);

s.count
}
3 changes: 1 addition & 2 deletions src/practice/p11/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
mod brackets_max_level;
mod shoes_groups;
mod counting_valleys;
Binary file added src/practice/p11/slides.pdf
Binary file not shown.
50 changes: 0 additions & 50 deletions src/practice/p12/counting_valleys.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/practice/p12/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
mod counting_valleys;
mod parking_dilemma;
Binary file added src/practice/p12/parking_dilemma.pdf
Binary file not shown.
56 changes: 49 additions & 7 deletions src/practice/p12/parking_dilemma.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,60 @@
fn car_parking_roof(cars: &[usize], k: usize) -> usize {
use std::cmp::min;

fn car_parking_roof1(cars: &[u32], k: usize) -> u32 {
let mut cars = cars.to_vec();
cars.sort();

let roof_size = |i1: usize, i2: usize| -> u32 { cars[i2] - cars[i1] + 1 };

let mut m = roof_size(0, k - 1);

println!("{:?}", cars);

for idx in 0..cars.len() - k + 1 {
let s = roof_size(idx, idx + (k - 1));
m = min(m, s);
println!("i: {idx}, size: {s}, min:{m}");
}

m
}

fn car_parking_roof2(cars: &[usize], k: usize) -> usize {
let mut cars = cars.to_vec();
cars.sort_unstable();
(0..(cars.len() - k))
.map(|idx| cars[idx + k - 1] - cars[idx] + 1)
let roof_size = |idx: usize| cars[idx + k - 1] - cars[idx] + 1;
(0..(cars.len() - k + 1))
.map(roof_size)
.min()
.unwrap()
}

fn car_parking_roof3(cars: &[u32], k: usize) -> u32 {
let mut cars = cars.to_vec();
cars.sort();
let roof_size = |i: (usize, usize)| cars[i.0].abs_diff(cars[i.1]) + 1;
let it1 = 0..;
let it2 = (k - 1)..cars.len();
it1.zip(it2)
.map(roof_size)
.min()
.unwrap()
}

#[test]
fn car_parking_roof_test() {
fn car_parking_roof_test1() {
let cars = [6, 2, 12, 7];
let k = 3;
let roof_expected = 6;
let roof_real = car_parking_roof2(&cars, k);
assert_eq!(roof_expected, roof_real);
}

let roof_size = car_parking_roof(&cars, k);
let expected = 6;
assert_eq!(expected, roof_size);
#[test]
fn car_parking_roof_test2() {
let cars = [6, 2, 12, 7, 20, 25, 26, 28];
let k = 3;
let roof_expected = 4;
let roof_real = car_parking_roof3(&cars, k);
assert_eq!(roof_expected, roof_real);
}
93 changes: 93 additions & 0 deletions src/practice/p13/brackets_full.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use std::collections::{HashMap, HashSet};

/// straightforward
fn brackets_is_valid_full(s: String) -> bool {
let pairs = HashMap::from([('}', '{'), (')', '('), (']', '['), ('>', '<')]);
let open = HashSet::<char>::from_iter("{[(<".chars());
let close = HashSet::<char>::from_iter("}])>".chars());

let mut stack = Vec::new();
for c in s.chars() {
match c {
c if open.contains(&c) => stack.push(c),
c if close.contains(&c) => match stack.pop() {
None => return false,
Some(top) => match pairs.get(&c) {
Some(&op) if op == top => (),
Some(_) | None => return false,
},
},
_ => continue,
}
}

stack.is_empty()
}

/// more functional
fn brackets_is_valid_full2(s: String) -> bool {
let pairs = HashMap::from([('}', '{'), (')', '('), (']', '['), ('>', '<')]);
let open = HashSet::<char>::from_iter("{[(<".chars());
let close = HashSet::<char>::from_iter("}])>".chars());

enum State {
Valid(Vec<char>),
Invalid,
}

impl State {
fn new() -> Self {
State::Valid(Vec::new())
}
}

let process = |mut st: State, c: char| match st {
State::Valid(ref mut stack) => match c {
c if open.contains(&c) => {
stack.push(c);
st
}
c if close.contains(&c) => match stack.pop() {
Some(top) => match pairs.get(&c) {
Some(&op) if op == top => st,
_ => State::Invalid,
},
_ => State::Invalid,
},
_ => st,
},
_ => State::Invalid,
};

let s = s
.chars()
.fold(State::new(), process);

match s {
State::Valid(stack) => stack.is_empty(),
State::Invalid => false,
}
}

#[test]
fn is_valid_test2() {
let test_data = [
//input expected
("{}", true),
("[{(<>()[])}<>]", true),
("{}{}", true),
("{{}}", true),
("{{}{}}", true),
("{{}", false),
("{}}", false),
("}{", false),
("{", false),
("}", false),
("<}", false),
];

for (s, r) in test_data {
assert_eq!(brackets_is_valid_full(s.to_string()), r);
assert_eq!(brackets_is_valid_full2(s.to_string()), r);
}
}
Empty file.
Loading

0 comments on commit 701d48a

Please sign in to comment.