...

View Full Version : anagrams recursion



chaz12
04-12-2008, 11:23 PM
I can't use any for or while loops. This is what I have in Python:

def anagrams(s):
if s == "":
return [s]
else:
ans = []
for w in anagrams(s[1:]):
for pos in range(len(w)+1):
ans.append(w[pos]+s[0]+w[pos:])
return ans


Also how do I convert this code to java? For example, the for loop:

for w in anagrams(s[1:]):

chaz12
04-13-2008, 09:09 AM
anyone? I'm been stuck on this problem for days. I have no idea how to find the anagrams without using for or while loops. Someone please help. I would think that one would need to use a for loop to add each starting letter to each position between the characters of the string. Am I correct?

Thanks

Trinithis
04-15-2008, 06:15 AM
Why not use a for loop?

As for the java for-in loop, the syntax is like so:


// abstractly
for(T t : TCollectionOrTArray) {
. . .
}

// concretely
String[] strs = new String[] {"a", "ab", "abc"};
for(String str : strs) {
System.out.println(str);
}

brad211987
04-15-2008, 05:17 PM
Can you explain the goal of the program a bit more? What exactly are you trying to do?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum