🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of florian-nguyen

florian-nguyen's solution

to Transpose in the Go Track

Published at Apr 02 2020 · 0 comments
Instructions
Test suite
Solution

Given an input text output it transposed.

Roughly explained, the transpose of a matrix:

ABC
DEF

is given by:

AD
BE
CF

Rows become columns and columns become rows. See https://en.wikipedia.org/wiki/Transpose.

If the input has rows of different lengths, this is to be solved as follows:

  • Pad to the left with spaces.
  • Don't pad to the right.

Therefore, transposing this matrix:

ABC
DE

results in:

AD
BE
C

And transposing:

AB
DEF

results in:

AD
BE
 F

In general, all characters from the input should also be present in the transposed output. That means that if a column in the input text contains only spaces on its bottom-most row(s), the corresponding output row should contain the spaces in its right-most column(s).

Running the tests

To run the tests run the command go test from within the exercise directory.

If the test suite contains benchmarks, you can run these with the --bench and --benchmem flags:

go test -v --bench . --benchmem

Keep in mind that each reviewer will run benchmarks on a different machine, with different specs, so the results from these benchmark tests may vary.

Further information

For more detailed information about the Go track, including how to get help if you're having trouble, please visit the exercism.io Go language page.

Source

Reddit r/dailyprogrammer challenge #270 [Easy]. https://www.reddit.com/r/dailyprogrammer/comments/4msu2x/challenge_270_easy_transpose_the_input_text

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

cases_test.go

package transpose

// Source: exercism/problem-specifications
// Commit: 92bc877 switch to new json-schema
// Problem Specifications Version: 1.1.0

var testCases = []struct {
	description string
	input       []string
	expected    []string
}{
	{
		"empty string",
		[]string{},
		[]string{},
	},
	{
		"two characters in a row",
		[]string{
			"A1",
		},
		[]string{
			"A",
			"1",
		},
	},
	{
		"two characters in a column",
		[]string{
			"A",
			"1",
		},
		[]string{
			"A1",
		},
	},
	{
		"simple",
		[]string{
			"ABC",
			"123",
		},
		[]string{
			"A1",
			"B2",
			"C3",
		},
	},
	{
		"single line",
		[]string{
			"Single line.",
		},
		[]string{
			"S",
			"i",
			"n",
			"g",
			"l",
			"e",
			" ",
			"l",
			"i",
			"n",
			"e",
			".",
		},
	},
	{
		"first line longer than second line",
		[]string{
			"The fourth line.",
			"The fifth line.",
		},
		[]string{
			"TT",
			"hh",
			"ee",
			"  ",
			"ff",
			"oi",
			"uf",
			"rt",
			"th",
			"h ",
			" l",
			"li",
			"in",
			"ne",
			"e.",
			".",
		},
	},
	{
		"second line longer than first line",
		[]string{
			"The first line.",
			"The second line.",
		},
		[]string{
			"TT",
			"hh",
			"ee",
			"  ",
			"fs",
			"ie",
			"rc",
			"so",
			"tn",
			" d",
			"l ",
			"il",
			"ni",
			"en",
			".e",
			" .",
		},
	},
	{
		"mixed line length",
		[]string{
			"The longest line.",
			"A long line.",
			"A longer line.",
			"A line.",
		},
		[]string{
			"TAAA",
			"h   ",
			"elll",
			" ooi",
			"lnnn",
			"ogge",
			"n e.",
			"glr",
			"ei ",
			"snl",
			"tei",
			" .n",
			"l e",
			"i .",
			"n",
			"e",
			".",
		},
	},
	{
		"square",
		[]string{
			"HEART",
			"EMBER",
			"ABUSE",
			"RESIN",
			"TREND",
		},
		[]string{
			"HEART",
			"EMBER",
			"ABUSE",
			"RESIN",
			"TREND",
		},
	},
	{
		"rectangle",
		[]string{
			"FRACTURE",
			"OUTLINED",
			"BLOOMING",
			"SEPTETTE",
		},
		[]string{
			"FOBS",
			"RULE",
			"ATOP",
			"CLOT",
			"TIME",
			"UNIT",
			"RENT",
			"EDGE",
		},
	},
	{
		"triangle",
		[]string{
			"T",
			"EE",
			"AAA",
			"SSSS",
			"EEEEE",
			"RRRRRR",
		},
		[]string{
			"TEASER",
			" EASER",
			"  ASER",
			"   SER",
			"    ER",
			"     R",
		},
	},
}

transpose_test.go

package transpose

import (
	"reflect"
	"testing"
)

func TestTranspose(t *testing.T) {
	for _, test := range testCases {
		actual := Transpose(test.input)
		if !reflect.DeepEqual(actual, test.expected) {
			// check for zero length slices
			if len(actual) == 0 || len(test.expected) == 0 {
				t.Fatalf("\n\tTranspose(%q): %s\n\n\tExpected: %q\n\tGot: %q",
					test.input, test.description, test.expected, actual)
			}
			// let's make the error more specific and find the row it's on
			min := min(len(test.expected), len(actual))
			for i := 0; i < min; i++ {
				if test.expected[i] != actual[i] {
					t.Fatalf("\n\tTranspose(%q): %s\n\n\tExpected: %q\n\tGot: %q\n\n\tRow %d Expected: %q Got: %q",
						test.input, test.description, test.expected, actual, i, test.expected[i], actual[i])
				}
			}
		}
	}
}

// helper function
// https://stackoverflow.com/questions/27516387/what-is-the-correct-way-to-find-the-min-between-two-integers-in-go
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func BenchmarkTranspose(b *testing.B) {
	for i := 0; i < b.N; i++ {
		for _, test := range testCases {
			Transpose(test.input)
		}
	}
}
package transpose

// Function Transpose computes the transpose matrix of a given string matrix
func Transpose(input []string) (t []string) {

	// Empty string case
	if len(input) == 0 {
		return []string{}
	}

	// Compute number of rows and columns (longest values)
	n_column := MaxStringLength(input)
	t = make([]string, n_column)

	// Computing transpose matrix by concatenating string characters one at a time
	for row, word := range input {
		for column, char := range word {
			t[column] += string(char)
		}
		// Space padding should only be done if a larger word exists after the current one
		for i := len(word); i < MaxStringLength(input[row:]); i++ {
			t[i] += " "
		}
	}
	return t
}

// Function maxStringLength is used to obtain the size of the largest string in a list
func MaxStringLength(s []string) (n int) {
	n = 0
	for _, word := range s {
		if len(word) > n {
			n = len(word)
		}
	}
	return n
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?