Stackexchange.Redis foreach 10000 times get return 1 or 2 times 'null' - stackexchange.redis

ICacheProvider cache = CacheFactory.CreateCacheProvider("FrameworkTest.Redis");
var k = 0;
for (int i = 0; i < 1000; i++)
{
var j = cache.Get<String>("lobi");
if (string.IsNullOrEmpty(j))
{
k++;
Console.Write("???");
}
}
Console.Write(k);
The param 'j' will be null one or two times.
Sorry,My English is bad.

Related

Which is faster, if inside a loop, or a just the loop?

I am trying to understand some concepts of performance in code.
If there is, for instance, a 2d boolean array with 5 trues inside it,
if we want to change all the elements in the array to false, which code would be faster?
number1:
for (i = 0; i < arr.length; i++) {
for (j = 0; j < arr[0].length; j++) {
arr[i][j] = false;
}
}
number2:
for (i = 0; i < arr.length; i++) {
for (j = 0; j < arr[0].length; j++) {
if (arr[i][j])
arr[i][j] = false;
}
}
You can measure it by using console.time() and console.timeEnd():
Number 1:
console.time('loop');
for (i = 0; i < arr.length; i++) {
for (j = 0; j < arr[0].length; j++) {
arr[i][j] = false;
}
}
console.timeEnd('loop');
loop: 0.046142578125ms
Number 2:
console.time('loop');
for (i = 0; i < arr.length; i++) {
for (j = 0; j < arr[0].length; j++) {
if (arr[i][j])
arr[i][j] = false;
}
}
console.timeEnd('loop');
loop: 0.031982421875ms
The array I used was arr[1][135] big and had 5 values in the second array that were true.
As you can see the loop with the if clause is faster.

why not pass all test case in problem balanced numbers in codewars?

Currently i am doing a challenge in codewars on Balanced numbers and i wrote the code in dart and it successfully completed 100 test cases but for long numbers it is not working properly...So i think that it need some conditions for this long numbers:
String balancedNum(numb) {
// your code here
var numb1 = numb.toString();
List a = numb1.split("");
var left_sum = 0;
var right_sum = 0;
for (var i = 0; i <= a.length / 2; i++) {
left_sum += int.parse(a[i]);
}
List<String> b = a.reversed.toList();
//print(b);
for (var i = 0; i < b.length / 2; i++) {
right_sum += int.parse(b[i]);
}
//print(right_sum);
if (left_sum == right_sum) {
return 'Balanced';
} else {
return 'Not Balanced';
}
}
link to the challenge:https://www.codewars.com/kata/balanced-number-special-numbers-series-number-1/train/dart
The compiler verdict for the code is wrong answer because of a simple logical error.
This is because the length of the list has not been calculated correctly
Kindly have a look at the corrected code below, which passes all the 110 test on the platform:
String balancedNum(numb) {
// your code here
var numb1 = numb.toString();
List a = numb1.split("");
var left_sum = 0;
var right_sum = 0;
double d = ((a.length-1) / 2);
int len = d.floor();
print(len);
for (var i = 0; i < len; i++) {
left_sum += int.parse(a[i]);
}
List<String> b = a.reversed.toList();
print(b);
for (var i = 0; i < len; i++) {
right_sum += int.parse(b[i]);
}
print(left_sum);
print(right_sum);
if (left_sum == right_sum) {
return 'Balanced';
} else {
return 'Not Balanced';
}
}
Your problem is not about "long numbers" since you code also fails for the test case using the number 13.
You properly did not read the following rules:
If the number has an odd number of digits then there is only one middle digit, e.g. 92645 has middle digit 6; otherwise, there are two middle digits , e.g. 1301 has middle digits 3 and 0
The middle digit(s) should not be considered when determining whether a number is balanced or not, e.g 413023 is a balanced number because the left sum and right sum are both 5.
So you need to test if the number are even or uneven since we need to use that information to know how many digits we should use in the sum on each side.
I have done that in the following implementation:
String balancedNum(int numb) {
final numb1 = numb.toString();
final a = numb1.split("");
var split = (a.length / 2).floor();
if (a.length % 2 == 0) {
split--; // the number is even
}
var left_sum = 0;
var right_sum = 0;
for (var i = 0; i < split; i++) {
print(a[i]);
left_sum += int.parse(a[i]);
}
final b = a.reversed.toList();
for (var i = 0; i < split; i++) {
right_sum += int.parse(b[i]);
}
if (left_sum == right_sum) {
return 'Balanced';
} else {
return 'Not Balanced';
}
}

