function finalPrice(price) { const vat = 0.1; const lux = 0.3; const luxThreshold = 1000; const extra = price >= luxThreshold ? lux : 0; return Math.round(price * (1 + vat + extra)); }
- function finalPrice(price) {
var vat = 0.1var lux = 0.3- const vat = 0.1;
- const lux = 0.3;
- const luxThreshold = 1000;
if(price >= 1000) {return Math.floor(price * (1+vat+lux))} else {return Math.floor(price * (1+vat))}- const extra = price >= luxThreshold ? lux : 0;
- return Math.round(price * (1 + vat + extra));
- }
The "direction" verification is done on the step parameter.
This allows avoiding situations in which the step goes farther from the stop value on every yield. This is just a choice, you could also decide to generate infinite values in that case.
Example:
start = 1
stop = 10
step = -1
Will not yield anything, rather than yielding infinite values.
function* generateInterval(start = 0, stop = Infinity, step = 1) { if (step === 0) { throw new Error("Invalid Step Parameter"); } const shouldStop = v => step > 0 ? v <= stop : v >= stop; for (let v = start; shouldStop(v); v += step) { yield v; } }
function* generateInterval(a = 0, b = NaN, c = 1) {if (isNaN(b)) [a, b] = [0, a];for (const s = Math.sign(c); (b - a) * s >= 0; a += c)yield a;}- function* generateInterval(start = 0, stop = Infinity, step = 1) {
- if (step === 0) {
- throw new Error("Invalid Step Parameter");
- }
- const shouldStop = v => step > 0 ? v <= stop : v >= stop;
// Python range() APIfunction* range(a = 0, b = NaN, c = 1) {if (isNaN(b)) [a, b] = [0, a];for (const s = Math.sign(c); (b - a) * s > 0; a += c)yield a;- for (let v = start; shouldStop(v); v += step) {
- yield v;
- }
- }
Here's an example of a refactor.
This separates the logic for reading the guess, playing a single round, and playing the whole game.
Additionally, the single match takes some parameters such as the interval size and the number of attempts.
import random def read_guess(): value = None while value is None: try: value = int(input('\nGuess: ')) except ValueError: print("\nInvalid Input!") return value def start_match(guess_limit, guess_interval): print(f'Guess a number between {guess_interval[0]} and {guess_interval[1]}') secret_number = random.randint(*guess_interval) guess_count = 0 while guess_count < guess_limit: guess = read_guess() guess_count += 1 if guess > secret_number: print('\nNo, too big.') elif guess < secret_number: print('\nNo, too small.') else: print('\nWow, you are a actully are a true mind reader!') print(f"The number was {secret_number} indeed! ") return print("Sorry, you're out of guesses!") def game_loop(): guess_limit = 5 guess_interval = (1, 25) start_match(guess_limit, guess_interval) replay = 'yes' while replay != 'no': replay = input('\nDo you want to play Guess game again? (yes/no) - ') if replay == 'yes': start_match(guess_limit, guess_interval) elif replay != 'no': print("I don't understand") print('\nBye! See you soon!') game_loop()
- import random
def code():print('Guess a number between 1 and 25')# secret numbersecret_number = random.randint(1, 25)guess_count = 0guess_limit = 5# mainloopwhile guess_count < guess_limit:- def read_guess():
- value = None
- while value is None:
- try:
guess = int(input('Guess: '))guess_count += 1- value = int(input('
- Guess: '))
- except ValueError:
- print("\nInvalid Input!")
continue- return value
- def start_match(guess_limit, guess_interval):
- print(f'Guess a number between {guess_interval[0]} and {guess_interval[1]}')
- secret_number = random.randint(*guess_interval)
- guess_count = 0
- while guess_count < guess_limit:
- guess = read_guess()
- guess_count += 1
- if guess > secret_number:
- print('\nNo, too big.')
guess_count += 1- elif guess < secret_number:
- print('\nNo, too small.')
guess_count += 1elif guess == secret_number:- else:
- print('\nWow, you are a actully are a true mind reader!')
- print(f"The number was {secret_number} indeed! ")
reply = input('\nDo you want to play guess again? (yes/no) - ')if reply == 'yes':code()else:exit()if guess_count == 5:print('\nSorry, you have failed:(')print(f'\nThe secret number was {secret_number}.')replay = input('\nDo you want to play Guess game again? (yes/no) - ')if replay == 'yes':code()elif replay == 'no':exit()else:print("I don't understand")- return
- print("Sorry, you're out of guesses!")
- def game_loop():
- guess_limit = 5
- guess_interval = (1, 25)
- start_match(guess_limit, guess_interval)
- replay = 'yes'
- while replay != 'no':
- replay = input('\nDo you want to play Guess game again? (yes/no) - ')
- if replay == 'yes':
- start_match(guess_limit, guess_interval)
- elif replay != 'no':
- print("I don't understand")
- print('\nBye! See you soon!')
code()- game_loop()
import codewars_test as test # TODO Write tests import solution # or from solution import example
- 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)