-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
59 changed files
with
370 additions
and
63 deletions.
There are no files selected for viewing
This file contains 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
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.
This file contains 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
This file contains 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
This file contains 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
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.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mod envelope1; | ||
mod envelope2; |
Binary file not shown.
Binary file not shown.
File renamed without changes.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
mod envelope; | ||
mod apple_and_oranges; |
Binary file not shown.
File renamed without changes.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
mod envelope; | ||
mod chessboard; |
Binary file not shown.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
mod apple_and_oranges; | ||
mod rhombus; |
File renamed without changes.
Binary file not shown.
File renamed without changes.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
mod chessboard; | ||
mod brackets_is_valid; |
Binary file not shown.
File renamed without changes.
File renamed without changes.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
mod rhombus; | ||
mod brackets_max_level; |
Binary file not shown.
This file contains 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
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.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/// https://www.hackerrank.com/challenges/counting-valleys/problem | ||
fn countingValleys1(steps: i32, path: &str) -> i32 { | ||
struct State { | ||
level: i32, | ||
count: i32, | ||
} | ||
|
||
impl State { | ||
fn new(level: i32, count: i32) -> State { | ||
State { level, count } | ||
} | ||
fn fresh() -> Self { | ||
Self::new(0, 0) | ||
} | ||
fn calc_delta(prev_level: i32, curr_level: i32) -> i32 { | ||
match curr_level { | ||
0 if prev_level < 0 => 1, | ||
_ => 0, | ||
} | ||
} | ||
fn up(&self) -> Self { | ||
let curr_level = self.level + 1; | ||
let delta_count = Self::calc_delta(self.level, curr_level); | ||
State::new(curr_level, self.count + delta_count) | ||
} | ||
fn down(&self) -> Self { | ||
let curr_level = self.level - 1; | ||
let delta_count = Self::calc_delta(self.level, curr_level); | ||
State::new(curr_level, self.count + delta_count) | ||
} | ||
fn process(&self, c: char) -> State { | ||
match c { | ||
'U' => self.up(), | ||
'D' => self.down(), | ||
_ => panic!("wrong char"), | ||
} | ||
} | ||
} | ||
|
||
path.chars() | ||
.fold(State::fresh(), |st, c| st.process(c)) | ||
.count | ||
} | ||
|
||
fn countingValleys2(steps: i32, path: &str) -> i32 { | ||
let mut prev_level = 0; | ||
let mut count = 0; | ||
for c in path.chars() { | ||
let curr_level = match c { | ||
'U' => prev_level + 1, | ||
'D' => prev_level - 1, | ||
_ => prev_level, | ||
}; | ||
let delta = match curr_level { | ||
0 if prev_level < 0 => 1, | ||
_ => 0, | ||
}; | ||
prev_level = curr_level; | ||
count += delta; | ||
} | ||
count | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
mod brackets_max_level; | ||
mod shoes_groups; | ||
mod counting_valleys; |
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
mod counting_valleys; | ||
mod parking_dilemma; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use std::collections::{HashMap, HashSet}; | ||
|
||
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() | ||
} | ||
|
||
#[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); | ||
} | ||
} |
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use itertools::Itertools; | ||
use std::collections::HashMap; | ||
|
||
fn char_indexes(text: &String) -> HashMap<char, Vec<usize>> { | ||
let mut outcome: HashMap<char, Vec<usize>> = HashMap::new(); | ||
text.chars() | ||
.into_iter() | ||
.zip(1..) | ||
.filter(|&(c, _)| c.is_alphabetic()) | ||
.for_each(|(c, idx)| { | ||
outcome | ||
.entry(c.to_ascii_lowercase()) | ||
.or_insert_with(Vec::new) | ||
.push(idx); | ||
}); | ||
|
||
outcome | ||
} | ||
|
||
// with help of https://docs.rs/itertools/latest/itertools/ | ||
fn char_indexes2(text: &String) -> HashMap<char, Vec<usize>> { | ||
text.chars() | ||
.into_iter() | ||
.zip(1..) | ||
.filter(|&(c, _)| c.is_alphabetic()) | ||
.map(|(c, idx)| (c.to_ascii_lowercase(), idx)) | ||
.into_group_map() | ||
} | ||
|
||
#[test] | ||
fn char_indexes_test() { | ||
let s = String::from("Hello, world!"); | ||
|
||
let indexes1 = char_indexes(&s); | ||
println!("{:?}", indexes1); | ||
|
||
let indexes2 = char_indexes2(&s); | ||
println!("{:?}", indexes2); | ||
|
||
assert_eq!(indexes1, indexes2); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
mod char_coordinates; | ||
mod char_indexes; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
mod permutations; | ||
mod pack_unpack; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// fold-left implementation | ||
fn pack2<A: Clone + PartialEq>(xs: &Vec<A>) -> Vec<(A, usize)> { | ||
let one = |a: &A| (a.clone(), 1); | ||
|
||
xs.iter() | ||
.fold(Vec::<(A, usize)>::with_capacity(xs.len()), |mut acc, a| { | ||
match acc.last() { | ||
// start | ||
None => vec![one(a)], | ||
// continue with the same element | ||
Some((aa, c)) if a == aa => { | ||
acc.last_mut() | ||
.unwrap() | ||
.1 = c + 1; | ||
acc | ||
} | ||
// continue with the different element | ||
Some(_) => { | ||
acc.push(one(a)); | ||
acc | ||
} | ||
} | ||
}) | ||
} | ||
|
||
// iterator-based implementation | ||
fn pack1<A: Clone + PartialEq>(xs: &Vec<A>) -> Vec<(A, usize)> { | ||
let mut iter = xs.iter(); | ||
let mut cur: Option<(A, usize)> = None; | ||
let mut acc = Vec::<(A, usize)>::new(); | ||
|
||
loop { | ||
match (iter.next(), &cur) { | ||
// finish with empty current | ||
(None, None) => break acc, | ||
// finish with non-empty current | ||
(None, Some((v, count))) => { | ||
acc.push((v.clone(), *count)); | ||
break acc; | ||
} | ||
// start | ||
(Some(a), None) => cur = Some((a.clone(), 1)), | ||
// continue with the same element | ||
(Some(a), Some((v, count))) if a == v => cur = Some((a.clone(), count + 1)), | ||
// continue with the different element | ||
(Some(a), Some((v, count))) => { | ||
acc.push((v.clone(), *count)); | ||
cur = Some((a.clone(), 1)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn unpack<A: Clone + PartialEq>(xs: &Vec<(A, usize)>) -> Vec<A> { | ||
xs.iter() | ||
.flat_map(|&(ref a, ref count)| vec![a.clone(); *count]) | ||
.collect() | ||
} | ||
|
||
#[test] | ||
fn pack_test() { | ||
let test_cases: Vec<(&str, Vec<(char, usize)>)> = vec![ | ||
("", vec![]), | ||
("a", vec![('a', 1)]), | ||
("aa", vec![('a', 2)]), | ||
("ab", vec![('a', 1), ('b', 1)]), | ||
("aab", vec![('a', 2), ('b', 1)]), | ||
("abb", vec![('a', 1), ('b', 2)]), | ||
("abba", vec![('a', 1), ('b', 2), ('a', 1)]), | ||
( | ||
"abbcccaabc", | ||
vec![('a', 1), ('b', 2), ('c', 3), ('a', 2), ('b', 1), ('c', 1)], | ||
), | ||
]; | ||
|
||
test_cases | ||
.iter() | ||
.for_each(|(s_unpacked, packed)| { | ||
let unpacked = s_unpacked | ||
.chars() | ||
.collect::<Vec<_>>(); | ||
|
||
// pack V1 | ||
let packed2 = pack1(&unpacked); | ||
assert_eq!(packed2, *packed); | ||
|
||
// pack V2 | ||
let packed2 = pack2(&unpacked); | ||
assert_eq!(packed2, *packed); | ||
|
||
// unpack | ||
let unpacked2 = unpack(&packed); | ||
assert_eq!(unpacked2, unpacked); | ||
}); | ||
} |
Empty file.
Oops, something went wrong.