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.
feat: Solution and Test for Sponge Case Challenge
- Loading branch information
1 parent
880841e
commit 24367d6
Showing
3 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
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 |
---|---|---|
@@ -1 +1,20 @@ | ||
# Solutions | ||
# 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 Link: [GitHub Issue](https:https://github.com/MIT-Emerging-Talent/ET6-foundations-group-04/issues/2) |
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,39 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
A module for converting strings into SpongeCase (alternating lower and upper case). | ||
Module contents: | ||
- spongecase: Converts a string into SpongeCase | ||
Created on 28/12/2024 | ||
Author: Chrismy Leprince Paul Augustin | ||
""" | ||
|
||
|
||
def spongecase(text: str) -> str: | ||
"""Converts a string into SpongeCase, where letters alternate between lower | ||
and upper case, starting with lower case. | ||
Parameters: | ||
text: str, a string to be converted into SpongeCase | ||
Returns -> str: The SpongeCase version of the input string | ||
Examples: | ||
>>> spongecase("hello world") | ||
'hElLo wOrLd' | ||
>>> spongecase("PYTHON") | ||
'pYtHoN' | ||
>>> spongecase("HELLO!") | ||
'hElLo!' | ||
""" | ||
result_chars = [] | ||
for index, char in enumerate(text): | ||
if index % 2 == 0: | ||
# Even index -> lowercase | ||
result_chars.append(char.lower()) | ||
else: | ||
# Odd index -> uppercase | ||
result_chars.append(char.upper()) | ||
return "".join(result_chars) |
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,82 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Test module for spongecase function. | ||
Test categories: | ||
- Basic cases: typical strings, single words | ||
- Edge cases: empty string, all uppercase, all lowercase | ||
- Special characters: punctuation, digits, symbols | ||
Created on 2024-12-28 | ||
Author: Chrismy Leprince Paul Augustin | ||
""" | ||
|
||
import unittest | ||
from solutions.spongecase import spongecase | ||
|
||
|
||
class TestSpongeCase(unittest.TestCase): | ||
"""Test suite for the spongecase function.""" | ||
|
||
def test_spongecase_basic(self): | ||
""" | ||
It should convert a typical string containing spaces into SpongeCase. | ||
Example: | ||
"hello world" -> "hElLo wOrLd" | ||
""" | ||
self.assertEqual(spongecase("hello world"), "hElLo wOrLd") | ||
|
||
def test_spongecase_single_word(self): | ||
""" | ||
It should convert a single word into SpongeCase. | ||
Example: | ||
"Python" -> "pYtHoN" | ||
""" | ||
self.assertEqual(spongecase("Python"), "pYtHoN") | ||
|
||
def test_spongecase_empty_string(self): | ||
""" | ||
It should handle an empty string by returning an empty string. | ||
Example: | ||
"" -> "" | ||
""" | ||
self.assertEqual(spongecase(""), "") | ||
|
||
def test_spongecase_all_caps(self): | ||
""" | ||
It should convert an all-caps string into SpongeCase. | ||
Example: | ||
"HELLO" -> "hElLo" | ||
""" | ||
self.assertEqual(spongecase("HELLO"), "hElLo") | ||
|
||
def test_spongecase_all_lowercase(self): | ||
""" | ||
It should convert an all-lowercase string into SpongeCase. | ||
Example: | ||
"world" -> "wOrLd" | ||
""" | ||
self.assertEqual(spongecase("world"), "wOrLd") | ||
|
||
def test_spongecase_special_characters(self): | ||
""" | ||
It should alternate case while preserving special characters | ||
and digits in place. | ||
Mapping indices: | ||
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 | ||
Result: "hElLo?1#aB" | ||
""" | ||
self.assertEqual(spongecase("Hello?1#Ab"), "hElLo?1#aB") | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |