Решение на упр.03 задача 3 от Стилиян Иванов

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

Към профила на Стилиян Иванов

Резултати

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

Код

use std::collections::HashMap;
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() -> EventLog {
EventLog { events: Vec::new() }
}
pub fn add_event(&mut self, event: Event) {
self.events.push(event);
}
pub fn user_spent(&self, user: &str) -> f64 {
let mut total_spent = 0.0;
for event in &self.events {
match event {
Event::Purchase { user: event_user, amount, .. } => {
if event_user == user {
total_spent += amount;
}
}
_ => {
}
}
}
total_spent
}
pub fn summaries_by_type(&self) -> HashMap<String, usize> {
let mut summary = HashMap::new();
for event in &self.events {
let type_name = match event {
Event::Login { .. } => "Login",
Event::Logout { .. } => "Logout",
Event::Purchase { .. } => "Purchase",
Event::Error { .. } => "Error",
};
let type_name_string = type_name.to_string();
if summary.contains_key(&type_name_string) {
if let Some(count) = summary.get_mut(&type_name_string) {
*count += 1;
}
} else {
summary.insert(type_name_string, 1);
}
}
summary
}
pub fn filter_events(&self, user: Option<&str>, after: Option<u64>) -> Vec<&Event> {
let mut filtered_events = Vec::new();
for event in &self.events {
let user_check = match user {
None => true,
Some(u) => match event {
Event::Login { user: eu, .. } => eu == u,
Event::Logout { user: eu, .. } => eu == u,
Event::Purchase { user: eu, .. } => eu == u,
Event::Error { .. } => false,
},
};
let timestamp_check = match after {
None => true,
Some(t) => {
let event_timestamp = match event {
Event::Login { timestamp, .. } => *timestamp,
Event::Logout { timestamp, .. } => *timestamp,
Event::Purchase { timestamp, .. } => *timestamp,
Event::Error { timestamp, .. } => *timestamp,
};
event_timestamp > t
}
};
if user_check && timestamp_check {
filtered_events.push(event);
}
}
filtered_events
}
}

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

Updating crates.io index
     Locking 17 packages to latest compatible versions
   Compiling proc-macro2 v1.0.103
   Compiling unicode-ident v1.0.22
   Compiling quote v1.0.41
   Compiling futures-core v0.3.31
   Compiling futures-sink v0.3.31
   Compiling futures-channel v0.3.31
   Compiling futures-io v0.3.31
   Compiling slab v0.4.11
   Compiling futures-task v0.3.31
   Compiling syn v2.0.108
   Compiling memchr v2.7.6
   Compiling pin-project-lite v0.2.16
   Compiling pin-utils v0.1.0
   Compiling solution v0.1.0 (/tmp/d20251030-1757769-1v90jpv/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.70s
     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 ... ok

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

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

Стилиян качи първо решение на 29.10.2025 19:52 (преди около 1 месеца)