Python:每日一题 338
今天的题目:给定一个单词列表,返回可以在美国键盘(如下图)的一行上使用字母表键入的单词。
示例 1:
输入:["Hello", "Alaska", "Dad", "Peace"]
输出:["Alaska", "Dad"]
{:10_298:}欢迎大家一起答题!{:10_298:} def func(list1):
set1 = set('qwertyuiop')
set2 = set('asdfghjkl')
set3 = set('zxcvbnm')
list2 = []
for each in list1:
temp = set(each.lower())
if temp.issubset(set1) or temp.issubset(set2) or temp.issubset(set3):
list2.append(each)
return list2 @zltzlt ,能不能解释清楚一点,我有点蒙,没读懂题{:10_277:} def fun338(lst):
top,middle,bottom='qwertyuiop','asdfghjkl','zxcvbnm'
result=[]
for i in lst:
if (not set(i.lower())-set(top)) or (not set(i.lower())-set(bottom)) or (not set(i.lower())-set(middle)):
result.append(i)
return result
差集 def fun338(lst):
top,middle,bottom='qwertyuiop','asdfghjkl','zxcvbnm'
result=[]
for i in lst:
if set(top)&set(i.lower())==set(i.lower()) or set(middle)&set(i.lower())==set(i.lower()) or set(bottom)&set(i.lower())==set(i.lower()):
result.append(i)
return result
交集 本帖最后由 fan1993423 于 2020-2-28 20:00 编辑
def fun338(lst):
top,middle,bottom='qwertyuiop','asdfghjkl','zxcvbnm'
result=[]
for i in lst:
if set(top)|set(i.lower())==set(top) or set(middle)|set(i.lower())==set(middle) or set(bottom)|set(i.lower())==set(bottom):
result.append(i)
return result
并集 def judge(word):
if word.isalpha():
word = set(word.lower())
else :
return False
part1, part2, part3 = 'qwertyuiop', "asdfghjkl", "zxcvbnm"
part_1, part_2, part_3 = 0, 0, 0
for each in word:
if each in part1:
part_1 += 1
if part_2 != 0 and part_3 != 0:
return False
if each in part2:
part_2 += 1
if part_1 != 0 and part_3 != 0:
return False
if each in part3:
part_3 += 1
if part_1 != 0 and part_2 != 0:
return False
return True
words = eval(input("请输入列表单词"))
data = []
for word in words:
if judge(word):
data.append(word)
print(data)
我好菜{:10_334:}
看看大佬代码{:10_256:} def func338(s:list) -> list:
keyboard = ["qwertyuiop", "asdfghjkl", "zxcvbnm"]
list1 = s
index = 0
for word_index in s:
string = word_index.lower()
for i in range(len(keyboard)):
if string in keyboard:
index = i
break
for i in string:
if i not in keyboard:
list1.remove(word_index)
break
return list1
def fun338(lst):
top,middle,bottom='qwertyuiop','asdfghjkl','zxcvbnm'
def istop_middle_botomm(s,t=''):
if s in top and (t=='' or t=='top'):
try:
return istop_middle_botomm(s,'top')
except:
return True
elif s in middle and (t=='' or t=='middle'):
try:
return istop_middle_botomm(s,'middle')
except:
return True
elif s in bottom and (t=='' or t=='bottom'):
try:
return istop_middle_botomm(s,'bottom')
except:
return True
else:return False
result=[]
for i in lst:
if istop_middle_botomm(i.lower()):result.append(i)
return result jianpan = [['q','w','e','r','t','y','u','i','o','p'],
['a','s','d','f','g','h','j','k','l'],
['z','x','c','v','b','n','m']]
a = eval(input())
d = list(map(lambda x:x.lower(),a))
for i in d:
if i in jianpan:
if not set(i)<set(jianpan):
d.remove(i)
elif i in jianpan:
if not set(i)<set(jianpan):
d.remove(i)
elif i in jianpan:
if not set(i)<set(jianpan):
d.remove(i)
for i in a:
if i.lower() in d:
pass
else:
a.remove(i)
print(a)
def f338(L:list)->list:
res=[]
L1=['QWERTYUIOP','ASDFGHJKL','ZXCVBNM']
for i in L:
L10,L11,L12=[],[],[]
for j in i:
if j.upper() in L1:
L10.append(j.upper())
elif j.upper() in L1:
L11.append(j.upper())
else:
L12.append(j.upper())
if ''.join(L10).title() in L:
res.append(''.join(L10).title())
if ''.join(L11).title() in L:
res.append(''.join(L11).title())
if ''.join(L12).title() in L:
res.append(''.join(L12).title())
return res
L=["Hello", "Alaska", "Dad", "Peace"]
print(f338(L)) 冬雪雪冬 发表于 2020-2-28 17:09
72 ms 本帖最后由 zltzlt 于 2020-2-29 12:43 编辑
此帐号已被注销 发表于 2020-2-28 17:29
我好菜
看看大佬代码
解答错误
输入:["Hello", "Alaska", "Dad", "Peace"]
输出:["Hello", "Alaska", "Dad"]
预期结果:["Alaska", "Dad"] yexing 发表于 2020-2-28 17:38
解答错误
输入:['qaz', 'wsx', 'edc', 'rfv', 'tgb', 'yhn', 'ujm', 'ik', 'pklo', 'ppppppp']
输出:['wsx', 'rfv', 'yhn', 'ik', 'ppppppp']
预期结果:['ppppppp'] fan1993423 发表于 2020-2-28 18:06
164 ms 本帖最后由 kinkon 于 2020-2-29 00:26 编辑
def f338(lst):
t = set("qwertyuiop")
m = set("asdfghjkl")
l = set("zxcvbnm")
n = []
for i in lst:
k = i.lower()
if set(k) & t == set(k) or set(k) & m == set(k) or set(k) & l == set(k):
n.append(k.title())
return n
print(f338(["Hello", "Alaska", "Dad", "Peace"]))
最后的魁拔 发表于 2020-2-28 18:09
解答错误
输入:['qaz', 'wsx', 'edc', 'rfv', 'tgb', 'yhn', 'ujm', 'ik', 'pklo', 'ppppppp']
输出:['wsx', 'rfv', 'yhn', 'ik', 'ppppppp']
预期结果:['ppppppp'] ouyunfu 发表于 2020-2-28 18:41
解答错误
输入:['qaz', 'wsx', 'edc', 'rfv', 'tgb', 'yhn', 'ujm', 'ik', 'pklo', 'ppppppp']
输出:[]
预期结果:['ppppppp'] kinkon 发表于 2020-2-28 19:35
先无脑解下
解答错误
输入:['qaz', 'wsx', 'edc', 'rfv', 'tgb', 'yhn', 'ujm', 'ik', 'pklo', 'ppppppp']
输出:['Ppppppp']
预期结果:['ppppppp'] fan1993423 发表于 2020-2-28 17:20
交集
解题思路居然和你的很接近{:10_277:}