Week 3
这周我们 ed 系统上有1个 Quiz 和5个 Challenges。
[TOC]
Quiz 3
这个 Quiz 写好了输入输出的代码。以及如何将输入的数字,保留所有前面的'0'
之后,剩余的数字由10进制转换为8进制。
# Written by *** and Eric Martin for COMP9021
# Reading the number written in base 8 from right to left,
# keeping the leading 0's, if any:
# 0: move N 1: move NE 2: move E 3: move SE
# 4: move S 5: move SW 6: move W 7: move NW
#
# We start from a position that is the unique position
# where the switch is on.
#
# Moving to a position switches on to off, off to on there.
import sys
on = '\u26aa'
off = '\u26ab'
code = input('Enter a non-strictly negative integer: ').strip()
try:
if code[0] == '-':
raise ValueError
int(code)
except ValueError:
print('Incorrect input, giving up.')
sys.exit()
nb_of_leading_zeroes = 0
for i in range(len(code) - 1):
if code[i] == '0':
nb_of_leading_zeroes += 1
else:
break
print("Keeping leading 0's, if any, in base 8,", code, 'reads as',
'0' * nb_of_leading_zeroes + f'{int(code):o}.'
)
print()
# INSERT YOUR CODE HERE
题目要求
Reading the number written in base 8 from right to left, keeping the leading 0's, if any:
- 0 Move North
- 1 Move North-East
- 2 Move East
- 3 Move South-East
- 4 Move South
- 5 Move South West
- 6 Move West
- 7 Move North-West
We start from a position that is the unique position where the switch is on.
Moving to a position switches on to off, off to on there.
依旧,pdf 文档中给出了 test cases 和希望的输出结果。详情请见 Quiz 3 要求 -- OneDrive 分享 。
在 shell 中输入:
$ python3 quiz_3_v2.py
Enter a non-strictly negative integer: 000123456789
期望得到的输出:
Keeping leading 0's, if any, in base 8, 000123456789 reads as 000726746425.
⚪⚫⚫⚫
⚪⚫⚫⚫
⚪⚫⚫⚪
⚪⚫⚪⚪
⚪⚫⚪⚪
⚫⚫⚪⚫
你需要做
- 按照老师代码转换出来的每一位数字,移动自己的位置。每移动到一个新的位置的同时,将这个位置的灯开关一次(也就是说,如果这个灯是 on 则切换为 off;如果是 off 则切换为 on)
- 找到最左、最右、最上、最下的四个 on 的灯,将其外所有 off 的灯去掉
- 输出。on 与 off 的灯的符号已经给出。
on = '\u26aa'
off = '\u26ab'
代码实现
(by Harriet and Eric Martin, All Right Reserved)
Anyone who is plagiarisming, which include copying, Inappropriate paraphrasing, Collusion, Inappropriate citation, Self-plagiarism will undermine academic integrity and is not tolerated at UNSW.
# Written by Harriet and Eric Martin for COMP9021
# Reading the number written in base 8 from right to left,
# keeping the leading 0's, if any:
# 0: move N 1: move NE 2: move E 3: move SE
# 4: move S 5: move SW 6: move W 7: move NW
#
# We start from a position that is the unique position
# where the switch is on.
#
# Moving to a position switches on to off, off to on there.
import sys
on = '\u26aa'
off = '\u26ab'
code = input('Enter a non-strictly negative integer: ').strip()
try:
if code[0] == '-':
raise ValueError
int(code)
except ValueError:
print('Incorrect input, giving up.')
sys.exit()
nb_of_leading_zeroes = 0
for i in range(len(code) - 1):
if code[i] == '0':
nb_of_leading_zeroes += 1
else:
break
#############################################################################################
# Record read string with '0's kept in `readstr`
# This is for later use
readstr = '0' * nb_of_leading_zeroes + f'{int(code):o}'
#############################################################################################
print("Keeping leading 0's, if any, in base 8,", code, 'reads as', '0' * nb_of_leading_zeroes + f'{int(code):o}.')
print()
#############################################################################################
# Variable Assignment
#############################################################################################
step_mov = {}
step_mov = {
0: [0, 1],
1: [1, 1],
2: [1, 0],
3: [1, -1],
4: [0, -1],
5: [-1, -1],
6: [-1, 0],
7: [-1, 1]
}
step_current = []
location_walk = []
location_walk = [0, 0]
max_dimension = len(readstr)
max_x = -2*max_dimension
max_y = -2*max_dimension
min_x = 2*max_dimension
min_y = 2*max_dimension
max_walking_map = [
[
0 for i in range(2*max_dimension+1)
]
for j in range(2*max_dimension+1)
]
origin_position = [max_dimension, max_dimension]
max_walking_map[origin_position[1]][origin_position[0]] = 1
#############################################################################################
# Function Defination
#############################################################################################
def mov(num_mov):
step_current = step_mov[num_mov]
location_walk[0] += step_current[0]
location_walk[1] += step_current[1]
def switch_on_off(x, y):
if max_walking_map[y][x] == 0:
max_walking_map[y][x] = 1
else:
max_walking_map[y][x] = 0
def translate(bin_on_off):
if bin_on_off == 1:
return on
else:
return off
#############################################################################################
# Main: Walking and switching
#############################################################################################
for i in range(max_dimension):
mov(int(readstr[i]))
xx = origin_position[0] + location_walk[0]
yy = origin_position[1] + location_walk[1]
switch_on_off(xx, yy)
#############################################################################################
# Detecting the acturally used map
#############################################################################################
for i in range(2*max_dimension+1):
for j in range(2*max_dimension+1):
if max_walking_map[j][i] == 1:
if i >= max_x:
max_x = i
if i <= min_x:
min_x = i
if j >= max_y:
max_y = j
if j <= min_y:
min_y = j
#############################################################################################
# Generate the really used map using detected threatholds `max_`/`min_` of x and y
#############################################################################################
real_map = [
[
0 for i in range(max_x - min_x + 1)
]
for j in range(max_y - min_y + 1)
]
for j in range(max_y - min_y + 1):
for i in range(max_x - min_x + 1):
real_map[j][i] = max_walking_map[j+min_y][i+min_x]
#############################################################################################
# Output
#############################################################################################
for j in range(max_y - min_y + 1):
str_line = ''
for i in range(max_x - min_x + 1):
str_line += translate(real_map[j][max_x - min_x - i])
print(str_line)