Skip to content

Commit 251246e

Browse files
committed
Add micro-blog exercise
This is an exercise requiring students to truncate unicode strings. Solves exercism#1507
1 parent 51418ec commit 251246e

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"exercise": "micro-blog",
3+
"version": "1.0.0",
4+
"comments": [
5+
"This exercise is only applicable to languages that use UTF-8, UTF-16",
6+
"or other variable width Unicode compatible encoding as their internal",
7+
"string representation.",
8+
"",
9+
"This exercise is probably too easy in languages that use Unicode aware",
10+
"string slicing.",
11+
"",
12+
"When adding additional tests to the problem specification, prefer tests",
13+
"that pass and fail the same for UTF-8 and UTF-16.",
14+
"",
15+
"Avoid adding tests that involve characters (graphemes) that are made up",
16+
"of multiple characters, or introduce them as a more advanced step.",
17+
"",
18+
"Consider adding a track specific hint.md about if your language uses",
19+
"UTF-8, UTF-16 or other for its internal string representation."
20+
],
21+
"cases": [
22+
{
23+
"description": "Truncate a micro blog post",
24+
"cases": [
25+
{
26+
"description": "English language short",
27+
"property": "truncate",
28+
"input": {
29+
"phrase": "Hi"
30+
},
31+
"expected": "Hi"
32+
},
33+
{
34+
"description": "English language long",
35+
"property": "truncate",
36+
"input": {
37+
"phrase": "Hello there"
38+
},
39+
"expected": "Hello"
40+
},
41+
{
42+
"description": "English and emoji short",
43+
"property": "truncate",
44+
"input": {
45+
"phrase": "Fly 🛫"
46+
},
47+
"expected": "Fly 🛫"
48+
},
49+
{
50+
"description": "Emoji short",
51+
"property": "truncate",
52+
"input": {
53+
"phrase": "💇"
54+
},
55+
"expected": "💇"
56+
},
57+
{
58+
"description": "Emoji long",
59+
"property": "truncate",
60+
"input": {
61+
"phrase": "❄🌡🤧🤒🏥🕰😀"
62+
},
63+
"expected": "❄🌡🤧🤒🏥"
64+
}
65+
]
66+
}
67+
]
68+
}

exercises/micro-blog/description.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
You have identified a gap in the social media market for very very short
2+
posts. Now that Twitter allows 280 character posts, people wanting quick
3+
social media updates aren't being served. You decide to create your own
4+
social media network.
5+
6+
To make your product noteworthy, you make it extreme and only allow posts
7+
of 5 or less characters. Any posts of more than 5 characters should be
8+
truncated to 5.
9+
10+
To allow your users to express themselves fully, you allow Emoji and
11+
other Unicode.
12+
13+
The task is to truncate input strings to 5 characters.
14+
15+
## Text Encodings
16+
17+
Text stored digitally has to be converted to a series of bytes.
18+
There are 3 ways to map characters to bytes in common use.
19+
* **ASCII** can encode English language characters. All
20+
characters are precisely 1 byte long.
21+
* **UTF-8** is a Unicode text encoding. Characters take between 1
22+
and 4 bytes.
23+
* **UTF-16** is a Unicode text encoding. Characters are either 2 or
24+
4 bytes long.
25+
26+
UTF-8 and UTF-16 are both Unicode encodings which means they're capable of
27+
representing a massive range of characters including:
28+
* English language text
29+
* Foreign language text
30+
* Historic text
31+
* Emoji
32+
33+
UTF-8 and UTF-16 are both variable length encodings, which means that
34+
different characters take up different amounts of space.
35+
36+
Consider the letter 'a' and the emoji '😛'. In UTF-16 the letter takes
37+
2 bytes but the emoji takes 4 bytes.
38+
39+
The trick to this exercise is to use APIs designed around Unicode
40+
characters (codepoints) instead of Unicode codeunits.

exercises/micro-blog/metadata.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
title: "Micro Blog"
3+
blurb: "Given an input string, truncate it to 5 characters."

0 commit comments

Comments
 (0)