Skip to content

Commit

Permalink
Edits to fix formatting issue
Browse files Browse the repository at this point in the history
  • Loading branch information
likechrisss committed Dec 29, 2024
1 parent 3585f31 commit 8ec9849
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 31 deletions.
25 changes: 8 additions & 17 deletions solutions/spongecase.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,28 @@
A module for converting strings into SpongeCase (alternating lower and upper case).
Module contents:
- spongecase: Converts a string into SpongeCase
- spongecase: Converts a string into SpongeCase
Created on 28/12/2024
Author: Chrismy Leprince Paul Augustin
"""


def spongecase(text: str) -> str:
"""
Convert a given string into SpongeCase, where letters alternate
between lower and upper case, starting with lower case.
"""Converts a 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.
Parameters:
text: str, a string to be converted into SpongeCase
Returns:
A new string in SpongeCase format.
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):
Expand Down
50 changes: 36 additions & 14 deletions solutions/tests/test_spongecase.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,72 @@
Created on 2024-12-28
Author: Chrismy Leprince Paul Augustin
"""
import unittest

import unittest
from solutions.spongecase import spongecase


class TestSpongeCase(unittest.TestCase):
"""Unit tests for the spongecase function."""
"""Test suite for the spongecase function."""

def test_spongecase_basic(self):
"""
Test spongecase with a basic string containing spaces.
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):
"""
Test spongecase with a single word.
It should convert a single word into SpongeCase.
Example:
"Python" -> "pYtHoN"
"""
self.assertEqual(spongecase("Python"), "pYtHoN")

def test_spongecase_empty_string(self):
"""
Test spongecase with an empty string.
It should handle an empty string by returning an empty string.
Example:
"" -> ""
"""
self.assertEqual(spongecase(""), "")

def test_spongecase_all_caps(self):
"""
Test spongecase with all caps.
It should convert an all-caps string into SpongeCase.
Example:
"HELLO" -> "hElLo"
"""
self.assertEqual(spongecase("HELLO"), "hElLo")

def test_spongecase_all_lowercase(self):
"""
Test spongecase with only lowercase.
It should convert an all-lowercase string into SpongeCase.
Example:
"world" -> "wOrLd"
"""
self.assertEqual(spongecase("world"), "wOrLd")

def test_spongecase_special_characters(self):
"""
Test spongecase with special characters.
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"
"""
# 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()

0 comments on commit 8ec9849

Please sign in to comment.