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

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

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

Резултати

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

Код

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::new() }
}
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 ev in &self.events {
match ev {
Event::Purchase { user: u,item: _, amount: a,timestamp: _ } =>
if(user == u) {sum+=a;}
_ => {}
}
}
sum
}
fn summaries_by_type(&self) -> HashMap<String, usize> {
let mut map: HashMap<String, usize> = HashMap::new();
map.insert("Login".to_string(),0);
map.insert("Logout".to_string(),0);
map.insert("Purchase".to_string(),0);
map.insert("Error".to_string(),0);
for ev in &self.events {
match ev {
Event::Login { .. } => *map.get_mut("Login").unwrap() += 1,
Event::Logout { .. } => *map.get_mut("Logout").unwrap() += 1,
Event::Purchase { .. } => *map.get_mut("Purchase").unwrap() += 1,
Event::Error { .. } => *map.get_mut("Error").unwrap() += 1,
}
}
map
}
fn filter_events(&self, user: Option<&str>, after: Option<u64>) -> Vec<&Event> {
let mut v: Vec<&Event> = Vec::new();
match user {
Some(u) => {
for ev in &self.events {
match ev {
Event::Login { user: ev_user, .. }
| Event::Logout { user: ev_user, .. }
| Event::Purchase { user: ev_user, .. } => {
if ev_user == u {
v.push(ev);
}
}
Event::Error { .. } => {}
}
}
}
None => {
for ev in &self.events {
match ev {
Event::Error { .. } => {}
_ => v.push(ev),
}
}
}
}
if let Some(t) = after {
let mut result: Vec<&Event> = Vec::new();
for ev in &v {
match ev {
Event::Login { timestamp, .. }
| Event::Logout { timestamp, .. }
| Event::Purchase { timestamp, .. }
| Event::Error { timestamp, .. } => {
if *timestamp > t {
result.push(ev);
}
}
}
}
return result;
}
v
}
}
fn main(){
}

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

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-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 futures-io v0.3.31
   Compiling slab v0.4.11
   Compiling pin-utils v0.1.0
   Compiling pin-project-lite v0.2.16
   Compiling memchr v2.7.6
   Compiling solution v0.1.0 (/tmp/d20251030-1757769-rgzrf9/solution)
warning: unnecessary parentheses around `if` condition
  --> src/lib.rs:27:19
   |
27 |                 if(user == u) {sum+=a;}
   |                   ^         ^
   |
   = note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
   |
27 -                 if(user == u) {sum+=a;}
27 +                 if user == u {sum+=a;}
   |

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:10:8
   |
10 | struct EventLog{
   |        ^^^^^^^^

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){
   |        ^^^^^^^^^
...
22 |     fn user_spent(&self, user: &str) -> f64 {
   |        ^^^^^^^^^^
...
33 |     fn summaries_by_type(&self) -> HashMap<String, usize> {
   |        ^^^^^^^^^^^^^^^^^
...
50 |     fn filter_events(&self, user: Option<&str>, after: Option<u64>) -> Vec<&Event> {
   |        ^^^^^^^^^^^^^

warning: function `main` is never used
  --> src/lib.rs:99:4
   |
99 | fn main(){
   |    ^^^^

warning: `solution` (lib) generated 5 warnings (run `cargo fix --lib -p solution` to apply 1 suggestion)
   Compiling futures-macro v0.3.31
   Compiling futures-util v0.3.31
   Compiling futures-executor v0.3.31
   Compiling futures v0.3.31
warning: unnecessary parentheses around `if` condition
  --> tests/../src/lib.rs:27:19
   |
27 |                 if(user == u) {sum+=a;}
   |                   ^         ^
   |
   = note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
   |
27 -                 if(user == u) {sum+=a;}
27 +                 if user == u {sum+=a;}
   |

warning: function `main` is never used
  --> tests/../src/lib.rs:99:4
   |
99 | fn main(){
   |    ^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: `solution` (test "solution_test") generated 2 warnings (run `cargo fix --test "solution_test"` to apply 1 suggestion)
    Finished `test` profile [unoptimized + debuginfo] target(s) in 8.65s
     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 ... FAILED

failures:

---- solution_test::test_empty stdout ----
thread 'solution_test::test_empty' panicked at tests/solution_test.rs:13:5:
assertion `left == right` failed
  left: {"Login": 0, "Error": 0, "Purchase": 0, "Logout": 0}
 right: {}
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    solution_test::test_empty

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

error: test failed, to rerun pass `--test solution_test`

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

Борис качи първо решение на 28.10.2025 14:20 (преди 3 дена)

Борис качи решение на 28.10.2025 14:24 (преди 3 дена)

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::new() }
}
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 ev in &self.events {
match ev {
Event::Purchase { user: u,item: _, amount: a,timestamp: _ } =>
if(user == u) {sum+=a;}
_ => {}
}
}
sum
}
+ fn summaries_by_type(&self) -> HashMap<String, usize> {
+ let mut map: HashMap<String, usize> = HashMap::new();
+ map.insert("Login".to_string(),0);
+ map.insert("Logout".to_string(),0);
+ map.insert("Purchase".to_string(),0);
+ map.insert("Error".to_string(),0);
+ for ev in &self.events {
+ match ev {
+ Event::Login { .. } => *map.get_mut("Login").unwrap() += 1,
+ Event::Logout { .. } => *map.get_mut("Logout").unwrap() += 1,
+ Event::Purchase { .. } => *map.get_mut("Purchase").unwrap() += 1,
+ Event::Error { .. } => *map.get_mut("Error").unwrap() += 1,
+ }
+ }
+ map
+ }
+
fn filter_events(&self, user: Option<&str>, after: Option<u64>) -> Vec<&Event> {
let mut v: Vec<&Event> = Vec::new();
-
- // --- 1️⃣ Филтрираме по user ---
match user {
Some(u) => {
for ev in &self.events {
match ev {
Event::Login { user: ev_user, .. }
| Event::Logout { user: ev_user, .. }
| Event::Purchase { user: ev_user, .. } => {
if ev_user == u {
v.push(ev);
}
}
Event::Error { .. } => {}
}
}
}
None => {
for ev in &self.events {
match ev {
Event::Error { .. } => {}
_ => v.push(ev),
}
}
}
}
if let Some(t) = after {
let mut result: Vec<&Event> = Vec::new();
for ev in &v {
match ev {
Event::Login { timestamp, .. }
| Event::Logout { timestamp, .. }
| Event::Purchase { timestamp, .. }
| Event::Error { timestamp, .. } => {
if *timestamp > t {
result.push(ev);
}
}
}
}
return result;
}
-
v
}
}
fn main(){
}