Merge pull request #263 from AnHaechan/main

fixed assignment 7, 10, 11, 12 & sync with private
This commit is contained in:
Jeehoon Kang
2023-09-12 19:01:13 +09:00
committed by GitHub
5 changed files with 44 additions and 98 deletions

View File

@@ -33,7 +33,7 @@ mod test {
let it = || (1..=5).cycle().my_zip((1..=3).cycle()).map(|(x, y)| x * y);
let take15 = vec![
2, // 1 * 1,
1, // 1 * 1,
4, // 2 * 2,
9, // 3 * 3,
4, // 4 * 1,

View File

@@ -24,8 +24,7 @@ mod test {
}
}
#[test]
fn can_every_husband_rescue_his_wife() {
fn can_every_husband_rescue_his_wife() -> bool {
// HINT: https://en.wikipedia.org/wiki/100_prisoners_problem
const WIVES: usize = 100;
@@ -37,7 +36,7 @@ mod test {
rooms
});
assert!((0..WIVES).all(|his_wife| {
(0..WIVES).all(|his_wife| {
// A new husband steps into the labyrinth to rescue his wife...!
let husband = Box::new(Husband::seeking(his_wife /*👩*/));
let strategy = Box::new(husband.has_devised_a_strategy());
@@ -57,6 +56,18 @@ mod test {
})
.is_some(/* The husband has successfully rescued his wife! 👫*/)
// or is_none(/* The unfortunate husband has encountered the Minotaur and... 🪓*/)
}));
})
}
#[test]
fn main() {
let mut num_success = 0;
for _ in 0..10000 {
if can_every_husband_rescue_his_wife() {
num_success += 1
}
}
assert!(num_success > 3000)
}
}

View File

