Compare length of two strings: Difference between revisions

Line 171:
 
=={{header|Python}}==
def naive_compare_and_report_length(str1, str2):
if len(str1) > len(str2):
print('"' + str1 + '"', 'has length', len(str1), 'and is longer')
print('"' + str2 + '"', 'has length', len(str2), 'and is shorter')
elif len(str1) < len(str2):
print('"' + str1 + '"', 'has length', len(str1), 'and is shorter')
print('"' + str2 + '"', 'has length', len(str2), 'and is longer')
else:
print('"' + str1 + '"', 'has length', len(str1), 'and is equal')
print('"' + str2 + '"', 'has length', len(str2), 'and is equal')
def compare_and_report_length(*objects):
"""
For objects given as parameters it prints which of them are the longest.
Note that it is possible that each of these objects has the same length.
"""
lengths = list(map(len, objects))
max_length = max(lengths)
for length, obj in zip(lengths, objects):
predicate = 'is' if length == max_length else 'is not'
print(f'"{obj}" has length {length} and {predicate} the longest string')
 
 
A = 'I am string'
B = 'I am string too'
 
print('Naive')
naive_compare_and_report_length(A, B)
print()
 
print('Sophisticated')
compare_and_report_length(A, B)
print()
 
=={{header|Raku}}==