Relearn Python (Algorithm I)

A memorandum to relearn Python from the ground up. I would like to produce results that are tackling basic problems.

FizzBuzz problem

fizz_buzz_problem The most basic problem that programmers can write.

Problem statement

Create a program that outputs numbers from 1 to 100 in order. However, when it is a multiple of 3, it outputs "Fizz" instead of a number, when it is a multiple of 5, it outputs "Buzz" instead of a number, and when it is a multiple of both 3 and 5, it outputs "FizzBuzz". And.

Creation program

#Create a program that outputs numbers from 1 to 100 in order.
#However, when it is a multiple of 3, it outputs "Fizz" instead of a number, when it is a multiple of 5, it outputs "Buzz" instead of a number, and when it is a multiple of both 3 and 5, it outputs "FizzBuzz". And.
for i in range(1, 101):
    if i % 3 == 0 and i % 5 == 0:
    #if (i % 3 == 0) and (i % 5 == 0):
        print('FizzBuzz', end = ' ')
    elif i % 3 == 0:
        print('Fizz', end = ' ')
    elif i % 5 == 0:
        print('Buzz', end = ' ')
    else:
        print(i, end = ' ')
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz 
Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 
Fizz Buzz 41 Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 
Fizz 58 59 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 
FizzBuzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91 
92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz 

Vending machine change problem

vending_machine_problem

Problem statement

There are vending machines that can hold up to 10,000 yen bills. At this time, create a program that outputs change with the fewest coins and banknotes.

Created program

import sys


insert_price = int(input('insert:'))
product_price = int(input('product:'))
change = insert_price - product_price
if change >= 0:
    print('change:', change)
else:
    print('Not enough money.')

#=>insert:1000
#=>product:239
#=>change: 761


coin = [10000, 5000, 1000, 500 , 100, 50, 10, 5 ,1]

for i in coin:
    r = change // i
    change %= i
    print(str(i) + ':' + str(r) + 'coins')

#=> 10000:0coins
#=> 5000:0coins
#=> 1000:0coins
#=> 500:1coins
#=> 100:2coins
#=> 50:1coins
#=> 10:1coins
#=> 5:0coins
#=> 1:1coins

Reference material

[Introduction to Algorithms Beginning with Python Standards and Computational Volumes Learned with Traditional Algorithms](https://www.amazon.co.jp/Python%E3%81%A7%E3%81%AF%E3%81%98%E3 % 82% 81% E3% 82% 8B% E3% 82% A2% E3% 83% AB% E3% 82% B4% E3% 83% AA% E3% 82% BA% E3% 83% A0% E5% 85 % A5% E9% 96% 80-% E4% BC% 9D% E7% B5% B1% E7% 9A% 84% E3% 81% AA% E3% 82% A2% E3% 83% AB% E3% 82% B4% E3% 83% AA% E3% 82% BA% E3% 83% A0% E3% 81% A7% E5% AD% A6% E3% 81% B6% E5% AE% 9A% E7% 9F% B3% E3% 81% A8% E8% A8% 88% E7% AE% 97% E9% 87% 8F-% E5% A2% 97% E4% BA% 95-% E6% 95% 8F% E5% 85% 8B- ebook / dp / B0822N5RMS / ref = sr_1_4? __mk_ja_JP =% E3% 82% AB% E3% 82% BF% E3% 82% AB% E3% 83% 8A & crid = 38MY4D6L4UM7I & dchild = 1 & keywords = python +% E3% 82% A2% E3 % 83% AB% E3% 82% B4% E3% 83% AA% E3% 82% BA% E3% 83% A0 & qid = 1603601139 & sprefix = python + algorithm% 2Caps% 2C291 & sr = 8-4)

My github URL https://github.com/jin237/algorism_python

Recommended Posts

Relearn Python (Algorithm I)
Python memorandum (algorithm)
I started python
A * algorithm (Python edition)
I tried Python> autopep8
Genetic algorithm in python
I implemented Python Logging
Algorithm in Python (Bellman-Ford)
I tried Python> decorator
Why I chose Python
I compared Python more-itertools 2.5 → 2.6
Algorithm in Python (Dijkstra's algorithm)
I tried to implement GA (genetic algorithm) in Python
I tried fp-growth with python
I wrote python in Japanese
Algorithm in Python (primality test)
I made blackjack with python!
I compared Java and Python!
Search algorithm using word2vec [python]
Reproduce Euclidean algorithm in Python
Algorithm in Python (binary search)
[Python3] Dijkstra's algorithm with 14 lines
I tried Python C extension
[Python] I tried using OpenPose
I made a python text
Implement Dijkstra's Algorithm in python
I ran python on windows
I tried gRPC with Python
I tried scraping with python
I understand Python in Japanese!
I made blackjack with Python.
What I learned in Python
I learned Python basic grammar
I made wordcloud with Python.
I downloaded the python source
I implemented Donald Knuth's unbiased sequential calculation algorithm in Python
Algorithm in Python (breadth-first search, bfs)
I made a Line-bot using Python!
Python
Sorting algorithm and implementation in Python
I can't install python3 with pyenv-vertualenv
I checked Mac Python environment construction
I tried web scraping with python.
I can't remember Python regular expressions
I made my own Python library
Notation I encountered while learning Python
[Python] I implemented peripheral Gibbs sampling
Write A * (A-star) algorithm in Python
I made a fortune with Python.
Develop an investment algorithm in Python 2
Algorithm in Python (depth-first search, dfs)
I sent an SMS with Python
I wrote Fizz Buzz in Python
I took Progete's Python Learning Course I
I tried using Thonny (Python / IDE)
I tried Grumpy (Go running Python).
I liked the tweet with python. ..
I learned about processes in Python
I can't install scikit-learn in Python
I played with PyQt5 and Python3
I wrote the queue in Python