Решение на упр.03 задача 3 от Йосиф Хамед

Обратно към всички решения

Към профила на Йосиф Хамед

Резултати

  • 1 точка от тестове
  • 0 бонус точки
  • 1 точка общо
  • 1 успешни тест(а)
  • 1 неуспешни тест(а)

Код

use std::collections::HashMap;
#[derive(Debug)]
pub enum Event {
Login {
user: String,
timestamp: u64,
},
Logout {
user: String,
timestamp: u64,
},
Purchase {
user: String,
item: String,
amount: f64,
timestamp: u64,
},
Error {
code: i32,
message: String,
timestamp: u64,
},
}
pub struct EventLog {
events: Vec<Event>,
}
impl EventLog {
pub fn new() -> Self {
return Self { events: vec![] };
}
pub fn add_event(&mut self, event: Event) {
self.events.push(event);
}
pub fn user_spent(&self, user_search: &str) -> f64 {
self.events
.iter()
.map(|e| match e {
Event::Purchase { amount, user, .. } => {
if *user == user_search {
*amount
} else {
0.0
}
}
_ => 0.0,
})
.fold(0.0, |a, b| a + b)
}
pub fn summaries_by_type(&self) -> HashMap<String, usize> {
let mut hash_map: HashMap<String, usize> = HashMap::new();
for e in &self.events[..] {
match e {
Event::Login { .. } => {
*hash_map.entry("Login".to_string()).or_insert(0) += 1;
}
Event::Logout { .. } => {
*hash_map.entry("Logout".to_string()).or_insert(0) += 1;
}
Event::Purchase { .. } => {
*hash_map.entry("Purchase".to_string()).or_insert(0) += 1;
}
Event::Error { .. } => {
*hash_map.entry("Error".to_string()).or_insert(0) += 1;
}
}
}
return hash_map;
}
pub fn filter_events(&self, user: Option<&str>, after: Option<u64>) -> Vec<&Event> {
if let Some(a) = after {
return self
.events
.iter()
.filter(|e| match e {
Event::Error { timestamp, .. } => *timestamp >= a,
Event::Login { timestamp, .. } => *timestamp >= a,
Event::Logout { timestamp, .. } => *timestamp >= a,
Event::Purchase { timestamp, .. } => *timestamp >= a,
})
.collect();
};
if let Some(u) = user {
return self
.events
.iter()
.filter(|e| match e {
Event::Error { .. } => false,
Event::Login { user, .. } => user == u,
Event::Logout { user, .. } => user == u,
Event::Purchase { user, .. } => user == u,
})
.collect();
};
return self.events.iter().collect();
}
}

Лог от изпълнението

Updating crates.io index
     Locking 17 packages to latest compatible versions
   Compiling proc-macro2 v1.0.103
   Compiling quote v1.0.41
   Compiling unicode-ident v1.0.22
   Compiling futures-core v0.3.31
   Compiling futures-sink v0.3.31
   Compiling futures-channel v0.3.31
   Compiling memchr v2.7.6
   Compiling syn v2.0.108
   Compiling pin-utils v0.1.0
   Compiling futures-io v0.3.31
   Compiling pin-project-lite v0.2.16
   Compiling futures-task v0.3.31
   Compiling slab v0.4.11
   Compiling solution v0.1.0 (/tmp/d20251030-1757769-1f1nijn/solution)
   Compiling futures-macro v0.3.31
   Compiling futures-util v0.3.31
   Compiling futures-executor v0.3.31
   Compiling futures v0.3.31
    Finished `test` profile [unoptimized + debuginfo] target(s) in 8.81s
     Running tests/solution_test.rs (target/debug/deps/solution_test-bfd50394249726db)

running 2 tests
test solution_test::test_empty ... ok
test solution_test::test_basic ... FAILED

failures:

---- solution_test::test_basic stdout ----
thread 'solution_test::test_basic' panicked at tests/solution_test.rs:150:9:
assertion failed: filtered.len() == expected.len() &&
    std::iter::zip(&filtered, expected).all(|(a, b)| eq_event(a, b))
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    solution_test::test_basic

test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

error: test failed, to rerun pass `--test solution_test`

История (1 версия и 0 коментара)

Йосиф качи първо решение на 24.10.2025 23:17 (преди 7 дена)