Skip to content

Commit 0d55ecc

Browse files
Add micro-blog (#269)
1 parent da34d3c commit 0d55ecc

File tree

7 files changed

+170
-0
lines changed

7 files changed

+170
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,14 @@
336336
"error_handling"
337337
]
338338
},
339+
{
340+
"slug": "micro-blog",
341+
"name": "Micro Blog",
342+
"uuid": "8d36d22e-f33f-4753-9d10-486159a256ab",
343+
"practices": [],
344+
"prerequisites": [],
345+
"difficulty": 2
346+
},
339347
{
340348
"slug": "resistor-color",
341349
"name": "Resistor Color",
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"source/micro_blog.d"
8+
],
9+
"test": [
10+
"source/micro_blog.d"
11+
],
12+
"example": [
13+
"example/micro_blog.d"
14+
]
15+
},
16+
"blurb": "Given an input string, truncate it to 5 characters."
17+
}
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?"

exercises/practice/micro-blog/dub.sdl

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name "micro-blog"
2+
buildRequirements "disallowDeprecations"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module micro_blog;
2+
3+
import std.algorithm : min;
4+
import std.conv : to;
5+
6+
pure string truncate(immutable string phrase)
7+
{
8+
dstring str = phrase.to!dstring;
9+
return str[0..min(5, str.length)].to!string;
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
module micro_blog;
2+
3+
pure string truncate(immutable string phrase)
4+
{
5+
// implement this function
6+
}
7+
8+
unittest
9+
{
10+
immutable int allTestsEnabled = 0;
11+
12+
// English language short
13+
assert(truncate("Hi") == "Hi");
14+
15+
static if (allTestsEnabled)
16+
{
17+
// English language long
18+
assert(truncate("Hello there") == "Hello");
19+
20+
// German language short (broth)
21+
assert(truncate("brühe") == "brühe");
22+
23+
// German language long (bear carpet → beards)
24+
assert(truncate("Bärteppich") == "Bärte");
25+
26+
// Bulgarian language short (good)
27+
assert(truncate("Добър") == "Добър");
28+
29+
// Greek language short (health)
30+
assert(truncate("υγειά") == "υγειά");
31+
32+
// Maths short
33+
assert(truncate("a=πr²") == "a=πr²");
34+
35+
// Maths long
36+
assert(truncate("∅⊊ℕ⊊ℤ⊊ℚ⊊ℝ⊊ℂ") == "∅⊊ℕ⊊ℤ");
37+
38+
// English and emoji short
39+
assert(truncate("Fly 🛫") == "Fly 🛫");
40+
41+
// Emoji short
42+
assert(truncate("💇") == "💇");
43+
44+
// Emoji long
45+
assert(truncate("❄🌡🤧🤒🏥🕰😀") == "❄🌡🤧🤒🏥");
46+
47+
// Royal Flush?
48+
assert(truncate("🃎🂸🃅🃋🃍🃁🃊") == "🃎🂸🃅🃋🃍");
49+
}
50+
}

0 commit comments

Comments
 (0)