[心得] 九九乘法表 不使用循环

楼主: 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