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'])))
  1. A. 'fl' '' 'a'
  2. B. 'fl' 'dog' 'a'
  3. C. 'fl' '' ''
  4. 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

Exams that ask this topic