Back to Freecodecamp

Challenge 163: Consonant Case

curriculum/challenges/english/blocks/daily-coding-challenges-python/694596b0585c11170ac7c7fb.md

latest1.7 KB
Original Source

--description--

Given a string representing a variable name, convert it to consonant case using the following rules:

  • All consonants should be converted to uppercase.
  • All vowels (a, e, i, o, u in any case) should be converted to lowercase.
  • All hyphens (-) should be converted to underscores (_).

--hints--

to_consonant_case("helloworld") should return "HeLLoWoRLD".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(to_consonant_case("helloworld"), "HeLLoWoRLD")`)
}})

to_consonant_case("HELLOWORLD") should return "HeLLoWoRLD".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(to_consonant_case("HELLOWORLD"), "HeLLoWoRLD")`)
}})

to_consonant_case("_hElLO-WOrlD-") should return "_HeLLo_WoRLD_".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(to_consonant_case("_hElLO-WOrlD-"), "_HeLLo_WoRLD_")`)
}})

to_consonant_case("_~-generic_~-variable_~-name_~-here-~_") should return "_~_GeNeRiC_~_VaRiaBLe_~_NaMe_~_HeRe_~_".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(to_consonant_case("_~-generic_~-variable_~-name_~-here-~_"), "_~_GeNeRiC_~_VaRiaBLe_~_NaMe_~_HeRe_~_")`)
}})

--seed--

--seed-contents--

py
def to_consonant_case(s):

    return s

--solutions--

py
def to_consonant_case(s):
    vowels = "aeiouAEIOU"
    result = ""

    for char in s:
        if char == "-":
            result += "_"
        elif char.isalpha():
            if char in vowels:
                result += char.lower()
            else:
                result += char.upper()
        else:
            result += char

    return result