checkio (Determine the order)

The Robots have found an encrypted message. We cannot decrypt it at the moment, but we can take the first steps toward doing so. You have a set of "words" all in lower case, each word contains symbols in "alphabetical order" (it's not your typical alphabetical order, but a new and different order). We need to determine the order of the symbols from each "word" and create a single "word" with all of these symbols, placing them in the new alphabetial order. In some cases, if we cannot determine the order for several symbols, you should use the traditional latin alphabetical order. For example: Given words "acb", "bd", "zwa". As we can see "z" and "w" must be before "a" and "d" after "b". So the result is "zwacbd".

Precondition: In each test, there can be the only one solution.

Input: A list of strings.

Output: A string.

Example:

?
1
2
3
4
5
checkio(["acb", "bd", "zwa"]) == "zwacbd"
checkio(["klm", "kadl", "lsm"]) == "kadlsm"
checkio(["a", "b", "c"]) == "abc"
checkio(["aazzss"]) == "azs"
checkio(["dfg", "frt", "tyg"]) == "dfrtyg"

把限制条件转换成树的结构,重新定义比较函数,排序得到答案。

def unique(data):
    ret = ''
    for c in data:
        if c not in ret:
            ret += c
    return ret

father = {}

def mycmp(self, other):
    #print self,other
    if not find(self, other):
        if not find(other, self):
            #print '='
            return cmp(self, other)
        else:
            #print '>'
            return -1
    #print '<'
    return 1


def find(x, target):
    while x in father:
        if father[x]==target:
            return True
        x = father[x]
    return False

    
def checkio(data):
    father.clear()
    word = []
    for condition in data:
        condition = unique(condition)
        for i in range(1,len(condition)):
            father[condition[i]] = condition[i-1]

    for condition in data:
        for c in condition:
            word.append(c)
    word = list(unique(word))
    word.sort(cmp=mycmp)
    #print word
    return ''.join(word)
  


#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
    
    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"

A了后看了下高手的代码:

def checkio(data):
    distinct_letters = sorted(set("".join(data)))
    print distinct_letters
    #We just get couples of letters (a, b) such that a must be before b
    constraints = [(word[i], word[i + 1]) for word in data for i in range(len(word) - 1)]
    
    solution_found = False
    while not solution_found:
        solution_found = True
        for (a, b) in constraints:
            ia = distinct_letters.index(a)
            ib = distinct_letters.index(b)
            if ia > ib:
                #We swap the letters
                distinct_letters[ia] = b
                distinct_letters[ib] = a
                solution_found = False
    return "".join(distinct_letters)



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值