Skip to content

Instantly share code, notes, and snippets.

@mdecourse
Last active October 6, 2021 06:16
Show Gist options
  • Select an option

  • Save mdecourse/a55dbe8816baee6981e9a4cf37cd667e to your computer and use it in GitHub Desktop.

Select an option

Save mdecourse/a55dbe8816baee6981e9a4cf37cd667e to your computer and use it in GitHub Desktop.
2021 Fall w2 programs
# 根據各班的電腦輔助設計實習課號, 以 Heroku 主機為跳板, 取出修課學員名單
cad2aurl = "https://nfulist.herokuapp.com/?semester=1101&courseno=0805&column=True"
# readlines() 可以將所讀的資料放入數列
data = open(cad2aurl).readlines()
# 確認所讀出的資料內容為學員學號數列
# print(data)
# 設計一個篩選學員學號的函式, 用來安排座號, 但以本班學員順序為主
def setSeat(firstNum):
# 利用 len() 計算學員總數
totalNum = len(data)
# 以 affList 代表選修學員數列
affList = []
# 計算最後一位選修學員序號
count = 0
for i in data:
# 查驗是否逐一列出學員學號
#print(i)
# 以本班第一位學員作為判斷分隔依據
if i == firstNum:
affList = data[:count]
otherList = data[count:]
count += 1
# 查驗是否正確
checkNum = len(affList) + len(otherList)
if checkNum == totalNum:
print("Total number of student matched!")
# 傳回分割完成的數列
#return affList, otherList
# 開始安排作為, 將 otherList 以每 9 人一排切割
oneRow = 9
continueRow, leftOver = seatList(0, oneRow, otherList, False)
continueRow, leftOver = seatList(continueRow, oneRow, leftOver+affList, False)
continueRow, leftOver = seatList(continueRow, oneRow, leftOver, True)
def seatList(startRow, oneRow, inputList, tail=False):
totalNum = len(inputList)
leftNum = totalNum%oneRow
#print(leftNum)
intoList = inputList[:(totalNum-leftNum)]
leftOver = inputList[-leftNum:]
#print(leftOver)
column = 0
row = startRow
count = 0
for i in intoList:
column += 1
if count%oneRow == 0:
row += 1
column = 0
print("-"*20)
print("("+str(row)+","+str(column+1)+")",i)
count += 1
column = 0
count = 0
if tail == True:
row += 1
print("-"*20)
for i in leftOver:
column += 1
print("("+str(row)+","+str(column)+")",i)
count += 1
return row, leftOver
setSeat("40923101")
# 根據各班的電腦輔助設計實習課號, 以 Heroku 主機為跳板, 取出修課學員名單
cad2burl = "https://nfulist.herokuapp.com/?semester=1101&courseno=0818&column=True"
# readlines() 可以將所讀的資料放入數列
data = open(cad2burl).readlines()
# 確認所讀出的資料內容為學員學號數列
# print(data)
# 設計一個篩選學員學號的函式, 用來安排座號, 但以本班學員順序為主
def setSeat(firstNum):
# 利用 len() 計算學員總數
totalNum = len(data)
# 以 affList 代表選修學員數列
affList = []
# 計算最後一位選修學員序號
count = 0
for i in data:
# 查驗是否逐一列出學員學號
#print(i)
# 以本班第一位學員作為判斷分隔依據
if i == firstNum:
affList = data[:count]
otherList = data[count:]
count += 1
# 查驗是否正確
checkNum = len(affList) + len(otherList)
if checkNum == totalNum:
print("Total number of student matched!")
# 傳回分割完成的數列
#return affList, otherList
# 開始安排作為, 將 otherList 以每 9 人一排切割
oneRow = 9
continueRow, leftOver = seatList(0, oneRow, otherList, False)
continueRow, leftOver = seatList(continueRow, oneRow, leftOver+affList, False)
continueRow, leftOver = seatList(continueRow, oneRow, leftOver, True)
def seatList(startRow, oneRow, inputList, tail=False):
totalNum = len(inputList)
leftNum = totalNum%oneRow
#print(leftNum)
intoList = inputList[:(totalNum-leftNum)]
leftOver = inputList[-leftNum:]
#print(leftOver)
column = 0
row = startRow
count = 0
for i in intoList:
column += 1
if count%oneRow == 0:
row += 1
column = 0
print("-"*20)
print("("+str(row)+","+str(column+1)+")",i)
count += 1
column = 0
count = 0
if tail == True:
row += 1
print("-"*20)
for i in leftOver:
column += 1
print("("+str(row)+","+str(column)+")",i)
count += 1
return row, leftOver
setSeat("40923201")
# 根據各班的計算機程式課號, 以 Heroku 主機為跳板, 取出修課學員名單
cp1aurl = "https://nfulist.herokuapp.com/?semester=1101&courseno=0766&column=True"
# readlines() 可以將所讀的資料放入數列
data = open(cp1aurl).readlines()
# 確認所讀出的資料內容為學員學號數列
# print(data)
# 設計一個篩選學員學號的函式, 用來安排座號, 但以本班學員順序為主
def setSeat(firstNum):
# 利用 len() 計算學員總數
totalNum = len(data)
# 以 affList 代表選修學員數列
affList = []
# 計算最後一位選修學員序號
count = 0
for i in data:
# 查驗是否逐一列出學員學號
#print(i)
# 以本班第一位學員作為判斷分隔依據
if i == firstNum:
affList = data[:count]
otherList = data[count:]
count += 1
# 查驗是否正確
checkNum = len(affList) + len(otherList)
if checkNum == totalNum:
print("Total number of student matched!")
# 傳回分割完成的數列
#return affList, otherList
# 開始安排作為, 將 otherList 以每 9 人一排切割
oneRow = 9
continueRow, leftOver = seatList(0, oneRow, otherList, False)
continueRow, leftOver = seatList(continueRow, oneRow, leftOver+affList, False)
continueRow, leftOver = seatList(continueRow, oneRow, leftOver, True)
def seatList(startRow, oneRow, inputList, tail=False):
totalNum = len(inputList)
leftNum = totalNum%oneRow
#print(leftNum)
intoList = inputList[:(totalNum-leftNum)]
leftOver = inputList[-leftNum:]
#print(leftOver)
column = 0
row = startRow
count = 0
for i in intoList:
column += 1
if count%oneRow == 0:
row += 1
column = 0
print("-"*20)
print("("+str(row)+","+str(column+1)+")",i)
count += 1
column = 0
count = 0
if tail == True:
row += 1
print("-"*20)
for i in leftOver:
column += 1
print("("+str(row)+","+str(column)+")",i)
count += 1
return row, leftOver
setSeat("41023101")
# 根據各班的計算機程式課號, 以 Heroku 主機為跳板, 取出修課學員名單
cp1burl = "https://nfulist.herokuapp.com/?semester=1101&courseno=0780&column=True"
# readlines() 可以將所讀的資料放入數列
data = open(cp1burl).readlines()
# 確認所讀出的資料內容為學員學號數列
# print(data)
# 設計一個篩選學員學號的函式, 用來安排座號, 但以本班學員順序為主
def setSeat(firstNum):
# 利用 len() 計算學員總數
totalNum = len(data)
# 以 affList 代表選修學員數列
affList = []
# 計算最後一位選修學員序號
count = 0
for i in data:
# 查驗是否逐一列出學員學號
#print(i)
# 以本班第一位學員作為判斷分隔依據
if i == firstNum:
affList = data[:count]
otherList = data[count:]
count += 1
# 查驗是否正確
checkNum = len(affList) + len(otherList)
if checkNum == totalNum:
print("Total number of student matched!")
# 傳回分割完成的數列
#return affList, otherList
# 開始安排作為, 將 otherList 以每 9 人一排切割
oneRow = 9
continueRow, leftOver = seatList(0, oneRow, otherList, False)
continueRow, leftOver = seatList(continueRow, oneRow, leftOver+affList, False)
continueRow, leftOver = seatList(continueRow, oneRow, leftOver, True)
def seatList(startRow, oneRow, inputList, tail=False):
totalNum = len(inputList)
leftNum = totalNum%oneRow
#print(leftNum)
intoList = inputList[:(totalNum-leftNum)]
leftOver = inputList[-leftNum:]
#print(leftOver)
column = 0
row = startRow
count = 0
for i in intoList:
column += 1
if count%oneRow == 0:
row += 1
column = 0
print("-"*20)
print("("+str(row)+","+str(column+1)+")",i)
count += 1
column = 0
count = 0
if tail == True:
row += 1
print("-"*20)
for i in leftOver:
column += 1
print("("+str(row)+","+str(column)+")",i)
count += 1
return row, leftOver
setSeat("41023201")
# 這個程式用於 demo 以 canvas 畫線以及寫字
# 從 Brython 程式庫中的 browser 模組導入 document 類別, 並以簡寫設定為 doc
from browser import document as doc
# 從 browser 模組導入 html 類別, 主要用於建立 CANVAS 標註物件, 並插入頁面中
from browser import html
# 利用 html 建立一個 CANVAS 標註物件, 與變數 canvas 對應
canvas = html.CANVAS(width = 800, height = 800)
# 將 canvas 標註的 id 設為 "canvas"
canvas.id = "canvas"
# 將 document 中 id 為 "brython_div" 的標註
# 設為與 brython_div 變數對應
brython_div = doc["brython_div"]
# 將 canvas 標註放入 brython_div 所在位置
# 頁面中原本就已經放入 <div id="brython_div"></div> 標註
brython_div <= canvas
# 將頁面中 id 為 canvas 的 CANVAS 設為與 canvas 變數對應
canvas = doc["canvas"]
# 將 canvas 的 2d 繪圖 context 命名為 ctx
ctx = canvas.getContext("2d")
# 建立一個 dRect() 函式
# s default 為 1, c defaul 為紅色
def dRect(lux, luy, w, h, s=1, c='#ff0000'):
ctx.lineWidth = s
ctx.strokeStyle = c
ctx.beginPath();
ctx.rect(lux, luy, w, h)
ctx.stroke();
# 建立 write Text 函式
def wText(x, y, t, s=14, c='#0000ff'):
ctx.font = str(s) + "px Arial";
ctx.fillText(t, x, y)
# 設定 w 寬度為 80
w = 80
# 設定 h 高度為 40
h = 40
# 設定繪圖座標點位置
x = 50
y = 50
# 利用迴圈與座標增量繪圖
for i in range(9):
xpos = x+i*w
wText(xpos+w/2, y-h/2, str(9-i))
for j in range(8):
wText(w/2-20, y+j*h+h/2+5, str(j+1))
ypos = y+j*h
dRect(xpos, ypos, w, h, 2, "black")
# 假如學號數列順序要配合行號, 必須進行座標轉換
wText(xpos+5, ypos+h/2+5, "12345678")
# 根據各班的計算機程式課號, 以 Heroku 主機為跳板, 取出修課學員名單
cp1aurl = "https://nfulist.herokuapp.com/?semester=1101&courseno=0766&column=True"
# readlines() 可以將所讀的資料放入數列
data = open(cp1aurl).readlines()
# 確認所讀出的資料內容為學員學號數列
# print(data)
finalList = []
# 設計一個篩選學員學號的函式, 用來安排座號, 但以本班學員順序為主
def setSeat(firstNum):
# 利用 len() 計算學員總數
totalNum = len(data)
# 以 affList 代表選修學員數列
affList = []
# 計算最後一位選修學員序號
count = 0
for i in data:
# 查驗是否逐一列出學員學號
#print(i)
# 以本班第一位學員作為判斷分隔依據
if i == firstNum:
affList = data[:count]
otherList = data[count:]
count += 1
# 查驗是否正確
checkNum = len(affList) + len(otherList)
if checkNum == totalNum:
print("Total number of student matched!")
# 傳回分割完成的數列
#return affList, otherList
# 開始安排作為, 將 otherList 以每 9 人一排切割
oneRow = 9
continueRow, leftOver = seatList(0, oneRow, otherList, False)
continueRow, leftOver = seatList(continueRow, oneRow, leftOver+affList, False)
continueRow, leftOver = seatList(continueRow, oneRow, leftOver, True)
def seatList(startRow, oneRow, inputList, tail=False):
totalNum = len(inputList)
leftNum = totalNum%oneRow
#print(leftNum)
intoList = inputList[:(totalNum-leftNum)]
leftOver = inputList[-leftNum:]
#print(leftOver)
column = 0
row = startRow
count = 0
rowSeat = []
for i in intoList:
column += 1
if count%oneRow == 0:
row += 1
column = 0
print("-"*20)
# finalList 只取非空數列的 rowList
if rowSeat != []:
finalList.append(rowSeat)
# 因為每一個 row 填滿後, rowSeat 必須歸零
rowSeat = []
print("("+str(row)+","+str(column+1)+")",i)
rowSeat.append(i)
count += 1
column = 0
count = 0
# 針對最後未滿 oneRow 數列的學員進行排位
# 將 rowSeat 數列歸零
rowSeat = []
if tail == True:
row += 1
print("-"*20)
for i in leftOver:
column += 1
print("("+str(row)+","+str(column)+")",i)
# 將學號逐一放入 rowSeat 數列中
rowSeat.append(i)
count += 1
# 將最後一列的學員數列放入 finalList
finalList.append(rowSeat)
return row, leftOver
setSeat("41023101")
# 電腦輔助設計室各列都已經排入 finalList 後列出檢查
print(finalList)
# 這個程式用於 demo 以 canvas 畫線以及寫字
# 從 Brython 程式庫中的 browser 模組導入 document 類別, 並以簡寫設定為 doc
from browser import document as doc
# 從 browser 模組導入 html 類別, 主要用於建立 CANVAS 標註物件, 並插入頁面中
from browser import html
# 利用 html 建立一個 CANVAS 標註物件, 與變數 canvas 對應
canvas = html.CANVAS(width = 600, height = 400)
# 將 canvas 標註的 id 設為 "canvas"
canvas.id = "canvas"
# 將 document 中 id 為 "brython_div" 的標註
# 設為與 brython_div 變數對應
brython_div = doc["brython_div"]
# 將 canvas 標註放入 brython_div 所在位置
# 頁面中原本就已經放入 <div id="brython_div"></div> 標註
brython_div <= canvas
# 將頁面中 id 為 canvas 的 CANVAS 設為與 canvas 變數對應
canvas = doc["canvas"]
# 將 canvas 的 2d 繪圖 context 命名為 ctx
ctx = canvas.getContext("2d")
# 設定繪圖寫字的尺寸與字型
ctx.font = "18px Arial";
# 若要以中空筆劃寫字, 可使用 strokeText()
#ctx.strokeText("可以寫中文", 10, 50)
# 若要以填色方式寫字, 則使用 fillText()
# (10, 50)為繪出文字的起始座標
ctx.fillText("可以寫中文", 10, 50)
# 這裡可以決定要使用筆劃或填色方式寫字
# ctx.strokeText("也可以寫 123abc", 10, 90)
ctx.fillText("也可以寫 123abc", 10, 90)
# 以下 demo 畫直線
ctx.strokeStyle = '#ff0000'
# 設定畫線尺寸
ctx.lineWidth = 5
ctx.beginPath()
ctx.moveTo(0, 5)
ctx.lineTo(300, 5)
# 執行畫線
ctx.stroke();
# 以下 demo 畫 rect(), 建立一個 dRect() 函式
# s default 為 1, c defaul 為紅色
def dRect(lux, luy, w, h, s=1, c='#ff0000'):
ctx.lineWidth = s
ctx.strokeStyle = c
ctx.beginPath();
ctx.rect(lux, luy, w, h);
ctx.stroke();
# 設定 w 寬度為 80
w = 80
# 設定 h 高度為 40
h = 40
# 利用 dRect() 繪出兩個重疊的紅色長方形
dRect(50, 150, w, h, 3)
dRect(50, 150+h, w, h, 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment