Algorithm learned with Python 2nd: Vending machine

#Algorithm learned in Python

Introduction

Implement the basic algorithm in Python to deepen your understanding of the algorithm. The second step is to deal with vending machine change calculations.

vending machine

First, the vending machine mentioned here is defined. It can handle all Japanese banknotes and coins, and cannot be purchased when the input amount is insufficient. It is assumed that the user inputs the input amount and the purchase amount.

Vending machine ver.1

Create a vending machine function that simply calculates the change. We do not consider shortages. The code is as follows.

code

calc_change1.py


"""
2020/12/15
@Yuya Shimizu

vending machine
Change calculation ver.1
For the time being, calculate the change
"""
#Simple fishing calculation: fishing=Input amount-Purchase price
insert_price = input('Input amount>> ')
product_price = input('Purchase price>> ')

change = int(insert_price) - int(product_price)

print('Change>> {}Circle'.format(change))

Execution result

Input amount>> 20000     #User input
Purchase price>> 1864      #User input
Change>>18136 yen#Display of calculation results

You can calculate the change, but how do you get the change back? It is preferable to return the change so that the number of banknotes and coins is as small as possible. In ver.2, change is returned with as few banknotes and coins as possible.

Vending machine ver.2

Create a vending machine function that returns change with as few banknotes and coins as possible. We do not consider shortages. The code is as follows.

code

calc_change2.py


"""
2020/12/15
@Yuya Shimizu

vending machine
Change calculation ver.2
Return change with as few banknotes as possible
"""
insert_price = input('Input amount>> ')
product_price = input('Purchase price>> ')

#Simple fishing calculation: fishing=Input amount-Purchase price
change = int(insert_price) - int(product_price)
total_change = change

#Return change with as few sheets as possible
cahnge_10000 = change//10000   #Number of 10,000 yen bills
change = change%10000             
cahnge_5000 = change//5000      #Number of 5000 yen bills
change = change%5000
cahnge_1000 = change//1000      #Number of 1000 yen bills
change = change%1000
cahnge_500 = change//500         #Number of 500-yen coins
change = change%500
cahnge_100 = change//100        #Number of 100-yen coins
change = change%100
cahnge_50 = change//50           #Number of 50-yen coins
change = change%50
cahnge_10 = change//10           #Number of 10-yen coins
change = change%10
cahnge_5 = change//5              #Number of five-yen coins
change = change%5
cahnge_1 = change//1              #Number of 1-yen coins


print('Change{}Circle\n'.format(total_change))
print('10000 yen bill:{}Sheet\n5000 yen bill:{}Sheet\n1000 yen bill:{}Sheet\n500 yen ball:{}Sheet\n100 yen ball:{}Sheet\n50 yen ball:{}Sheet\n10 yen ball:{}Sheet\n5 yen ball:{}Sheet\n1 yen ball:{}Sheet\n'
      .format(cahnge_10000, cahnge_5000, cahnge_1000, cahnge_500, cahnge_100, cahnge_50, cahnge_10, cahnge_5, cahnge_1))


Execution result

Input amount>> 20000
Purchase price>> 1423
Change 18577 yen

10000 yen bill: 1 sheet
5000 yen bill: 1 sheet
1000 yen bill: 3 sheets
500 yen coin: 1 piece
100-yen coin: 0
50-yen coin: 1 piece
10-yen coin: 2 pieces
5 yen coin: 1 piece
1-yen coin: 2 pieces

I was able to return the change so that the number of banknotes and coins was as small as possible. Looking at the program, the calculation of the number of banknotes and coins is a repetition of the same thing. In ver.3, the same thing is repeated and the code is simplified.

Vending machine ver.3

Use a list to simplify the previous code. The code is as follows.

code

calc_change3.py


"""
2020/12/15
@Yuya Shimizu

vending machine
Change calculation ver.3
Simplify the code
"""
insert_price = input('Input amount>> ')
product_price = input('Purchase price>> ')

#Simple fishing calculation: fishing=Input amount-Purchase price
change = int(insert_price) - int(product_price)
total_change = change

#Banknotes Collect coins in a list and organize codes
money_list = [10000, 5000, 1000, 500, 100, 50, 10, 5, 1]

print('Change{}Circle\n'.format(total_change))
#Return change with as few sheets as possible
for money in money_list:
    change_money = change//money
    change = change%money
    print('{}Yen bill:{}Sheet'.format(money, change_money))

Execution result

Input amount>> 20000
Purchase price>> 1423
Change 18577 yen

10000 yen bill: 1 sheet
5000 yen bill: 1 sheet
1000 yen bill: 3 sheets
500 yen coin: 1 piece
100-yen coin: 0
50-yen coin: 1 piece
10-yen coin: 2 pieces
5 yen coin: 1 piece
1-yen coin: 2 pieces

The execution result did not change, and the code could be very short and simplified. Finally, in ver.4, we will create a vending machine function that responds to errors including shortages.

Vending machine ver.4

At the time of user input, it is assumed that the amount is correct, but you may mistakenly enter characters instead of numbers. In addition, the input amount may be insufficient for the purchase amount. In such a case, I want to send an error message and forcibly terminate the program. Here, the method of the character string that can judge whether the character string `isdecimal ()` is composed entirely of numbers is judged whether the user input information is correct, and `` `sys. Use exit () ```. The code is as follows.

