Skip to content

Commit

Permalink
Solution for Sponge case challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
likechrisss committed Dec 29, 2024
1 parent 35e91b8 commit 40ff42f
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 12 deletions.
1 change: 0 additions & 1 deletion solutions/README.md

This file was deleted.

20 changes: 20 additions & 0 deletions solutions/challenge_2/README.md
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.
32 changes: 32 additions & 0 deletions solutions/challenge_2/spongecase_challenge.py
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.
53 changes: 53 additions & 0 deletions solutions/test/challenge_2/test_spongecase_challenge.py
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()
11 changes: 0 additions & 11 deletions solutions/tests/test_example.py

This file was deleted.

0 comments on commit 40ff42f

Please sign in to comment.