Skip to content

Commit 19dc365

Browse files
committed
Add micro-blog exercise
This is an exercise requiring students to truncate unicode strings. exercism#1507
1 parent 901b11d commit 19dc365

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
"Use emoji that need 4 bytes in UTF-16 to avoid having different",
13+
"behaviours in UTF-8 and UTF-16 languages.",
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+
"cases": [
19+
{
20+
"description": "Truncate a micro blog post",
21+
"cases": [
22+
{
23+
"description": "English language short",
24+
"property": "truncate",
25+
"input": {
26+
"phrase": "Hi"
27+
},
28+
"expected": "Hi"
29+
},
30+
{
31+
"description": "English language long",
32+
"property": "truncate",
33+
"input": {
34+
"phrase": "Hello there"
35+
},
36+
"expected": "Hello"
37+
},
38+
{
39+
"description": "English and emoji short",
40+
"property": "truncate",
41+
"input": {
42+
"phrase": "Fly 🛫"
43+
},
44+
"expected": "Fly 🛫"
45+
},
46+
{
47+
"description": "Emoji short",
48+
"property": "truncate",
49+
"input": {
50+
"phrase": "💇"
51+
},
52+
"expected": "💇"
53+
},
54+
{
55+
"description": "Emoji long",
56+
"property": "truncate",
57+
"input": {
58+
"phrase": "❄🌡🤧🤒🏥🕰😀"
59+
},
60+
"expected": "❄🌡🤧🤒🏥"
61+
}
62+
]
63+
}
64+
]
65+
}

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)