The second half of my Lomuto Partition Sort isn't working

I can't seem to get my sorting algorithm to work. The partition works great, but I can't get the recursive part to work. I know the problem is my conditional value that starts the recursion isn't working correctly, but I can't figure out what I would put instead.
var partition = function(arr, i_lo, i_hi) {
var pivot = arr[i_hi];
var i = i_lo
for (var j = i_lo; j < i_hi; j++) {
if (arr[j] <= pivot) {
var swap = arr[i];
arr[i] = arr[j];
arr[j] = swap;
i++
}
}
var swap = arr[i];
arr[i] = arr[i_hi]
arr[i_hi] = swap;
return i;
}
//1 3 9 8 2 7 5
var quickSort = function(arr, i_lo, i_hi) {
console.log(arr[i_lo], arr[i_hi])
if (arr[i_lo] < arr[i_hi]) {
var p = partition(arr, i_lo, i_hi);
arr = quickSort(arr, i_lo, p-1);
arr = quickSort(arr, p + 1, i_hi);
console.log(arr);
}
return arr;
}
console.log(quickSort(arr, 0, arr.length-1))

Making your own wildcard and recognize patterns to zip your list

I have an algorithm trouble, I don't know how to do it and how to call it (Does it have any specific name?)
For example, if I have this sequence:
000 CASE_01
001 CASE_02
010 CASE_03
011 CASE_02
100 CASE_02
101 CASE_01
110 CASE_01
111 CASE_01
I want to convert it in something like this:
000 CASE_01
0-1 CASE_02
010 CASE_03
100 CASE_02
1-1 CASE_01
11- CASE_01
I have called it wildcard because I think is the most correct way...
They not necessarily have 3 bits, you must do it with n bits
If only I had the psudo-code I could write it to any language (Python in my way)
The code below generates all possible wildcard strings for the number of bits encountered in the input (e.g. --, -0, -1, 0-, 00, 01, 1-, 10 and 11 for 2 bits), and also creates patterns where numbers are inverted and wildcards become 1 (so wildcard 0-1 has pattern 110), and masks where numbers become 1 and wildcards become 0 (so wildcard 0-1 has mask 101).
The binary numbers in the input are then XOR-ed with the patterns and AND-ed with the masks to check whether they fit a certain wildcard. If a wildcard has the required number of matching numbers (2 ^ number_of_wildcards), it is added to the output and the matching numbers are removed from the input.
Run the code snippet to see the algorithm in action with your example input, to which I added a fourth case with larger binary numbers.
function wildcard(input) {
var output = [], cases = [], wilds = [], patts = [], masks = [];
var bits = groupCases(cases);
for (var i = 0; i <= bits; i++) wilds[i] = [];
wildStrings(bits);
convertStrings(wilds, patts, "-01", "110");
convertStrings(wilds, masks, "-01", "011");
for (var c = 0; c < cases.length; c++) {
for (var i = 0, j = Math.pow(2, bits); i <= bits; i++, j /= 2) {
for (var k = 0; k < patts[i].length; k++) {
var patt = patts[i][k];
var mask = masks[i][k];
var matches = [];
for (var d = 0; d < cases[c].nums.length; d++) {
var num = cases[c].nums[d];
if (((num ^ patt) & mask) == mask) matches.push(d);
}
if (matches.length == j) {
output.push(wilds[i][k] + " " + cases[c].id);
for (var l = j - 1; l >= 0; l--) cases[c].nums.splice(matches[l], 1);
}
}
}
}
return output;
function groupCases(cases) {
var max = 0;
for (var i = 0; i < input.length; i++) {
var num = parseInt(input[i], 2);
if (num > max) max = num;
var id = input[i].slice(input[i].indexOf(" ") + 1);
var pos = 0;
while (cases[pos] != undefined && cases[pos].id != id) ++pos;
if (cases[pos] == undefined) cases[pos] = {id: id, nums: []};
cases[pos].nums.push(num);
}
return Math.ceil(Math.log(max) / Math.log(2));
}
function wildStrings(len, wild, str) {
wild = wild || 0;
str = str || "";
for (var i = 0; i < 3; i++) {
var w = (i == 0) ? 1 : 0;
var s = str + ["-","0","1"][i];
if (len > 1) wildStrings(len - 1, wild + w, s)
else wilds[bits - wild - w].push(s);
}
}
function convertStrings(input, output, from, to) {
for (var i = 0; i < input.length; i++) {
output[i] = [];
for (var j = 0; j < input[i].length; j++) {
var str = input[i][j], s = "";
for (var k = 0; k < str.length; k++) {
s += to.charAt(from.indexOf(str.charAt(k)));
}
output[i].push(parseInt(s, 2));
}
}
}
}
var a = ["000 CASE_01", "001 CASE_02", "010 CASE_03", "1010 CASE_04",
"011 CASE_02", "100 CASE_02", "1110 CASE_04", "101 CASE_01",
"110 CASE_01", "1100 CASE_04", "1000 CASE_04", "111 CASE_01"];
var w = wildcard(a);
document.write(w.length + " wildcards:<BR><PRE>");
for (var i in w) document.write(w[i] + "<BR>");

