33559 www.hacker rank.com/challenges/iterable s-and-iterators/problem
problem
theitertoolsmodulestandardizesacoresetoffast,memoryefficienttoolsthatareusefulbythemselvesorincombination.together, theyformaniteratoralgebramakingitpossibletoconstructspecializedtoolssuccinctlyandefficientlyinpurepython。
toreadmoreaboutthefunctionsinthismodule,check out their documentation here。
youaregivenalistoflowercaseenglishletters.foragiveninteger,youcanselectanyindices (基于assume的索引) withauniformping
findtheprobabilitythatatleastoneoftheindicesselectedwillcontaintheletter 3360‘’。
输入格式
theinputconsistsofthreelines.thefirstlinecontainstheinteger,denotingthelengthofthelist.thenextlineconsistsofspace-space
thethirdandthelastlineofinputcontainstheinteger,denotingthenumberofindicestobeselected。
输出格式
outputasinglelineconsistingoftheprobabilitythatatleastoneoftheindicesselectedcontainstheletter 3360’。
note : theanswermustbecorrectupto3decimal places。
约束条件
allthelettersinthelistarelowercaseenglishletters。
样本输入
4
acd、acd
2
样本输出
0.8333
解决方案:
fromitertoolsimportcombinationsn=int (l=input (l=input ).split (k=int ) input ) ) c=list (组合) ) d=list (过滤器(lambdax : ' a ' inx,c ) ) (打印) ({0:4f} ).format ) ) l
打印(列表(组合([ 1,2,3 ],2 ) )
result : [ (1,2 ),1,3 ),2,3 ] ]
filter将函数应用于后续列表
print (list (过滤器(lambdaa : a 2,[ 1,2,5,6,0 ] ) )
result [ 5,6 ]