[Tutor] swapping list elements based on some criterion
steve at pearwood.info
Sat Oct 8 07:51:16 CEST 2011
Emad Nawfal (عمـ نوفل ـاد) wrote:
> Hello Tutors,
> It's been quite some time since I last posted something here, and now I'm
> back with a question:
> I want to re-structure English so that the adjectives appear after the
> nouns, instead of before.
> If I have a sentence like:
> The tall man plays well
> I need to change it to
> The man tall plays well
> So far, I have thought of the following function, but I'm not sure it's the
> best solution. It assumes that the sentence has been augmented with part of
> speech tags
> (Also plz note that there may be multiple adjectives)
And what do you expect to happen with multiple adjectives?
"the tall fat bald man"
=> "the man tall fat bald"
=> "the man fat tall bald"
=> "the man bald fat tall"
=> "the man fat bald tall"
=> something else?
The approach I would take is this:
Walk along the sentence, inspecting each word.
Collect all consecutive adjectives into a list of adjectives
Other words get copied straight into the buffer.
When you reach a noun, copy the noun into the buffer, plus the list
of adjectives (in whichever order you like!), then clear the list
of adjectives ready for the next noun.
Does that help?
Something like this:
buffer = 
adjectives = 
for word in sentence.split():
adjectives = 
return ' '.join(buffer)
P.S. Since this looks like homework, I have left a deliberate mistake in
my code for you to fix. But it should be easy to fix if you study the
code and think about what it is doing.
More information about the Tutor