← run

rs-01-rle

0.333
1/3 tests· basic
Challenge · difficulty 1/5
# Run-length encoding

Implement the library file **`src/lib.rs`** exposing:

```rust
pub fn run_length_encode(input: &str) -> String
```

Encode consecutive runs of the same character as `<char><count>`. A single character still
gets a count of `1`.

- `run_length_encode("aaabbc")` → `"a3b2c1"`
- `run_length_encode("")` → `""`
- `run_length_encode("abc")` → `"a1b1c1"`

Tests live in `tests/` and call it as `challenge::run_length_encode`.
tests/rle.rs
use challenge::run_length_encode;

#[test]
fn basic_runs() {
    assert_eq!(run_length_encode("aaabbc"), "a3b2c1");
}

#[test]
fn empty() {
    assert_eq!(run_length_encode(""), "");
}

#[test]
fn all_singles() {
    assert_eq!(run_length_encode("abc"), "a1b1c1");
}
System prompt
You are an expert programmer. Solve the task exactly as specified. Output your solution as fenced code blocks using the required file name(s) and the exact function/type signatures requested. Prefer correctness; do not include prose outside code unless asked.
Proposed solution
I'll implement the run-length encoding function as specified.

```rust
// src/lib.rs

pub fn run_length_encode(input: &str) -> String {
    if input.is_empty() {
        return String::new();
    }
    
    let mut result = String::new();
    let chars: Vec<char> = input.chars().collect();
    let mut i = 0;
    
    while i < chars.len() {
        let current_char = chars[i];
        let mut count = 1;
        
        // Count consecutive occurrences of the same character
        while i + count < chars.len() && chars[i + count] == current_char {
            count += 1;
        }
        
        // Add character and count to result
        if count == 1 {
            result.push(current_char);
        } else {
            result.push(current_char);
            result.push_str(&count.to_string());
        }
        
        i += count;
    }
    
    result
}
```

Test output (stdout)


running 0 tests

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


running 3 tests
. 1/3
basic_runs --- FAILED
all_singles --- FAILED

failures:

---- basic_runs stdout ----

thread 'basic_runs' (734962) panicked at tests/rle.rs:5:5:
assertion `left == right` failed
  left: "a3b2c"
 right: "a3b2c1"
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- all_singles stdout ----

thread 'all_singles' (734961) panicked at tests/rle.rs:15:5:
assertion `left == right` failed
  left: "abc"
 right: "a1b1c1"


failures:
    all_singles
    basic_runs

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

Errors (stderr)

error: test failed, to rerun pass `--test rle`
Self-repair attempts
[
  {
    "total": 3,
    "answer": "I'll implement the run-length encoding function as specified.\n\n```rust\n// src/lib.rs\n\npub fn run_length_encode(input: &str) -> String {\n    if input.is_empty() {\n        return String::new();\n    }\n    \n    let mut result = String::new();\n    let chars: Vec<char> = input.chars().collect();\n    let mut i = 0;\n    \n    while i < chars.len() {\n        let current_char = chars[i];\n        let mut count = 1;\n        \n        // Count consecutive occurrences of the same character\n        while i + count < chars.len() && chars[i + count] == current_char {\n            count += 1;\n        }\n        \n        // Add character and count to result\n        if count == 1 {\n            result.push(current_char);\n        } else {\n            result.push(current_char);\n            result.push_str(&count.to_string());\n        }\n        \n        i += count;\n    }\n    \n    result\n}\n```",
    "passed": 1,
    "test_error": "running 0 tests\n\ntest result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s\n\n\nrunning 3 tests\n. 1/3\nbasic_runs --- FAILED\nall_singles --- FAILED\n\nfailures:\n\n---- basic_runs stdout ----\n\nthread 'basic_runs' (734962) panicked at tests/rle.rs:5:5:\nassertion `left == right` failed\n  left: \"a3b2c\"\n right: \"a3b2c1\"\nnote: run with `RUST_BACKTRACE=1` environment variable to display a backtrace\n\n---- all_singles stdout ----\n\nthread 'all_singles' (734961) panicked at tests/rle.rs:15:5:\nassertion `left == right` failed\n  left: \"abc\"\n right: \"a1b1c1\"\n\n\nfailures:\n    all_singles\n    basic_runs\n\ntest result: FAILED. 1 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s\n\n\nerror: test failed, to rerun pass `--test rle`"
  },
  {
    "total": 3,
    "answer": "Looking at the test failures, I can see the issue. The problem is that when a character appears only once, it should still have a count of 1, but my implementation was not handling this correctly.\n\nLet me analyze the failing cases:\n- `run_length_encode(\"aaabbc\")` should return `\"a3b2c1\"` but I'm getting `\"a3b2c\"`\n- `run_length_encode(\"abc\")` should return `\"a1b1c1\"` but I'm getting `\"abc\"`\n\nThe issue is that when a character appears only once, I'm not adding the count \"1\" to the result.\n\nLet me fix this:\n\n```rust\n// src/lib.rs\n\npub fn run_length_encode(input: &str) -> String {\n    if input.is_empty() {\n        return String::new();\n    }\n    \n    let mut result = String::new();\n    let chars: Vec<char> = input.chars().collect();\n    let mut i = 0;\n    \n    while i < chars.len() {\n        let current_char = chars[i];\n        let mut count = 1;\n        \n        // Count consecutive occurrences of the same character\n        while i + count < chars.len() && chars[i + count] == current_char {\n            count += 1;\n        }\n        \n        // Add character and count to result\n        result.push(current_char);\n        result.push_str(&count.to_string());\n        \n        i += count;\n    }\n    \n    result\n}\n```",
    "passed": 3,
    "test_error": ""
  }
]