Решение на упр.03 задача 1 от Димитър Николов

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

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

Резултати

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

Код

fn rainbow_replace(text: &str, word: &str, palette: &[char]) -> String {
let palette_iter = &mut std::iter::repeat(palette.iter()).flatten();
let word_len = word.chars().count();
let mut result = String::with_capacity(text.len());
let mut split = text.split(word);
// SAFETY: this unwrap is safe due to the nature of the `.split()` method,
// which always returns at least a single element - the string itself.
result.push_str(split.next().unwrap());
split.fold(result, |mut acc, el| {
palette_iter.take(word_len).for_each(|ch| acc.push(*ch));
acc.push_str(el);
acc
})
}

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

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 pin-utils v0.1.0
   Compiling memchr v2.7.6
   Compiling syn v2.0.108
   Compiling futures-task v0.3.31
   Compiling slab v0.4.11
   Compiling futures-io v0.3.31
   Compiling pin-project-lite v0.2.16
   Compiling solution v0.1.0 (/tmp/d20251030-1757769-qy05cm/solution)
warning: function `rainbow_replace` is never used
 --> src/lib.rs:1:4
  |
1 | fn rainbow_replace(text: &str, word: &str, palette: &[char]) -> String {
  |    ^^^^^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

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

running 1 test
test solution_test::test_basic ... ok

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

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

Димитър качи първо решение на 23.10.2025 20:04 (преди 8 дена)

Димитър качи решение на 23.10.2025 23:50 (преди 7 дена)

fn rainbow_replace(text: &str, word: &str, palette: &[char]) -> String {
let palette_iter = &mut std::iter::repeat(palette.iter()).flatten();
let word_len = word.chars().count();
let mut result = String::with_capacity(text.len());
let mut split = text.split(word);
// SAFETY: this unwrap is safe due to the nature of the `.split()` method,
// which always returns at least a single element - the string itself.
result.push_str(split.next().unwrap());
split.fold(result, |mut acc, el| {
palette_iter.take(word_len).for_each(|ch| acc.push(*ch));
acc.push_str(el);
acc
})
-}
-
+}
-fn main() {
- assert_eq!(
- rainbow_replace("плодова салата", "плодова", &['🍆', '🍎', '🍒']),
- "🍆🍎🍒🍆🍎🍒🍆 салата".to_string(),
- );
-}