python - Calculating Distance Between Two Items in a List
问题描述
I am trying to Calculate the Distance or Length between two items in a list
Question: Find the length of the longest subsequence in which the value 6 does not occur. (This number can be zero, if only 6 were in the list.)
For example, in the trial 66423612345654 the longest subsequence which the value 6 does not occur is 12345. and such the distance or length we can say is 5
Note: i have tried using trackers and for loops but i guess i am having trouble with the break positioning maybe
Throws=[6,6,2,5,3,6,6,3,6,6,3,6,6,6]
Max=0
Min=0
for i in range (0,len(Throws)):
Tracker1 = Throws[i]
if Tracker1==6:
for k in range (i+1,len(Throws)):
Tracker2 = Throws[k]
if Tracker2 ==6 and Throws[k-1]!=6:
Min = k-i
if Min>Max:
Max=Min
break
print(Max)
The Max value for the given code should be 3 ,Thanks in advance
解决方案
Easy to understand:
THROWS = [6, 6, 2, 5, 3, 6, 6, 3, 6, 6, 3, 6, 6, 6]
CONTROL_NUM = 6
max = 0
count = 0
for current in THROWS:
if current != CONTROL_NUM:
count += 1
if count > max:
max = count
else:
count = 0
print(max)
推荐阅读
- python - 将向量的每个元素重复由另一个计数向量提供的次数
- ios - 无法在 Xcode 的 Interface Builder 中处理“限制到边距”
- powershell - Get-adcomputer 字段到变量包括 cr/lf
- ios - Web VR 模式在 iOS13 上的 Safari 中不再工作吗?
- perl - SOAP::WSDL 返回错误 Can't use an undefined value as an ARRAY reference
- c# - C# 编译器扩展在构建时不显示消息
- r - Keras (R):训练后移除层
- java - Java:无法理解 newInstance() 和 getSuperClass() 类的方法是如何工作的
- sparql - SPARQL:如何从 dbpedia 检索所有犬种及其所有信息框数据?
- excel - 使用数组向表中添加新行 - 关于公式的问题