Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Determine the Order by nhanphanminh
def init_weight(weights: dict, char: str):
if( weights.get(char) == None ):
weights[char] = set()
return weights.get(char)
def calculate_weights(weights: dict, text: str) -> dict:
if( len(text) == 0):
return weights
init_weight(weights, text[0])
index = 1
while( index < len(text) ):
weight = init_weight(weights, text[index])
previous_char = text[index - 1]
if text[index] != previous_char:
weight.update(previous_char)
weight.update(weights.get(previous_char))
index += 1
return weights
def is_previous_char(weights: dict, char_a: str, char_b: str) -> bool:
weight = weights.get(char_b)
if(char_a in weight):
return True
for char in weight:
if(is_previous_char(weights, char_a, char)):
return True
return False
def compare_chars(weights: dict, char_a: str, char_b: str) -> bool:
if is_previous_char(weights, char_a, char_b):
return -1
if is_previous_char(weights, char_b, char_a):
return 1
if( char_b > char_a ):
return -1
return 1
def insert_char(target: str, char: str, weights: dict) -> str:
if len(target) == 0:
return target + char
index = 0
insert_weight = len(weights.get(char))
while index < len(target):
current_weight = len(weights.get(target[index]))
if( compare_chars(weights, target[index], char) != -1):
break
index += 1
target = target[0:index] + char + target[index:]
return target
def checkio(data):
print(data)
print(type(data))
weights = {}
print("-------------------------------------")
print("START MERGING")
for text in data:
weights = calculate_weights(weights, text)
print("weights: ", weights)
target = ""
for char in weights.keys():
target = insert_char(target, char, weights)
print("NEW TARGET: ", target)
print("-------------------------------------")
return target
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
checkio(["hello","low","lino","itttnosw"])
assert checkio(["acb", "bd", "zwa"]) == "zwacbd", \
"Just concatenate it"
assert checkio(["klm", "kadl", "lsm"]) == "kadlsm", \
"Paste in"
assert checkio(["a", "b", "c"]) == "abc", \
"Cant determine the order - use english alphabet"
assert checkio(["aazzss"]) == "azs", \
"Each symbol only once"
assert checkio(["dfg", "frt", "tyg"]) == "dfrtyg", \
"Concatenate and paste in"
April 23, 2020