Решение на упр.03 задача 3 от Борил Василев

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

Към профила на Борил Василев

Резултати

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

Код

use std::collections::HashMap;
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() -> Self
{
return EventLog
{
events: Vec::new(),
};
}
fn add_event(&mut self, event: Event)
{
self.events.push(event);
}
fn user_spent(&self, specific_user: &str) -> f64
{
return self.events.iter().filter_map(|element|
{
if let Event::Purchase { user, amount, .. } = element
{
if user == specific_user
{
return Some(amount);
}
else
{
return None;
}
}
else
{
return None;
}
}).sum();
}
fn summaries_by_type(&self) -> HashMap<String, usize>
{
let mut map = HashMap::new();
for element in &self.events
{
let key = match element
{
Event::Login { .. } => "Login",
Event::Logout { .. } => "Logout",
Event::Purchase { .. } => "Purchase",
Event::Error { .. } => "Error",
};
map.entry(key.to_string()).and_modify(|count| *count += 1).or_insert(1);
}
return map;
}
fn filter_events(&self, specific_user: Option<&str>, threshold_timestamp: Option<u64>) -> Vec<&Event>
{
return self.events
.iter()
.filter(|element|
{
match element
{
Event::Error { .. } => false,
Event::Login { user, timestamp, .. } | Event::Logout { user, timestamp, .. }
| Event::Purchase { user, timestamp, .. } =>
{
let user_match = if let Some(u) = specific_user { user == u } else { true };
let timestamp_match = if let Some(t) = threshold_timestamp { *timestamp > t } else { true };
return user_match && timestamp_match;
}
}
}).collect();
}
}

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

Updating crates.io index
    Blocking waiting for file lock on package cache
     Locking 17 packages to latest compatible versions
    Blocking waiting for file lock on package cache
    Blocking waiting for file lock on package cache
    Blocking waiting for file lock on package cache
   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 pin-utils v0.1.0
   Compiling slab v0.4.11
   Compiling futures-task v0.3.31
   Compiling futures-io v0.3.31
   Compiling memchr v2.7.6
   Compiling syn v2.0.108
   Compiling pin-project-lite v0.2.16
   Compiling solution v0.1.0 (/tmp/d20251030-1757769-11cbpog/solution)
warning: enum `Event` is never used
 --> src/lib.rs:3:6
  |
3 | enum Event
  |      ^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: struct `EventLog` is never constructed
  --> src/lib.rs:11:8
   |
11 | struct EventLog { events: Vec<Event>, }
   |        ^^^^^^^^

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

warning: `solution` (lib) generated 3 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 15.35s
     Running tests/solution_test.rs (target/debug/deps/solution_test-bfd50394249726db)

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

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

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

Борил качи първо решение на 28.10.2025 16:06 (преди 2 дена)