comparison

String comparison fails

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 19:56:28
问题 output = subprocess.check_output("./mount.sh", shell=True) print output if output == "expected_String": print "Hurray!" (The print command is just to check if the output is what I expected).The comparison fails everytime and I don't get why. I tried it with this instead of check_output (stdout, stderr) = Popen(["./mount.sh"], stdout=PIPE).communicate() mountout = stdout but I don't think that's the problem here because print output gives me what I expect but if I try to compare it to my

What are the general rules for comparing different data types in C?

主宰稳场 提交于 2019-12-17 19:40:25
问题 Lets say I have the following scenarios: int i = 10; short s = 5; if (s == i){ do stuff... } else if (s < i) { do stuff... } When C does the comparison does it convert the smaller data type, in this case short to int or does it convert the data type on the right to the data type on the left? In this case int to short? 回答1: This is governed by the usual arithmetic conversions . For simple cases, the general rule of thumb is that the type with "less" precision is converted to match the type

Why put the constant before the variable in a comparison?

笑着哭i 提交于 2019-12-17 19:33:41
问题 I noticed for a while now the following syntax in some of our code: if( NULL == var){ //... } or if( 0 == var){ //... } and similar things. Can someone please explain why did the person who wrote this choose this notation instead of the common var == 0 way)? Is it a matter of style, or does it somehow affect performance? 回答1: It's a mechanism to avoid mistakes like this: if ( var = NULL ) { // ... } If you write it with the variable name on the right hand side the compiler will be able catch

Why String.Equals is returning false?

故事扮演 提交于 2019-12-17 19:27:12
问题 I have the following C# code (from a library I'm using) that tries to find a certificate comparing the thumbprint. Notice that in the following code both mycert.Thumbprint and certificateThumbprint are strings. var certificateThumbprint = AppSettings.CertificateThumbprint; var cert = myStore.Certificates.OfType<X509Certificate2>().FirstOrDefault( mycert => mycert.Thumbprint != null && mycert.Thumbprint.Equals(certificateThumbprint) ); This fails to find the certificate with the thumbprint

Pointer equality in Haskell?

我是研究僧i 提交于 2019-12-17 19:21:18
问题 Is there any notion of pointer quality in Haskell? == requires things to be deriving Eq, and I have something which contains a (Value -> IO Value), and neither -> nor IO derive Eq. EDIT: I'm creating an interpreter for another language which does have pointer equality, so I'm trying to model this behavior while still being able to use Haskell functions to model closures. EDIT: Example: I want a function special that would do this: > let x a = a * 2 > let y = x > special x y True > let z a = a

Javascript Arrays - Checking two arrays of objects for same contents, ignoring order

為{幸葍}努か 提交于 2019-12-17 19:19:50
问题 I have two JavaScript arrays ( A and B ) that contain objects that I created. I want to check that all the objects in array A are contained in array B , but not necessarily in the same order. What is the best way to do this? Edit: They are all actual objects, not primitives, so I will need to compare their contents and structure as well (maybe using something like JSON.stringify ). I want to do this because I'm learning Test-Driven Development, and I want to test functions that return lists

comparing arrays in php, without caring for the order

偶尔善良 提交于 2019-12-17 19:19:49
问题 I have two arrays, $a and $b here, and need to check if they contain exactly the same elements (independently of the order). I am thinking of using if (sizeof($a)==sizeof($b) AND array_diff($a,$b)==array()) { } But I am new to PHP, so I wonder: Is there a better way? Since I need to use them as sets, maybe I should not use arrays at all but something else. 回答1: The accepted answer is was wrong. It will would fail on: https://3v4l.org/U8U5p $a = ['x' => 1, 'y' => 2]; $b = ['x' => 1, 'y' => 1];

Comparing two hashmaps for equal values and same key sets?

会有一股神秘感。 提交于 2019-12-17 18:25:48
问题 How can I best compare two HashMap s, if I want to find out if none of them contains different keys than the other, and if the values of that keys match each other. Map<objA, objB> mapA = new HashMap<objA, objB>(); mapA.put("A", "1"); mapA.put("B", "2"); Map<objA, objB> mapB = new HashMap<objA, objB>(); mapB.put("D", "4"); mapB.put("A", "1"); When comparing A with B, it should fail due to different keys B and D. How could I best compare non-sorted hashmaps? 回答1: Make an equals check on the

LINQ Distinct operator, ignore case?

孤街醉人 提交于 2019-12-17 17:54:34
问题 Given the following simple example: List<string> list = new List<string>() { "One", "Two", "Three", "three", "Four", "Five" }; CaseInsensitiveComparer ignoreCaseComparer = new CaseInsensitiveComparer(); var distinctList = list.Distinct(ignoreCaseComparer as IEqualityComparer<string>).ToList(); It appears the CaseInsensitiveComparer is not actually being used to do a case-insensitive comparison. In other words distinctList contains the same number of items as list . Instead I would expect, for

Why does >= return false when == returns true for null values?

折月煮酒 提交于 2019-12-17 17:33:57
问题 I have two variables of type int? (or Nullable<int> if you will). I wanted to do a greater-than-or-equal (>=) comparison on the two variables but as it turns out, this returns false if both variables are null, while obviously the == operator returns true. Can someone explain to me why that is logical because the semantical definition of the >= operator contains the word "or"? 回答1: There was a huge debate about this oddity when the feature was originally designed back in C# 2.0. The problem is