程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[computer test questions (implementation language: python3)] find brother words --itertools

編輯:Python

Title Description
To define a word “ Brother word ” by : Exchange the alphabetical order of the word , Without adding 、 Delete 、 Words that can be generated by modifying the original letters .
Brother word requirements are different from the original word . for example :ab and ba It's a brother word .ab and ab It's not a brother word .
Now give you n Word , I'll give you another word str, Let you find str In my brother's word , Dictionary preface No k What is the big word ?
Be careful : There may be duplicate words in the dictionary . This question contains multiple groups of input data .
Input description :

 First enter the number of words n, Input again n Word .
Enter another word , For the word to be found x
Finally, enter the number k

Output description :

 Output found x The number of brothers' words m
Then output the... Found after sorting according to the dictionary order k A brother word , Not in accordance with article k You don't have to output .

Example 1
Input

3 abc bca cab abc 1

Output

2
bca

The code implementation is as follows :

import itertools
def func():
while True:
try:
lists = input().split()
n = lists[0]
str_list = lists[1:-2]
search = lists[-2]
num = int(lists[-1])
res = []
for i in itertools.permutations(search):
v = ''.join(list(i))
#print(i)
if v != search and (v not in res):
for j in str_list:
#print("==v:",v,str_list.count(v))
if v == j :
res.append(v)
res.sort()
#print(res)
print(len(res))
if len(res)>=num-1:
print(res[num-1])
except Exception as e:
#print(e)
break
if __name__ == '__main__':
func()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved