寻找兄弟单词

输入描述:
先输入字典中单词的个数,再输入n个单词作为字典单词。
输入一个单词,查找其在字典中兄弟单词的个数
再输入数字n


输出描述:
根据输入,输出查找到的兄弟单词的个数

输入例子:
3 abc bca cab abc 1

输出例子:
2 bca
python编程实现:
# -*- coding:utf-8 -*-
import sys
def findBrother(words,word,k):
    brotherWords=[]
    #从兄弟单词中找出第K个兄弟单词返回
    charWord=sorted([c for c in word])
    #print charWord
    for item in words:
        #将每个单词的字符进行分割
        itemList=sorted([c for c in item])
        #print itemList
#==是弱等号 即只要元素以及位置相等,就返回true
        if (charWord==itemList and item !=word):
            brotherWords.append(item)
    #对brotherWords列表进行排序 然后找出第K个兄弟
    brotherWordList=sorted(brotherWords)
    return len(brotherWordList),brotherWordList[k-1]
if __name__ == '__main__':
    try:
        lines=sys.stdin.readline().split()
        number=int(lines[0])
        words=[]
        for i in range(1,number+1):
            words.append(lines[i])

        #输入要寻找兄弟单词的单词
        word=lines[-2]
        #输入要寻找的第k个兄弟的k
        k=int(lines[-1])
        # print words,word,k
        # print "=======================----"
        brotherWordLen,brotherWord=findBrother(words,word,k)
        print brotherWordLen,brotherWord
    except:
        pass


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值