How to find number of times a number repeated in pascal's triangle?

Can anyone give an algorithm to find the number of times a number repeats in pascal's triangle? For example
num - No of times
1 - infinite
2 - 1
3 - 2
4 - 2
. .
6 - 3
. .
10 - 4
. .
for image Link
Or in other way, how many nCr 's are possible for nCr = x , where x is any given integer?
Just count. You know n > 1 can only appear in the first n+1 rows of Pascal's triangle. And that each row is symmetric, and increasing (for the first half). That saves time.
See http://oeis.org/A003016 for more about the sequence
I had to write something similar for a hackathon challenge. This code will find all the numbers 1 to MAX_NUMBER_TO_SEARCH that have a count of more than MINIMUM_COUNT in the Pascal Triangle of size PASC_SIZE. You can obviously alter it to only count for a single number. Not super efficient obviously.
function pasc(n) {
var xx = [];
var d = 0;
var result = [];
result[0] = [1];
result[1] = [1, 1];
for (var row = 2; row < n; row++) {
result[row] = [1];
for (var col = 1; col <= row - 1; col++) {
result[row][col] = result[row - 1][col] + result[row - 1][col - 1];
result[row].push(1);
}
for (var ff = 0; ff < result[row].length; ff++) {
xx[d++] = (result[row][ff]);
}
}
return xx;
}
function countInArray(array, what) {
var count = 0;
for (var i = 0; i < array.length; i++) {
if (array[i] === what) {
count++;
}
}
return count;
}
var MAX_NUMBER_TO_SEARCH = 5000;
var MINIMUM_COUNT = 5;
var PASC_SIZE = 1000;
var dataset = pasc(PASC_SIZE);
for (var i = 0; i < MAX_NUMBER_TO_SEARCH; i++) {
if (countInArray(dataset, i) >= MINIMUM_COUNT) {
console.log(i + " Count:" + countInArray(dataset, i) + "\n");
}
}

Resources