Skip to content

Commit 6e2fa9e

Browse files
Add micro-blog (#478)
1 parent 01ca48f commit 6e2fa9e

File tree

7 files changed

+210
-0
lines changed

7 files changed

+210
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,14 @@
574574
"prerequisites": [],
575575
"difficulty": 3
576576
},
577+
{
578+
"slug": "micro-blog",
579+
"name": "Micro Blog",
580+
"uuid": "0441593f-23d3-42b8-aa21-b47166c7c86d",
581+
"practices": [],
582+
"prerequisites": [],
583+
"difficulty": 3
584+
},
577585
{
578586
"slug": "perfect-numbers",
579587
"name": "Perfect Numbers",
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+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"micro_blog.zig"
8+
],
9+
"test": [
10+
"test_micro_blog.zig"
11+
],
12+
"example": [
13+
".meta/example.zig"
14+
]
15+
},
16+
"blurb": "Given an input string, truncate it to 5 characters."
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
pub fn truncate(phrase: []const u8) []const u8 {
2+
var index: usize = 0;
3+
var remaining: usize = 6;
4+
while (index < phrase.len) {
5+
if (phrase[index] & 0xc0 != 0x80) {
6+
remaining -= 1;
7+
if (remaining == 0) {
8+
// start of 6th character
9+
break;
10+
}
11+
}
12+
index += 1;
13+
}
14+
15+
return phrase[0..index];
16+
}
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pub fn truncate(phrase: []const u8) []const u8 {
2+
_ = phrase;
3+
@compileError("please implement the truncate function");
4+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
const std = @import("std");
2+
const testing = std.testing;
3+
4+
const micro_blog = @import("micro_blog.zig");
5+
6+
test "English language short" {
7+
const expected: []const u8 = "Hi";
8+
const actual = micro_blog.truncate("Hi");
9+
try testing.expectEqualStrings(expected, actual);
10+
}
11+
12+
test "English language long" {
13+
const expected: []const u8 = "Hello";
14+
const actual = micro_blog.truncate("Hello there");
15+
try testing.expectEqualStrings(expected, actual);
16+
}
17+
18+
test "German language short (broth)" {
19+
const expected: []const u8 = "brühe";
20+
const actual = micro_blog.truncate("brühe");
21+
try testing.expectEqualStrings(expected, actual);
22+
}
23+
24+
test "German language long (bear carpet → beards)" {
25+
const expected: []const u8 = "Bärte";
26+
const actual = micro_blog.truncate("Bärteppich");
27+
try testing.expectEqualStrings(expected, actual);
28+
}
29+
30+
test "Bulgarian language short (good)" {
31+
const expected: []const u8 = "Добър";
32+
const actual = micro_blog.truncate("Добър");
33+
try testing.expectEqualStrings(expected, actual);
34+
}
35+
36+
test "Greek language short (health)" {
37+
const expected: []const u8 = "υγειά";
38+
const actual = micro_blog.truncate("υγειά");
39+
try testing.expectEqualStrings(expected, actual);
40+
}
41+
42+
test "Maths short" {
43+
const expected: []const u8 = "a=πr²";
44+
const actual = micro_blog.truncate("a=πr²");
45+
try testing.expectEqualStrings(expected, actual);
46+
}
47+
48+
test "Maths long" {
49+
const expected: []const u8 = "∅⊊ℕ⊊ℤ";
50+
const actual = micro_blog.truncate("∅⊊ℕ⊊ℤ⊊ℚ⊊ℝ⊊ℂ");
51+
try testing.expectEqualStrings(expected, actual);
52+
}
53+
54+
test "English and emoji short" {
55+
const expected: []const u8 = "Fly 🛫";
56+
const actual = micro_blog.truncate("Fly 🛫");
57+
try testing.expectEqualStrings(expected, actual);
58+
}
59+
60+
test "Emoji short" {
61+
const expected: []const u8 = "💇";
62+
const actual = micro_blog.truncate("💇");
63+
try testing.expectEqualStrings(expected, actual);
64+
}
65+
66+
test "Emoji long" {
67+
const expected: []const u8 = "❄🌡🤧🤒🏥";
68+
const actual = micro_blog.truncate("❄🌡🤧🤒🏥🕰😀");
69+
try testing.expectEqualStrings(expected, actual);
70+
}
71+
72+
test "Royal Flush?" {
73+
const expected: []const u8 = "🃎🂸🃅🃋🃍";
74+
const actual = micro_blog.truncate("🃎🂸🃅🃋🃍🃁🃊");
75+
try testing.expectEqualStrings(expected, actual);
76+
}
77+
78+
test "ideograms" {
79+
const expected: []const u8 = "二兎を追う";
80+
const actual = micro_blog.truncate("二兎を追う者は一兎をも得ず");
81+
try testing.expectEqualStrings(expected, actual);
82+
}

0 commit comments

Comments
 (0)