Решение на упр.06 задача 1 от Виктор Карталов

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

Към профила на Виктор Карталов

Резултати

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

Код

//

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

Updating crates.io index
     Locking 17 packages to latest compatible versions
   Compiling proc-macro2 v1.0.103
   Compiling quote v1.0.42
   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 memchr v2.7.6
   Compiling syn v2.0.110
   Compiling slab v0.4.11
   Compiling pin-utils v0.1.0
   Compiling futures-task v0.3.31
   Compiling futures-io v0.3.31
   Compiling pin-project-lite v0.2.16
   Compiling solution v0.1.0 (/tmp/d20251120-1757769-ym2a61/solution)
   Compiling futures-macro v0.3.31
   Compiling futures-util v0.3.31
   Compiling futures-executor v0.3.31
   Compiling futures v0.3.31
error[E0433]: failed to resolve: use of undeclared type `HashMap`
  --> tests/solution_test.rs:12:23
   |
12 |         let mut map = HashMap::new();
   |                       ^^^^^^^ use of undeclared type `HashMap`
   |
help: consider importing this struct
   |
6  + use std::collections::HashMap;
   |

error[E0433]: failed to resolve: use of undeclared type `HashMap`
  --> tests/solution_test.rs:29:23
   |
29 |         let mut map = HashMap::new();
   |                       ^^^^^^^ use of undeclared type `HashMap`
   |
help: consider importing this struct
   |
6  + use std::collections::HashMap;
   |

error[E0433]: failed to resolve: use of undeclared type `HashMap`
  --> tests/solution_test.rs:47:23
   |
47 |         let mut map = HashMap::new();
   |                       ^^^^^^^ use of undeclared type `HashMap`
   |
help: consider importing this struct
   |
6  + use std::collections::HashMap;
   |

error[E0433]: failed to resolve: use of undeclared type `HashMap`
  --> tests/solution_test.rs:61:23
   |
61 |         let mut map = HashMap::new();
   |                       ^^^^^^^ use of undeclared type `HashMap`
   |
help: consider importing this struct
   |
6  + use std::collections::HashMap;
   |

error[E0405]: cannot find trait `Read` in this scope
  --> tests/solution_test.rs:75:27
   |
