diff --git a/strings/camel_case_to_snake_case.py b/strings/camel_case_to_snake_case.py index 582907be2edb..a01b8e4c06bb 100644 --- a/strings/camel_case_to_snake_case.py +++ b/strings/camel_case_to_snake_case.py @@ -5,8 +5,8 @@ def camel_to_snake_case(input_str: str) -> str: >>> camel_to_snake_case("someRandomString") 'some_random_string' - >>> camel_to_snake_case("SomeRandomStr#ng") - 'some_random_str_ng' + >>> camel_to_snake_case("SomeRandomString") + 'some_random_string' >>> camel_to_snake_case("123someRandom123String123") '123_some_random_123_string_123' diff --git a/strings/number_guessing.py b/strings/number_guessing.py new file mode 100644 index 000000000000..5c727b1cc7a6 --- /dev/null +++ b/strings/number_guessing.py @@ -0,0 +1,76 @@ +"""Number Guessing Game. + +A simple game where the user guesses a randomly generated number +within a user-defined range. + +Reference: https://en.wikipedia.org/wiki/Guessing_game +""" + +import random + + +def get_top_of_range() -> int: + """ + Get the upper bound of the guessing range from the user. + + >>> isinstance(get_top_of_range(), int) + True + """ + top_of_range = input("Enter the upper bound number: ") + if top_of_range.isdigit(): + top_of_range = int(top_of_range) + if top_of_range <= 0: + print("Please enter a number larger than 0.") + quit() + else: + print("Please enter a valid digit.") + quit() + return top_of_range + + +def get_guess() -> int: + """ + Get a valid integer guess from the user. + + >>> isinstance(get_guess(), int) + True + """ + while True: + guess = input("Please make a guess: ") + if guess.isdigit(): + return int(guess) + print("Please type a number next time.") + + +def play_game(top_of_range: int) -> int: + """ + Play the number guessing game and return the number of guesses. + + >>> import random + >>> random.seed(42) + >>> play_game(1) + Whoa! You got it. + 1 + """ + random_number = random.randint(0, top_of_range) + guesses = 0 + + while True: + guesses += 1 + guess = get_guess() + + if guess == random_number: + print("Whoa! You got it.") + break + elif guess < random_number: + print("Too low! Try higher.") + else: + print("Too high! Try lower.") + + return guesses + + +if __name__ == "__main__": + top = get_top_of_range() + total_guesses = play_game(top) + print(f"You got it in {total_guesses} guesses!") diff --git a/strings/secret_language.py b/strings/secret_language.py new file mode 100644 index 000000000000..98d5f098e062 --- /dev/null +++ b/strings/secret_language.py @@ -0,0 +1,93 @@ +# Encoding & Decoding + +import random +import string + + +def random_chars() -> str: + """ + Generate a random string of 3 ASCII letters. + + >>> import random + >>> len(random_chars()) == 3 + True + + + >>> all(c in string.ascii_letters for c in random_chars()) + True + + + + """ + return "".join(random.choices(string.ascii_letters, k=3)) + + +def random_digits() -> str: + """ + create a random string of 3 digits. + + >>> len(random_digits()) == 3 + True + + + + >>> all(c in string.digits for c in random_digits()) + True + """ + return "".join(random.choices(string.digits, k=3)) + + +def encode(code: str) -> str: + """ + Encode a string by shifting the first character to the end and + wrapping it with random padding of 3 letters and 3 digits on each side. + + Reference: https://en.wikipedia.org/wiki/Caesar_cipher + + >>> len(encode('hello')) == len('hello') + 12 + True + >>> len(encode('hi')) == len('hi') + 12 + True + + + """ + if len(code) >= 3: + code = code[1:] + code[0] + code = ( + random_chars() + random_digits() + code + random_digits() + random_chars() + ) + else: + code = code[::-1] + code = ( + random_chars() + random_digits() + code + random_digits() + random_chars() + ) + return code + + +def decode(code: str) -> str: + """ + Decode an encoded string by stripping the random padding and + reversing the character shift. + + >>> decode(encode('hello')) + 'hello' + >>> decode(encode('hi')) + 'hi' + >>> decode(encode('python')) + 'python' + + + + """ + code = code[6:-6] + code = code[-1] + code[:-1] if len(code) >= 3 else code[::-1] + return code + + +if __name__ == "__main__": + code = input("Enter the code: ") + encoded = encode(code) + decoded = decode(encoded) + print(f"Original → {code}") + print(f"Encoded → {encoded}") + print(f"Decoded → {decoded}")