Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import choice, randint
- from string import ascii_lowercase
- def create_strs(num=5, low=3, high=6):
- "return list of two-string tuples with <num> elements"
- "strings formed as per the new_str function"
- def new_str():
- "return string consisting of a lower case letter repeated"
- "a random number of times (between <low> and <high>)"
- return choice(ascii_lowercase) * randint(low, high)
- return [(new_str(), new_str()) for _ in range(randint(5, 10))]
- def balance(alpha, beta):
- "return string of whichever is longer of <alpha> or <beta>"
- "that constitutes the substring of the longer part"
- "(return empty string if same length)"
- len_alpha = len(alpha)
- len_beta = len(beta)
- if len_alpha < len_beta:
- return beta[len_alpha:]
- elif len_beta < len_alpha:
- return alpha[len_beta:]
- else:
- return ""
- source = create_strs(20) # list of 20 tuples of random strings
- for first, second in source:
- merged = "".join(
- [letter for letter_pair in zip(first, second) for letter in letter_pair]
- ) + balance(first, second)
- print(f"{merged:12} {len(merged):2}: {len(first)} + {len(second)}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement