pull down to refresh

My goal for this year is to get good at trees. I'm a self-taught programmer. I'm very capable. Built a lot of systems, blah blah blah. But, if you ask me to solve a real computer science-y type problem, I am a deer in head lights. I would be fine not getting good at leetcode like problems or algorithms, but I want to. And I think being able to think in trees always seems useful. So, this is my way of keeping myself accountable.
The goal is to learn and get better. No strong rules. I might use AI to help we understand something better. When I do I try to prompt to it doesn't give me the answer (i.e. "do not give me any code"), because then it's hard to unsee and come up with my own solution. But, it is useful to get an understanding. Also, I'm using this as a chance to get good at Rust, so that's what I'll be using.
Join in if you are interested. If there is no real participation, I'll probably just stop posting. But, if there is, this could be fun!

My solution:
use std::collections::HashMap; pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> { let mut map = HashMap::new(); for (i, &num) in nums.iter().enumerate() { let complement = target - num; if let Some(&index) = map.get(&complement) { return vec![index as i32, i as i32]; } map.insert(num, i); } vec![] } #[cfg(test)] mod tests { use super::*; #[test] fn test_example_1() { let nums = vec![1, 8, 10, 21]; let target = 18; assert_eq!(two_sum(nums, target), vec![1, 2]); } #[test] fn test_example_2() { let nums = vec![5, 12, 19, 3]; let target = 8; assert_eq!(two_sum(nums, target), vec![0, 3]); } #[test] fn test_example_3() { let nums = vec![6, 6]; let target = 12; assert_eq!(two_sum(nums, target), vec![0, 1]); } }
reply
What language is this?
Edit: nevermind, I see in your post that you’re using Rust
reply
Yeah, I want to give it a real go. Try to get good at it to decide if it’s a language I want to use long term. Also thought of going through @jimmysong’s Programming Bitcoin book but doing it in rust instead of python. Could be fun.
reply