seed: curriculum content
This commit is contained in:
43
1.solar-system/3.strings/03.the-methods/index.md
Normal file
43
1.solar-system/3.strings/03.the-methods/index.md
Normal file
@@ -0,0 +1,43 @@
|
||||
---
|
||||
type: challenge
|
||||
title: "The Methods"
|
||||
xp: 50
|
||||
duration: 30
|
||||
difficulty: 2
|
||||
---
|
||||
|
||||
# The Methods
|
||||
|
||||
> **[INCOMING — Mission Control, Earth]**
|
||||
>
|
||||
> Cadet, every string carries a toolkit of methods. The most-used:
|
||||
>
|
||||
> - `s.upper()` — uppercase copy
|
||||
> - `s.lower()` — lowercase copy
|
||||
> - `s.strip()` — drops leading/trailing whitespace
|
||||
> - `s.replace(a, b)` — every `a` becomes `b`
|
||||
>
|
||||
> Methods can be *chained* — each returns a new string you can call
|
||||
> the next on:
|
||||
>
|
||||
> ```python
|
||||
> " Hello World ".strip().lower().replace(" ", "_")
|
||||
> # → "hello_world"
|
||||
> ```
|
||||
>
|
||||
> Implement `transform(s)` that returns a dict with three keys:
|
||||
>
|
||||
> - `upper` — input uppercased
|
||||
> - `lower` — input lowercased
|
||||
> - `clean` — input stripped, lowercased, spaces replaced by `_`
|
||||
>
|
||||
> [END TRANSMISSION]
|
||||
|
||||
## Your Task
|
||||
|
||||
In `starter/starter.py`, build the dict using the four methods.
|
||||
|
||||
## Objectives
|
||||
|
||||
- `transform(" Hello World ")["clean"]` returns `"hello_world"`
|
||||
- All three keys present and correct for any string
|
||||
15
1.solar-system/3.strings/03.the-methods/starter/starter.py
Normal file
15
1.solar-system/3.strings/03.the-methods/starter/starter.py
Normal file
@@ -0,0 +1,15 @@
|
||||
def transform(s):
|
||||
"""Apply three string transformations and return them in a dict.
|
||||
|
||||
Keys:
|
||||
"upper" — s.upper()
|
||||
"lower" — s.lower()
|
||||
"clean" — s stripped of leading/trailing whitespace,
|
||||
lowercased, with spaces replaced by underscores
|
||||
|
||||
transform(" Hello World ") returns:
|
||||
{"upper": " HELLO WORLD ",
|
||||
"lower": " hello world ",
|
||||
"clean": "hello_world"}
|
||||
"""
|
||||
pass
|
||||
@@ -0,0 +1,22 @@
|
||||
from solution import transform
|
||||
|
||||
|
||||
def test_padded_hello_world():
|
||||
r = transform(" Hello World ")
|
||||
assert r["upper"] == " HELLO WORLD "
|
||||
assert r["lower"] == " hello world "
|
||||
assert r["clean"] == "hello_world"
|
||||
|
||||
|
||||
def test_mission_ready():
|
||||
r = transform("MISSION READY")
|
||||
assert r["upper"] == "MISSION READY"
|
||||
assert r["lower"] == "mission ready"
|
||||
assert r["clean"] == "mission_ready"
|
||||
|
||||
|
||||
def test_andromeda_sector():
|
||||
r = transform("andromeda sector")
|
||||
assert r["upper"] == "ANDROMEDA SECTOR"
|
||||
assert r["lower"] == "andromeda sector"
|
||||
assert r["clean"] == "andromeda_sector"
|
||||
Reference in New Issue
Block a user