Hi all its me needing help again :P
My exercise says: Write a function wordPop that accepts a text and a length N and returns the list of all the words that are N letters long, sorted by their length.

okay so what I have is 2 fuctions first:

``````def length_compare(x, y):
return len(x) - len(y)``````

and

``````def wordPop(text, n):

nwords = []

words = text.split()

for word in words:

if (len(word) <= n):
nwords.append(word)

nwords = sorted(nwords,cmp=length_compare)
return nwords``````

Here is where i run into the problem i type in

``nwords = wordPop("the cat ate the rat in the hat",3)``

``````def wordPop(text, n):
nwords = []
words = text.split()
for word in words:
if (len(word) >= n):
nwords.append(word)
return sorted(nwords)

w = 'This is a test off word lenght'
print wordPop(w.lower(), 4)
#->['lenght', 'test', 'this', 'word']``````

So i removed you length_compare function.
sorted(nwords,cmp=length_compare)
So this get wrong,first off cmp is a python key word.
And your "length_compare" function is not comparing anything.

``````>>> cmp
<function length_compare at 0x0265EF30>
>>> help(cmp)
Help on function cmp=length_compare in module __main__:

length_compare(x, y)``````
``````def length_compare(x, y):
return len(x) - len(y)

x = 'hi'
x = 'test'
print length_compare(x, y)  #->2
#If you call function you have to use 2 argument
#And it`s not comparing anything,just extract test(4) from hi(2)``````

Unfortunately the answer in snippsat's nice post is sorting alphabetically, not by length. Here fix and list comprehenision to do the same.

``````def wordPop(text, n):
nwords = []
words = text.split()
for word in words:
if (len(word) >= n):
nwords.append(word)
return sorted(nwords,
key=len,
reverse= True ## if longest first
)

text = 'This is a test off word lenght almost working'
print wordPop(text.lower(), 4)
# original code (length is before working in alphabetic order
#->['almost', 'lenght', 'test', 'this', 'word', 'working']
# after fix key=len
#->['this', 'test', 'word', 'lenght', 'almost', 'working']
#with reverse=True
# ->['working', 'lenght', 'almost', 'this', 'test', 'word']
print 'Shortly:',sorted((word.lower()
for word in text.split()
if len(word)>=4),
key=len,
reverse=True)``````

Yes,forgot sorted by lenght was thinking alphabetically sort.
Thanks tony.

...
My exercise says: Write a function wordPop that accepts a text and a length N and returns the list of all the words that are N letters long, sorted by their length.
...

According to this, the function has to return only words with the length N, so what is the sense of sorting them by length?

Nice catch vegaseat something strange with definition of task. I assumed longer than limit, but definition said equal to limit. I would guess that we did the right code with wrong definition, but who knows... Yes, the starter of thread knows. How is it?

Thank you so much everyone, yes the question is really one that cannot be solved by the program, I sent my professor an email advising I altered the question just a little. Thanks again for all your help :)

Be a part of the DaniWeb community

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