75 |     struct ReadCounter<R: Read> {
   |                           ^^^^ not found in this scope
   |
help: consider importing this trait
   |
6  + use std::io::Read;
   |

error[E0433]: failed to resolve: use of undeclared crate or module `io`
  --> tests/solution_test.rs:81:47
   |
81 |         fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
   |                                               ^^ use of undeclared crate or module `io`
   |
help: a builtin type with a similar name exists
   |
81 |         fn read(&mut self, buf: &mut [u8]) -> i8::Result<usize> {
   |                                               ~~
help: consider importing one of these modules
   |
6  + use std::io;
   |
6  + use futures::io;
   |

error[E0433]: failed to resolve: use of undeclared type `HashMap`
  --> tests/solution_test.rs:89:19
   |
89 |     let mut map = HashMap::new();
   |                   ^^^^^^^ use of undeclared type `HashMap`
   |
help: consider importing this struct
   |
6  + use std::collections::HashMap;
   |

error[E0433]: failed to resolve: use of undeclared type `Path`
   --> tests/solution_test.rs:132:34
    |
132 |     let output = aggregate_logs(&Path::new(&dirname));
    |                                  ^^^^ use of undeclared type `Path`
    |
help: consider importing this struct
    |
6   + use std::path::Path;
    |

error[E0433]: failed to resolve: use of undeclared type `LogLevel`
  --> tests/solution_test.rs:14:29
   |
14 |         assert_eq!(map.get(&LogLevel::Error).copied().unwrap_or(0), 0);
   |                             ^^^^^^^^ use of undeclared type `LogLevel`

error[E0433]: failed to resolve: use of undeclared type `LogLevel`
  --> tests/solution_test.rs:15:29
   |
15 |         assert_eq!(map.get(&LogLevel::Warn).copied().unwrap_or(0), 0);
   |                             ^^^^^^^^ use of undeclared type `LogLevel`

error[E0433]: failed to resolve: use of undeclared type `LogLevel`
  --> tests/solution_test.rs:16:29
   |
16 |         assert_eq!(map.get(&LogLevel::Info).copied().unwrap_or(0), 0);
   |                             ^^^^^^^^ use of undeclared type `LogLevel`

error[E0433]: failed to resolve: use of undeclared type `LogLevel`
  --> tests/solution_test.rs:17:29
   |
17 |         assert_eq!(map.get(&LogLevel::Debug).copied().unwrap_or(0), 0);
   |                             ^^^^^^^^ use of undeclared type `LogLevel`

error[E0433]: failed to resolve: use of undeclared type `LogLevel`
  --> tests/solution_test.rs:31:29
   |
31 |         assert_eq!(map.get(&LogLevel::Error).copied().unwrap_or(0), 1);
   |                             ^^^^^^^^ use of undeclared type `LogLevel`

error[E0433]: failed to resolve: use of undeclared type `LogLevel`
  --> tests/solution_test.rs:32:29
   |
32 |         assert_eq!(map.get(&LogLevel::Warn).copied().unwrap_or(0), 1);
   |                             ^^^^^^^^ use of undeclared type `LogLevel`

error[E0433]: failed to resolve: use of undeclared type `LogLevel`
  --> tests/solution_test.rs:33:29
   |
33 |         assert_eq!(map.get(&LogLevel::Info).copied().unwrap_or(0), 1);
   |                             ^^^^^^^^ use of undeclared type `LogLevel`

error[E0433]: failed to resolve: use of undeclared type `LogLevel`
  --> tests/solution_test.rs:34:29
   |
34 |         assert_eq!(map.get(&LogLevel::Debug).copied().unwrap_or(0), 1);
   |                             ^^^^^^^^ use of undeclared type `LogLevel`

error[E0433]: failed to resolve: use of undeclared type `LogLevel`
  --> tests/solution_test.rs:49:29
   |
49 |         assert_eq!(map.get(&LogLevel::Error).copied().unwrap_or(0), 0);
   |                             ^^^^^^^^ use of undeclared type `LogLevel`

error[E0433]: failed to resolve: use of undeclared type `LogLevel`
  --> tests/solution_test.rs:50:29
   |
50 |         assert_eq!(map.get(&LogLevel::Warn).copied().unwrap_or(0), 1);
   |                             ^^^^^^^^ use of undeclared type `LogLevel`

error[E0433]: failed to resolve: use of undeclared type `LogLevel`
  --> tests/solution_test.rs:51:29
   |
51 |         assert_eq!(map.get(&LogLevel::Info).copied().unwrap_or(0), 3);
   |                             ^^^^^^^^ use of undeclared type `LogLevel`

error[E0433]: failed to resolve: use of undeclared type `LogLevel`
  --> tests/solution_test.rs:52:29
   |
52 |         assert_eq!(map.get(&LogLevel::Debug).copied().unwrap_or(0), 1);
   |                             ^^^^^^^^ use of undeclared type `LogLevel`

error[E0433]: failed to resolve: use of undeclared type `ParseLogError`
  --> tests/solution_test.rs:62:62
   |
62 |         assert!(matches!(parse_log_file(data, &mut map), Err(ParseLogError::ParseLine)));
   |                                                              ^^^^^^^^^^^^^ use of undeclared type `ParseLogError`

error[E0433]: failed to resolve: use of undeclared type `LogLevel`
   --> tests/solution_test.rs:135:45
    |
135 |             assert_eq!(info.log_counts.get(&LogLevel::Error).copied().unwrap_or(0), 1);
    |                                             ^^^^^^^^ use of undeclared type `LogLevel`

error[E0433]: failed to resolve: use of undeclared type `LogLevel`
   --> tests/solution_test.rs:136:45
    |
136 |             assert_eq!(info.log_counts.get(&LogLevel::Warn).copied().unwrap_or(0), 1);
    |                                             ^^^^^^^^ use of undeclared type `LogLevel`

error[E0433]: failed to resolve: use of undeclared type `LogLevel`
   --> tests/solution_test.rs:137:45
    |
137 |             assert_eq!(info.log_counts.get(&LogLevel::Info).copied().unwrap_or(0), 4);
    |                                             ^^^^^^^^ use of undeclared type `LogLevel`

error[E0433]: failed to resolve: use of undeclared type `LogLevel`
   --> tests/solution_test.rs:138:45
    |
138 |             assert_eq!(info.log_counts.get(&LogLevel::Debug).copied().unwrap_or(0), 1);
    |                                             ^^^^^^^^ use of undeclared type `LogLevel`

error[E0425]: cannot find function `parse_log_file` in this scope
  --> tests/solution_test.rs:13:26
   |
13 |         assert!(matches!(parse_log_file(data, &mut map), Ok(())));
   |                          ^^^^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find function `parse_log_file` in this scope
  --> tests/solution_test.rs:30:26
   |
30 |         assert!(matches!(parse_log_file(data, &mut map), Ok(())));
   |                          ^^^^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find function `parse_log_file` in this scope
  --> tests/solution_test.rs:48:26
   |
48 |         assert!(matches!(parse_log_file(data, &mut map), Ok(())));
   |                          ^^^^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find function `parse_log_file` in this scope
  --> tests/solution_test.rs:62:26
   |
62 |         assert!(matches!(parse_log_file(data, &mut map), Err(ParseLogError::ParseLine)));
   |                          ^^^^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find function `parse_log_file` in this scope
  --> tests/solution_test.rs:90:11
   |
90 |     match parse_log_file(&mut reader, &mut map) {
   |           ^^^^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find function `aggregate_logs` in this scope
   --> tests/solution_test.rs:132:18
    |
132 |     let output = aggregate_logs(&Path::new(&dirname));
    |                  ^^^^^^^^^^^^^^ not found in this scope

Some errors have detailed explanations: E0405, E0425, E0433.
For more information about an error, try `rustc --explain E0405`.
error: could not compile `solution` (test "solution_test") due to 31 previous errors

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

Виктор качи първо решение на 19.11.2025 21:48 (преди 16 дена)

Виктор качи решение на 19.11.2025 22:09 (преди 16 дена)

-/*use std::collections::HashMap;
-use std::hash::Hash;
+//
-pub trait MyFromIterator<A> {
- fn my_from_iter<I>(iter: I) -> Self
- where
- I: Iterator<Item = A>;
-}
-
-impl<T> MyFromIterator<T> for Vec<T> {
- fn my_from_iter<I>(iter: I) -> Self
- where
- I: Iterator<Item = T>,
- {
- let mut v = Vec::new();
- for item in iter {
- v.push(item);
- }
- v
- }
-}
-
-impl MyFromIterator<char> for String {
- fn my_from_iter<I>(iter: I) -> Self
- where
- I: Iterator<Item = char>,
- {
- let mut s = String::new();
- for ch in iter {
- s.push(ch);
- }
- s
- }
-}
-
-impl<K, V> MyFromIterator<(K, V)> for HashMap<K, V>
-where
- K: Eq + Hash,
-{
- fn my_from_iter<I>(iter: I) -> Self
- where
- I: Iterator<Item = (K, V)>,
- {
- let mut map = HashMap::new();
- for (k, v) in iter {
- map.insert(k, v);
- }
- map
- }
-}
-
-fn my_collect<I, C>(iter: I) -> C
-where
- I: Iterator,
- C: MyFromIterator<I::Item>,
-{
- C::my_from_iter(iter)
-}
-
-fn main() {
- let bytes: Vec<u8> = my_collect("hello".bytes());
- let chars: Vec<char> = my_collect("hello".chars());
- let index_to_char: HashMap<usize, char> = my_collect("hello".char_indices());
- let digits: String = my_collect("1a2b3c4d".chars().filter(|c| c.is_digit(10)));
-}
-*/
-
-use std::collections::HashMap;
-use std::hash::Hash;
-
-#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
-enum SensorMetric {
- Load,
- Frequency,
- Temperature,
-}
-
-struct Event {
- timestamp: u64,
- sensor: String,
- metric: SensorMetric,
- value: f64,
-}
-
-fn group_and_aggregate<O, E, D, K>(
- events: &[E],
- group_fn: impl Fn(&E) -> K,
- data_fn: impl Fn(&E) -> Option<D>,
- aggregate: impl Fn(&[D]) -> O,
-) -> HashMap<K, O>
-where
- K: Eq + Hash,
-{
- let mut buckets: HashMap<K, Vec<D>> = HashMap::new();
-
- for e in events {
- if let Some(d) = data_fn(e) {
- let key = group_fn(e);
- buckets.entry(key).or_insert_with(Vec::new).push(d);
- }
- }
-
- let mut out: HashMap<K, O> = HashMap::with_capacity(buckets.len());
- for (k, vec_d) in buckets {
- let res = aggregate(&vec_d);
- out.insert(k, res);
- }
- out
-}
-
-trait Aggregator {
- type Output;
-
- fn name(&self) -> String;
- fn aggregate(&self, events: &[Event]) -> Vec<(String, Self::Output)>;
-}
-fn mean(xs: &[f64]) -> f64 {
- if xs.is_empty() {
- 0.0
- } else {
- let sum: f64 = xs.iter().sum();
- sum / (xs.len() as f64)
- }
-}
-
-struct TotalAggregator {}
-impl Aggregator for TotalAggregator {
- type Output = f64;
-
- fn name(&self) -> String {
- "total".to_string()
- }
-
- fn aggregate(&self, events: &[Event]) -> Vec<(String, f64)> {
- let map: HashMap<SensorMetric, f64> = group_and_aggregate(
- events,
- |e: &Event| e.metric,
- |e: &Event| Some(e.value),
- |vals: &[f64]| mean(vals),
- );
-
- vec![
- ("Load".to_string(), *map.get(&SensorMetric::Load).unwrap_or(&0.0)),
- (
- "Frequency".to_string(),
- *map.get(&SensorMetric::Frequency).unwrap_or(&0.0),
- ),
- (
- "Temperature".to_string(),
- *map.get(&SensorMetric::Temperature).unwrap_or(&0.0),
- ),
- ]
- }
-}
-
-struct MetricAggregator {
- metric: SensorMetric,
-}
-impl Aggregator for MetricAggregator {
- type Output = f64;
-
- fn name(&self) -> String {
- format!("{:?}", self.metric)
- }
-
- fn aggregate(&self, events: &[Event]) -> Vec<(String, f64)> {
- let map: HashMap<String, f64> = group_and_aggregate(
- events,
- |e: &Event| e.sensor.clone(), // ключ: собствен string на сензора
- |e: &Event| {
- if e.metric == self.metric {
- Some(e.value)
- } else {
- None
- }
- },
- |vals: &[f64]| mean(vals),
- );
- map.into_iter().collect()
- }
-}
-
-fn main() {
- let all: Vec<Box<dyn Aggregator<Output=f64>>> = vec![
- Box::new(TotalAggregator{}),
- Box::new(MetricAggregator{metric: SensorMetric::Load}),
- Box::new(MetricAggregator{metric: SensorMetric::Temperature}),
- ];
-
- let events = &[
- Event { timestamp: 0, sensor: String::from("cpu0"), metric: SensorMetric::Load, value: 0.98 },
- Event { timestamp: 0, sensor: String::from("cpu1"), metric: SensorMetric::Load, value: 0.04 },
- Event { timestamp: 0, sensor: String::from("gpu0"), metric: SensorMetric::Load, value: 13.45 },
- Event { timestamp: 0, sensor: String::from("cpu0"), metric: SensorMetric::Frequency, value: 3100.00 },
- Event { timestamp: 0, sensor: String::from("cpu1"), metric: SensorMetric::Frequency, value: 3100.00 },
- Event { timestamp: 0, sensor: String::from("gpu0"), metric: SensorMetric::Frequency, value: 300.00 },
- Event { timestamp: 0, sensor: String::from("cpu0"), metric: SensorMetric::Temperature, value: 56.20 },
- Event { timestamp: 0, sensor: String::from("cpu1"), metric: SensorMetric::Temperature, value: 33.33 },
- Event { timestamp: 0, sensor: String::from("gpu0"), metric: SensorMetric::Temperature, value: 25.00 },
- ];
-
- for aggr in &all {
- for (key, val) in aggr.aggregate(events) {
- println!("{} {:?}: {:.02}", aggr.name(), key, val);
- }
- }
-}