Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.Start your free trial
Phillip Bailey1,822 Points
I just can't wrap my head around these last 3 challenges
I only kinda understand the decorator, but but the examples given were not as clear as others in the tract. I believe I would need to make an iterative loop to convert the string but I'm drawing a blank on how.
class Letter: def __init__(self, pattern=None): self.pattern = pattern def __iter__(self): yield from self.pattern def __str__(self): output =  for blip in self: if blip == '.': output.append('dot') else: output.append('dash') return '-'.join(output) class S(Letter): def __init__(self): pattern = ['.', '.', '.'] super().__init__(pattern)
Chris FreemanTreehouse Moderator 68,332 Points
Create a class method in Letter named from_string that takes a string like "dash-dot" and creates an instance with the correct pattern (['_', '.']).
Let's break down the problem from the description.
Create a class method... -- need to creating a method decorated with
from_string... -- method is called
from_stringand goes in the class
- ... that takes a string like "dash-dot"... -- the method has one parameter, you can expect it to be a string
- ...and creates an instance... -- classmethods do not have an instance to operate on, so you can create one (see below)
...with the correct pattern (['_', '.']). -- You're correct, in that a loop will be needed to parse the string (using
spliton a hyphen seems appropriate) and convert each "dash" to an '_' underscore and each "dot" to a '.' period. Use this list of parsed characters called, say 'pattern
, you can create a new instance usinginst = cls(pattern)`
- remember to
returnthe new instance.
Post back if you need more help. Good luck!