我想创建这个功能
>>> str(Link('Hello')) 'Hello' >>> str(Link(1, Link(2))) '1 -> 2' >>> print(Link(1 / 2, Link(1 // 2))) 0.5 -> 0 >>> str(Link(Link(1, Link(2, Link(3))), Link(4, Link(5)))) '(1 -> 2 -> 3) -> 4 -> 5' >>> print(Link(Link(Link(Link('Wow'))))) (((Wow))) >>> print(Link(Link('a'), Link(Link('b'), Link(Link('c'))))) (a) -> (b) -> (c)这是我的代码:
def __str__(self): result = '' while self.rest is not Link.empty: result += '{0} -> '.format(self.first) self = self.rest return result + '{0}'.format(self.first)但是,为了完成最后三个doctests,我不知道该怎么做。 帮帮我!!!
class Link(object): empty = () def __init__(self, first, rest=empty): self.first = first self.rest = restI want to create this function
>>> str(Link('Hello')) 'Hello' >>> str(Link(1, Link(2))) '1 -> 2' >>> print(Link(1 / 2, Link(1 // 2))) 0.5 -> 0 >>> str(Link(Link(1, Link(2, Link(3))), Link(4, Link(5)))) '(1 -> 2 -> 3) -> 4 -> 5' >>> print(Link(Link(Link(Link('Wow'))))) (((Wow))) >>> print(Link(Link('a'), Link(Link('b'), Link(Link('c'))))) (a) -> (b) -> (c)Here is my code:
def __str__(self): result = '' while self.rest is not Link.empty: result += '{0} -> '.format(self.first) self = self.rest return result + '{0}'.format(self.first)However, I do not know what to do in order to fulfill the last three doctests. Help!!!
class Link(object): empty = () def __init__(self, first, rest=empty): self.first = first self.rest = rest最满意答案
看起来规则应该是如果列表的头部本身是一个列表,则格式化该列表,并将其放在括号中。 像这样的东西:
first = '({0})'.format(self.first) if isinstance(self.first, Link) else first result += '{0} -> '.format(first)现在,避免在rest使用递归有点奇怪,但之后间接使用递归。 (这就是'{0}'.format(…)作用 - 如果你还没有定义__format__方法,它会调用你的__str__方法。)
因此,假设这是一个赋值,如果赋值告诉您不使用递归,则需要将其转换为循环。 另一方面,如果赋值不是为了避免递归,那么只对两者进行递归会简单得多:
first = str(self.first) if isinstance(self.first, Link): first = '({0})'.format(first) if self.rest is Link.empty: return first return '{0} -> {1}'.format(first, self.rest)作为旁注:这是一个股票计划练习很糟糕地移植到Python(这意味着你的老师要么不喜欢或不喜欢Python,这不是一个好兆头...),但它缺少一块。
通常,您应该处理(例如, Link(1, 2)与Link(1, Link(2)) 。 (在Lisp术语中,那是(1 . 2) 1.2 (1 . 2)与(1 2) 。)但是你没有给出测试的例子,因此不清楚你应该为前者输出什么 。 Sp ...准备好标记为不读老师的想法,除非你想做这样的事情:
first = str(self.first) if isinstance(self.first, Link): first = '({0})'.format(first) if self.rest is Link.empty: return first rest = str(self.rest) if isinstance(self.rest, Link): return '{0} -> {1}'.format(first, self.rest) else: # surely (1 2 . 3) is not the same list as (1 2 3) but the spec left it out? return '{0} .. {1}'.format(first, self.rest)It looks like the rule is supposed to be that if the head of a list is itself a list, you format that list, and put it in parentheses. Something like this:
first = '({0})'.format(self.first) if isinstance(self.first, Link) else first result += '{0} -> '.format(first)Now, it's a bit strange to avoid using recursion on rest, but then indirectly use recursion on first. (That's what '{0}'.format(…) does—if you haven't defined a __format__ method, it calls your __str__ method.)
So, assuming this is an assignment, if the assignment tells you to not use recursion, you're going to need to turn that into a loop. If, on the other hand, the assignment doesn't say to avoid recursion, it would be a lot simpler to just recurse on both:
first = str(self.first) if isinstance(self.first, Link): first = '({0})'.format(first) if self.rest is Link.empty: return first return '{0} -> {1}'.format(first, self.rest)As a side note: This is a stock Scheme exercise ported badly to Python (which implies that your teacher either doesn't get or doesn't like Python, which isn't a great sign…), but it's missing a piece.
Normally, you're supposed to handle, e.g., Link(1, 2) differently from Link(1, Link(2)). (In Lisp terms, that's (1 . 2) vs. (1 2).) But none of the examples you gave test for that, so it's not clear exactly what you're supposed to output for the former. Sp… be prepared to be marked off for not reading your teacher's mind, unless you want to do something like this:
first = str(self.first) if isinstance(self.first, Link): first = '({0})'.format(first) if self.rest is Link.empty: return first rest = str(self.rest) if isinstance(self.rest, Link): return '{0} -> {1}'.format(first, self.rest) else: # surely (1 2 . 3) is not the same list as (1 2 3) but the spec left it out? return '{0} .. {1}'.format(first, self.rest)更多推荐
发布评论