2010-01-24 52 views
2

我想对Ruby的几行Python的转换,从由托马斯客户签署本优秀的文章:http://wordaligned.org/articles/drawing-chess-positions代码翻译,用Python和Ruby

(注:我是一个真正做大的Python小白)

这里是原来的Python版本的副本:

def expand_blanks(fen): 
    '''Expand the digits in an FEN string into spaces 

    >>> expand_blanks("rk4q3") 
    'rk q ' 
    ''' 
    def expand(match): 
     return ' ' * int(match.group(0)) 
    return re.compile(r'\d').sub(expand, fen) 

def outer_join(sep, ss): 
    '''Like string.join, but encloses the result with outer separators. 

    Example: 
    >>> outer_join('|', ['1', '2', '3']) 
    '|1|2|3|' 
    ''' 
    return '%s%s%s' % (sep, sep.join(ss), sep) 

def ascii_draw_chess_position(fen): 
    '''Returns an ASCII picture of pieces on a chessboard.''' 
    pieces = expand_blanks(fen).replace('/', '') 
    divider = '+-+-+-+-+-+-+-+-+\n' 
    rows = ((outer_join('|', pieces[r: r + 8]) + '\n') 
      for r in range(0, 8 * 8, 8)) 
    return outer_join(divider, rows) 

用例:

>>> fen = "r2q1rk1/pp2ppbp/1np2np1/2Q3B1/3PP1b1/2N2N2/PP3PPP/3RKB1R" 
>>> print ascii_draw_chess_position(fen) 
+-+-+-+-+-+-+-+-+ 
|r| | |q| |r|k| | 
+-+-+-+-+-+-+-+-+ 
|p|p| | |p|p|b|p| 
+-+-+-+-+-+-+-+-+ 
| |n|p| | |n|p| | 
+-+-+-+-+-+-+-+-+ 
| | |Q| | | |B| | 
+-+-+-+-+-+-+-+-+ 
| | | |P|P| |b| | 
+-+-+-+-+-+-+-+-+ 
| | |N| | |N| | | 
+-+-+-+-+-+-+-+-+ 
|P|P| | | |P|P|P| 
+-+-+-+-+-+-+-+-+ 
| | | |R|K|B| |R| 
+-+-+-+-+-+-+-+-+ 

我已经尝试做了一些修改,以在Ruby中的每一行转换......我不知道,如果是糟糕的开局:秒,但我无论如何发布:

def expand_blanks(fen) 
    def expand(match) 
    return ' ' * int(match.group(0)) 
    end 

    # bugged: 
    re.compile(r'\d').sub(expand, fen) 
end 

def outer_join(sep, ss) 
    sep + sep.join(ss) + sep 
end 

def ascii_draw_chess_position(fen) 
    pieces = expand_blanks(fen).replace('/', '') 
    puts pieces.class 
    divider = "+-+-+-+-+-+-+-+-+\n" 

    # bugged lines: 
    rows = ((outer_join('|', pieces[r, r + 8]) + '\n') 
    for r in range(0, 8 * 8, 8)) 
    return outer_join(divider, rows) 
    end 
end 

fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR" 
puts ascii_draw_chess_position(fen) 

如果你看到一些线,我可以解决,我会对我很酷。谢谢你们。

回答

2

依次查看每种方法,从outer_join开始。

在Python中,join是一个字符串方法,它接受一个序列并加入由字符串分隔的元素。例如'|'.join(['p, 'p', 'p'])

在Ruby中,join是将分隔符作为参数的一种方法,例如['p', 'p', 'p'].join('|')

所以outer_join Ruby的版本是:

def outer_join(sep, ss): 
    sep + ss.join(sep) + sep 
end 

expand_blanks接下来看看,这里的意图是用空格当量数更换数字。在Ruby中,可以使用gsub来替换字符串中所有出现的模式。 gsub可以用一个将被传递每个匹配子字符串的块来调用,并返回匹配应该被替换的字符串。所以Ruby代码将是:

def expand_blanks(fen) 
    fen.gsub(/\d/) { |match| ' ' * match.to_i } 
end 

最后在ascii_draw_chess_position我们可以再次使用gsub作为等同于Python的replace方法和使用Ruby的map功能的地方使用Python的列表理解如下达到什么样的:

def ascii_draw_chess_position(fen) 
    pieces = expand_blanks(fen).gsub('/', '') 
    divider = "+-+-+-+-+-+-+-+-+\n" 
    rows = (0...8).map do |i| 
    row = pieces[i * 8...(i + 1) * 8].split('') 
    outer_join("|",row) + "\n" 
    end 
    puts outer_join(divider, rows) 
end 

让我知道你是否需要关于上述任何更多细节。

+0

非常感谢Mikej和Grifaton。现在我得到了我所要做的每件事。再次感谢! 此致敬礼。 – Denis 2010-01-24 23:57:08

1

这会做你想要什么:

def expand(match) 
    ' ' * match.to_i 
end 

def expand_blanks(fen) 
    fen.gsub(/\d/) {|d| expand d} 
end 

def outer_join(sep, ss) 
    sep + ss.join(sep) + sep 
end 

def ascii_draw_chess_position(fen) 
    pieces = expand_blanks(fen).gsub('/', '') 
    divider = "+-+-+-+-+-+-+-+-+\n" 
    rows = (0...8).collect do |i| 
    row = pieces[i*8...(i+1)*8].split('') 
    outer_join("|",row) + "\n" 
    end 
    puts outer_join(divider, rows) 
end 

让我知道如果这里有你不明白任何代码 - 但要确保你已经看过了在ruby docs

如果方法您对Ruby和Python的区别感兴趣,请参阅https://stackoverflow.com/questions/234721/what-are-the-biggest-differences-between-python-and-ruby-from-a-philosophical-perhttp://www.ruby-lang.org/en/documentation/ruby-from-other-languages/to-ruby-from-python/,有关一些很好的示例,请参阅http://ruby.brian-amberg.de/editierdistanz/

+0

非常感谢,Grifaton!我会研究并看到不同之处。 – Denis 2010-01-24 23:55:41