Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

[Python] permutation with duplicates

I am implementing a algorithm that prints out all permutations of a string with duplicates allowed. For example, "Hello" will have 5! number of outcomes. However, I am getting errors for my functions that I don't understand how to debug. Please help me.

def freq(s):
    my_dict = {}
    for l in s:
        if l not in my_dict:
            my_dict[l] = 0
        my_dict[l] += 1
    return my_dict

def printPerms1(letter_count_list, prefix, remaining, result):

    if len(remaining) == 0:
        result += prefix
        return result
    else:
        for l in letter_count_list:
            count = letter_count_list[l]
            if count > 0:
                result += l

                printPerms1(letter_count_list, prefix + l, remaining - 1, result)
    return result

def printPerms(s):
    letter_count_list = freq(s)
    prefix = s[0]
    remaining = len(s) - 1
    result = ""
    printPerms1(letter_count_list, prefix, remaining, result)


printPerms("Hello")

The only error I found is in your line

if len(remaining) == 0:

Since remaining is an int, it has no length. However, the following may achieve your desired effect:

if remaining == 0:

However, I tried this, and discovered printPerms("Hello") returns None, which is obviously not your intended result. So there is more work to be done here.