@@ -66,7 +66,7 @@ impl<T: Debug> SinglyLinkedList<T> {
}
/// Convert the current list into a vector.
pub fn as_vec(&self) -> Vec<T> {
pub fn into_vec(self) -> Vec<T> {
todo!()
}
@@ -80,36 +80,7 @@ impl<T: Debug> SinglyLinkedList<T> {
/// # Examples
///
/// `self`: `[1, 2]`, `f`: `|x| x + 1` ==> `[2, 3]`
pub fn map<F: Fn(T) -> T>(&mut self, f: F) {
todo!()
}
/// Insert given list `another` at the specified index `idx`.
/// If `idx` is out-of-bound of `self`, append `another` at the end of `self`.
///
/// # Examples
///
/// `self`: `[1, 2]`, `another`: `[3, 4]`, `idx`: `1` ==> `[1, 3, 4, 2]`
/// `self`: `[1, 2]`, `another`: `[3, 4]`, `idx`: `5` ==> `[1, 2, 3, 4]`
pub fn insert(&mut self, another: &Self, idx: usize) {
todo!()
}
/// Reverse the list in a chunk of size `n`.
/// If `n == 0`, do nothing.
///
/// # Examples
///
/// `self`: `[1, 2, 3, 4, 5, 6, 7, 8, 9]`, `n`: `3`
/// // each chunk of size `3`: `[1, 2, 3]`, `[4, 5, 6]`, `[7, 8, 9]`
/// // reversed sequence of chunks: `[7, 8, 9]`, `[4, 5, 6]`, `[1, 2, 3]`
/// ==> `[7, 8, 9, 4, 5, 6, 1, 2, 3]`,
///
/// `self`: `[1, 2, 3, 4, 5, 6, 7, 8, 9]`, `n`: `4`
/// // each chunk of size `4`: `[1, 2, 3, 4]`, `[5, 6, 7, 8]`, `[9]`
/// // reversed sequence of chunks: `[9]`, `[5, 6, 7, 8]`, `[1, 2, 3, 4]`
/// ==> `[9, 5, 6, 7, 8, 1, 2, 3, 4]`
pub fn chunk_reverse(&mut self, n: usize) {
pub fn map<F: Fn(T) -> T>(self, f: F) -> Self {
todo!()
}
@@ -122,7 +93,10 @@ impl<T: Debug> SinglyLinkedList<T> {
/// // each adjacent pair of elements: `(1, 2)`, `(2, 3)`, `(3, 4)`
/// // apply `f` to each pair: `f(1, 2) == 3`, `f(2, 3) == 5`, `f(3, 4) == 7`
/// ==> `[3, 5, 7]`
pub fn pair_map<F: Fn(T, T) -> T>(&mut self, f: F) {
pub fn pair_map<F: Fn(T, T) -> T>(self, f: F) -> Self
where
T: Clone,
{
todo!()
}
}

View File

@@ -24,10 +24,10 @@ mod test_linked_list {
}
#[test]
fn test_from_as_vec() {
assert_eq!(SinglyLinkedList::<i32>::new().as_vec(), vec![]);
fn test_from_into_vec() {
assert_eq!(SinglyLinkedList::<i32>::new().into_vec(), vec![]);
assert_eq!(
SinglyLinkedList::from_vec(vec![1, 2, 3]).as_vec(),
SinglyLinkedList::from_vec(vec![1, 2, 3]).into_vec(),
vec![1, 2, 3]
);
}
@@ -40,68 +40,29 @@ mod test_linked_list {
#[test]
fn test_map() {
let mut list = SinglyLinkedList::from_vec(vec![1, 2, 3]);
let incr = |x: i32| x + 1;
list.map(incr);
assert_eq!(list.as_vec(), vec![2, 3, 4]);
}
#[test]
fn test_insert() {
let mut list1 = SinglyLinkedList::from_vec(vec![1, 2, 3]);
let mut list2 = SinglyLinkedList::from_vec(vec![1, 2, 3]);
let mut list3 = SinglyLinkedList::from_vec(vec![1, 2, 3]);
let list4 = SinglyLinkedList::from_vec(vec![4, 5, 6]);
list1.insert(&list4, 0);
assert_eq!(list1.as_vec(), vec![4, 5, 6, 1, 2, 3]);
list2.insert(&list4, 1);
assert_eq!(list2.as_vec(), vec![1, 4, 5, 6, 2, 3]);
list3.insert(&list4, 4);
assert_eq!(list3.as_vec(), vec![1, 2, 3, 4, 5, 6]);
}
#[test]
fn test_chunk_reverse() {
let mut list1 = SinglyLinkedList::from_vec(vec![1, 2, 3, 4, 5, 6, 7, 8, 9]);
list1.chunk_reverse(3);
assert_eq!(list1.as_vec(), vec![7, 8, 9, 3, 4, 5, 1, 2, 3]);
let mut list2 = SinglyLinkedList::from_vec(vec![1, 2, 3, 4, 5, 6, 7, 8]);
list2.chunk_reverse(3);
assert_eq!(list2.as_vec(), vec![7, 8, 4, 5, 6, 1, 2, 3]);
let mut list3 = SinglyLinkedList::from_vec(vec![1, 2, 3]);
list3.chunk_reverse(4);
assert_eq!(list3.as_vec(), vec![1, 2, 3]);
let mut list4 = SinglyLinkedList::from_vec(vec![1, 2, 3, 4]);
list4.chunk_reverse(1);
assert_eq!(list4.as_vec(), vec![4, 3, 2, 1]);
let mut list5 = SinglyLinkedList::from_vec(vec![1, 2, 3, 4]);
list4.chunk_reverse(0);
assert_eq!(list4.as_vec(), vec![1, 2, 3, 4]);
let list = SinglyLinkedList::from_vec(vec![1, 2, 3]);
let list_ = list.map(|x: i32| x + 1);
assert_eq!(list_.into_vec(), vec![2, 3, 4]);
}
#[test]
fn test_pair_map() {
let mut list = SinglyLinkedList::from_vec(vec![1, 2, 3, 4, 5, 6, 7, 8, 9]);
let add = |x: i32, y: i32| x + y;
list.pair_map(add);
assert_eq!(list.as_vec(), vec![3, 5, 7, 9, 11, 13, 15, 17]);
let list1 = SinglyLinkedList::from_vec(vec![1, 2, 3, 4, 5, 6, 7, 8, 9]).pair_map(add);
let vec1 = list1.into_vec();
assert_eq!(vec1.clone(), vec![3, 5, 7, 9, 11, 13, 15, 17]);
list.pair_map(add);
assert_eq!(list.as_vec(), vec![8, 12, 16, 20, 24, 28, 32]);
let list2 = SinglyLinkedList::from_vec(vec1).pair_map(add);
let vec2 = list2.into_vec();
assert_eq!(vec2.clone(), vec![8, 12, 16, 20, 24, 28, 32]);
list.pair_map(add);
assert_eq!(list.as_vec(), vec![20, 28, 36, 44, 52, 60]);
let list3 = SinglyLinkedList::from_vec(vec2).pair_map(add);
let vec3 = list3.into_vec();
assert_eq!(vec3.clone(), vec![20, 28, 36, 44, 52, 60]);
list.pair_map(add);
assert_eq!(list.as_vec(), vec![48, 64, 80, 96, 112]);
let list4 = SinglyLinkedList::from_vec(vec3).pair_map(add);
assert_eq!(list4.into_vec(), vec![48, 64, 80, 96, 112]);
}
#[test]
@@ -115,7 +76,7 @@ mod test_linked_list {
let list_list = SinglyLinkedList::from_vec(vec![list1, list2, list3, list4, list5]);
assert_eq!(
list_list.flatten().as_vec(),
list_list.flatten().into_vec(),
vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
);
}

View File

@@ -9,9 +9,9 @@ mod test_card {
use std::thread;
use std::time::Duration;
const NUM_CARDS: usize = 10_000;
const DURATION: u64 = 1;
const NUM_ENEMIES: usize = 100;
const NUM_CARDS: usize = 10000;
const DURATION: u64 = 20;
const NUM_ENEMIES: usize = 25;
#[derive(Clone, Debug)]
struct Card {
@@ -104,7 +104,7 @@ mod test_card {
let idx = init + (cnt % dist);
match ground.get_card_color(idx) {
Color::White => ground.flip_card(idx, Color::Blue),
Color::Blue => thread::sleep(Duration::from_micros(1)),
Color::Blue => thread::sleep(Duration::from_micros(10)),
};
cnt += 1;
}