import re
from itertools import combinations
def is_valid_name(name, symbol):
if not re.match(r'^[A-Z][a-z]$', symbol): return False
one, two = symbol.lower()
name = name.lower()
try:
if one == two:
if name.count(one) < 2: return False
if name.index(one) > name.rindex(two): return False
except: return False
return True
# Bonus 1:
def find_symbol(name):
name = name.lower()
first = min(name)
if name.index(first) == len(name) - 1:
first = min(name[:-1])
second = min(name[name.index(first)+1:])
return first.upper() + second
# Bonus 2:
def how_many(name):
c = combinations(name.lower(), 2)
return len(set(c))
# Bonus 3:
def how_many2(name):
total = len(set(name)) + 1
total += sum(len(set(combinations(name, x))) for x in range(2, len(name)))
return total
# # * # # * # # * # # * Tests: # # * # # * # # * # # *
valid_test = [('Spenglerium', 'Ee'),
('Zeddemorium', 'Zr'),
('Venkmine', 'Kn'),
('Stantzon', 'Zt'),
('Melintzum','Nn'),
('Tullium','Ty'),
('Test', 'ts')]
for test in valid_test:
print(test[0], test[1], is_valid_name(test[0], test[1]))
print(' * * * Bonus 1: * * * ')
bonus1 = ('Gozerium', 'Slimyrine')
for test in bonus1:
print(test, find_symbol(test))
print(' * * * Bonus 2: * * * ')
print('Zuulon', how_many('Zuulon'))
print(' * * * Bonus 3: * * * ')
print('Zuulon', how_many2('Zuulon'))
aW1wb3J0IHJlCmZyb20gaXRlcnRvb2xzIGltcG9ydCBjb21iaW5hdGlvbnMKCmRlZiBpc192YWxpZF9uYW1lKG5hbWUsIHN5bWJvbCk6CglpZiBub3QgcmUubWF0Y2gocideW0EtWl1bYS16XSQnLCBzeW1ib2wpOiByZXR1cm4gRmFsc2UKCW9uZSwgdHdvID0gc3ltYm9sLmxvd2VyKCkKCW5hbWUgPSBuYW1lLmxvd2VyKCkKCXRyeToKCQlpZiBvbmUgPT0gdHdvOgoJCQlpZiBuYW1lLmNvdW50KG9uZSkgPCAyOiByZXR1cm4gRmFsc2UKCQlpZiBuYW1lLmluZGV4KG9uZSkgPiBuYW1lLnJpbmRleCh0d28pOiByZXR1cm4gRmFsc2UKCWV4Y2VwdD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MV0pKQoKcHJpbnQoJyAqICogKiBCb251cyAxOiAqICogKiAnKQpib251czEgPSAoJ0dvemVyaXVtJywgJ1NsaW15cmluZScpCmZvciB0ZXN0IGluIGJvbnVzMToKCXByaW50KHRlc3QsIGZpbmRfc3ltYm9sKHRlc3QpKQoJCnByaW50KCcgKiAqICogQm9udXMgMjogKiAqICogJykKcHJpbnQoJ1p1dWxvbicsIGhvd19tYW55KCdadXVsb24nKSkKCnByaW50KCcgKiAqICogQm9udXMgMzogKiAqICogJykKcHJpbnQoJ1p1dWxvbicsIGhvd19tYW55MignWnV1bG9uJykpCg==