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])- A. 3 3 aab
- B. 6 6 aab
- C. 6 3 aba
- 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
- What is printed? python def makeops(): ops = {} for name, sym in [("add", "+"), ("mul", "")]: ops[name] = eval(f"lambda a, b: a {sym} b")…
- What is printed? python x = 1 def f(): return x def g(): x = 2 return f() def h(): global x x = 3 return f() print(g(), h(), f())
- What is printed? python class Celsius: def init(self, t=0): self.t = t def get(self, obj, owner): return self if obj is None else obj.c…
- What is printed? python class Vec: def init(self, c): self.c = c def mul(self, k): return Vec((x k for x in self.c)) rmul = mul def…
- What is printed? python class Ordered: def init(self, v): self.v = v def lt(self, o): print("lt", end=" ") return self.v < o.v def…
- What is printed? python import functools @functools.totalordering class V: def init(self, n): self.n = n def eq(self, o): return self.n ==…