[心得] 九九乘法表 不使用迴圈

作者: Schottky (順風相送)   2021-02-27 04:57:52
基本的語法稍微練習過之後,可以開始來練習比較難的題目了,
這次挑了非常老梗的考古題,不使用迴圈印出九九乘法表。
☆☆☆☆☆☆☆☆直觀法
首先當我們在考卷上看到這題時,一百個人裡有八十七個會直接 print 出來
這樣的作法我覺得是最完美的,完全符合題目要求,絕不會有 bug,所見即所得,
而且可以任意排版成喜歡的樣子。在後面的幾個作法中可以看到,要排版成這樣是
有一點點麻煩的。
但是有些閱卷老師不喜歡。
#
# print.py
#
print('''\
2 x 2 = 4 3 x 2 = 6 4 x 2 = 8 5 x 2 = 10
2 x 3 = 6 3 x 3 = 9 4 x 3 = 12 5 x 3 = 15
2 x 4 = 8 3 x 4 = 12 4 x 4 = 16 5 x 4 = 20
2 x 5 = 10 3 x 5 = 15 4 x 5 = 20 5 x 5 = 25
2 x 6 = 12 3 x 6 = 18 4 x 6 = 24 5 x 6 = 30
2 x 7 = 14 3 x 7 = 21 4 x 7 = 28 5 x 7 = 35
2 x 8 = 16 3 x 8 = 24 4 x 8 = 32 5 x 8 = 40
2 x 9 = 18 3 x 9 = 27 4 x 9 = 36 5 x 9 = 45
6 x 2 = 12 7 x 2 = 14 8 x 2 = 16 9 x 2 = 18
6 x 3 = 18 7 x 3 = 21 8 x 3 = 24 9 x 3 = 27
6 x 4 = 24 7 x 4 = 28 8 x 4 = 32 9 x 4 = 36
6 x 5 = 30 7 x 5 = 35 8 x 5 = 40 9 x 5 = 45
6 x 6 = 36 7 x 6 = 42 8 x 6 = 48 9 x 6 = 54
6 x 7 = 42 7 x 7 = 49 8 x 7 = 56 9 x 7 = 63
6 x 8 = 48 7 x 8 = 56 8 x 8 = 64 9 x 8 = 72
6 x 9 = 54 7 x 9 = 63 8 x 9 = 72 9 x 9 = 81
''', end="")
# End of print.py
☆☆☆☆☆☆☆☆Web Request 法
這個方法的優點是充滿神秘感,而且程式非常簡短,考試時可以拼提早交卷。
#
# web.py
#
import requests
x = requests.get('https://pastebin.com/raw/kuim3iR8')
print(x.text)
# End of web.py
☆☆☆☆☆☆☆☆Alarm 法
時間的巨輪緩緩轉動,這個方法雖然慢了一點,但如秒針一樣慢慢浮出的九九乘法表,
呈現有如精密鐘錶一樣的藝術感
缺點是只有 UNIX 系列的作業系統才能使用。
#
# alarm.py
#
import signal
n = 0
stop_flag = False
def myhandler(signum, frame):
global n, stop_flag
print("{} x {} = {:2d}".format(n//8+2, n%8+2, (n//8+2)*(n%8+2)))
if (n==63):
stop_flag = True
n+=1
signal.signal(signal.SIGALRM, myhandler)
while stop_flag!=True:
signal.alarm(1)
signal.pause()
# End of alarm.py
☆☆☆☆☆☆☆☆Web Request 第二彈
前一個 web request 感覺太短,拿不到墨水分數,而且要背熟網址。
為了改進這個缺點,改用另一個角度來利用 web request
URL 寫的是 google 讓人覺得非常高級。
速度雖然有點慢,但和前一個 alarm 法比起來已經快多了
#
# web2.py
#
import requests
n = 0
def f(r, **kwargs):
global n
a = n//32*4+n%4+2
b = n%32//4+2
if (n==31):
e = "\r\n\r\n"
elif (n%4==3):
e = "\r\n"
else:
e = " "
print("{} x {} = {:2d}{}".format(a, b, a*b, e), end="", flush=True)
n += 1
if (n < 64):
myget()
def myget():
s = requests.Session()
s.hooks['response'].append(f)
s.get('https://www.google.com/')
myget()
# End of web2.py
☆☆☆☆☆☆☆☆排序法
也沒有真的要排序,只是藉著 list.sort() 去不斷觸發程式
速度比前一個方法快,而且可以使用在沒有網路的環境中
#
# sort.py
#
n = 0
garbage_list = [*range(64)]
def myfunc(k):
global n
page = n//32
a = page*4+n%4+2
b = n%32//4+2
if (n%4==0):
pre = "\t"
else:
pre = " "
post = ""
if (n%4==3):
post += "\r\n"
if (n==31):
post += "\r\n"
print("{}{} x {} = {:2d}{}".format(pre, a, b, a*b, post), end="")
n += 1
return k
garbage_list.sort(key = myfunc)
# End of sort.py
☆☆☆☆☆☆☆☆多程序法
既然不准用迴圈依序執行,那全部同時執行是個很好的思考方向
雖然 multiprocessing 的 overhead 反而讓這程式執行得更慢,
但不得不承認 multiprocessing 就是帥。
#
# mp.py
#
from multiprocessing import Pool
table = list(range(64))
def f(x):
global table
a = x//32*4+x%4+2
b = x//4%8+2
if (x==31):
e = "\r\n\r\n"
elif (x%4==3):
e = "\r\n"
else:
e = " "
return "{} x {} = {:2d}{}".format(a, b, a*b, e)
with Pool(64) as p:
print(*p.map(f, range(64)), sep="", end="")
# End of mp.py
☆☆☆☆☆☆☆☆結語
只是為了練習寫 Python 所以寫了這些,但這個晚上覺得學到不少東西,
最重要的是有練習到 list 的操作以及 str.format() 的使用
另外這題是真的有看過出現在各種考試中,不見得是 Python 就是了。
至於上面六種作法到底哪一種的得分會比較高,這我就不敢保證了。
只好盡人事聽天命................
作者: TheOneisNEO (Thomas Anderson)   2021-02-27 10:14:00
你這幾篇都有意思hahaha 我也喜歡偶爾寫來跑數學
作者: gawyfish (00)   2021-02-27 12:14:00
作者: kobe8112 (小B)   2021-02-28 00:32:00
小學誰在算200階乘啦(丟筆
作者: hongyan (Yan)   2021-03-01 10:58:00
想法好多好有趣,像我第一直覺只有想到雙重迴圈而已XD
作者: mirror0227 (鏡子)   2021-03-05 13:05:00
while也是迴圈吧...不然我就while i < 64 然後 I++就好啦

Links booklink

Contact Us: admin [ a t ] ucptt.com