Random Seats
- Suppose you have a file which lists students in the following
format:
102241035:黃瓊儀
103321001:游思愉
103321002:張仕達
103321007:黃柏元
103321008:張秝榕
103321009:楊甄誼
103321010:賴郁伶
103321016:邱俊翔
103321017:謝長霖
103321019:鮑惟仁
- We want to arrange these students into a few seats, so we have a
file in the following format, which designates the number of seats in
each row:
6
5
4
5
6
- Suppose we have already defined a class Student as follows:
class Student:
def __init__(self, n, id):
self.name = n
self.id = id
self.seq = randrange(1000)
# When an object is created, a random number is assigned to it
- Write a function readStudents() to read students data into a list.
- Write a function readSeatTable() to read the number of seats into a
list.
- Write a function printStudents(list) to print out students in
a list according to the number specified in the seats file. For
example, for the file
6
5
4
5
6
it will print 6 students in the first row, 5 students in the second row,
4 students in the third row, and so on.
- You may test your program with this
students file and this
seats file.
- If your browser cannot show the Chinese characters in the
students file, change its encoding to "UTF-8".
- In Internet Explorer, choose View - Encoding - Unicode
(UTF-8).
- In Firefox, choose View - Character Encoding - Unicode
- In Google Chrome, choose Settings - Show advanced settings
- Web content.
Click the button "Customize fonts".
Encoding - Unicode (UTF-8)
- An easier way (for someones) is, choose More tools -
Encoding - Unicode (UTF-8).
- Or, you may visit the URL chrome://settings/fonts/,
scroll down to the bottom of that page,
and choose Encoding - Unicode (UTF-8).
After pressing Done. Reload the webpage containing the
Chinese characters. You can see them displayed normally now.
Test your functions with the following main program:
def main():
studentList = readStudents("students-26.txt")
seatList = readSeatTable("seats-4.txt")
printStudents(studentList, seatList)
The program may run as follows:
102241035:黃瓊儀 103321001:游思愉 103321002:張仕達 103321007:黃柏元
103321008:張秝榕 103321009:楊甄誼 103321010:賴郁伶 103321016:邱俊翔
103321017:謝長霖 103321019:鮑惟仁 103321021:胡瑋哲 103321022:陳冠銘
103321024:潘嘉玲 103321025:余正威 103321026:張寬宏 103321027:黃仲誼
103321029:王韋翔 103321030:陳政樺 103321032:曾冠迪 103321037:范均泓
103321038:吳怡蓓 103321042:詹雅惠 103321047:吳芠萱 103321049:陳思穎
103321050:周霈菱 103321063:吳嘉偉