Skip to content

Commit 732d677

Browse files
authored
Add Micro Blog Exercise (#649)
1 parent e72a35b commit 732d677

File tree

7 files changed

+266
-0
lines changed

7 files changed

+266
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,14 @@
11481148
"prerequisites": [],
11491149
"difficulty": 5
11501150
},
1151+
{
1152+
"slug": "micro-blog",
1153+
"name": "Micro Blog",
1154+
"uuid": "203ba680-cdbd-494d-a1b6-af426bc84e59",
1155+
"practices": [],
1156+
"prerequisites": [],
1157+
"difficulty": 1
1158+
},
11511159
{
11521160
"slug": "yacht",
11531161
"name": "Yacht",
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Instructions
2+
3+
You have identified a gap in the social media market for very very short posts.
4+
Now that Twitter allows 280 character posts, people wanting quick social media updates aren't being served.
5+
You decide to create your own social media network.
6+
7+
To make your product noteworthy, you make it extreme and only allow posts of 5 or less characters.
8+
Any posts of more than 5 characters should be truncated to 5.
9+
10+
To allow your users to express themselves fully, you allow Emoji and other Unicode.
11+
12+
The task is to truncate input strings to 5 characters.
13+
14+
## Text Encodings
15+
16+
Text stored digitally has to be converted to a series of bytes.
17+
There are 3 ways to map characters to bytes in common use.
18+
19+
- **ASCII** can encode English language characters.
20+
All characters are precisely 1 byte long.
21+
- **UTF-8** is a Unicode text encoding.
22+
Characters take between 1 and 4 bytes.
23+
- **UTF-16** is a Unicode text encoding.
24+
Characters are either 2 or 4 bytes long.
25+
26+
UTF-8 and UTF-16 are both Unicode encodings which means they're capable of representing a massive range of characters including:
27+
28+
- Text in most of the world's languages and scripts
29+
- Historic text
30+
- Emoji
31+
32+
UTF-8 and UTF-16 are both variable length encodings, which means that different characters take up different amounts of space.
33+
34+
Consider the letter 'a' and the emoji '😛'.
35+
In UTF-16 the letter takes 2 bytes but the emoji takes 4 bytes.
36+
37+
The trick to this exercise is to use APIs designed around Unicode characters (codepoints) instead of Unicode codeunits.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"authors": [
3+
"tomasnorre"
4+
],
5+
"files": {
6+
"solution": [
7+
"MicroBlog.php"
8+
],
9+
"test": [
10+
"MicroBlogTest.php"
11+
],
12+
"example": [
13+
".meta/example.php"
14+
]
15+
},
16+
"blurb": "Given an input string, truncate it to 5 characters."
17+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
class MicroBlog
6+
{
7+
public function truncate(string $text): string
8+
{
9+
return mb_substr($text, 0, 5);
10+
}
11+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.
11+
12+
[b927b57f-7c98-42fd-8f33-fae091dc1efc]
13+
description = "English language short"
14+
15+
[a3fcdc5b-0ed4-4f49-80f5-b1a293eac2a0]
16+
description = "English language long"
17+
18+
[01910864-8e15-4007-9c7c-ac956c686e60]
19+
description = "German language short (broth)"
20+
21+
[f263e488-aefb-478f-a671-b6ba99722543]
22+
description = "German language long (bear carpet → beards)"
23+
24+
[0916e8f1-41d7-4402-a110-b08aa000342c]
25+
description = "Bulgarian language short (good)"
26+
27+
[bed6b89c-03df-4154-98e6-a61a74f61b7d]
28+
description = "Greek language short (health)"
29+
30+
[485a6a70-2edb-424d-b999-5529dbc8e002]
31+
description = "Maths short"
32+
33+
[8b4b7b51-8f48-4fbe-964e-6e4e6438be28]
34+
description = "Maths long"
35+
36+
[71f4a192-0566-4402-a512-fe12878be523]
37+
description = "English and emoji short"
38+
39+
[6f0f71f3-9806-4759-a844-fa182f7bc203]
40+
description = "Emoji short"
41+
42+
[ce71fb92-5214-46d0-a7f8-d5ba56b4cc6e]
43+
description = "Emoji long"
44+
45+
[5dee98d2-d56e-468a-a1f2-121c3f7c5a0b]
46+
description = "Royal Flush?"
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
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+
25+
declare(strict_types=1);
26+
27+
class MicroBlog
28+
{
29+
public function truncate(string $text): string
30+
{
31+
throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__));
32+
}
33+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
class MicroBlogTest extends PHPUnit\Framework\TestCase
6+
{
7+
private MicroBlog $microBlog;
8+
9+
public static function setUpBeforeClass(): void
10+
{
11+
require_once 'MicroBlog.php';
12+
}
13+
14+
protected function setUp(): void
15+
{
16+
$this->microBlog = new MicroBlog();
17+
}
18+
19+
/**
20+
* uuid: b927b57f-7c98-42fd-8f33-fae091dc1efc
21+
*/
22+
public function testEnglishLanguageShort(): void
23+
{
24+
$this->assertEquals('Hi', $this->microBlog->truncate('Hi'));
25+
}
26+
27+
/**
28+
* uuid: a3fcdc5b-0ed4-4f49-80f5-b1a293eac2a0
29+
*/
30+
public function testEnglishLanguageLong(): void
31+
{
32+
$this->assertEquals('Hello', $this->microBlog->truncate('Hello there'));
33+
}
34+
35+
/**
36+
* uuid: 01910864-8e15-4007-9c7c-ac956c686e60
37+
*/
38+
public function testGermanLanguageShortBroth(): void
39+
{
40+
$this->assertEquals('brühe', $this->microBlog->truncate('brühe'));
41+
}
42+
43+
/**
44+
* uuid: f263e488-aefb-478f-a671-b6ba99722543
45+
*/
46+
public function testGermanLanguageLongBearCarpetToBeards(): void
47+
{
48+
$this->assertEquals('Bärte', $this->microBlog->truncate('Bärteppich'));
49+
}
50+
51+
/**
52+
* uuid: 0916e8f1-41d7-4402-a110-b08aa000342c
53+
*/
54+
public function testBulgarianLanguageShortGood(): void
55+
{
56+
$this->assertEquals('Добър', $this->microBlog->truncate('Добър'));
57+
}
58+
59+
/**
60+
* uuid: bed6b89c-03df-4154-98e6-a61a74f61b7d
61+
*/
62+
public function testGreekLanguageShortHealth(): void
63+
{
64+
$this->assertEquals('υγειά', $this->microBlog->truncate('υγειά'));
65+
}
66+
67+
/**
68+
* uuid: 485a6a70-2edb-424d-b999-5529dbc8e002
69+
*/
70+
public function testMathShort(): void
71+
{
72+
$this->assertEquals('a=πr²', $this->microBlog->truncate('a=πr²'));
73+
}
74+
75+
/**
76+
* uuid: 8b4b7b51-8f48-4fbe-964e-6e4e6438be28
77+
*/
78+
public function testMathLong(): void
79+
{
80+
$this->assertEquals('∅⊊ℕ⊊ℤ', $this->microBlog->truncate('∅⊊ℕ⊊ℤ⊊ℚ⊊ℝ⊊ℂ'));
81+
}
82+
83+
/**
84+
* uuid: 71f4a192-0566-4402-a512-fe12878be523
85+
*/
86+
public function testEnglishAndEmojiShort(): void
87+
{
88+
$this->assertEquals('Fly 🛫', $this->microBlog->truncate('Fly 🛫'));
89+
}
90+
91+
/**
92+
* uuid: 6f0f71f3-9806-4759-a844-fa182f7bc203
93+
*/
94+
public function testEmojiShort(): void
95+
{
96+
$this->assertEquals('💇', $this->microBlog->truncate('💇'));
97+
}
98+
99+
/**
100+
* uuid: ce71fb92-5214-46d0-a7f8-d5ba56b4cc6e
101+
*/
102+
public function testEmojiLong(): void
103+
{
104+
$this->assertEquals('❄🌡🤧🤒🏥', $this->microBlog->truncate('❄🌡🤧🤒🏥🕰😀'));
105+
}
106+
107+
/**
108+
* uuid: 5dee98d2-d56e-468a-a1f2-121c3f7c5a0b
109+
*/
110+
public function testRoyalFlush(): void
111+
{
112+
$this->assertEquals('🃎🂸🃅🃋🃍', $this->microBlog->truncate('🃎🂸🃅🃋🃍🃁🃊'));
113+
}
114+
}

0 commit comments

Comments
 (0)