Skip to content

Commit 6fea088

Browse files
authored
Sync transpose (exercism#1016)
1 parent 117e17e commit 6fea088

7 files changed

Lines changed: 145 additions & 80 deletions

File tree

bin/auto-sync.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ state-of-tic-tac-toe
9191
strain
9292
sublist
9393
swift-scheduling
94+
transpose
9495
tournament
9596
triangle
9697
twelve-days

config.json

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -489,22 +489,6 @@
489489
"floating_point_numbers"
490490
]
491491
},
492-
{
493-
"slug": "transpose",
494-
"name": "Transpose",
495-
"uuid": "cb413881-99b6-493f-bea2-89563dc173a6",
496-
"practices": [],
497-
"prerequisites": [],
498-
"difficulty": 1,
499-
"topics": [
500-
"arrays",
501-
"lists",
502-
"loops",
503-
"matrices",
504-
"strings",
505-
"text_formatting"
506-
]
507-
},
508492
{
509493
"slug": "triangle",
510494
"name": "Triangle",
@@ -763,6 +747,22 @@
763747
"math"
764748
]
765749
},
750+
{
751+
"slug": "transpose",
752+
"name": "Transpose",
753+
"uuid": "cb413881-99b6-493f-bea2-89563dc173a6",
754+
"practices": [],
755+
"prerequisites": [],
756+
"difficulty": 2,
757+
"topics": [
758+
"arrays",
759+
"lists",
760+
"loops",
761+
"matrices",
762+
"strings",
763+
"text_formatting"
764+
]
765+
},
766766
{
767767
"slug": "twelve-days",
768768
"name": "Twelve Days",

exercises/practice/transpose/.docs/instructions.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ BE
1717
CF
1818
```
1919

20-
Rows become columns and columns become rows. See <https://en.wikipedia.org/wiki/Transpose>.
20+
Rows become columns and columns become rows.
21+
See [transpose][].
2122

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

@@ -55,5 +56,6 @@ BE
5556
```
5657

5758
In general, all characters from the input should also be present in the transposed output.
58-
That means that if a column in the input text contains only spaces on its bottom-most row(s),
59-
the corresponding output row should contain the spaces in its right-most column(s).
59+
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).
60+
61+
[transpose]: https://en.wikipedia.org/wiki/Transpose

exercises/practice/transpose/.meta/config.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"arueckauer",
77
"kunicmarko20",
88
"kytrinyx",
9-
"petemcfarlane"
9+
"petemcfarlane",
10+
"resu-xuniL"
1011
],
1112
"files": {
1213
"solution": [
@@ -21,5 +22,5 @@
2122
},
2223
"blurb": "Take input text and output it transposed.",
2324
"source": "Reddit r/dailyprogrammer challenge #270 [Easy].",
24-
"source_url": "https://www.reddit.com/r/dailyprogrammer/comments/4msu2x/challenge_270_easy_transpose_the_input_text"
25+
"source_url": "https://web.archive.org/web/20230630051421/https://old.reddit.com/r/dailyprogrammer/comments/4msu2x/challenge_270_easy_transpose_the_input_text/"
2526
}

exercises/practice/transpose/.meta/example.php

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
11
<?php
22

3-
/*
4-
* By adding type hints and enabling strict type checking, code can become
5-
* easier to read, self-documenting and reduce the number of potential bugs.
6-
* By default, type declarations are non-strict, which means they will attempt
7-
* to change the original type to match the type specified by the
8-
* type-declaration.
9-
*
10-
* In other words, if you pass a string to a function requiring a float,
11-
* it will attempt to convert the string value to a float.
12-
*
13-
* To enable strict mode, a single declare directive must be placed at the top
14-
* of the file.
15-
* This means that the strictness of typing is configured on a per-file basis.
16-
* This directive not only affects the type declarations of parameters, but also
17-
* a function's return type.
18-
*
19-
* For more info review the Concept on strict type checking in the PHP track
20-
* <link>.
21-
*
22-
* To disable strict typing, comment out the directive below.
23-
*/
24-
253
declare(strict_types=1);
264

