-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added micro_blog #3268
base: main
Are you sure you want to change the base?
added micro_blog #3268
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,37 @@ | ||||||||||
# Instructions | ||||||||||
|
||||||||||
You have identified a gap in the social media market for very very short posts. | ||||||||||
Now that Twitter allows 280 character posts, people wanting quick social media updates aren't being served. | ||||||||||
You decide to create your own social media network. | ||||||||||
|
||||||||||
To make your product noteworthy, you make it extreme and only allow posts of 5 or less characters. | ||||||||||
Any posts of more than 5 characters should be truncated to 5. | ||||||||||
|
||||||||||
To allow your users to express themselves fully, you allow Emoji and other Unicode. | ||||||||||
|
||||||||||
The task is to truncate input strings to 5 characters. | ||||||||||
|
||||||||||
## Text Encodings | ||||||||||
|
||||||||||
Text stored digitally has to be converted to a series of bytes. | ||||||||||
There are 3 ways to map characters to bytes in common use. | ||||||||||
|
||||||||||
- **ASCII** can encode English language characters. | ||||||||||
All characters are precisely 1 byte long. | ||||||||||
- **UTF-8** is a Unicode text encoding. | ||||||||||
Characters take between 1 and 4 bytes. | ||||||||||
- **UTF-16** is a Unicode text encoding. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
Characters are either 2 or 4 bytes long. | ||||||||||
|
||||||||||
UTF-8 and UTF-16 are both Unicode encodings which means they're capable of representing a massive range of characters including: | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
- Text in most of the world's languages and scripts | ||||||||||
- Historic text | ||||||||||
- Emoji | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
UTF-8 and UTF-16 are both variable length encodings, which means that different characters take up different amounts of space. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
Consider the letter 'a' and the emoji '😛'. | ||||||||||
In UTF-16 the letter takes 2 bytes but the emoji takes 4 bytes. | ||||||||||
|
||||||||||
The trick to this exercise is to use APIs designed around Unicode characters (codepoints) instead of Unicode codeunits. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,19 @@ | ||||||
{ | ||||||
"blurb": "Given an input string, truncate it to 5 characters.", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
"authors": [ | ||||||
"meatball133", | ||||||
"Bethanyg" | ||||||
], | ||||||
"contributors": [], | ||||||
"files": { | ||||||
"solution": [ | ||||||
"micro_blog.py" | ||||||
], | ||||||
"test": [ | ||||||
"micro_blog_test.py" | ||||||
], | ||||||
"example": [ | ||||||
".meta/example.py" | ||||||
] | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def truncate(letters): | ||
return letters[:5] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{%- import "generator_macros.j2" as macros with context -%} | ||
{% macro test_case(case) -%} | ||
{% if "→" in case["description"] %} | ||
def test_german_language_long(self): | ||
{% else %} | ||
def test_{{case["description"] | to_snake}}(self): | ||
{% endif %} | ||
self.assertEqual({{ case["property"] | to_snake }}("{{case["input"]["phrase"]}}"), | ||
"{{case["expected"]}}" | ||
) | ||
{%- endmacro %} | ||
{{ macros.header()}} | ||
|
||
class {{ exercise | camel_case }}Test(unittest.TestCase): | ||
{% for case in cases -%} | ||
{{ test_case(case) }} | ||
{% endfor %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# This is an auto-generated file. | ||
# | ||
# Regenerating this file via `configlet sync` will: | ||
# - Recreate every `description` key/value pair | ||
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications | ||
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) | ||
# - Preserve any other key/value pair | ||
# | ||
# As user-added comments (using the # character) will be removed when this file | ||
# is regenerated, comments can be added via a `comment` key. | ||
|
||
[b927b57f-7c98-42fd-8f33-fae091dc1efc] | ||
description = "English language short" | ||
|
||
[a3fcdc5b-0ed4-4f49-80f5-b1a293eac2a0] | ||
description = "English language long" | ||
|
||
[01910864-8e15-4007-9c7c-ac956c686e60] | ||
description = "German language short (broth)" | ||
|
||
[f263e488-aefb-478f-a671-b6ba99722543] | ||
description = "German language long (bear carpet → beards)" | ||
|
||
[0916e8f1-41d7-4402-a110-b08aa000342c] | ||
description = "Bulgarian language short (good)" | ||
|
||
[bed6b89c-03df-4154-98e6-a61a74f61b7d] | ||
description = "Greek language short (health)" | ||
|
||
[485a6a70-2edb-424d-b999-5529dbc8e002] | ||
description = "Maths short" | ||
|
||
[8b4b7b51-8f48-4fbe-964e-6e4e6438be28] | ||
description = "Maths long" | ||
|
||
[71f4a192-0566-4402-a512-fe12878be523] | ||
description = "English and emoji short" | ||
|
||
[6f0f71f3-9806-4759-a844-fa182f7bc203] | ||
description = "Emoji short" | ||
|
||
[ce71fb92-5214-46d0-a7f8-d5ba56b4cc6e] | ||
description = "Emoji long" | ||
|
||
[5dee98d2-d56e-468a-a1f2-121c3f7c5a0b] | ||
description = "Royal Flush?" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def truncate(string): | ||
return string[:5] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import unittest | ||
|
||
from micro_blog import ( | ||
truncate, | ||
) | ||
|
||
# Tests adapted from `problem-specifications//canonical-data.json` | ||
|
||
|
||
class MicroBlogTest(unittest.TestCase): | ||
def test_english_language_short(self): | ||
|
||
self.assertEqual(truncate("Hi"), "Hi") | ||
|
||
def test_english_language_long(self): | ||
|
||
self.assertEqual(truncate("Hello there"), "Hello") | ||
|
||
def test_german_language_short_broth(self): | ||
|
||
self.assertEqual(truncate("brühe"), "brühe") | ||
|
||
def test_german_language_long(self): | ||
|
||
self.assertEqual(truncate("Bärteppich"), "Bärte") | ||
|
||
def test_bulgarian_language_short_good(self): | ||
|
||
self.assertEqual(truncate("Добър"), "Добър") | ||
|
||
def test_greek_language_short_health(self): | ||
|
||
self.assertEqual(truncate("υγειά"), "υγειά") | ||
|
||
def test_maths_short(self): | ||
|
||
self.assertEqual(truncate("a=πr²"), "a=πr²") | ||
|
||
def test_maths_long(self): | ||
|
||
self.assertEqual(truncate("∅⊊ℕ⊊ℤ⊊ℚ⊊ℝ⊊ℂ"), "∅⊊ℕ⊊ℤ") | ||
|
||
def test_english_and_emoji_short(self): | ||
|
||
self.assertEqual(truncate("Fly 🛫"), "Fly 🛫") | ||
|
||
def test_emoji_short(self): | ||
|
||
self.assertEqual(truncate("💇"), "💇") | ||
|
||
def test_emoji_long(self): | ||
|
||
self.assertEqual(truncate("❄🌡🤧🤒🏥🕰😀"), "❄🌡🤧🤒🏥") | ||
|
||
def test_royal_flush(self): | ||
|
||
self.assertEqual(truncate("🃎🂸🃅🃋🃍🃁🃊"), "🃎🂸🃅🃋🃍") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.