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

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

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

Резултати

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

Код

use std::collections::HashMap;
#[derive(Clone)]
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 {
let empty: Vec<Event> = [].to_vec();
EventLog {events: empty}
}
fn add_event(&mut self, event: Event) {
self.events.push(event);
}
fn user_spent(&self, user: &str) -> f64 {
let mut sum: f64 = 0.0;
for event in &self.events {
match event {
Event::Purchase {user: u, item: _, amount, timestamp: _} => {
if u == user {
sum += amount;
}
}
_ => {}
}
}
return sum;
}
fn summaries_by_type(&self) -> HashMap<String, usize> {
let mut res: HashMap<String, usize> = HashMap::new();
let login = "Login".to_string();
let logout = "Logout".to_string();
let purchase = "Purchase".to_string();
let error = "Error".to_string();
for event in &self.events {
match event {
Event::Login {..} => {
if res.contains_key(&login) {
res.insert(login.clone(), res.get(&login).unwrap()+1);
}
else {
res.insert(login.clone(), 1);
}
}
Event::Logout {..} => {
if res.contains_key(&logout) {
res.insert(logout.clone(), res.get(&logout).unwrap()+1);
}
else {
res.insert(logout.clone(), 1);
}
}
Event::Purchase {..} => {
if res.contains_key(&purchase) {
res.insert(purchase.clone(), res.get(&purchase).unwrap()+1);
}
else {
res.insert(purchase.clone(), 1);
}
}
Event::Error {..} => {
if res.contains_key(&error) {
res.insert(error.clone(), res.get(&error).unwrap()+1);
}
else {
res.insert(error.clone(), 1);
}
}
}
}
res
}
fn filter_events(&self, user: Option<&str>, after: Option<u64>) -> Vec<&Event> {
let mut filtered: Vec<&Event> = [].to_vec();
for event in &self.events {
let mut condition = true;
match user {
Some(name) => {
match event {
Event::Error{..} => {condition = false;}
Event::Login{user: u, ..}
| Event::Logout{user: u, ..}
| Event::Purchase{user: u, ..}=> {
if name != u {
condition = false;
}
}
}
}
None => {}
}
match after {
Some(stamp) => {
match event {
Event::Error{timestamp: ts, ..}
| Event::Login{timestamp: ts, ..}
| Event::Logout{timestamp: ts, ..}
| Event::Purchase{timestamp: ts, ..} => {
if ts < &stamp {
condition = false;
}
}
}
}
_ => {}
}
if condition {
filtered.push(event);
}
}
filtered
}
}

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

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 memchr v2.7.6
   Compiling syn v2.0.108
   Compiling pin-project-lite v0.2.16
   Compiling pin-utils v0.1.0
   Compiling futures-task v0.3.31
   Compiling slab v0.4.11
   Compiling futures-io v0.3.31
   Compiling solution v0.1.0 (/tmp/d20251030-1757769-1hnfbnl/solution)
warning: enum `Event` is never used
 --> src/lib.rs:4:6
  |
4 | 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:14:8
   |
13 | impl EventLog {
   | ------------- associated items in this implementation
14 |     fn new() -> EventLog {
   |        ^^^
...
19 |     fn add_event(&mut self, event: Event) {
   |        ^^^^^^^^^
...
23 |     fn user_spent(&self, user: &str) -> f64 {
   |        ^^^^^^^^^^
...
38 |     fn summaries_by_type(&self) -> HashMap<String, usize> {
   |        ^^^^^^^^^^^^^^^^^
...
84 |     fn filter_events(&self, user: Option<&str>, after: 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 8.72s
     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 21:27 (преди 3 дена)