Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,12 @@
|
|||
A = 'I am string'
|
||||
B = 'I am string too'
|
||||
|
||||
if len(A) > len(B):
|
||||
print('"' + A + '"', 'has length', len(A), 'and is the longest of the two strings')
|
||||
print('"' + B + '"', 'has length', len(B), 'and is the shortest of the two strings')
|
||||
elif len(A) < len(B):
|
||||
print('"' + B + '"', 'has length', len(B), 'and is the longest of the two strings')
|
||||
print('"' + A + '"', 'has length', len(A), 'and is the shortest of the two strings')
|
||||
else:
|
||||
print('"' + A + '"', 'has length', len(A), 'and it is as long as the second string')
|
||||
print('"' + B + '"', 'has length', len(B), 'and it is as long as the second string')
|
||||
|
|
@ -0,0 +1,82 @@
|
|||
"""
|
||||
An example code for the task "Compare length of two strings" (Rosseta Code).
|
||||
|
||||
This example code can handle not only strings, but any objects.
|
||||
"""
|
||||
|
||||
|
||||
def _(message):
|
||||
"""Translate: an placeholder for i18n and l10n gettext or similar."""
|
||||
return message
|
||||
|
||||
|
||||
def compare_and_report_length(*objects, sorted_=True, reverse=True):
|
||||
"""
|
||||
For objects given as parameters it prints which of them are the longest.
|
||||
|
||||
So if the parameters are strings, then the strings are printed, their
|
||||
lengths and classification as the longest, shortest or average length.
|
||||
|
||||
Note that for N > 0 such objects (e.g., strings, bytes, lists) it is
|
||||
possible that exactly M > 0 of them will be of the maximum length, K > 0 of
|
||||
them will be of the minimum length. In particular, it is possible that all
|
||||
objects will be exactly the same length. So we assume that if an object has
|
||||
both the maximum and minimum length, it is referred to as a string with the
|
||||
maximum length.
|
||||
|
||||
Args:
|
||||
*objects (object): Any objects with defined length.
|
||||
sorted_ (bool, optional): If sorted_ is False then objects are not
|
||||
sorted. Defaults to True.
|
||||
reverse (bool, optional): If reverse is True and sorted_ is True
|
||||
objects are sorted in the descending order. If reverse is False
|
||||
and sorted_ is True objects are sorted in the ascending order.
|
||||
Defaults to True.
|
||||
|
||||
Returns:
|
||||
None.
|
||||
"""
|
||||
lengths = list(map(len, objects))
|
||||
max_length = max(lengths)
|
||||
min_length = min(lengths)
|
||||
lengths_and_objects = zip(lengths, objects)
|
||||
|
||||
# Longer phrases make translations into other natural languages easier.
|
||||
#
|
||||
has_length = _('has length')
|
||||
if all(isinstance(obj, str) for obj in objects):
|
||||
predicate_max = _('and is the longest string')
|
||||
predicate_min = _('and is the shortest string')
|
||||
predicate_ave = _('and is neither the longest nor the shortest string')
|
||||
else:
|
||||
predicate_max = _('and is the longest object')
|
||||
predicate_min = _('and is the shortest object')
|
||||
predicate_ave = _('and is neither the longest nor the shortest object')
|
||||
|
||||
if sorted_:
|
||||
lengths_and_objects = sorted(lengths_and_objects, reverse=reverse)
|
||||
|
||||
for length, obj in lengths_and_objects:
|
||||
if length == max_length:
|
||||
predicate = predicate_max
|
||||
elif length == min_length:
|
||||
predicate = predicate_min
|
||||
else:
|
||||
predicate = predicate_ave
|
||||
print(obj, has_length, length, predicate)
|
||||
|
||||
|
||||
A = 'I am string'
|
||||
B = 'I am string too'
|
||||
LIST = ["abcd", "123456789", "abcdef", "1234567"]
|
||||
|
||||
|
||||
print('Two strings')
|
||||
print()
|
||||
compare_and_report_length(A, B)
|
||||
print()
|
||||
|
||||
print('A list of strings')
|
||||
print()
|
||||
compare_and_report_length(*LIST)
|
||||
print()
|
||||
Loading…
Add table
Add a link
Reference in a new issue