code

calc_change4.py


"""
2020/12/15
@Yuya Shimizu

vending machine
Change calculation ver.4
Respond to errors
"""
import sys

#Banknotes Collect coins in a list and organize codes
money_list = [10000, 5000, 1000, 500, 100, 50, 10, 5, 1]

insert_price = input('Input amount>> ')
#Forced termination if input is not an integer
if not insert_price.isdecimal():    #The string consists of only numbers(integer)Can be checked
    print('Please enter an integer')
    sys.exit() #Forced program termination
    
product_price = input('Purchase price>> ')
#Forced termination if input is not an integer
if not product_price.isdecimal():
    print('Please enter an integer')
    sys.exit()
    
#Simple fishing calculation: fishing=Input amount-Purchase price
change = int(insert_price) - int(product_price)

#Forced termination when change becomes negative
if change < 0:
    print('Insufficient input amount')
    sys.exit()

print('Change{}Circle\n'.format(change))
#Return change with as few sheets as possible
for money in money_list:
    change_money = change//money
    change %= money
    print('{}Yen bill:{}Sheet'.format(money, change_money))


Execution result

=========Execution result 1: When a non-number is entered in the input amount=========
Input amount>> 5.5
Please enter an integer
#forced termination
=========Execution result 2: When a non-number is entered in the purchase price=========
Input amount>> 5
Purchase price>>Ah
Please enter an integer
#forced termination
=========Execution result 3: When the input amount is insufficient=========
Input amount>> 5
Purchase price>> 150
Insufficient input amount
#forced termination
=========Execution result 4: In case of normal input=========
Input amount>> 500
Purchase price>> 150
Change 350 yen

10000 yen bill: 0 sheets
5000 yen bill: 0 sheets
1000 yen bill: 0 sheets
500 yen bill: 0 sheets
100-yen bill: 3
50-yen bill: 1
10-yen bill: 0
5 yen bill: 0 sheets
1 yen bill: 0 sheets

Execution results 1 to 3 show the output when an abnormal input or condition is met. As you can see from the result, it has been dealt with well. With this, we were able to create a vending machine function that responds to errors.

Impressions

I knew the mechanism of the vending machine function to some extent, but it was a good opportunity to use the isdecimal and sys functions used in this program for the first time. Especially, I haven't used sys so much and I don't know much about it. I hope I can study in the future. In addition, I was able to reconfirm the simplification of the code by putting it together in a list again.

References

Introduction to algorithms starting with Python: Standards and computational complexity learned with traditional algorithms Written by Toshikatsu Masui, Shoeisha

Recommended Posts

Algorithm learned with Python 2nd: Vending machine
Algorithm learned with Python 10th: Binary search
Algorithm learned with Python 5th: Fibonacci sequence
Algorithm learned with Python 9th: Linear search
Algorithm learned with Python 7th: Year conversion
Algorithm learned with Python 8th: Evaluation of algorithm
Algorithm learned with Python 4th: Prime numbers
Algorithm learned with Python 19th: Sorting (heapsort)
Algorithm learned with Python 6th: Leap year
Algorithm learned with Python 3rd: Radix conversion
Algorithm learned with Python 12th: Maze search
Algorithm learned with Python 11th: Tree structure
Algorithm learned with Python 13th: Tower of Hanoi
Algorithm learned with Python 16th: Sorting (insertion sort)
Algorithm learned with Python 14th: Tic-tac-toe (ox problem)
Algorithm learned with Python 15th: Sorting (selection sort)
Algorithm learned with Python 17th: Sorting (bubble sort)
Machine learning learned with Pokemon
Algorithm learned with Python 18th: Sorting (stack and queue)
Machine learning with Python! Preparation
[Python3] Dijkstra's algorithm with 14 lines
Beginning with Python machine learning
2nd Algorithm Practical Test Solve past questions with python (unfinished)
[Python] Object-oriented programming learned with Pokemon
Perceptron learning experiment learned with Python
Python data structures learned with chemoinformatics
Efficient net pick-up learned with Python
1. Statistics learned with Python 1-1. Basic statistics (Pandas)
Implementation of Dijkstra's algorithm with python
"Scraping & machine learning with Python" Learning memo
Python algorithm
[Python] Reactive Extensions learned with RxPY (3.0.1) [Rx]
Amplify images for machine learning with python
Machine learning with python (2) Simple regression analysis
Search the maze with the python A * algorithm
[Shakyo] Encounter with Python for machine learning
1. Statistics learned with Python 1-3. Calculation of various statistics (statistics)
Let's develop an investment algorithm with Python 1
Build AI / machine learning environment with Python
Source code of sound source separation (machine learning practice series) learned with Python
[Python] Easy introduction to machine learning with python (SVM)
FizzBuzz with Python3
Scraping with Python
Machine learning starting with Python Personal memorandum Part2
Statistics with python
Python memorandum (algorithm)
Scraping with Python
Python with Go
Machine learning starting with Python Personal memorandum Part1
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
1. Statistics learned with Python 1-2. Calculation of various statistics (Numpy)
python starts with ()
[Python] Collect images with Icrawler for machine learning [1000 images]
Machine learning starting from scratch (machine learning learned with Kaggle)
with syntax (Python)
Bingo with python
Zundokokiyoshi with python