Решение на упр.03 задача 3 от Пламен Колев

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

Към профила на Пламен Колев

Резултати

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

Код

use std::collections::HashMap;
#[derive(Debug)]
#[allow(dead_code)]
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 },
}
struct EventLog { events: Vec<Event>, }
impl EventLog {
fn new() -> EventLog{
EventLog { events: Vec::new() }
}
fn add_event(&mut self, event: Event) {
self.events.push(event);
}
fn user_spent(&self, user: &str) -> f64 {
self.events
.iter()
.map(|event|
match event {
Event::Purchase { user: u, amount, .. }
if u == user => *amount,
_ => 0.0,
}
)
.sum()
}
fn summaries_by_type(&self) -> HashMap<String, usize> {
let mut summaries = HashMap::new();
for event in &self.events {
let event_types = match event {
Event::Login { .. } => "Login",
Event::Logout { .. } => "Logout",
Event::Purchase { .. } => "Purchase",
Event::Error { .. } => "Error",
};
*summaries.entry(event_types.to_string()).or_insert(0) += 1;
}
summaries
}
fn filter_events(&self, user: Option<&str>, after: Option<u64>) -> Vec<&Event> {
if user.is_none() && after.is_none() {
return self.events.iter().collect();
}
else if user.is_some() && after.is_none() {
let u = user.unwrap();
return self.events.iter()
.filter(|event|
match event {
Event::Login { user, .. } => user == u,
Event::Logout { user, .. } => user == u,
Event::Purchase { user, .. } => user == u,
Event::Error { .. } => false,
})
.collect();
}
else if user.is_none() && after.is_some() {
let t = after.unwrap();
return self.events.iter()
.filter(|event|
match event {
Event::Login { timestamp, .. } => *timestamp >= t,
Event::Logout { timestamp, .. } => *timestamp >= t,
Event::Purchase { timestamp, .. } => *timestamp >= t,
Event::Error { timestamp, .. } => *timestamp >= t,
})
.collect();
}
else if user.is_some() && after.is_some() {
let u = user.unwrap();
let t = after.unwrap();
let user_exists = self.events.iter().any(|event|
match event {
Event::Login { user, .. } => user == u,
Event::Logout { user, .. } => user == u,
Event::Purchase { user, .. } => user == u,
Event::Error { .. } => false,
});
if user_exists {
return self.events.iter()
.filter(|event|
match event {
Event::Login { user, timestamp } => user == u && *timestamp >= t,
Event::Logout { user, timestamp } => user == u && *timestamp >= t,
Event::Purchase { user, timestamp, .. } => user == u && *timestamp >= t,
Event::Error { .. } => false,
})
.collect();
}
else {
return self.events.iter()
.filter(|event|
match event {
Event::Login { timestamp, .. } => *timestamp >= t,
Event::Logout { timestamp, .. } => *timestamp >= t,
Event::Purchase { timestamp, .. } => *timestamp >= t,
Event::Error { timestamp, .. } => *timestamp >= t,
})
.collect();
}
}
else {
Vec::new()
}
}
}

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

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-sink v0.3.31
   Compiling futures-core v0.3.31
   Compiling futures-channel v0.3.31
   Compiling futures-task v0.3.31
   Compiling syn v2.0.108
   Compiling memchr v2.7.6
   Compiling pin-utils v0.1.0
   Compiling slab v0.4.11
   Compiling pin-project-lite v0.2.16
   Compiling futures-io v0.3.31
   Compiling solution v0.1.0 (/tmp/d20251030-1757769-urxzed/solution)
warning: struct `EventLog` is never constructed
  --> src/lib.rs:12:8
   |
12 | struct EventLog { events: Vec<Event>, }
   |        ^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: associated items `new`, `add_event`, `user_spent`, `summaries_by_type`, and `filter_events` are never used
  --> src/lib.rs:15:8
   |
14 | impl EventLog {
   | ------------- associated items in this implementation
15 |     fn new() -> EventLog{
   |        ^^^
...
19 |     fn add_event(&mut self, event: Event) {
   |        ^^^^^^^^^
...
23 |     fn user_spent(&self, user: &str) -> f64 {
   |        ^^^^^^^^^^
...
36 |     fn summaries_by_type(&self) -> HashMap<String, usize> {
   |        ^^^^^^^^^^^^^^^^^
...
51 |     fn filter_events(&self, user: Option<&str>, after: Option<u64>) -> Vec<&Event> {
   |        ^^^^^^^^^^^^^

warning: `solution` (lib) generated 2 warnings
   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.76s
     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 коментара)

Пламен качи първо решение на 28.10.2025 12:53 (преди 3 дена)