Skip to content

Instantly share code, notes, and snippets.

@dawnandrew100
Last active June 5, 2024 16:18
Show Gist options
  • Save dawnandrew100/529375d8e8703f5a9565b4bfc115247e to your computer and use it in GitHub Desktop.
Save dawnandrew100/529375d8e8703f5a9565b4bfc115247e to your computer and use it in GitHub Desktop.
A case of snakes Kata challenge from hackattic
# Source: https://hackattic.com/kata/a_case_of_snakes
import re
"""
Main function is currently specifically for the challenge but
can be edited to fit any Hungary case to Snake case transfer needs
"""
def main():
for i in range(16):
hungary = input()
snake = to_Snake(hungary)
print(snake)
def to_Snake(case):
if case.startswith(("p","b","w","d","u","i","ch","dw","f","fn")):
if re.search('^[^A-Z][^A-Z]?[\d]?[\d]?[A-Z]', case):
snake = typedHungaryToSnake(case)
else:
snake = untypedHungaryToSnake(case)
else:
snake = untypedHungaryToSnake(case)
return snake
def typedHungaryToSnake(Hungary):
snake = re.findall('[A-Z][^A-Z]*', Hungary)
snake = "_".join(snake)
snake = snake.lower()
return snake
def untypedHungaryToSnake(Hungary):
front = re.search('[^A-Z]*', Hungary).group(0)
back = re.findall('[A-Z][^A-Z]*', Hungary)
snake = front + "_" + "_".join(back)
snake = snake.lower()
return snake
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment