diff --git a/solutions/README.md b/solutions/README.md deleted file mode 100644 index 9852346d2..000000000 --- a/solutions/README.md +++ /dev/null @@ -1 +0,0 @@ -# Solutions diff --git a/solutions/challenge_2/README.md b/solutions/challenge_2/README.md new file mode 100644 index 000000000..3b3ba87af --- /dev/null +++ b/solutions/challenge_2/README.md @@ -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) diff --git a/solutions/tests/__init__.py b/solutions/challenge_2/__init__ .py similarity index 100% rename from solutions/tests/__init__.py rename to solutions/challenge_2/__init__ .py diff --git a/solutions/challenge_2/spongecase_challenge.py b/solutions/challenge_2/spongecase_challenge.py new file mode 100644 index 000000000..47e0c25ff --- /dev/null +++ b/solutions/challenge_2/spongecase_challenge.py @@ -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) diff --git a/solutions/tests/README.md b/solutions/test/challenge_2/README.md similarity index 100% rename from solutions/tests/README.md rename to solutions/test/challenge_2/README.md diff --git a/solutions/test/challenge_2/__init__.py b/solutions/test/challenge_2/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/solutions/test/challenge_2/test_spongecase_challenge.py b/solutions/test/challenge_2/test_spongecase_challenge.py new file mode 100644 index 000000000..e08712ca0 --- /dev/null +++ b/solutions/test/challenge_2/test_spongecase_challenge.py @@ -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() diff --git a/solutions/tests/test_example.py b/solutions/tests/test_example.py deleted file mode 100644 index 5e6cd7a00..000000000 --- a/solutions/tests/test_example.py +++ /dev/null @@ -1,11 +0,0 @@ -"""Test example module.""" - -import unittest - - -class TestExample(unittest.TestCase): - """Test example class.""" - - def test_addition(self): - """Test addition method.""" - self.assertEqual(1 + 1, 2)