-2

So my code is pretty much done. the input is: Julia Lucas Mia

The only problem is my output should be:

Julia, Lucas, Mia
Julia, Mia, Lucas
Lucas, Julia, Mia
Lucas, Mia, Julia
Mia, Julia, Lucas
Mia, Lucas, Julia

the output I am getting is:

JuliaLucasMia
JuliaMiaLucas
LucasJuliaMia
LucasMiaJulia
MiaJuliaLucas
MiaLucasJulia

here is my code:

def all_permutations(permList, nameList):
    def createPermutationsList(nameList):
        x = len(nameList)
        if x == 0:
            return [] 
        if x == 1:
            return [nameList]  
        permList = []
        
        for i in range(x):
            first = nameList[i]
            remaining = nameList[:i] + nameList[i+1:]
            for perm in createPermutationsList(remaining):
                permList.append([first] + perm)
        return permList

    permList = createPermutationsList(nameList)
    for perm in permList:
        for val in perm:
            print(val, end = '')
        print()

if __name__ == "__main__":
    nameList = input().split(' ')
    permList = []
    all_permutations(permList, nameList)

I do not know where to put the comma correctly & i cannot use join :/

Sam
  • 1
  • 1
  • "*i cannot use join :/*" Why? Seems a pretty arbitrary requirement, considering this is precisely a use case for which `join` was implemented for. Is there some reason it's not available in your environment...? – esqew Jan 28 '23 at 15:30
  • I was told I cannot use join because our books haven't discussed that function yet. I still tried to use join anyways and my book wont accept my code. – Sam Jan 28 '23 at 15:35
  • `for perm in permList: print(*perm, sep=', ')`. – ekhumoro Jan 28 '23 at 20:18

1 Answers1

0

You could refactor your for val in perm loop slightly to append a comma and a whitespace to each val except the last one within each perm:

for perm in permList:
    for index in range(0, len(perm)):
        print(perm[index], end = ', ' if index < len(perm) - 1 else '')
    print()

Repl.it

esqew
  • 42,425
  • 27
  • 92
  • 132