Python 从2013年起,需要关于过去acsl计划的帮助
我的老师让我们做一个旧的ACSL练习项目,他说我们可以使用我们想要的任何资源。该项目从2013年开始。链接此处: 我们启动了这个项目,但我们遇到了一堵墙,我们不知道从这里该怎么办:Python 从2013年起,需要关于过去acsl计划的帮助,python,acsl,Python,Acsl,我的老师让我们做一个旧的ACSL练习项目,他说我们可以使用我们想要的任何资源。该项目从2013年开始。链接此处: 我们启动了这个项目,但我们遇到了一堵墙,我们不知道从这里该怎么办: board = [ [ 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25] ] for i in range(1,
board = [
[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25]
]
for i in range(1, 6):
pieces = input("%d. "%(i)).split(",")
white = []
black = []
black_start = int(pieces[0])+1
for j in range(1, black_start):
white.append(int(pieces[j]))
for k in range(black_start+1, len(pieces)):
black.append(int(pieces[k]))
print(white)
print(black)
for pair in board:
有人能给我们一些提示吗?我们正在用Python编写代码。我认为
board
实际上没有任何用途
将输入解析为黑白片段位置的代码看起来不错,可能应该将其制作成一个函数
对于调试而言,使用一个函数获取黑白工件位置并打印电路板是很有用的,例如,使用字符#
表示黑色,O
表示白色,
表示空。这将帮助您了解您的程序正在做什么
对于每个方向,您都应该有一个函数(即left
),用于返回下一个位置(或None
,如果它位于电路板边缘)和另一个函数(即left_lst
),用于返回该方向上连续位置的列表。所以left(17)
返回16
,left(16)
返回None
,left_lst(19)
返回[18,17,16]
。提示:如何在left
方面实现left
然后:对于每个白色片段,检查向每个方向移动或远离每个方向可以捕捉到什么。你知道应该只有一个解决方案,所以一旦你找到它,你就可以返回它;如果没有找到,请返回None
希望有帮助
为了好玩和有趣,我想出了一个解决办法。希望你学到很多
# https://s3.amazonaws.com/iedu-attachments-question/5a989d787772b7fd88c063aff8393d34_1bee2d300c35eec13edf0a3af515a5a5.pdf
NUM_PROBS = 5
# Board looks like
# 1 2 3 4 5
# 6 7 8 9 10
# 11 12 13 14 15
# 16 17 18 19 20
# 21 22 23 24 25
WIDTH = 5
HEIGHT = 5
# display characters for printing:
WHITE = 'O'
BLACK = '#'
EMPTY = '.'
from itertools import product
def parse(s):
"""
Input: string of comma-delimited integers
ex: "3, 12, 17, 22, 3, 9, 14, 10"
format is num_white, *[white locations], num_black, *[black locations]
Output: [white locations], [black locations]
ex: [12, 17, 22], [9, 14, 10]
"""
ints = [int(i) for i in s.split(',')]
num_white = ints[0]
num_black = ints[num_white + 1]
return ints[1:num_white + 1], ints[-num_black:]
def location_to_coords(location):
"""
Given a location on the board,
return 0-based (y,x) coordinates
ex: location_to_coords(16) returns (3, 0)
"""
return divmod(location - 1, WIDTH)
def coords_to_location(y, x):
"""
Given (y, x) coordinates,
return a location on the board
ex: coords_to_location(3, 0) returns 16
"""
return y * WIDTH + x + 1
def print_board(whites, blacks):
# make an empty board
board = [[EMPTY] * WIDTH for row in range(HEIGHT)]
# add white pieces
for location in whites:
y, x = location_to_coords(location)
board[y][x] = WHITE
# add black pieces
for location in blacks:
y, x = location_to_coords(location)
board[y][x] = BLACK
# show the result
print('\n'.join(''.join(row) for row in board))
def make_dir_fn(dy, dx):
"""
Given a direction, make a function
which, given a location, returns
the next location in that direction
(or None if no such location exists)
"""
def dir_fn(location):
y, x = location_to_coords(location)
if 0 <= y + dy < HEIGHT and 0 <= x + dx < WIDTH:
return coords_to_location(y + dy, x + dx)
else:
return None
return dir_fn
def make_lst_fn(dir_fn):
"""
Given a direction function, make a function
which, given a location, returns a list
of all consecutive locations in that direction
to the edge of the board
"""
def lst_fn(location):
result = []
while True:
location = dir_fn(location)
if location is None:
break
else:
result.append(location)
return result
return lst_fn
# direction functions (one step in the named direction)
left = make_dir_fn( 0, -1)
right = make_dir_fn( 0, 1)
up = make_dir_fn(-1, 0)
down = make_dir_fn( 1, 0)
# relationships between direction functions
dir_fns = [left, right, up, down]
lst_fn = {dir_fn: make_lst_fn(dir_fn) for dir_fn in dir_fns}
opposite_dir_fn = {left: right, right: left, up: down, down: up}
def attack_toward(location, dir_fn, whites, blacks):
"""
Return a list of pieces captured by attacking toward given direction
"""
# make sure attacker is white (swap if needed)
if location in blacks:
whites, blacks = blacks, whites
# make sure we have a valid attacker
if location not in whites:
return []
# make sure we have a space to move to
next_location = dir_fn(location)
if (next_location is None) or (next_location in whites) or (next_location in blacks):
return []
# get list of attacked locations
attacked = lst_fn[dir_fn](next_location)
captured = []
for location in attacked:
if location in blacks:
captured.append(location)
else:
break
return captured
def attack_away(location, dir_fn, whites, blacks):
"""
Return a list of pieces captured by attacking away from given direction
"""
# make sure attacker is white (swap if needed)
if location in blacks:
whites, blacks = blacks, whites
# make sure we have a valid attacker
if location not in whites:
return []
# make sure we have a space to move to
next_location = opposite_dir_fn[dir_fn](location)
if (next_location is None) or (next_location in whites) or (next_location in blacks):
return []
# get list of attacked locations
attacked = lst_fn[dir_fn](location)
captured = []
for location in attacked:
if location in blacks:
captured.append(location)
else:
break
return captured
attack_fns = [attack_toward, attack_away]
def main():
for prob in range(NUM_PROBS):
# get problem
whites, blacks = parse(input())
# pick an attacker, a direction, and an attack method
for location, dir_fn, attack_fn in product(whites, dir_fns, attack_fns):
captured = attack_fn(location, dir_fn, whites, blacks)
# stop when a successful attack is found!
if captured: break
# display results
if captured:
print(", ".join(str(i) for i in sorted(captured)))
else:
print("NONE")
if __name__ == "__main__":
main()
#https://s3.amazonaws.com/iedu-attachments-question/5a989d787772b7fd88c063aff8393d34_1bee2d300c35eec13edf0a3af515a5a5.pdf
NUM_PROBS=5
#板子看起来像
# 1 2 3 4 5
# 6 7 8 9 10
# 11 12 13 14 15
# 16 17 18 19 20
# 21 22 23 24 25
宽度=5
高度=5
#显示用于打印的字符:
白色='O'
黑色=“#”
空='。'
来自itertools进口产品
def解析:
"""
输入:逗号分隔的整数字符串
例:“3、12、17、22、3、9、14、10”
格式为num_白色、*[白色位置]、num_黑色、*[黑色位置]
输出:[白色位置],[黑色位置]
例:[12,17,22],[9,14,10]
"""
ints=[int(i)表示s.split(',')中的i]
num_white=整数[0]
num_black=ints[num_white+1]
返回整数[1:num\u白色+1],整数[-num\u黑色:]
def位置到坐标(位置):
"""
在黑板上给定一个位置,
返回基于0的(y,x)坐标
例如:位置到坐标(16)返回(3,0)
"""
返回divmod(位置-1,宽度)
def坐标到坐标位置(y,x):
"""
给定(y,x)坐标,
返回板上的位置
例如:coords_to_位置(3,0)返回16
"""
返回y*宽度+x+1
def打印板(白色、黑色):
#做一块空木板
board=[[EMPTY]*范围内行的宽度(高度)]
#加入白色的碎片
以白色显示的位置:
y、 x=位置到坐标(位置)
电路板[y][x]=白色
#加入黑色的碎片
对于黑色的位置:
y、 x=位置到坐标(位置)
板[y][x]=黑色
#显示结果
打印('\n'.join(''.join(行)用于线路板中的行))
def make_dir_fn(dy,dx):
"""
给一个方向,做一个函数
给定一个位置,返回
该方向的下一个位置
(如果不存在此类位置,则无)
"""
def dir_fn(位置):
y、 x=位置到坐标(位置)
如果0在电路板上迭代,则不会得到对,而是6个元素的列表。不管怎样,你想对黑板上的每一个“配对”做什么?我本来打算找到输入的位置在黑板上的位置,但我不知道怎么做。你说的“位置”是什么意思?谢谢你的帮助!我会尽量考虑你告诉我的一些技巧,希望我能完成这个项目。