我试图计算两个像myArray{a,b,c}和的数组urArray{a,b,c,c}
我想检查两个元素是否都有相同的元素,例如在上面的条件中,第二个数组urArray有一个额外的'c'.
如果代码具有相同的元素,则代码应该能够等同两组数组,并且元素的顺序无关紧要.只是两个数组都应该有相同的元素,即如果一个有两个'c',另一个也应该有两个'c',否则条件是假的.
所以我做的是:
char[] myArray = new char[] {'a','b','c','c'};
char[] urArray = new char[] { 'a', 'b', 'c' ,'a'};
List<char> tmp2 = new List<char>(urArray);
for (int i = 0; i < myArray.Length; ++i)
{
for (int j = 0; j < urArray.Length; ++j)
{
if (myArray[i] == urArray[j])
{
Console.WriteLine(urArray[j] + "--> " + "urArray"+" myArray"+"--> "+myArray[i]);
tmp2.Remove(urArray[j]);
urArray = tmp2.ToArray();
}
else if (myArray[i] != urArray[j])
{
Console.WriteLine(myArray[i] + …Run Code Online (Sandbox Code Playgroud) 我只是挖掘了一下Haskell,我开始尝试计算文本中两个单词的Phi-Coefficient.但是,我遇到了一些我无法解释的非常奇怪的行为.
在删除所有内容后,我最终使用此代码重现问题:
let sumTup = (sumTuples?concat) frequencyLists
let sumFixTup = (138, 136, 17, 204)
putStrLn (show ((138, 136, 17, 204) == sumTup))
putStrLn (show (phi sumTup))
putStrLn (show (phi sumFixTup))
Run Code Online (Sandbox Code Playgroud)
这输出:
True
NaN
0.4574206676616167
Run Code Online (Sandbox Code Playgroud)
因此虽然sumTup和sumFixTup显示相同,但它们在传递时表现不同phi.
定义phi是:
phi (a, b, c, d) =
let dividend = fromIntegral(a * d - b * c)
divisor = sqrt(fromIntegral((a + b) * (c + d) * (a + c) * (b + d)))
in dividend / …Run Code Online (Sandbox Code Playgroud) Why this condition is never true ? Both parts of the equation are integers, so there must be equality for index = 0, 10, 20, 30, 40. I am compiling this code using g++.
for(int index = 0; index < 50; index++){
if ( (int) (10 * ( 0.1 * index) == (int)(10 * ( int ) ( 0.1 * index ) ) ) )
{
std::cout << "equal";
}
}
Run Code Online (Sandbox Code Playgroud)
使用MSVS 2010编译器,这些问题不会发生......
0 0
1 0
2 0
3 …Run Code Online (Sandbox Code Playgroud) 当我认为它应该返回true时,以下脚本返回false.知道这里发生了什么吗?非常感谢,伙计们!
test=['Pop']
test1='Pop'
if (test==('POP' or 'Pop' or 'pop' or ['POP'] or ['Pop'] or ['pop'])):
print "yes"
else:
print "no"
Run Code Online (Sandbox Code Playgroud)
目前,输出是'不'.
我在字符串数组"balance array"中输入"4 + 4-(4 + 4 +(4 + 4)))"
我正在尝试执行此代码:
String expression = "";
for(int j=2 ; j<balance.length-1 ; j++)
{
if(!(balance[j].equals("+")) || !(balance[j].equals("-")) || !(balance[j].equals("(")) || !(balance[j].equals(")")))
expression = expression + balance[j];
}
Run Code Online (Sandbox Code Playgroud)
在代码的末尾,表达式应该包含"444444",但它不起作用.
我使用!.equals的东西和|| 事情是错误的吗?
我想用||将这4个语句组合在一起 它们之间.
根据我的理解,如果我有两个long或者int,==由于自动装箱,测试值相等的运算符有时不起作用.
我需要做些什么来确保==在处理原语时能够在每种可能的情况下工作?
我正在编写一个模板引擎,我正在寻找一种检测模板是否已更改的好方法.
为此,我有以下要求(按重要性顺序):
这不是一个大问题,如果有时候没有检测到相等的字符串是相同的,因为这只会触发一个不需要的"重新渲染",但是由于这个"繁重的工作",这应该发生的次数少于可能.
我首先考虑使用String.GetHashCode(),但是为两个不相等的字符串获取相同哈希码的顺从性非常高.
是否有任何良好的组合,如检查哈希码,并将Length错误检测到的不相等字符串的概率等于不切实际发生的低数字?
或者使用一些哈希算法,如MD5或SHA,一个很好的选择(在哈希码相等之后)?
我的渲染看起来如下所示:
public string RenderTemplate(string name, string template)
{
var cachedTemplate = Cache.Get(name);
if(cachedTemplate == null || !cachedTemplate.Equals(template)) // <= Equals
{
cachedTemplate = new Template(name, template);
cachedTemplate.Render();
Cache.Set(name, cachedTemplate);
}
return cachedTemplate.Result;
}
Run Code Online (Sandbox Code Playgroud)
这Equals是我要问的问题.
我也愿意接受其他建议如何解决这个问题.
更新:
添加一些数字以获得更多上下文:
我希望有> 1000个单独的模板,每个模板最多至少有几千个字符.
这就是为什么我想避免将整个模板字符串"存储在内存中"仅用于比较.
大多数模板都存储在DB中.
更新2:
您如何考虑RenderTemplate使用timestampNikola建议的扩展方法:
public string RenderTemplate(string name, string template, DateTime timestamp)
Run Code Online (Sandbox Code Playgroud)
然后我可以比较name,GetHashCode并且timestamp不需要太多内存,应该非常快,并且"错误检测到的相等"的概率实际为0. timestamp我可以从DB(已经存在)或"最后更改日期"读取"从文件系统中获取基于文件的模板.
请考虑以下代码
public class Rectangle : IEquatable<Rectangle>
{
public int Width { get; set; }
public int Height { get; set; }
public bool Equals(Rectangle other)
{
return Width == other.Width
&& Height == other.Height;
}
}
IEquatable<Rectangle> a = new Rectangle() { Width = 10, Height = 20 };
IEquatable<Rectangle> b = new Rectangle() { Width = 10, Height = 20 };
a.Equals(b); // returns false;
Run Code Online (Sandbox Code Playgroud)
我知道这个类的语义很糟糕(对不起).我通常遵循C#IEquatable<T>实施指南,其中指出object.Equals(object obj)还需要覆盖. - 这只是猜测.
因为a和b被声明为IEquatable<Rectangle> …
我正在阅读Java中的equals方法,我听到人们说==测试引用相等(它们是否是同一个对象)..equals()价值平等的测试(它们在逻辑上是否"相等").
我相信这是真的,但是,如果你看一下源代码.equals(),它只是顺从了==
从Object类:
public boolean equals(Object obj) {
return (this == obj);
}
Run Code Online (Sandbox Code Playgroud)
现在我很困惑.我看到的是我们正在测试当前对象是否具有与显式参数相同的引用.它是测试引用相等还是值相等?
假设我有一个函数:
fun equality() {
var a = "kotlin"
var b = "kotlin"
var c = a
println(a==b) //true
println(a===b) //false
println(a==c) //true
println(a===c) //true
}
Run Code Online (Sandbox Code Playgroud)
根据kotlin === a和b是不同的实例,所以我的预期输出是:
true
false
true
true
Run Code Online (Sandbox Code Playgroud)
但实际上显示:
true
true
true
true
Run Code Online (Sandbox Code Playgroud)
我不明白a === b是怎么回事。
equality ×10
c# ×3
equals ×3
java ×3
arrays ×2
autoboxing ×1
c++ ×1
comparison ×1
g++ ×1
haskell ×1
iequatable ×1
if-statement ×1
kotlin ×1
list ×1
performance ×1
primitive ×1
python ×1
string ×1
tuples ×1