我正在使用 for 循环遍历一系列数据点,并计算出如何对数据显示的趋势进行分类
这是标记点的逻辑:
我们优先考虑上升/下降趋势,而不是低于或高于均值
我认为我使用的逻辑不正确,但我不确定为什么。这是该系列句子中的一些逻辑:
“对于每个点,如果接下来的 5 个点低于此点,然后是他们之前的点,则将所有这些点标记为“下降趋势”。否则,如果一个点未标记趋势,请查看以下 6 个点是否来自此点都高于或低于平均值"
您可以完整地复制和粘贴此内容:
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
type points struct {
quantity float64
lowerBound float64
upperBound float64
mean float64
pattern string
}
func main() {
var pointsToUse = []points{
points{quantity: 3.5, lowerBound: 1, upperBound: 4.8,, mean: 4, pattern: "Common Variation"},
points{
quantity: 3.4,
lowerBound: 3,
upperBound: 4.8,
mean: 4,
pattern: "Common Variation",
},
points{
quantity: 3.2,
lowerBound: 3,
upperBound: 4.8,
mean: 4,
pattern: "Common Variation",
},
points{
quantity: 3.19,
lowerBound: 3,
upperBound: 4.8,
mean: 4,
pattern: "Common Variation",
},
points{
quantity: 3.17,
lowerBound: 3,
upperBound: 4.8,
mean: 4,
pattern: "Common Variation",
},
points{
quantity: 3.14,
lowerBound: 3,
upperBound: 4.8,
mean: 4,
pattern: "Common Variation",
},
points{
quantity: 3.12,
lowerBound: 3,
upperBound: 4.8,
mean: 4,
pattern: "Common Variation",
},
points{
quantity: 3.09,
lowerBound: 3,
upperBound: 4.8,
mean: 4,
pattern: "Common Variation",
},
points{
quantity: 4.1,
lowerBound: 3,
upperBound: 4.8,
mean: 4,
pattern: "Common Variation",
},
points{
quantity: 3.16,
lowerBound: 3,
upperBound: 4.8,
mean: 4,
pattern: "Common Variation",
},
points{
quantity: 3,
lowerBound: 3,
upperBound: 4.8,
mean: 4,
pattern: "Common Variation",
},
points{
quantity: 3,
lowerBound: 3,
upperBound: 4.8,
mean: 4,
pattern: "Common Variation",
},
points{
quantity: 3.11,
lowerBound: 3,
upperBound: 4.8,
mean: 4,
pattern: "Common Variation",
},
points{
quantity: 3.12,
lowerBound: 3,
upperBound: 4.8,
mean: 4,
pattern: "Common Variation",
},
points{
quantity: 3.14,
lowerBound: 3,
upperBound: 4.8,
mean: 4,
pattern: "Common Variation",
},
points{
quantity: 3.13,
lowerBound: 3,
upperBound: 4.8,
mean: 4,
pattern: "Common Variation",
},
points{
quantity: 3.17,
lowerBound: 3,
upperBound: 4.8,
mean: 4,
pattern: "Commons Variation",
},
}
for i, point := range pointsToUse {
if len(pointsToUse) >= 6 && len(pointsToUse)-(i+1) >= 5 {
if point.quantity > pointsToUse[i+1].quantity && pointsToUse[i+1].quantity > pointsToUse[i+2].quantity && pointsToUse[i+2].quantity > pointsToUse[i+3].quantity && pointsToUse[i+3].quantity > pointsToUse[i+4].quantity && pointsToUse[i+4].quantity > pointsToUse[i+5].quantity {
point.pattern = "Decreasing Trend"
pointsToUse[i+1].pattern = "Decreasing Trend"
pointsToUse[i+2].pattern = "Decreasing Trend"
pointsToUse[i+3].pattern = "Decreasing Trend"
pointsToUse[i+4].pattern = "Decreasing Trend"
pointsToUse[i+5].pattern = "Decreasing Trend"
continue
} else if point.quantity < pointsToUse[i+1].quantity && pointsToUse[i+1].quantity < pointsToUse[i+2].quantity && pointsToUse[i+2].quantity < pointsToUse[i+3].quantity && pointsToUse[i+3].quantity < pointsToUse[i+4].quantity && pointsToUse[i+4].quantity < pointsToUse[i+5].quantity {
point.pattern = "Increasing Trend"
pointsToUse[i+1].pattern = "Increasing Trend"
pointsToUse[i+2].pattern = "Increasing Trend"
pointsToUse[i+3].pattern = "Increasing Trend"
pointsToUse[i+4].pattern = "Increasing Trend"
pointsToUse[i+5].pattern = "Increasing Trend"
continue
}
}
if (point.pattern != "Decreasing Trend" || point.pattern != "Increasing Trend") && len(pointsToUse)-(i+1) >= 6 {
if point.quantity < pointsToUse[i].mean && pointsToUse[i+1].quantity < pointsToUse[i].mean && pointsToUse[i].quantity < pointsToUse[i].mean && pointsToUse[i+3].quantity < pointsToUse[i].mean && pointsToUse[i+4].quantity < pointsToUse[i].mean && pointsToUse[i+5].quantity < pointsToUse[i].mean && pointsToUse[i+6].quantity < pointsToUse[i].mean {
pointsToUse[i].pattern = "Below Mean"
pointsToUse[i+1].pattern = "Below Mean"
pointsToUse[i+2].pattern = "Below Mean"
pointsToUse[i+3].pattern = "Below Mean"
pointsToUse[i+4].pattern = "Below Mean"
pointsToUse[i+5].pattern = "Below Mean"
pointsToUse[i+6].pattern = "Below Mean"
} else if point.quantity > pointsToUse[i].mean && pointsToUse[i+1].quantity > pointsToUse[i].mean && pointsToUse[i].quantity > pointsToUse[i].mean && pointsToUse[i+3].quantity > pointsToUse[i].mean && pointsToUse[i+4].quantity > pointsToUse[i].mean && pointsToUse[i+5].quantity < pointsToUse[i].mean && pointsToUse[i+6].quantity < pointsToUse[i].mean {
pointsToUse[i].pattern = "Above Mean"
pointsToUse[i+1].pattern = "Above Mean"
pointsToUse[i+2].pattern = "Above Mean"
pointsToUse[i+3].pattern = "Above Mean"
pointsToUse[i+4].pattern = "Above Mean"
pointsToUse[i+5].pattern = "Above Mean"
pointsToUse[i+6].pattern = "Above Mean"
}
}
}
fmt.Println(pointsToUse)
}
我需要显示的输出是这样的:
3.50 | Decreasing Trend
3.40 | Decreasing Trend
3.20 | Decreasing Trend
3.19 | Decreasing Trend
3.17 | Decreasing Trend
3.14 | Decreasing Trend
3.12 | Decreasing Trend
3.09 | Decreasing Trend
4.1 | Common Variation
3.16 | Below Mean
3.00 | Below Mean
3.00 | Below Mean
3.11 | Below Mean
3.12 | Below Mean
3.14 | Below Mean
3.13 | Below Mean
3.17 | Below Mean
有人可以确定我哪里出错了吗?
最佳答案
这一行:
if (point.pattern != "下降趋势"|| point.pattern != "上升趋势") && len(pointsToUse)-(i+1) >= 6 {
等于:
if true && len(pointsToUse)-(i+1) >= 6 {
此外,您需要使用 pointsToUse[i].pattern = "Decreasing Trend" 而不是 point.pattern = "Decreasing Trend" 因为 point 是您的循环值,不会在您的 slice 中分配。
为什么 for _,a := range b 引用一个副本,查看 Change values while iterating in Golang (计算器问题)
关于loops - For 循环逻辑通过查看 future 的点值来标记一系列数据点的趋势类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56426698/
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我脑子里浮现出一些关于一种新编程语言的想法,所以我想我会尝试实现它。一位friend建议我尝试使用Treetop(Rubygem)来创建一个解析器。Treetop的文档很少,我以前从未做过这种事情。我的解析器表现得好像有一个无限循环,但没有堆栈跟踪;事实证明很难追踪到。有人可以指出入门级解析/AST指南的方向吗?我真的需要一些列出规则、常见用法等的东西来使用像Treetop这样的工具。我的语法分析器在GitHub上,以防有人希望帮助我改进它。class{initialize=lambda(name){receiver.name=name}greet=lambda{IO.puts("He
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
这里是Ruby新手。完成一些练习后碰壁了。练习:计算一系列成绩的字母等级创建一个方法get_grade来接受测试分数数组。数组中的每个分数应介于0和100之间,其中100是最大分数。计算平均分并将字母等级作为字符串返回,即“A”、“B”、“C”、“D”、“E”或“F”。我一直返回错误:avg.rb:1:syntaxerror,unexpectedtLBRACK,expecting')'defget_grade([100,90,80])^avg.rb:1:syntaxerror,unexpected')',expecting$end这是我目前所拥有的。我想坚持使用下面的方法或.join,
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)
我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("
似乎无法为此找到有效的答案。我正在阅读Rails教程的第10章第10.1.2节,但似乎无法使邮件程序预览正常工作。我发现处理错误的所有答案都与教程的不同部分相关,我假设我犯的错误正盯着我的脸。我已经完成并将教程中的代码复制/粘贴到相关文件中,但到目前为止,我还看不出我输入的内容与教程中的内容有什么区别。到目前为止,建议是在函数定义中添加或删除参数user,但这并没有解决问题。触发错误的url是http://localhost:3000/rails/mailers/user_mailer/account_activation.http://localhost:3000/rails/mai
有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