We would like to implement the lexical order for lists. For simplicity, we only
ID: 3554855 • Letter: W
Question
We would like to implement the lexical order for lists. For simplicity, we only consider lists of numbers, where <, <=, ==, >, >= have their usual meaning.
Write a function compL(L1, L2), where L1, L2 are lists of numbers; the function should return -1 if L1 comes before L2 in lexical order, 0 if L1 == L2, and 1 otherwise.
[Recall the lexical order for lists: We skip the longest common prefix of L1 and L2; then we compare the next entry of L1 and the next entry of L2, and return the result of that comparison. In the special case where L1, L2 both have no next entry after their common prefix, we conclude that L1 == L2. In the special case where L1 has no next entry but L2 does, we conclude that L1 comes first; if L2 has no next entry but L1 does, we conclude that L2 comes first.] Simply put, we are not just going to say L1 > L2 or if L1 < L2. We are supposed to write a program that basically recreates the lexical order in lists.
Explanation / Answer
"""The Code is as follows:
PM me if you do not understand any part.
Cheers!!
"""
import os.path
L1 = ['5','0', '1', '1997', '2000'];
L2 = ['5', '0'];
ans1 = 1;
ans2 = 0;
ans3 = -1;
def commonprefix(L1,L2):
L3 = os.path.commonprefix([L1,L2])
a = len(L3)
b = len(L1)
c = len(L2)
if(b ==a):
print ans1
elif(c==a):
print ans3
elif( b == c==a):
print ans3
else:
print ans2
commonprefix(L1,L2)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.