From f921ec982f1a8d9020ec9316437afb6eb6eaa28b Mon Sep 17 00:00:00 2001 From: soma-enyi Date: Wed, 29 Oct 2025 15:59:45 +0100 Subject: [PATCH 1/2] feat:completed the week 2 assignment --- code/src/main.rs | 3 +++ code/src/week_2/mod.rs | 5 +++- code/src/week_2/soma_task_1.rs | 48 ++++++++++++++++++++++++++++++++++ code/src/week_2/soma_task_2.rs | 18 +++++++++++++ code/src/week_2/soma_task_3.rs | 36 +++++++++++++++++++++++++ 5 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 code/src/week_2/soma_task_1.rs create mode 100644 code/src/week_2/soma_task_2.rs create mode 100644 code/src/week_2/soma_task_3.rs diff --git a/code/src/main.rs b/code/src/main.rs index 4308031..e6de4ca 100644 --- a/code/src/main.rs +++ b/code/src/main.rs @@ -7,4 +7,7 @@ fn main() { week_2::function::run(); week_2::loops::run(); week_2::primitive_types::run(); + week_2::soma_task_1::main(); + week_2::soma_task_2::mine_blocks(10); + week_2::soma_task_3::main() } \ No newline at end of file diff --git a/code/src/week_2/mod.rs b/code/src/week_2/mod.rs index 1747ed4..8dd00db 100644 --- a/code/src/week_2/mod.rs +++ b/code/src/week_2/mod.rs @@ -3,4 +3,7 @@ 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 soma_task_1; +pub mod soma_task_2; +pub mod soma_task_3; \ No newline at end of file diff --git a/code/src/week_2/soma_task_1.rs b/code/src/week_2/soma_task_1.rs new file mode 100644 index 0000000..0cadb60 --- /dev/null +++ b/code/src/week_2/soma_task_1.rs @@ -0,0 +1,48 @@ +// // Function that takes BTC amount and exchange rate, returns USD value +// fn btc_value_in_usd(btc: f64, rate: f64) -> f64 { +// { +// btc * rate +// } +// } + +// fn main() { + +// let btc_amount = 1.0; // I own 1.0 BTC +// let exchange_rate = 65000.0; // Current price of BTC: $65,000 per BTC + +// // Calculate USD value +// let usd_value = btc_value_in_usd(btc_amount, exchange_rate); + +// println!("{} BTC = ${:.2} USD", btc_amount, usd_value); +// } + +// Function: btc_value_in_usd +// Takes amount of Bitcoin (btc) and current USD rate per BTC (rate) +// Returns the total value in USD using an expression block +fn btc_value_in_usd(btc: f64, rate: f64) -> f64 { + // Expression block: the result of btc * rate is automatically returned + // No semicolon means this is the return value + btc * rate +} + +pub fn main() { + // Example usage with different values + let btc_amount: f64 = 0.5; // You own 0.5 BTC + let usd_per_btc: f64 = 65000.0; // Current price: $65,000 per BTC + + // Call the function + let value_in_usd = btc_value_in_usd(btc_amount, usd_per_btc); + + // Print result with formatting + println!("{btc_amount} BTC at ${usd_per_btc} per BTC = ${value_in_usd}"); + + // Demonstrate with another value using direct expression + println!( + "2.3 BTC is worth ${:.2}", + btc_value_in_usd(2.3, 68000.0) + ); + + // Show that we can use complex expressions inside the function if needed + let portfolio_value = btc_value_in_usd(1.0, 70000.0) + btc_value_in_usd(0.25, 70000.0); + println!("Total portfolio: ${portfolio_value}"); +} \ No newline at end of file diff --git a/code/src/week_2/soma_task_2.rs b/code/src/week_2/soma_task_2.rs new file mode 100644 index 0000000..7b67306 --- /dev/null +++ b/code/src/week_2/soma_task_2.rs @@ -0,0 +1,18 @@ +pub fn mine_blocks(limit: u8) { + let mut height = 1; + let mut difficulty = 1; + + while height <= limit { + println!("Mining block #{}, Difficulty: {}", height, difficulty); + + // Check for checkpoints every 5 blocks + if height % 5 == 0 { + println!("Checkpoint reached!"); + + // Increase difficulty after each checkpoint + difficulty += 1; + } + + height += 1; + } +} \ No newline at end of file diff --git a/code/src/week_2/soma_task_3.rs b/code/src/week_2/soma_task_3.rs new file mode 100644 index 0000000..7353608 --- /dev/null +++ b/code/src/week_2/soma_task_3.rs @@ -0,0 +1,36 @@ +enum Network { + Mainnet, + Testnet, + Regtest, +} + +fn get_rpc_url(network: &Network) -> &str { + match network { + Network::Mainnet => "https://mainnet.example.com", + Network::Testnet => "https://testnet.example.com", + Network::Regtest => "http://localhost:8332", + } +} + +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."), + } +} + +pub fn main() { + let network = Network::Regtest; + + print_network_details(&network); + println!("RPC URL: {}", get_rpc_url(&network)); + let network = Network::Mainnet; + + print_network_details(&network); + println!("RPC URL: {}", get_rpc_url(&network)); + let network = Network::Testnet; + + print_network_details(&network); + println!("RPC URL: {}", get_rpc_url(&network)); +} From acc3b3d50de157d0ead5a14b9c01bab3633c3003 Mon Sep 17 00:00:00 2001 From: soma-enyi Date: Wed, 29 Oct 2025 16:09:31 +0100 Subject: [PATCH 2/2] feat:completed the week 2 assignment --- code/src/week_2/soma_task_1.rs | 54 ++++++++-------------------------- 1 file changed, 12 insertions(+), 42 deletions(-) diff --git a/code/src/week_2/soma_task_1.rs b/code/src/week_2/soma_task_1.rs index 0cadb60..25e6a2a 100644 --- a/code/src/week_2/soma_task_1.rs +++ b/code/src/week_2/soma_task_1.rs @@ -1,48 +1,18 @@ -// // Function that takes BTC amount and exchange rate, returns USD value -// fn btc_value_in_usd(btc: f64, rate: f64) -> f64 { -// { -// btc * rate -// } -// } +// Function that takes BTC amount and exchange rate, returns USD value +fn btc_value_in_usd(btc: f64, rate: f64) -> f64 { + { + btc * rate + } +} -// fn main() { +pub fn main() { -// let btc_amount = 1.0; // I own 1.0 BTC -// let exchange_rate = 65000.0; // Current price of BTC: $65,000 per BTC + let btc_amount = 1.0; // I own 1.0 BTC + let exchange_rate = 65000.0; // Current exchange_rate of BTC: $65,000 per BTC -// // Calculate USD value -// let usd_value = btc_value_in_usd(btc_amount, exchange_rate); + // Calculate USD value + let usd_value = btc_value_in_usd(btc_amount, exchange_rate); -// println!("{} BTC = ${:.2} USD", btc_amount, usd_value); -// } - -// Function: btc_value_in_usd -// Takes amount of Bitcoin (btc) and current USD rate per BTC (rate) -// Returns the total value in USD using an expression block -fn btc_value_in_usd(btc: f64, rate: f64) -> f64 { - // Expression block: the result of btc * rate is automatically returned - // No semicolon means this is the return value - btc * rate + println!("{} BTC = ${:.2} USD", btc_amount, usd_value); } -pub fn main() { - // Example usage with different values - let btc_amount: f64 = 0.5; // You own 0.5 BTC - let usd_per_btc: f64 = 65000.0; // Current price: $65,000 per BTC - - // Call the function - let value_in_usd = btc_value_in_usd(btc_amount, usd_per_btc); - - // Print result with formatting - println!("{btc_amount} BTC at ${usd_per_btc} per BTC = ${value_in_usd}"); - - // Demonstrate with another value using direct expression - println!( - "2.3 BTC is worth ${:.2}", - btc_value_in_usd(2.3, 68000.0) - ); - - // Show that we can use complex expressions inside the function if needed - let portfolio_value = btc_value_in_usd(1.0, 70000.0) + btc_value_in_usd(0.25, 70000.0); - println!("Total portfolio: ${portfolio_value}"); -} \ No newline at end of file