ほとんどのチップ電卓アプリは、単に食事の価格の一定の割合を取ります。たとえば、食事が23.45ドルの場合、15%のチップ= 3.52ドル、またはより寛大な20%のチップ= 4.69ドルを残すことができます。
クレジットカードユーザーにとって十分に便利です。しかし、現金のヒントを残したい場合はそうではありません。その場合、これらの奇妙なセント額が邪魔になります。それでは、現金ユーザーにとってより便利になるようにアイデアを修正しましょう。
あなたの割り当て
入力として受け取るプログラムまたは関数を可能な限り少ないバイトで書き込みます。
- 食事の価格
- 最小チップ率
- 最大チップ率
そして、必要な紙幣/紙幣と硬貨の数を最小化する範囲[価格* min_percentage / 100、価格* max_percentage / 100]内のチップ量を出力します。
米国の通貨単位を1¢、5¢、10¢、25¢、$ 1、$ 5、$ 10、$ 20、$ 50、および$ 100と仮定します。
例
Pythonでのゴルフ以外のサンプルプログラムを次に示します。
import math
import sys
# Do the math in cents so we can use integer arithmetic
DENOMINATIONS = [10000, 5000, 2000, 1000, 500, 100, 25, 10, 5, 1]
def count_bills_and_coins(amount_cents):
# Use the Greedy method, which works on this set of denominations.
result = 0
for denomination in DENOMINATIONS:
num_coins, amount_cents = divmod(amount_cents, denomination)
result += num_coins
return result
def optimize_tip(meal_price, min_tip_percent, max_tip_percent):
min_tip_cents = int(math.ceil(meal_price * min_tip_percent))
max_tip_cents = int(math.floor(meal_price * max_tip_percent))
best_tip_cents = None
best_coins = float('inf')
for tip_cents in range(min_tip_cents, max_tip_cents + 1):
num_coins = count_bills_and_coins(tip_cents)
if num_coins < best_coins:
best_tip_cents = tip_cents
best_coins = num_coins
return best_tip_cents / 100.0
# Get inputs from command-line
meal_price = float(sys.argv[1])
min_tip_percent = float(sys.argv[2])
max_tip_percent = float(sys.argv[3])
print('{:.2f}'.format(optimize_tip(meal_price, min_tip_percent, max_tip_percent)))
いくつかのサンプル入力および出力:
~$ python tipcalc.py 23.45 15 20
4.00
~$ python tipcalc.py 23.45 15 17
3.55
~$ python tipcalc.py 59.99 15 25
10.00
~$ python tipcalc.py 8.00 13 20
1.05
a program that takes as input (stdin, command-line arguments, or GUI input box, whichever is most convenient in your language)
これは、入力および出力のデフォルトをオーバーライドすることを意図していますか?つまり、たとえば、3つの数値を取り、結果を返す関数は許可されますか?
3.51
して3.75
も、テストケースのための有効な出力ですか23.45 15 17
?同じ量のコインを使用し、範囲内にあります。