Решение на упр.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() -> EventLog {
EventLog { events: vec![] }
}
fn add_event(&mut self, event: Event) {
self.events.push(event);
}
fn user_spent(&self, target: &str) -> f64 {
self.events.iter().fold(0.0, |acc, e| match e {
Event::Purchase { user, amount, .. } if user == target => acc + amount,
_ => acc,
})
}
fn summaries_by_type(&self) -> HashMap<String, usize> {
self.events
.iter()
.fold(HashMap::new(), |mut acc: HashMap<String, usize>, e| {
match e {
Event::Login { .. } => *acc.entry("Login".to_string()).or_default() += 1,
Event::Logout { .. } => *acc.entry("Logout".to_string()).or_default() += 1,
Event::Purchase { .. } => *acc.entry("Purchase".to_string()).or_default() += 1,
Event::Error { .. } => *acc.entry("Error".to_string()).or_default() += 1,
};
acc
})
}
fn filter_events(&self, target: Option<&str>, after: Option<u64>) -> Vec<&Event> {
self.events
.iter()
.filter(|e| match e {
| Event::Login { user, timestamp }
| Event::Logout { user, timestamp }
| Event::Purchase { user, timestamp, .. } => {
target.map_or(true, |t| user.as_str() == t) && after.map_or(true, |t| *timestamp >= t)
}
Event::Error { timestamp, .. } => {
target.is_none() && after.map_or(true, |t| *timestamp >= t)
}
})
.collect()
}
}

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

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-task v0.3.31
   Compiling syn v2.0.108
   Compiling memchr v2.7.6
   Compiling pin-utils v0.1.0
   Compiling futures-io v0.3.31
   Compiling pin-project-lite v0.2.16
   Compiling slab v0.4.11
   Compiling solution v0.1.0 (/tmp/d20251030-1757769-1dyodpt/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:25:8
   |
25 | struct EventLog {
   |        ^^^^^^^^

warning: associated items `new`, `add_event`, `user_spent`, `summaries_by_type`, and `filter_events` are never used
  --> src/lib.rs:30:8
   |
29 | impl EventLog {
   | ------------- associated items in this implementation
30 |     fn new() -> EventLog {
   |        ^^^
...
34 |     fn add_event(&mut self, event: Event) {
   |        ^^^^^^^^^
...
38 |     fn user_spent(&self, target: &str) -> f64 {
   |        ^^^^^^^^^^
...
45 |     fn summaries_by_type(&self) -> HashMap<String, usize> {
   |        ^^^^^^^^^^^^^^^^^
...
59 |     fn filter_events(&self, target: 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 14.89s
     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

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

Димитър качи първо решение на 24.10.2025 04:37 (преди 7 дена)

Димитър качи решение на 24.10.2025 04:38 (преди 7 дена)

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() -> EventLog {
EventLog { events: vec![] }
}
fn add_event(&mut self, event: Event) {
self.events.push(event);
}
fn user_spent(&self, user: &str) -> f64 {
self.events.iter().fold(0.0, |acc, e| match e {
Event::Purchase { user: u, amount: a, .. } if u == user => acc + a,
_ => acc,
})
}
fn summaries_by_type(&self) -> HashMap<String, usize> {
self.events
.iter()
.fold(HashMap::new(), |mut acc: HashMap<String, usize>, e| {
match e {
Event::Login { .. } => *acc.entry("Login".to_string()).or_default() += 1,
Event::Logout { .. } => *acc.entry("Logout".to_string()).or_default() += 1,
Event::Purchase { .. } => *acc.entry("Purchase".to_string()).or_default() += 1,
Event::Error { .. } => *acc.entry("Error".to_string()).or_default() += 1,
};
acc
})
}
fn filter_events(&self, target: Option<&str>, after: Option<u64>) -> Vec<&Event> {
self.events
.iter()
.filter(|e| match e {
| Event::Login { user, timestamp }
| Event::Logout { user, timestamp }
| Event::Purchase { user, timestamp, .. } => {
target.map_or(true, |t| user.as_str() == t) && after.map_or(true, |t| *timestamp >= t)
}
Event::Error { timestamp, .. } => {
- target.is_none() && after.map_or(true, |t| *timestamp > t)
+ target.is_none() && after.map_or(true, |t| *timestamp >= t)
}
})
.collect()
}
}

Димитър качи решение на 24.10.2025 04:40 (преди 7 дена)

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() -> EventLog {
EventLog { events: vec![] }
}
fn add_event(&mut self, event: Event) {
self.events.push(event);
}
- fn user_spent(&self, user: &str) -> f64 {
+ fn user_spent(&self, target: &str) -> f64 {
self.events.iter().fold(0.0, |acc, e| match e {
- Event::Purchase { user: u, amount: a, .. } if u == user => acc + a,
+ Event::Purchase { user, amount, .. } if user == target => acc + amount,
_ => acc,
})
}
fn summaries_by_type(&self) -> HashMap<String, usize> {
self.events
.iter()
.fold(HashMap::new(), |mut acc: HashMap<String, usize>, e| {
match e {
Event::Login { .. } => *acc.entry("Login".to_string()).or_default() += 1,
Event::Logout { .. } => *acc.entry("Logout".to_string()).or_default() += 1,
Event::Purchase { .. } => *acc.entry("Purchase".to_string()).or_default() += 1,
Event::Error { .. } => *acc.entry("Error".to_string()).or_default() += 1,
};
acc
})
}
fn filter_events(&self, target: Option<&str>, after: Option<u64>) -> Vec<&Event> {
self.events
.iter()
.filter(|e| match e {
| Event::Login { user, timestamp }
| Event::Logout { user, timestamp }
| Event::Purchase { user, timestamp, .. } => {
target.map_or(true, |t| user.as_str() == t) && after.map_or(true, |t| *timestamp >= t)
}
Event::Error { timestamp, .. } => {
target.is_none() && after.map_or(true, |t| *timestamp >= t)
}
})
.collect()
}
}