I'm trying to find out why I do get these strange results from this sort query:
redis> sort set:package:1:all_games by hash:game:*->rating DESC LIMIT 0 10 GET hash:game:*->rating
1. "10"
2. "10"
3. "10"
4. "9,1"
5. "9"
6. "9,2"
7. "9"
8. "9,1"
9. "9"
10. "9,4"
redis>
I know the data uses , in stead of . and it will be fixed. But why is it sorted so inconsistently? I'd at least expect it to give consistent results (The 9,1's in sequence).
Can anyone explain what's going on here?
As mentioned by Ofer, by default, the sort is numeric and the elements are compared as double precision floating point numbers.
Redis sort function works by populating a C array from the original container. The initial order of the items in this array is the order of the items in the initial container (which is undefined for set for instance).
Each item is then tagged with a score value. Redis uses the standard strtod function to convert the string value to a double. This function works with a best effort approach: it tries to convert as many characters as possible. So the "9", "9,1" "9,2" and "9,4" strings will all be converted to 9.0
Finally the array is sorted using either the standard qsort algorithm, either the BENTLEY/McILROY algorithm (depending if limit parameters are set or not). AFAIK, none of these sort algorithms are stable. It means the order of the items with the same score will be random in the result.
This is exactly what you get on the example: you have the "10" items first, and then the "9" items. Order of the "9" items is random.
Add ALPHA at the end:
sort set:package:1:all_games by hash:game:*->rating DESC LIMIT 0 10 GET hash:game:*->rating ALPHA
By default, sorting is numeric and elements are compared by their value interpreted as double precision floating point number.
When a list (or set) contains string values and you want to sort them lexicographically, use the ALPHA modifier.
See: http://redis.io/commands/sort
Related
I want to calculate the irrational number, expressed by the following formula in gap:
3^(1/7). I've read through the related description here, but still can't figure out the trick. Will numbers like this appear in the computation of the character table and corresponding (unitary) representations?
P.S. Basically, I want to figure out the following question: For the computation of the character table and corresponding (unitary) representations, what is the minimum complete set of atomic irrational numbers used to express the results?
Regards,
HZ
You can't do that with GAP's standard cyclotomic numbers, as seventh roots of 3 are not cyclotomic. Indeed, suppose $r$ is such a root, i.e. a rot of the polynomial $f = x^7-3 \in \mathbb{Q}[x]$. Then $r$ is cyclotomic if and only if the field extension \mathbb{Q}[x] is a subfield of a cyclotomic field. By Kronecker-Weber this is equivalent to that field being an abelian extension, i.e., the Galois group is abelian. One can check that this is not the case here (the Galois group is a semidirect product of C_7 with C_6).
So, $r$ is not cyclotomic.
I am using below code to sort. Everything works fine till E999. E1000 goes into hiding somewhere and is not part of sorting.
_order= 'employee_code desc'
Records are sorted based on string comparison. It compares first characters and if these are equal, second character is compared and so on.
In your case comparison works only to E999, because the next code has one character more than all previous codes.
When comparing E1000 with E999, the first code is smaller, because the second character of the first code is smaller.
The order of codes will be like this:
E098, E099, E100, E1000, E101, E102
In smaller scale you can see the same situation here:
x = ['E01', 'E02', 'E22', 'E99', 'E100', 'E10', 'E11']
x.sort()
print(x)
Output:
['E01', 'E02', 'E10', 'E100', 'E11', 'E22', 'E99']
So, to solve this issue you just need to increase the length of code (by adding 0 before the number if it is to short), so it always has the same length for all codes.
I have string output:
1 4 2 1 4
I want to get each character in string to compare.
I did it to want to know whether the list is sorted yet.
It's not exactly clear to me what you are trying to achieve. Going by "to know whether the list is sorted", and assuming a list of integers, you can use tcl::mathop::< or tcl::mathop::<=, depending on whether you want to allow duplicate values:
if {[tcl::mathop::<= {*}$list]} {
puts "List is sorted"
} else {
puts "List is mixed up"
}
This will also work for ASCII comparison of strings. For more complex comparisons, like using dictionary rules or case insensitive, it's probably easiest to combine that with lsort along with the -indices option:
tcl::mathop::< {*}[lsort -indices -dictionary $list]
The -indices option returns the original index of each list element in sorted order. By checking if those indices are in incremental order, you know if the original list was already sorted.
Of course, if the point of the exercise was to avoid unnecessary sorting, then this is no use. But then again, bubble sort of an already sorted list is very fast and will basically do exactly the comparisons you described. So just sorting will probably be faster than first checking for a sorted list via a scripted loop.
To get each character in the string, do split $the_string "" (yes, on the empty string). That gives you a list of all the characters in the string; you can use foreach to iterate over them. Remember, you can iterate over two (or more) lists at once:
foreach c1 [split $the_string ""] c2 $target_comparison_list {
if {$c1 ne $c2} {
puts "The first not equal character is “$c1” when “$c2” was expected"
break
}
}
Note that it's rarely useful to continue comparison after a difference is found as the most common differences are (relative to the target string) insertions and deletions; almost everything after either of those will differ.
Consider:
>> print max 5 6 7 8
6
== 8
The documentation states that max only takes two arguments, so I understand the first line. But from the second line it looks like the interpreter is still able to find the max of an arbitrary number of args.
What's going on here? What is the difference between the two results returned? Is there a way to capture the second one?
I don't really know Rebol but what I do notice is that you're using print inside of th REPL. The first output is from print, which is outputting the result of max 5 6. The second output is from the REPL, which is outputting the value of your whole expression — which is maybe just the last item in the list? If you changed the order of your inputs, I bet you would see a different result.
max is an abbreviation for maximum. As #hobbs correctly guessed, it takes two arguments, and what you're seeing is just the evaluator's logic of turning the crank...and becoming equal to the last value in the expression. In this case you're not using that result, so the interpreter shows it to you with "==". But you could have assigned that whole expression to a variable (for instance).
What you were intending is something that gets the maximum value out of a series. In the DO dialect all functions have fixed arity, and the right way to design such a beast would be to make it take one argument...the series.
Such a thing does exist, though there isn't an abbreviation...
>> print maximum-of [5 6 7 8]
8
For example:
code = '7777-5';
input = code.substring(0, 4); // Returns '7777'
checkdigit = f(input); // f() produces a checkdigit
assert.areEqual(code, input + "-" + checkdigit)
Is there a technical term for input used above?
Specifically I'm calculating checkdigits for ISBNs, but that shouldn't effect the answer.
Is "original number excluding the check digit" technical enough? :)
Actually, it's often the case, as in the link you posted, that the check digit or checksum ensures a property about the full input:
...[the check digit] must be such that the sum of all the ten digits, each multiplied by the integer weight, descending from 10 to 1, is a multiple of the number 11.
Thus, you'd check the full number and see if it meets this property.
It's "backwards" when you're initially generating the check digit. In that case, the function would be named generate_check_digit or similar, and I'd just name its parameter as "input".
Although I am not sure if there is a well-known specific technical term for the input, what LukeH suggested (message/data) seems common enough.
Wiki for checksum:
With this checksum, any transmission error that flips a single bit of the message, or an odd number of bits, will be detected as an incorrect checksum
Wiki for check digit:
A check digit is a form of redundancy check used for error detection, the decimal equivalent of a binary checksum. It consists of a single digit computed from the other digits in the message.