seed: curriculum content
This commit is contained in:
43
1.solar-system/3.strings/05.the-mirror/index.md
Normal file
43
1.solar-system/3.strings/05.the-mirror/index.md
Normal file
@@ -0,0 +1,43 @@
|
||||
---
|
||||
type: challenge
|
||||
title: "The Mirror"
|
||||
xp: 75
|
||||
duration: 30
|
||||
difficulty: 3
|
||||
---
|
||||
|
||||
# The Mirror
|
||||
|
||||
> **[INCOMING — Mission Control, Earth]**
|
||||
>
|
||||
> Cadet, a *palindrome* reads the same forwards and backwards.
|
||||
> `racecar`. `level`. `madam`.
|
||||
>
|
||||
> A reverse-slice flips a string in one move:
|
||||
>
|
||||
> ```python
|
||||
> "Hello"[::-1] # "olleH"
|
||||
> ```
|
||||
>
|
||||
> A string is a palindrome when it equals its reverse. The `==`
|
||||
> operator returns `True` or `False` directly.
|
||||
>
|
||||
> Implement `mirror(s)` that returns a dict with three keys:
|
||||
>
|
||||
> - `original` — the input
|
||||
> - `reversed` — the input reversed
|
||||
> - `palindrome` — `True` if equal to its reverse
|
||||
>
|
||||
> Match case exactly — `racecar` and `RaceCar` are different inputs.
|
||||
>
|
||||
> [END TRANSMISSION]
|
||||
|
||||
## Your Task
|
||||
|
||||
In `starter/starter.py`, build the dict using `[::-1]` and `==`.
|
||||
|
||||
## Objectives
|
||||
|
||||
- `mirror("racecar")["palindrome"]` is `True`
|
||||
- `mirror("hello")["palindrome"]` is `False`
|
||||
- All three keys correct for any input string
|
||||
19
1.solar-system/3.strings/05.the-mirror/starter/starter.py
Normal file
19
1.solar-system/3.strings/05.the-mirror/starter/starter.py
Normal file
@@ -0,0 +1,19 @@
|
||||
def mirror(s):
|
||||
"""Detect if s is a palindrome and return a dict.
|
||||
|
||||
Keys:
|
||||
"original" — s unchanged
|
||||
"reversed" — s reversed
|
||||
"palindrome" — True if s equals its reverse, False otherwise
|
||||
|
||||
Reverse a string with [::-1]:
|
||||
"Hello"[::-1] -> "olleH"
|
||||
|
||||
mirror("racecar") -> {"original": "racecar",
|
||||
"reversed": "racecar",
|
||||
"palindrome": True}
|
||||
mirror("hello") -> {"original": "hello",
|
||||
"reversed": "olleh",
|
||||
"palindrome": False}
|
||||
"""
|
||||
pass
|
||||
@@ -0,0 +1,27 @@
|
||||
from solution import mirror
|
||||
|
||||
|
||||
def test_palindrome():
|
||||
r = mirror("racecar")
|
||||
assert r == {"original": "racecar", "reversed": "racecar", "palindrome": True}
|
||||
|
||||
|
||||
def test_not_palindrome():
|
||||
r = mirror("hello")
|
||||
assert r == {"original": "hello", "reversed": "olleh", "palindrome": False}
|
||||
|
||||
|
||||
def test_level():
|
||||
r = mirror("level")
|
||||
assert r["palindrome"] is True
|
||||
|
||||
|
||||
def test_andromeda():
|
||||
r = mirror("andromeda")
|
||||
assert r["palindrome"] is False
|
||||
assert r["reversed"] == "ademordna"
|
||||
|
||||
|
||||
def test_noon():
|
||||
r = mirror("noon")
|
||||
assert r == {"original": "noon", "reversed": "noon", "palindrome": True}
|
||||
Reference in New Issue
Block a user