UNSW 9021 quiz2

Quiz 2  

问题背景:

输入一组数字,将其转化为八进制后(首位的0位置不变)从右向左读入,按以下方式移动:

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

我们从开关为开的位置开始前进,如果移动到位置为关的开关则打开,移动到位置为开的开关则关掉


举例描述:

例如:Enter a non-strictly negative integer: 0256
Keeping leading 0's, if any, in base 8, 0256 reads as 0400.

Enter a non-strictly negative integer: 3654
Keeping leading 0’s, if any, in base 8, 3654 reads as 7106.
⚪⚫
⚫⚪
⚪⚫
⚪⚪


解决方案及代码:

给出的基本格式和输入
import sys

on = '\u26aa' #开的unicode字符
off = '\u26ab' #关的unicode字符
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): #找出数字前0的个数
    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()

主要代码:

  1. Reverse input and set a move dic
octal = '0' * nb_of_leaading_zeroes + f'{int(code):o}' #转化为八进制的数字
reoctal = reversed(octal) #翻转字符串使其从右至左读入

#建立对应题目要求的移动字典,拿坐标系来判断东南西北
move = {
    '0': (0, 1),
    '1': (1, 1),
    '2': (1, 0),
    '3': (1, -1),
    '4': (0, -1),
    '5': (-1,-1),
    '6': (-1, 0),
    '7': (-1, 1)
}
  1. Store the coordinate points obtained by the movement into the list
list = [(0,0)] # 建立一个带有起始点(坐标原点)的列表用来存放出现过的坐标点
start_x = 0 #起始x
start_y = 0 #起始y

for i in reoctal:
    x, y = move[i] #在move字典中查找i值对应的坐标点并赋值给x和y
    start_x += x #按照对应坐标点依次变化
    start_y += y
    if (start_x, start_y) not in list: #如果坐标点不在list列表里则加入,如果已存在则删除,对应开关
        list.append((start_x, start_y))
    else:
        list.remove((start_x, start_y))
  1. Use max() and min() to find the boundary value in order to build the matrix,
    set the initial horizontal and vertical coordinates
    (set to unreasonable values:The largest coordinate is smaller than the smallest coordinate)
max_x = 0 #设置初始坐标边界值(不合理情况:max比min大)
min_x = 1
max_y = 0
min_y = 1

for i in list: #寻找边界值
    x, y = i
    max_x = max(x, max_x)
    min_x = min(x, min_x)
    max_y = max(y, max_y)
    min_y = min(y, min_y)
  1. Loop through the boundary value to find whether the array exists in the list,
    store it in a str and then print out
switch = '' #存放开关
for y in range(max_y, min_y-1, -1): #按边界值从上到下,从左至右的方向循环每个坐标点
    for x in range(min_x, max_x+1, 1):
        if (x, y) in list: #如果循环到的坐标点在list中将开的图形加入到字符串当中,否则加入关的图形
            switch += on
        else:
            switch += off
    switch += '\n' #循环一遍x轴后加入一个换行符
print(switch) #打印最后图形

总结

主要使用了列表存入坐标点的方法,还可以考虑使用元组或字典等。
仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值