A memorandum to relearn Python from the ground up. I would like to produce results that are tackling basic problems.
fizz_buzz_problem The most basic problem that programmers can write.
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.
#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_problem
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.
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
[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