Simplified the function, but there's still room for improvement (for example, make it accept multiple types, like it is suggested by the test cases)
std::string digest(const std::string param) { std::string result; for (char letter: param) { result += std::basic_string(1, letter) + " "; } result.pop_back(); return result; }
using namespace std;string digest(string param) {string result;for (int i = 0; i < param.size() - 1; i++) {result += param[i];result += ' ';- std::string digest(const std::string param) {
- std::string result;
- for (char letter: param) {
- result += std::basic_string(1, letter) + " ";
- }
result += param[param.size() - 1];- result.pop_back();
- return result;
- }
// TODO: Replace examples and use TDD by writing your own tests Describe(any_group_name_you_want) { It(should_do_something) { Assert::That(digest("Burger"), Equals("B u r g e r")); Assert::That(digest("FoEMfIp"), Equals("F o E M f I p")); Assert::That(digest("Haggis"), Equals("H a g g i s")); Assert::That(digest("chitlins"), Equals("c h i t l i n s")); Assert::That(digest("SPAM"), Equals("S P A M")); Assert::That(digest("S 1P 2A 3M"), Equals("S 1 P 2 A 3 M")); //Assert::That(digest(1234567), Equals("1 2 3 4 5 6 7")); //Assert::That(digest({1,2,3,4,5,6,7}), Equals("{ 1 , 2 , 3 , 4 , 5 , 6 , 7 }"")); //Assert::That(digest({1:'a',2:'b',3:'c'}), Equals("{ 1 : ' a ' , 2 : ' b ' , 3 : ' c ' }")); } };
- // TODO: Replace examples and use TDD by writing your own tests
- Describe(any_group_name_you_want)
- {
- It(should_do_something)
- {
- Assert::That(digest("Burger"), Equals("B u r g e r"));
- Assert::That(digest("FoEMfIp"), Equals("F o E M f I p"));
- Assert::That(digest("Haggis"), Equals("H a g g i s"));
- Assert::That(digest("chitlins"), Equals("c h i t l i n s"));
- Assert::That(digest("SPAM"), Equals("S P A M"));
//ssert::That(digest(1234567), Equals("1 2 3 4 5 6 7"));- Assert::That(digest("S 1P 2A 3M"), Equals("S 1 P 2 A 3 M"));
- //Assert::That(digest(1234567), Equals("1 2 3 4 5 6 7"));
- //Assert::That(digest({1,2,3,4,5,6,7}), Equals("{ 1 , 2 , 3 , 4 , 5 , 6 , 7 }""));
- //Assert::That(digest({1:'a',2:'b',3:'c'}), Equals("{ 1 : ' a ' , 2 : ' b ' , 3 : ' c ' }"));
- }
- };
Replaced the pronoun 'he' for 'they'
from random import randint def main(): GUESS_LIMIT = 10 while True: secret_number = randint(1, 100) for guess_count in range(GUESS_LIMIT): while True: # Validating user input guess = input('Guess a number between 1 and 100 \n> ') if guess.isdigit() and 1 <= int(guess) <= 100: break else: print('Invalid input!') if int(guess) == secret_number: print(f'Congratulations! You guessed the secret number in {guess_count+1} guesses') break elif int(guess) > secret_number: print(int(guess), secret_number) print(f"Too low!") else: print("Too high!") else: print(f'Game over! The secret number was {secret_number}.') if play_again() is False: break def play_again(): # Ask the player if they want to play again while True: user_input = input('Do you want to play again (yes or no)?\n> ') if user_input.isalpha(): if user_input.lower() == 'yes': return True elif user_input.lower() == "no": return False print('Invalid input!') if __name__ == '__main__': main()
- from random import randint
- def main():
- GUESS_LIMIT = 10
- while True:
- secret_number = randint(1, 100)
- for guess_count in range(GUESS_LIMIT):
- while True:
- # Validating user input
- guess = input('Guess a number between 1 and 100 \n> ')
- if guess.isdigit() and 1 <= int(guess) <= 100:
- break
- else:
- print('Invalid input!')
- if int(guess) == secret_number:
- print(f'Congratulations! You guessed the secret number in {guess_count+1} guesses')
- break
- elif int(guess) > secret_number:
- print(int(guess), secret_number)
- print(f"Too low!")
- else:
- print("Too high!")
- else:
- print(f'Game over! The secret number was {secret_number}.')
- if play_again() is False:
- break
- def play_again():
# Ask the player if he wants to play again- # Ask the player if they want to play again
- while True:
- user_input = input('Do you want to play again (yes or no)?\n> ')
- if user_input.isalpha():
- if user_input.lower() == 'yes':
- return True
- elif user_input.lower() == "no":
- return False
- print('Invalid input!')
- if __name__ == '__main__':
- main()
import codewars_test as test # TODO Write tests import solution # or from solution import example # test.assert_equals(actual, expected, [optional] message) @test.describe("Example") def test_group(): @test.it("test case") def test_case(): test.assert_equals(1 + 1, 2)
- import codewars_test as test
- # TODO Write tests
- import solution # or from solution import example
- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Example")
- def test_group():
- @test.it("test case")
- def test_case():
- test.assert_equals(1 + 1, 2)
Made some simple refactoring, like only importing randint from random instead of the whole library, not using sys.exit at all, made the play_again function only return True or False, instead of handling the the script exit, and fixing a game-breaking bug that the secret_number was a constant, instead of being generated in every game.
from random import randint def main(): GUESS_LIMIT = 10 while True: secret_number = randint(1, 100) for guess_count in range(GUESS_LIMIT): while True: # Validating user input guess = input('Guess a number between 1 and 100 \n> ') if guess.isdigit() and 1 <= int(guess) <= 100: break else: print('Invalid input!') if int(guess) == secret_number: print(f'Congratulations! You guessed the secret number in {guess_count+1} guesses') break elif int(guess) > secret_number: print(int(guess), secret_number) print(f"Too low!") else: print("Too high!") else: print(f'Game over! The secret number was {secret_number}.') if play_again() is False: break def play_again(): # Ask the player if he wants to play again while True: user_input = input('Do you want to play again (yes or no)?\n> ') if user_input.isalpha(): if user_input.lower() == 'yes': return True elif user_input.lower() == "no": return False print('Invalid input!') if __name__ == '__main__': main()
import randomimport sys- from random import randint
# Set up constantsSECRET_NUMBER = random.randint(1, 100)GUESS_LIMIT = 10- def main():
- GUESS_LIMIT = 10
- while True:
- secret_number = randint(1, 100)
- for guess_count in range(GUESS_LIMIT):
- while True:
- # Validating user input
- guess = input('Guess a number between 1 and 100 \n> ')
- if guess.isdigit() and 1 <= int(guess) <= 100:
- break
- else:
- print('Invalid input!')
def main():"""Main game function."""guess_count = 0while True: # main game loopwhile True: # Validate user inputguess = input('Guess a number between 1 and 100\n> ')if not guess.isdigit() or int(guess) not in range(1, 101):print('Invalid input!')continueelse:guess_count += 1- if int(guess) == secret_number:
- print(f'Congratulations! You guessed the secret number in {guess_count+1} guesses')
- break
if guess_count == GUESS_LIMIT:print(f'Game over! The secret number was {SECRET_NUMBER}.')breakif int(guess) == SECRET_NUMBER:print(f'Congratulations! You guessed the secret number in {guess_count} guesses')break- elif int(guess) > secret_number:
- print(int(guess), secret_number)
- print(f"Too low!")
if int(guess) > SECRET_NUMBER:print('Too high!')- else:
- print("Too high!")
- else:
print('Too low!')play_again()- print(f'Game over! The secret number was {secret_number}.')
- if play_again() is False:
- break
- def play_again():
"""Play again options"""- # Ask the player if he wants to play again
- while True:
user_input = input('Do you want to play again (yes or no)?> ').lower()if user_input not in ['yes', 'no']:print('Invalid input!')continueelse:breakif user_input == 'yes':return main()else:print('Thanks for playing, Goodbye!')sys.exit()- user_input = input('Do you want to play again (yes or no)?
- > ')
- if user_input.isalpha():
- if user_input.lower() == 'yes':
- return True
- elif user_input.lower() == "no":
- return False
- print('Invalid input!')
- if __name__ == '__main__':
- main()