ProgrammingHardCognizant GenC · TCS NQT
What is printed?
What is printed?
def lcp(words):
out = []
for col in zip(*words):
if len(set(col)) == 1:
out.append(col[0])
else:
break
return ''.join(out)
print(repr(lcp(['flower', 'flow', 'flight'])), repr(lcp(['dog', 'racecar'])), repr(lcp(['a'])))- A. 'fl' '' 'a'
- B. 'fl' 'dog' 'a'
- C. 'fl' '' ''
- D. 'fl' 'd' 'a'
Correct answer: A. 'fl' '' 'a'
zip(*words) walks down the columns, and the loop stops at the first column whose letters differ. A single word is its own prefix, and repr shows the empty string as ''.
More Python Programming questions
- What is printed? python s = 'abc' 2 + 'd' print(s[::3], 'bcd' in s, s.index('c', 3))
- What is printed? python print(range(0, 10, 3) == range(0, 11, 3), range(0, 10, 3)[-1], len(range(1018)))
- What is printed? python s = 'abc' t = s s += 'd' u = list('abc') v = u u += ['d'] print(s, t, u is v, v)
- What is printed? python print(bin(0b1011011).count('1'), (0b1011011).bitcount(), 0b1011011 & (0b1011011 - 1))
- What is printed? python def f(n): c = 0 while n: n &= n - 1 c += 1 return c print(f(255), f(-1 & 0xFF), f(0))
- What is printed? python s = 'abc' print(s[::-1] + s[0], s[1:] + s[:1], s[-1] + s[-2:])