[英]Linked List Class __str__ function
I 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. 但是,为了完成最后三个doctests,我不知道该怎么做。 Help!!! 救命!!!
class Link(object):
empty = ()
def __init__(self, first, rest=empty):
self.first = first
self.rest = 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
. 现在,这是一个有点奇怪避免使用递归rest
,但随后间接使用递归的first
。 (That's what '{0}'.format(…)
does—if you haven't defined a __format__
method, it calls your __str__
method.) (这就是'{0}'.format(…)
作用 - 如果你还没有定义__format__
方法,它会调用你的__str__
方法。)
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. 作为旁注:这是一个股票计划练习很糟糕地移植到Python(这意味着你的老师要么不喜欢或不喜欢Python,这不是一个好兆头...),但它缺少一块。
Normally, you're supposed to handle, eg, Link(1, 2)
differently from Link(1, Link(2))
. 通常,您应该处理(例如, Link(1, 2)
与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. (在Lisp术语中,那是(1 . 2)
1.2 (1 . 2)
与(1 2)
。)但是你没有给出测试的例子,因此不清楚你应该为前者输出什么 。 Sp… be prepared to be marked off for not reading your teacher's mind, unless you want to do something like this: 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.