Transpose: ensure all lines are right-trimmed

TL;DR: Transpose: ensure all lines are right-trimmed by homersimpsons · Pull Request #2198 · exercism/problem-specifications · GitHub

The transpose exercise asks a user to transpose a given text input: Transpose in JavaScript on Exercism

Some solutions only trim the last line insead of all the lines. I opened this Pull Request to test those edge cases: Transpose: ensure all lines are right-trimmed by homersimpsons · Pull Request #2198 · exercism/problem-specifications · GitHub.

For context I had the following solution (in JavaScript) that was just trimming the last line of the input. The mentee was aware enough to find those failing cases

export const transpose = (input) => {
  if (input.length == 0) return input;
  const rows = input.length;
  // Find the length of the longest string
  const columns = Math.max(...input.map((arr) => arr.length));
  // Now pad the smaller string to make them all equal in length
  input = input.map((str) => str.padEnd(columns));
  let output = Array(columns)
    .fill()
    .map(() => Array(rows).fill(''));
  for (let i = 0; i < rows; i++) {
    for (let j = 0; j < columns; j++) {
      output[j][i] = input[i][j];
    }
  }
  output = output.map((arr) => arr.join(''));
  // remove the right padding at the last item, but can't it occur in the last 2 rows?
  output[output.length - 1] = output[output.length - 1].trimEnd();
  return output;
};

Because that’s what the tests do as well. On the other hand the description reads “Don’t pad to the right”.

Pretty sure there are many solutions that trim only the last line.

Thank you for your suggestion! If you haven’t read it yet, this doc is worth a read: Suggesting Exercise Improvements | Exercism's Docs

Doesn’t test eccd3784-45f0-4a3f-865a-360cb323d314 check the last three lines are trimmed?

It does, but the Javascript track is missing that test.

Ah. I see. Then if there’s already a test for this, syncing the JS track from the specs probably makes more sense than adding a new test :slight_smile:

(cc @SleeplessByte)

Doesn’t test eccd3784-45f0-4a3f-865a-360cb323d314 check the last three lines are trimmed?

Nice catch @IsaacG

I may be able to push a PR to update JS track tests.

1 Like

Hereby approved. Please tag me on GitHub if you open it in the JS track?

1 Like

Hereby approved. Please tag me on GitHub if you open it in the JS track?

@SleeplessByte I just opened ✨ Sync `transpose` with problem-specifications by homersimpsons · Pull Request #2149 · exercism/javascript · GitHub

1 Like

Thank you for this addition!