Skip to content

Commit 7eb1680

Browse files
authored
Add micro-blog (#335)
1 parent b483f08 commit 7eb1680

File tree

7 files changed

+169
-0
lines changed

7 files changed

+169
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,14 @@
287287
"prerequisites": [],
288288
"difficulty": 2
289289
},
290+
{
291+
"slug": "micro-blog",
292+
"name": "Micro Blog",
293+
"uuid": "13eb6029-cf8b-4a2f-bc76-f18e8883fd5b",
294+
"practices": [],
295+
"prerequisites": [],
296+
"difficulty": 2
297+
},
290298
{
291299
"slug": "nucleotide-count",
292300
"name": "Nucleotide Count",
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+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"micro-blog.coffee"
8+
],
9+
"test": [
10+
"micro-blog.spec.coffee"
11+
],
12+
"example": [
13+
".meta/example.coffee"
14+
]
15+
},
16+
"blurb": "Given an input string, truncate it to 5 characters."
17+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Microblog
2+
@truncate: (phrase) ->
3+
Array.from phrase
4+
.slice 0, 5
5+
.join ''
6+
7+
module.exports = Microblog
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+
class Microblog
2+
@truncate: (phrase) ->
3+
4+
module.exports = Microblog
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
Microblog = require './micro-blog'
2+
3+
describe 'Micro Blog', ->
4+
it 'English language short', ->
5+
result = Microblog.truncate 'Hi'
6+
expect(result).toEqual 'Hi'
7+
8+
xit 'English language long', ->
9+
result = Microblog.truncate 'Hello there'
10+
expect(result).toEqual 'Hello'
11+
12+
xit 'German language short (broth)', ->
13+
result = Microblog.truncate 'brühe'
14+
expect(result).toEqual 'brühe'
15+
16+
xit 'German language long (bear carpet → beards)', ->
17+
result = Microblog.truncate 'Bärteppich'
18+
expect(result).toEqual 'Bärte'
19+
20+
xit 'Bulgarian language short (good)', ->
21+
result = Microblog.truncate 'Добър'
22+
expect(result).toEqual 'Добър'
23+
24+
xit 'Greek language short (health)', ->
25+
result = Microblog.truncate 'υγειά'
26+
expect(result).toEqual 'υγειά'
27+
28+
xit 'Maths short', ->
29+
result = Microblog.truncate 'a=πr²'
30+
expect(result).toEqual 'a=πr²'
31+
32+
xit 'Maths long', ->
33+
result = Microblog.truncate '∅⊊ℕ⊊ℤ⊊ℚ⊊ℝ⊊ℂ'
34+
expect(result).toEqual '∅⊊ℕ⊊ℤ'
35+
36+
xit 'English and emoji short', ->
37+
result = Microblog.truncate 'Fly 🛫'
38+
expect(result).toEqual 'Fly 🛫'
39+
40+
xit 'Emoji short', ->
41+
result = Microblog.truncate '💇'
42+
expect(result).toEqual '💇'
43+
44+
xit 'Emoji long', ->
45+
result = Microblog.truncate '❄🌡🤧🤒🏥🕰😀'
46+
expect(result).toEqual '❄🌡🤧🤒🏥'
47+
48+
xit 'Royal Flush?', ->
49+
result = Microblog.truncate '🃎🂸🃅🃋🃍🃁🃊'
50+
expect(result).toEqual '🃎🂸🃅🃋🃍'

0 commit comments

Comments
 (0)