275
/**
@@ -34,25 +12,19 @@
3412
*/
3513
function transpose($text)
3614
{
37-
$findMaxLength = function ($lines) {
38-
return array_reduce($lines, function ($max, $line) {
39-
return max($max, strlen($line));
40-
}, 0);
41-
};
15+
$pad = function ($lines) {
16+
for ($i = count($lines) - 2; $i >= 0; $i--) {
17+
$lines[$i] = str_pad($lines[$i], strlen($lines[$i + 1]), ' ', STR_PAD_RIGHT);
18+
}
4219

43-
$pad = function ($lines, $length) {
44-
return array_map(function ($line) use ($length) {
45-
return str_pad($line, $length, ' ', STR_PAD_RIGHT);
46-
}, $lines);
20+
return $lines;
4721
};
4822

4923
if ($text === ['']) {
5024
return $text;
5125
}
5226

53-
$maxLength = $findMaxLength($text);
54-
55-
$lines = $pad($text, $maxLength);
27+
$lines = $pad($text);
5628

5729
$result = [];
5830

exercises/practice/transpose/.meta/tests.toml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
# This is an auto-generated file. Regular comments will be removed when this
2-
# file is regenerated. Regenerating will not touch any manually added keys,
3-
# so comments can be added in a "comment" key.
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
411

512
[404b7262-c050-4df0-a2a2-0cb06cd6a821]
613
description = "empty string"
@@ -34,3 +41,6 @@ description = "rectangle"
3441

3542
[b80badc9-057e-4543-bd07-ce1296a1ea2c]
3643
description = "triangle"
44+
45+
[76acfd50-5596-4d05-89f1-5116328a7dd9]
46+
description = "jagged triangle"

exercises/practice/transpose/TransposeTest.php

Lines changed: 101 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,9 @@
11
<?php
22

3-
/*
4-
* By adding type hints and enabling strict type checking, code can become
5-
* easier to read, self-documenting and reduce the number of potential bugs.
6-
* By default, type declarations are non-strict, which means they will attempt
7-
* to change the original type to match the type specified by the
8-
* type-declaration.
9-
*
10-
* In other words, if you pass a string to a function requiring a float,
11-
* it will attempt to convert the string value to a float.
12-
*
13-
* To enable strict mode, a single declare directive must be placed at the top
14-
* of the file.
15-
* This means that the strictness of typing is configured on a per-file basis.
16-
* This directive not only affects the type declarations of parameters, but also
17-
* a function's return type.
18-
*
19-
* For more info review the Concept on strict type checking in the PHP track
20-
* <link>.
21-
*
22-
* To disable strict typing, comment out the directive below.
23-
*/
24-
253
declare(strict_types=1);
264

275
use PHPUnit\Framework\TestCase;
6+
use PHPUnit\Framework\Attributes\TestDox;
287

298
class TransposeTest extends TestCase
309
{
@@ -33,13 +12,21 @@ public static function setUpBeforeClass(): void
3312
require_once 'Transpose.php';
3413
}
3514

15+
/**
16+
* uuid: 404b7262-c050-4df0-a2a2-0cb06cd6a821
17+
*/
18+
#[TestDox('Empty string')]
3619
public function testEmptyString(): void
3720
{
3821
$input = [""];
3922
$expected = [""];
4023
$this->assertEquals($expected, transpose($input));
4124
}
4225

26+
/**
27+
* uuid: a89ce8a3-c940-4703-a688-3ea39412fbcb
28+
*/
29+
#[TestDox('Two characters in a row')]
4330
public function testTwoCharactersInARow(): void
4431
{
4532
$input = [
@@ -52,6 +39,10 @@ public function testTwoCharactersInARow(): void
5239
$this->assertEquals($expected, transpose($input));
5340
}
5441

42+
/**
43+
* uuid: 855bb6ae-4180-457c-abd0-ce489803ce98
44+
*/
45+
#[TestDox('Two characters in a column')]
5546
public function testTwoCharactersInAColumn(): void
5647
{
5748
$input = [
@@ -64,6 +55,10 @@ public function testTwoCharactersInAColumn(): void
6455
$this->assertEquals($expected, transpose($input));
6556
}
6657

58+
/**
59+
* uuid: 5ceda1c0-f940-441c-a244-0ced197769c8
60+
*/
61+
#[TestDox('Simple')]
6762
public function testSimple(): void
6863
{
6964
$input = [
@@ -78,6 +73,10 @@ public function testSimple(): void
7873
$this->assertEquals($expected, transpose($input));
7974
}
8075

76+
/**
77+
* uuid: a54675dd-ae7d-4a58-a9c4-0c20e99a7c1f
78+
*/
79+
#[TestDox('Single line')]
8180
public function testSingleLine(): void
8281
{
8382
$input = [
@@ -100,6 +99,10 @@ public function testSingleLine(): void
10099
$this->assertEquals($expected, transpose($input));
101100
}
102101

102+
/**
103+
* uuid: 0dc2ec0b-549d-4047-aeeb-8029fec8d5c5
104+
*/
105+
#[TestDox('First line longer than second line')]
103106
public function testFirstLineLongerThanSecondLine(): void
104107
{
105108
$input = [
@@ -127,6 +130,10 @@ public function testFirstLineLongerThanSecondLine(): void
127130
$this->assertEquals($expected, transpose($input));
128131
}
129132

133+
/**
134+
* uuid: 984e2ec3-b3d3-4b53-8bd6-96f5ef404102
135+
*/
136+
#[TestDox('Second line longer than first line')]
130137
public function testSecondLineLongerThanFirstLine(): void
131138
{
132139
$input = [
@@ -154,6 +161,44 @@ public function testSecondLineLongerThanFirstLine(): void
154161
$this->assertEquals($expected, transpose($input));
155162
}
156163

164+
/**
165+
* uuid: eccd3784-45f0-4a3f-865a-360cb323d314
166+
*/
167+
#[TestDox('Mixed line length')]
168+
public function testMixedLineLength(): void
169+
{
170+
$input = [
171+
"The longest line.",
172+
"A long line.",
173+
"A longer line.",
174+
"A line.",
175+
];
176+
$expected = [
177+
"TAAA",
178+
"h ",
179+
"elll",
180+
" ooi",
181+
"lnnn",
182+
"ogge",
183+
"n e.",
184+
"glr",
185+
"ei ",
186+
"snl",
187+
"tei",
188+
" .n",
189+
"l e",
190+
"i .",
191+
"n",
192+
"e",
193+
".",
194+
];
195+
$this->assertEquals($expected, transpose($input));
196+
}
197+
198+
/**
199+
* uuid: 85b96b3f-d00c-4f80-8ca2-c8a5c9216c2d
200+
*/
201+
#[TestDox('Square')]
157202
public function testSquare(): void
158203
{
159204
$input = [
@@ -173,6 +218,10 @@ public function testSquare(): void
173218
$this->assertEquals($expected, transpose($input));
174219
}
175220

221+
/**
222+
* uuid: b9257625-7a53-4748-8863-e08e9d27071d
223+
*/
224+
#[TestDox('Rectangle')]
176225
public function testRectangle(): void
177226
{
178227
$input = [
@@ -194,6 +243,10 @@ public function testRectangle(): void
194243
$this->assertEquals($expected, transpose($input));
195244
}
196245

246+
/**
247+
* uuid: b80badc9-057e-4543-bd07-ce1296a1ea2c
248+
*/
249+
#[TestDox('Triangle')]
197250
public function testTriangle(): void
198251
{
199252
$input = [
@@ -215,6 +268,32 @@ public function testTriangle(): void
215268
$this->assertEquals($expected, transpose($input));
216269
}
217270

271+
/**
272+
* uuid: 76acfd50-5596-4d05-89f1-5116328a7dd9
273+
*/
274+
#[TestDox('Jagged triangle')]
275+
public function testJaggedTriangle(): void
276+
{
277+
$input = [
278+
"11",
279+
"2",
280+
"3333",
281+
"444",
282+
"555555",
283+
"66666",
284+
];
285+
$expected = [
286+
"123456",
287+
"1 3456",
288+
" 3456",
289+
" 3 56",
290+
" 56",
291+
" 5",
292+
];
293+
$this->assertEquals($expected, transpose($input));
294+
}
295+
296+
#[TestDox('Many lines')]
218297
public function testManyLines(): void
219298
{
220299
$input = [

0 commit comments

Comments
 (0)