Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 8th, 2006, 10:41 PM   #1
titaniumdecoy
Programming Guru
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 1,007
Rep Power: 5 titaniumdecoy will become famous soon enough
Send a message via AIM to titaniumdecoy
What's wrong with this code?

I'm writing a function that takes a number (eg, 2345) and selects those names from a pre-defined list of names (in a text file) that can be formed by replacing each number with one of three corresponding letters, shown below:

2: ['A', 'B', 'C']
3: ['D', 'E', 'F']
4: ['G', 'H', 'I']
5: ['J', 'K', 'L']
6: ['M', 'N', 'O']
7: ['P', 'R', 'S']
8: ['T', 'U', 'V']
9: ['W', 'X', 'Y']
I wrote a recursive function to create a list of all possible combinations of letters, then select those that match up to names in the pre-defined list of names. This worked but was extremely slow for longer numbers (up to 12 digits). I figure (although I may be wrong) that such an algorithm would be O(3^n) -- Is that correct?

# works, but slow
def lettercombos(num):
	if (len(num) == 1):
		return numdict[int(num)]
	for n in num:
		ret = []
		for l in numdict[int(n)]:
			for x in lettercombos(num[1:]):
				ret.append(l + x)
		return ret
My next approach was to first remove all names from the list that were not the same length as the number. Then, I iterate through each digit of the number, and remove names from the new list of names that do not have the corresponding letters for that number in that location. I ended up writing the following code:

# does not work
for i in range(len(num)):
	letters = numdict[int(num[i])]
	for name in namelist:
		if name[i] not in letters:
			namelist.remove(name)
However, this does not work properly. I cannot figure out what is going wrong. Am I using the list.remove() function correctly? I can't seem to figure out what the problem could be.

Thanks for your help.
titaniumdecoy is offline   Reply With Quote
Old Jun 8th, 2006, 10:46 PM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,188
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
I think the problem may be that it's iterating through a list that you're changing.

Create a temporary copy of the list before, then copy back after like so:

# does not work
for i in range(len(num)): # i is the index of each digit
	letters = numdict[int(num[i])]
        templist = namelist[:] # create a direct copy of the list rather then referencing it
	for name in namelist:
		if name[i] not in letters:
			templist.remove(name)
        namelist = templist[:]

If that doesn't work, please post a runnable version of the code to demonstrate the problem. Right now I don't know how to piece your snippets together.
Sane is offline   Reply With Quote
Old Jun 8th, 2006, 11:14 PM   #3
titaniumdecoy
Programming Guru
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 1,007
Rep Power: 5 titaniumdecoy will become famous soon enough
Send a message via AIM to titaniumdecoy
I've solved the problem by rewriting the function using filter():

for i in range(len(num)):
	letters = numdict[int(num[i])]
	namelist = filter(lambda w: w[i] in letters, namelist)
I am still puzzled as to why the previous function I wrote does not work, however. I have done extensive debugging and everything seems fine, except the output.

EDIT: Ooops, Sane: It looks like you were right. I was still trying to remove from the list I was iterating through... The working function is as follows:

nc = list(namelist)
for i in range(len(num)):
	letters = numdict[int(num[i])]
	for name in nc:
		if name in namelist and name[i] not in letters:
			namelist.remove(name)
titaniumdecoy is offline   Reply With Quote
Old Jun 8th, 2006, 11:21 PM   #4
splinter9x
Hobbyist Programmer
 
splinter9x's Avatar
 
Join Date: Jun 2006
Posts: 137
Rep Power: 0 splinter9x is an unknown quantity at this point
This question can be answered simply by this statement.... *It is Python*
__________________
Visit my Blog
I support WINDOWS...
splinter9x is offline   Reply With Quote
Old Jun 8th, 2006, 11:23 PM   #5
titaniumdecoy
Programming Guru
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 1,007
Rep Power: 5 titaniumdecoy will become famous soon enough
Send a message via AIM to titaniumdecoy
Quote:
Originally Posted by splinter9x
This question can be answered simply by this statement.... *It is Python*
Huh? Of course it's Python...
titaniumdecoy is offline   Reply With Quote
Old Jun 8th, 2006, 11:24 PM   #6
splinter9x
Hobbyist Programmer
 
splinter9x's Avatar
 
Join Date: Jun 2006
Posts: 137
Rep Power: 0 splinter9x is an unknown quantity at this point
Are you really that airheaded?
__________________
Visit my Blog
I support WINDOWS...
splinter9x is offline   Reply With Quote
Old Jun 8th, 2006, 11:24 PM   #7
titaniumdecoy
Programming Guru
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 1,007
Rep Power: 5 titaniumdecoy will become famous soon enough
Send a message via AIM to titaniumdecoy
I guess... :o

BTW, why do I have to confirm "name in namelist"? Since nc is a copy of namelist, they should both contain the same names, but I get an error without this check. (Maybe the list contains duplicates? ... but that shouldn't be a problem... should it?)
titaniumdecoy is offline   Reply With Quote
Old Jun 8th, 2006, 11:26 PM   #8
splinter9x
Hobbyist Programmer
 
splinter9x's Avatar
 
Join Date: Jun 2006
Posts: 137
Rep Power: 0 splinter9x is an unknown quantity at this point
Wow, it was a joke about the actual language...
__________________
Visit my Blog
I support WINDOWS...
splinter9x is offline   Reply With Quote
Old Jun 8th, 2006, 11:32 PM   #9
titaniumdecoy
Programming Guru
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 1,007
Rep Power: 5 titaniumdecoy will become famous soon enough
Send a message via AIM to titaniumdecoy
Ohhhhhh... I thought you wrote "Is it Python", not "It is Python"...
titaniumdecoy is offline   Reply With Quote
Old Jun 8th, 2006, 11:40 PM   #10
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,188
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Quote:
Originally Posted by titaniumdecoy
why do I have to confirm "name in namelist"?
Because one of the items may have already been removed by an earlier iteration of _i_. list().remove will raise an error that you could catch if you don't want to check for occurance in the list. The latter should be faster actually (but less understandable).
Sane is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:59 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC