ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
def gen(n):
if n == 0:
yield ""
return
for s in gen(n - 1):
yield s + "0"
if not s.endswith("1"):
yield s + "1"
print(list(gen(3)))- A. ['000', '001', '010', '011', '100', '101', '110', '111']
- B. ['000', '001', '010', '100', '101', '110']
- C. ['000', '001', '010', '100', '101']
- D. ['000', '010', '100', '101']
Correct answer: C. ['000', '001', '010', '100', '101']
A '1' is appended only when the string does not already end in '1', so no two 1s are adjacent. This recursive generator yields the 5 such strings of length 3 in that order.
More Python Programming questions
- What is printed? python cache = {} def ways(n): if n < 0: return 0 if n == 0: return 1 if n not in cache: cache[n] = ways(n - 1) + ways(n…
- What is printed? python 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:])…
- 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…