From fea74c1c49838298b2ca1c0852333d047072640a Mon Sep 17 00:00:00 2001 From: RichoKD Date: Thu, 30 Oct 2025 20:58:12 +0100 Subject: [PATCH] chore: week 2 assignment --- code/src/main.rs | 10 ++++++++++ code/src/week_2/mod.rs | 6 +++++- code/src/week_2/task_1.rs | 10 +++++++++- code/src/week_2/task_2.rs | 18 +++++++++++++----- code/src/week_2/task_3.rs | 26 ++++++++------------------ 5 files changed, 45 insertions(+), 25 deletions(-) diff --git a/code/src/main.rs b/code/src/main.rs index 4308031..cb9e797 100644 --- a/code/src/main.rs +++ b/code/src/main.rs @@ -4,6 +4,16 @@ mod week_2; // entry point to run all topics covered fn main() { // call functions from session_3 modules + println!("Task 1:"); + week_2::task_1::main(); + + println!("\nTask 2:"); + week_2::task_2::main(); + + println!("\nTask 3:"); + week_2::task_3::main(); + + println!("\nOther topics:"); week_2::function::run(); week_2::loops::run(); week_2::primitive_types::run(); diff --git a/code/src/week_2/mod.rs b/code/src/week_2/mod.rs index 1747ed4..a0ff703 100644 --- a/code/src/week_2/mod.rs +++ b/code/src/week_2/mod.rs @@ -3,4 +3,8 @@ pub mod function; pub mod loops; pub mod primitive_types; pub mod slices; -pub mod variables; \ No newline at end of file +pub mod variables; + +pub mod task_1; +pub mod task_2; +pub mod task_3; \ No newline at end of file diff --git a/code/src/week_2/task_1.rs b/code/src/week_2/task_1.rs index f47db00..b96e7c2 100644 --- a/code/src/week_2/task_1.rs +++ b/code/src/week_2/task_1.rs @@ -2,10 +2,18 @@ fn btc_value_in_usd(btc: f64, rate: f64) -> f64 { btc * rate } + +fn mine_blocks(limit: u8) { + for height in 1..=limit { + println!("Mining block #{}", height); + } +} + //main run main -fn main() { +pub fn main() { let btc = 0.25; //a dream amount let rate = 65_000.0; // example rate here let value = btc_value_in_usd(btc, rate); println!("{} BTC is worth ${} USD", btc, value); + mine_blocks(10); } diff --git a/code/src/week_2/task_2.rs b/code/src/week_2/task_2.rs index aca8ad6..fab213a 100644 --- a/code/src/week_2/task_2.rs +++ b/code/src/week_2/task_2.rs @@ -1,21 +1,29 @@ +use core::time; + fn mine_blocks(limit: u8) { let mut difficulty = 1; for height in 1..=limit { println!("Mining block #{}", height); - // Simulate difficulty - while difficulty < 3 { + let time_to_mine = difficulty * 1000; + + while difficulty < 5 { println!("Simulating difficulty level {}", difficulty); - difficulty += 1; // the number of starting zero's + // Simulate mining time + std::thread::sleep(std::time::Duration::from_millis(time_to_mine)); + break; } if height % 5 == 0 { println!("Checkpoint reached"); } + + difficulty += 1; + } } -fn main() { - mine_blocks(10); +pub fn main() { + mine_blocks(21); } diff --git a/code/src/week_2/task_3.rs b/code/src/week_2/task_3.rs index 0b0b29a..981b4ca 100644 --- a/code/src/week_2/task_3.rs +++ b/code/src/week_2/task_3.rs @@ -12,25 +12,15 @@ fn get_rpc_url(network: &Network) -> &str { } } -fn print_network_details(network: &Network) { - match network { - Network::Mainnet => println!("This is the main Bitcoin network."), - Network::Testnet => println!("This is the test Bitcoin network."), - Network::Regtest => println!("This is the regtest Bitcoin network."), - } -} - -fn main() { - let network = Network::Regtest; +pub fn main() { - print_network_details(&network); - println!("RPC URL: {}", get_rpc_url(&network)); - let network = Network::Mainnet; + let mut network = Network::Regtest; + println!("Net: {}", get_rpc_url(&network)); - print_network_details(&network); - println!("RPC URL: {}", get_rpc_url(&network)); - let network = Network::Testnet; + network = Network::Mainnet; + println!("Net: {}", get_rpc_url(&network)); - print_network_details(&network); - println!("RPC URL: {}", get_rpc_url(&network)); + network = Network::Testnet; + println!("Net: {}", get_rpc_url(&network)); + }