ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
def perms(s):
    if len(s) <= 1:
        return [s]
    out = []
    for i, c in enumerate(s):
        for p in perms(s[:i] + s[i + 1:]):
            out.append(c + p)
    return out
r = perms("aba")
print(len(r), len(set(r)), sorted(set(r))[0])
  1. A. 3 3 aab
  2. B. 6 6 aab
  3. C. 6 3 aba
  4. D. 6 3 aab

Correct answer: D. 6 3 aab

The recursion permutes positions, so 3! = 6 strings are produced, with duplicates because 'a' appears twice. Only 3 are distinct (aab, aba, baa), and the smallest is 'aab'.

More Python Programming questions

Exams that ask this topic