forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
35e91b8
commit 40ff42f
Showing
8 changed files
with
105 additions
and
12 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Challenge: SpongeCase | ||
|
||
## Description | ||
|
||
SpongeCase is a style of text where letters alternately appear | ||
in lower and upper case. | ||
For example, the word in spongeCase would be `sPoNgEcAsE`. | ||
|
||
Write a function that converts the given string into spongeCase. | ||
|
||
## Example | ||
|
||
```python | ||
spongecase("hello world") | ||
# Output: "hElLo wOrLd" | ||
|
||
spongecase("Python") | ||
# Output: "pYtHoN" | ||
|
||
**Challenge Reference**: [ET6 Foundations Group 04 - Challenge #2: SpongeCase](https://github.com/MIT-Emerging-Talent/ET6-foundations-group-04/issues/2) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
def spongecase(text: str) -> str: | ||
""" | ||
Convert a given string into SpongeCase, where letters alternate | ||
between lower and upper case, starting with lower case. | ||
The position of the character in the string (0-indexed) determines | ||
its case: | ||
- Even indices: lower case | ||
- Odd indices: upper case | ||
Non-alphabetic characters are unaffected except they still | ||
participate in the alternation count. | ||
Args: | ||
text: The input string to be converted. | ||
Returns: | ||
A new string in SpongeCase format. | ||
Examples: | ||
>>> spongecase("hello world") | ||
'hElLo wOrLd' | ||
>>> spongecase("PYTHON") | ||
'pYtHoN' | ||
""" | ||
result_chars = [] | ||
for index, char in enumerate(text): | ||
if index % 2 == 0: | ||
result_chars.append(char.lower()) | ||
else: | ||
result_chars.append(char.upper()) | ||
return "".join(result_chars) |
File renamed without changes.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
""" | ||
This module contains unit tests for the `spongecase` function. | ||
The tests validate various scenarios, such as: | ||
- Basic strings with spaces | ||
- Single words | ||
- Empty strings | ||
""" | ||
import unittest | ||
from solutions.challenge_2.spongecase_challenge import spongecase | ||
|
||
class TestSpongeCase(unittest.TestCase): | ||
"""Unit tests for the spongecase function.""" | ||
|
||
def test_spongecase_basic(self): | ||
""" | ||
Test spongecase with a basic string containing spaces. | ||
""" | ||
self.assertEqual(spongecase("hello world"), "hElLo wOrLd") | ||
|
||
def test_spongecase_single_word(self): | ||
""" | ||
Test spongecase with a single word. | ||
""" | ||
self.assertEqual(spongecase("Python"), "pYtHoN") | ||
|
||
def test_spongecase_empty_string(self): | ||
""" | ||
Test spongecase with an empty string. | ||
""" | ||
self.assertEqual(spongecase(""), "") | ||
def test_spongecase_all_caps(self): | ||
""" | ||
Test spongecase with all caps. | ||
""" | ||
self.assertEqual(spongecase("HELLO"), "hElLo") | ||
|
||
def test_spongecase_all_lowercase(self): | ||
""" | ||
Test spongecase with only lowercase. | ||
""" | ||
self.assertEqual(spongecase("world"), "wOrLd") | ||
|
||
def test_spongecase_special_characters(self): | ||
""" | ||
Test spongecase with special characters. | ||
""" | ||
# Index: 0 1 2 3 4 5 6 7 8 9 | ||
# Char: H e l l o ? 1 # A b | ||
# Case: h E l L o ? 1 # a B | ||
# => "hElLo?1#aB" | ||
self.assertEqual(spongecase("Hello?1#Ab"), "hElLo?1#aB") | ||
if __name__ == "__main__": | ||
unittest.main() |
This file was deleted.
Oops, something went wrong.