Skip to content

Commit 213f578

Browse files
authored
Implement micro-blog exercise. (exercism#1805)
* Implement micro-blog exercise. * Fix implementation of reference solution. Needs to use appendCodePoint rather than append. * Use an instance method instead of a static class method. * Clean up the micro-blog exercise to fit latest changes.
1 parent 5e81dd8 commit 213f578

File tree

8 files changed

+207
-0
lines changed

8 files changed

+207
-0
lines changed

β€Žconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,16 @@
271271
"strings"
272272
]
273273
},
274+
{
275+
"slug": "micro-blog",
276+
"uuid": "8295ae71-5c0e-49d0-bbe9-9b43a85bf2dd",
277+
"core": false,
278+
"unlocked_by": "two-fer",
279+
"difficulty": 3,
280+
"topics": [
281+
"strings"
282+
]
283+
},
274284
{
275285
"slug": "yacht",
276286
"uuid": "0cb45688-9598-49aa-accc-ed48c5d6962d",
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class MicroBlog {
2+
public String truncate(String input) {
3+
return input.codePoints()
4+
.limit(5)
5+
.collect(
6+
StringBuilder::new,
7+
StringBuilder::appendCodePoint,
8+
StringBuilder::append)
9+
.toString();
10+
}
11+
}

β€Žexercises/micro-blog/.meta/version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.0.0

β€Žexercises/micro-blog/README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Micro Blog
2+
3+
You have identified a gap in the social media market for very very short
4+
posts. Now that Twitter allows 280 character posts, people wanting quick
5+
social media updates aren't being served. You decide to create your own
6+
social media network.
7+
8+
To make your product noteworthy, you make it extreme and only allow posts
9+
of 5 or less characters. Any posts of more than 5 characters should be
10+
truncated to 5.
11+
12+
To allow your users to express themselves fully, you allow Emoji and
13+
other Unicode.
14+
15+
The task is to truncate input strings to 5 characters.
16+
17+
## Text Encodings
18+
19+
Text stored digitally has to be converted to a series of bytes.
20+
There are 3 ways to map characters to bytes in common use.
21+
* **ASCII** can encode English language characters. All
22+
characters are precisely 1 byte long.
23+
* **UTF-8** is a Unicode text encoding. Characters take between 1
24+
and 4 bytes.
25+
* **UTF-16** is a Unicode text encoding. Characters are either 2 or
26+
4 bytes long.
27+
28+
UTF-8 and UTF-16 are both Unicode encodings which means they're capable of
29+
representing a massive range of characters including:
30+
* Text in most of the world's languages and scripts
31+
* Historic text
32+
* Emoji
33+
34+
UTF-8 and UTF-16 are both variable length encodings, which means that
35+
different characters take up different amounts of space.
36+
37+
Consider the letter 'a' and the emoji 'πŸ˜›'. In UTF-16 the letter takes
38+
2 bytes but the emoji takes 4 bytes.
39+
40+
The trick to this exercise is to use APIs designed around Unicode
41+
characters (codepoints) instead of Unicode codeunits.
42+
43+
## Setup
44+
45+
Go through the setup instructions for Java to install the necessary
46+
dependencies:
47+
48+
[https://exercism.io/tracks/java/installation](https://exercism.io/tracks/java/installation)
49+
50+
# Running the tests
51+
52+
You can run all the tests for an exercise by entering the following in your
53+
terminal:
54+
55+
```sh
56+
$ gradle test
57+
```
58+
59+
In the test suites all tests but the first have been skipped.
60+
61+
Once you get a test passing, you can enable the next one by removing the
62+
`@Ignore("Remove to run test")` annotation.
63+
64+
65+
## Submitting Incomplete Solutions
66+
It's possible to submit an incomplete solution so you can see how others have
67+
completed the exercise.

β€Žexercises/micro-blog/build.gradle

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apply plugin: "java"
2+
apply plugin: "eclipse"
3+
apply plugin: "idea"
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testCompile "junit:junit:4.13"
11+
testImplementation "org.assertj:assertj-core:3.15.0"
12+
}
13+
14+
test {
15+
testLogging {
16+
exceptionFormat = 'short'
17+
showStandardStreams = true
18+
events = ["passed", "failed", "skipped"]
19+
}
20+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class MicroBlog {
2+
public String truncate(String input) {
3+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
4+
}
5+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import static org.junit.Assert.assertEquals;
2+
3+
import org.junit.Ignore;
4+
import org.junit.Test;
5+
6+
public class MicroBlogTest {
7+
8+
private final MicroBlog microBlog = new MicroBlog();
9+
10+
@Test
11+
public void englishLanguageShort() {
12+
String expected = "Hi";
13+
assertEquals(expected, microBlog.truncate("Hi"));
14+
}
15+
16+
@Ignore("Remove to run test")
17+
@Test
18+
public void englishLanguageLong() {
19+
String expected = "Hello";
20+
assertEquals(expected, microBlog.truncate("Hello there"));
21+
}
22+
23+
@Ignore("Remove to run test")
24+
@Test
25+
public void germanLanguageShort_broth() {
26+
String expected = "brΓΌhe";
27+
assertEquals(expected, microBlog.truncate("brΓΌhe"));
28+
}
29+
30+
@Ignore("Remove to run test")
31+
@Test
32+
public void germanLanguageLong_bearCarpet_to_beards() {
33+
String expected = "BΓ€rte";
34+
assertEquals(expected, microBlog.truncate("BΓ€rteppich"));
35+
}
36+
37+
@Ignore("Remove to run test")
38+
@Test
39+
public void bulgarianLanguageShort_good() {
40+
String expected = "Π”ΠΎΠ±ΡŠΡ€";
41+
assertEquals(expected, microBlog.truncate("Π”ΠΎΠ±ΡŠΡ€"));
42+
}
43+
44+
@Ignore("Remove to run test")
45+
@Test
46+
public void greekLanguageShort_health() {
47+
String expected = "υγΡιά";
48+
assertEquals(expected, microBlog.truncate("υγΡιά"));
49+
}
50+
51+
@Ignore("Remove to run test")
52+
@Test
53+
public void mathsShort() {
54+
String expected = "a=Ο€rΒ²";
55+
assertEquals(expected, microBlog.truncate("a=Ο€rΒ²"));
56+
}
57+
58+
@Ignore("Remove to run test")
59+
@Test
60+
public void mathsLong() {
61+
String expected = "βˆ…βŠŠβ„•βŠŠβ„€";
62+
assertEquals(expected, microBlog.truncate("βˆ…βŠŠβ„•βŠŠβ„€βŠŠβ„šβŠŠβ„βŠŠβ„‚"));
63+
}
64+
65+
@Ignore("Remove to run test")
66+
@Test
67+
public void englishAndEmojiShort() {
68+
String expected = "Fly πŸ›«";
69+
assertEquals(expected, microBlog.truncate("Fly πŸ›«"));
70+
}
71+
72+
@Ignore("Remove to run test")
73+
@Test
74+
public void emojiShort() {
75+
String expected = "πŸ’‡";
76+
assertEquals(expected, microBlog.truncate("πŸ’‡"));
77+
}
78+
79+
@Ignore("Remove to run test")
80+
@Test
81+
public void emojiLong() {
82+
String expected = "β„πŸŒ‘πŸ€§πŸ€’πŸ₯";
83+
assertEquals(expected, microBlog.truncate("β„πŸŒ‘πŸ€§πŸ€’πŸ₯πŸ•°πŸ˜€"));
84+
}
85+
86+
@Ignore("Remove to run test")
87+
@Test
88+
public void royalFlush() {
89+
String expected = "πŸƒŽπŸ‚ΈπŸƒ…πŸƒ‹πŸƒ";
90+
assertEquals(expected, microBlog.truncate("πŸƒŽπŸ‚ΈπŸƒ…πŸƒ‹πŸƒπŸƒπŸƒŠ"));
91+
}
92+
}

β€Žexercises/settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ include 'markdown'
5555
include 'matching-brackets'
5656
include 'matrix'
5757
include 'meetup'
58+
include 'micro-blog'
5859
include 'minesweeper'
5960
include 'nth-prime'
6061
include 'nucleotide-count'

0 commit comments

Comments
Β (0)