I am required to create a function that returns a list of all instances of single characters in 'text' that immediately follow 'last'. For example...

def follow_char(text,last):
"""
>>> follow_char('as we consider the operations', 'co')

"""

I think I am required to solve this using linked lists but am not sure how to do this, any help will be greatly appreciated, cheers =)

Recommended Answers

There is many ways, if the text is big, usually you would build dictionary with letter pairs as keys (for example for random text generation).

Here is one example of more advanced way of doing it by list comprehension:

>>> def follow_char(big, small):
	return [c for …
Jump to Post

All 2 Replies

There is many ways, if the text is big, usually you would build dictionary with letter pairs as keys (for example for random text generation).

Here is one example of more advanced way of doing it by list comprehension:

>>> def follow_char(big, small):
	return [c for n, c in enumerate(big[len(small):], len(small))
		if big[:n].endswith(small)]

>>> follow_char('as we consider the operations', 'co')
['n']
>>> follow_char('as we consider the operations coconut', 'co')
['n', 'c', 'n']
>>>

Cheers pyTony, thanks for your reply!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.