I'm trying to iterate through a Redis bitmap and get the offset of all set bits.
I tried several methods but all of them have failed, here is a snipped I got from Redis Essentials book by Maxwell. I went through the code several times, still unable to figure out whats going wrong.
var bitmap = 'test';
redisClient.get(bitmap, function(err, bitmapValue){
var userIds = [];
var data = bitmapValue.toJSON().data;
data.forEach(function(byte, byteIndex){
for(var bitIndex = 7; bitIndex >= 0; bitIndex--) {
var visited = byte >> bitIndex & 1;
}
if(visited === 1) {
var userId = byteIndex * 8 + (7 - bitIndex);
userIds.push(userId);
}
})
console.log(userIds);
});
Most of the time I get empty result, usually when the bitmap is small. When I try large bitmaps I get array full of multiples of 8. A sample result I got is shown below, which is incorrect, the bitcount of that bitmap is 690, while I got only 91.
[8,
16,
32,
144,
176,
256,
320,
440,
456,
520,
584,
592,
624,
640,
648,
680,
696,
704,
712,
720,
752,
760,
848,
1056,
1088,
1104,
1112,
1120,
1136,
1144,
1160,
1168,
1224,
1248,
1264,
1280,
1312,
1328,
1424,
1480,
1496,
1544,
1600,
1680,
1792,
1824,
1840,
1904,
1976,
2080,
2112,
2144,
2224,
2240,
2296,
2392,
2400,
2504,
2512,
2584,
2616,
2712,
2856,
2880,
2912,
2976,
3048,
3288,
3328,
3352,
3360,
3368,
3376,
3432,
3440,
3496,
3600,
3616,
3624,
3632,
3704,
3752,
3776,
3832,
3880,
3936,
4056,
4264,
4296,
4352,
4376]
I don't have the bitmap you've stored under the key test, so testing is difficult. However, reading the code it looks like the if statement is placed wrongly - it should be:
var bitmap = 'test';
redisClient.get(bitmap, function(err, bitmapValue){
var userIds = [];
var data = bitmapValue.toJSON().data;
data.forEach(function(byte, byteIndex){
for(var bitIndex = 7; bitIndex >= 0; bitIndex--) {
var visited = byte >> bitIndex & 1;
if(visited === 1) {
var userId = byteIndex * 8 + (7 - bitIndex);
userIds.push(userId);
}
}
})
console.log(userIds);
});
Related
why it is showing me 36 even though the minimum number is 27
var combination = [27, 36]
for (let x in combination) {
if (combination[x] < 50) {
var min = Math.min(combination[x])
}
}
console.log(min)
i tried this multiple ways like
var combination = [27, 30, 40, 44, 3, 239, 329, 2, 5, 20923, 96]
for (let x in combination) {
if (combination[x] < 50) {
var min = Math.min(combination[x])
}
}
console.log(min) //output-- 5 //it should be 2
in this third example i add (-) to 2
var combination = [27, 30, 40, 44, 3, 239, 329, -2, 5, 20923, 96]
for (let x in combination) {
if (combination[x] < 50) {
var min = Math.min(combination[x])
}
}
console.log(min) // output-- still 5 // it should be -2
again when am adding (-) to other numbers like in -96 or -5 the output was okay (-96) but when im adding (-) to 2 it is not showing me -2 in the output instead it showing me 5
not only in javascript i tried this with lua, php but output was same as js
can anyone explain me why this happen and how solve this
You're not comparing values to determine the minimum, but instead just replacing the min variable with the last number in the array that is smaller than 50. This can be fixed as follows:
let min = undefined;
for (let x in combination) {
if (combination[x] < 50) {
min = min == undefined ? combination[x] : Math.min(min, combination[x])
}
}
Using filter and reduce, this can be made a lot shorter:
combination.filter(x => x < 50).reduce((x, y) => Math.min(x, y))
i´m new to JavaScript and learning with the Help of the Website https://www.jshero.net/koans/roman1.html.
The exercise is to code a converter, that converts roman numbers from a string 'CDLXXXIII' to the arabic number.
I made a code with a "while loop" that works, but the website wants me to do it with a recursive function.
Heres my code:
function roman(roemische){
let romBuchstaben = ['I','IV','V','IX','X','XL','L','XC','C','CD','D','CM', 'M'];
let romZahlen = [1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000];
let summe = 0;
while (roemische.length > 0){
let suchzeichen = roemische[0] + roemische[1];
if (romBuchstaben.indexOf(suchzeichen) !== -1){
summe += romZahlen[romBuchstaben.indexOf(suchzeichen)];
roemische = roemische.substr(2,roemische.length-2);
} else {
summe += romZahlen[romBuchstaben.indexOf(roemische[0])];
roemische = roemische.substr(1, roemische.length-1);
}
}
return summe;
}
(I´m sorry that the var's are in german).
I´m not that familiar with recursion, could some1 give me an example, how to do it with one?
Greetings Marcel
You could change the storage of the values a bit by taking an object with roman signs as keys and decimal values.
For crating a recursive function, you could add an exit condition which is here just a check for an empty string and return zero in this case.
Then check if two character are in the object and if so take the value and add the result of calling the function again with the rest of the string.
If not take only the first character and the value and call the function again for getting the rest of the string.
function roman(number) {
const
values = { I: 1, IV: 4, V: 5, IX: 9, X: 10, XL: 40, L: 50, XC: 90, C: 100, CD: 400, D: 500, CM: 900, M: 1000 },
two = number.slice(0, 2);
if (!number) return 0;
return two in values
? values[two] + roman(number.slice(2))
: values[number[0]] + roman(number.slice(1));
}
console.log(roman('CDLXXXIII')); // 483
I have the following result from an API
data: {
"total_slots": int,
"occupied_count": int,
"occupied_slots" : [int] e.g.[452,453,459] (here each int in the array signfies an occupied slot),
"slots_down" : [int] e.g.[460,462] (here each int in the array signfies a down slot)
}
I want the following conditions
VAR OCCUPIED, length of the list which are occupied minus length of common slots in occupied and slots_down
VAR TOTAL_SLOTS = total slots (which are 31 i think) - slots which are down
VAR AVAILABLE = (31 - length(slots_down)) - length( slots occupied AND not down)
The slots are 31 that are fixed.
var ALL_SLOTS = [452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 508, 509, 510, 511, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523];
How can I meet the OR/AND conditions like VAR AVAILABLE = (31 - length(slots_down)) - length( slots occupied AND not down)
This is my CURRENT CODE
s = JSON.parse(s);
console.log(s);
SLOTS_DOWN = s.slots_down;
var total_slots = s.total_slots;
SLOTS_DOWN = s.slots_down.length;
console.log('down slots are ' + SLOTS_DOWN);
total_slots = parseInt(total_slots) - parseInt(SLOTS_DOWN);
var occupied = s.occupied_count;
var available = parseInt(total_slots) - parseInt(occupied) - parseInt(SLOTS_DOWN);
According to these:
VAR OCCUPIED, length of the list which are occupied minus length of common
slots in occupied and slots_down
VAR TOTAL_SLOTS = total slots (which are 31 i think) - slots which are down
VAR AVAILABLE = (31 - length(slots_down)) - length( slots occupied AND not down)
you could try this:
var OCCUPIED = data.occupied_count -
data.occupied_slots.filter(function(elem){
return data.slots_down.indexOf(elem)>-1;
}).length;
var TOTAL_SLOTS = data.total_slots.length - data.slots_down.length;
var AVAILABLE = ((data.total_slots.length - data.slots_down.length)) -
data.occupied_slots.filter(function(elem){
return data.slots_down.indexOf(elem)==-1;
}).length
This should work:
data = {
"total_slots": 124,
"occupied_count": 3,
"occupied_slots" : [245, 326, 256],
"slots_down" : [245, 136]
}
var down_but_not_occ = 0;
data.slots_down.map(function(v, i){
if(data.occupied_slots.indexOf(v) === -1){
down_but_not_occ ++;
};
})
var available_slots = parseInt(data.total_slots) - data.occupied_slots.length - down_but_not_occ;
console.log(available_slots);
Where you were wrong,
total_slots = parseInt(total_slots) - parseInt(SLOTS_DOWN); // slots down subtracted
var occupied = s.occupied_count;
var available = parseInt(total_slots) - parseInt(occupied) - parseInt(SLOTS_DOWN); // slots down AGAIN subtracted
I have this path:
<path id="secnb1l1" class="lungon"
fill="none" stroke="black" stroke-width="1"
d="M 93.00,444.00
C 93.00,444.00 114.00,506.00 114.00,506.00
102.30,512.28 100.00,518.71 100.00,531.00
100.00,531.00 86.00,534.00 86.00,534.00
86.00,534.00 68.95,485.00 68.95,485.00
68.95,485.00 58.00,452.00 58.00,452.00
58.00,452.00 93.00,444.00 93.00,444.00 Z
M 75.00,458.00
C 75.00,458.00 79.00,458.00 79.00,458.00
78.99,466.29 79.26,463.93 76.00,471.00
76.00,471.00 86.00,471.00 86.00,471.00
82.12,462.60 83.00,464.37 83.00,455.00
83.00,455.00 75.00,458.00 75.00,458.00 Z" />
And I want to convert it into JSON. Just like on this LINK
quick example of SVG converted into JSON:
(SVG):
<path d=" M 10 25
L 10 75
L 60 75
L 10 25"
stroke="red" stroke-width="2" fill="none" />
(JSON):
var lineData = [ { "x": 1, "y": 5}, { "x": 20, "y": 20},
{ "x": 40, "y": 10}, { "x": 60, "y": 40},
{ "x": 80, "y": 5}, { "x": 100, "y": 60}];
I'm afraid that there's no generator to make my works easier. It's a pain to convert it manually.
Thanks in advance for your help :)
EDIT
I'm using d3.js for zoom and pan.
If you do not want to fiddle around with string operations, you can also use the DOM Interface.
Interface
PathSegList
From there you can iterate over the path elements path segments list and create a json string from that.
var path = document.getElementById(<pathid>),
seglist = path.pathSegList,
length = seglist.numberOfItems, i = 0, seg,
out = [], data, type;
for (; i < length; i++) {
seg = seglist.getItem(i);
type = seg.pathSegTypeAsLetter;
data = { type: seg.pathSegTypeAsLetter };
switch (type) {
case 'M':
case 'm':
case 'l' :
case 'L' :
data.x = seg.x;
data.y = seg.y;
break;
case 'C':
case 'c':
data.x = seg.x;
data.y = seg.x;
data.x1 = seg.x1;
data.y1 = seg.y1;
data.x2 = seg.x2;
data.y2 = seg.y2;
break;
/*
* to
* be
* continued
*/
}
out.push(data);
}
return JSON.stringify(out);
I have not test the code above, it should outline the basic process of iterating the Paths' segments. Since there are quite a couple of different types of segments, you can, as outlines above, »switch« over the type and create suitable data from there. Or, instead of path.pathSegList you could use path.normalizedPathSegList what return a list of segments, where each segment is converted to the type of cubic Bezier, but when tried it the last time, I got an »Not yet implemented Error«.
I have created a fiddle showing that the code works!
Given that you haven't specified things such as resolution etc. for your path around curves, I've just created a JSON with an extra property for the path type from that co-ordinate to the next one.
var d = document.getElementById('secnb1l1').getAttribute('d');
d = d.replace(/\s{2,}/g, ' '); // Remove multiple spaces
d = d.replace(/([a-zA-Z])\s[0-9]/g, '$1,'); // Add letters to coords group
d = d.split(" "); // Split on space
var coords = [];
for (var i = 0; i < d.length; i++) {
var coordString = d[i];
var coordArray = coordString.split(",");
var coord = {
x: coordArray[coordArray.length - 2],
y: coordArray[coordArray.length - 1]
};
if (coordArray.length > 2) {
coord.path = coordArray[0];
}
coords.push(coord);
}
You can see it working in this fiddle.
You can head over here, we have both node packages and API, as well as a simple web interface.
Github: https://github.com/Rexfont/svgJson
The API instruction is given in there as well
example:
svgjson({data: 'filepath/string', web: false, file: true, remove: true, output: true, filename: 'example.txt'})
You can use the API if you wish to use it multiple times on web or use the npm for node
I got this a function eval in javascript - http://pastebin.com/E1PXQeKj
but i don't know how read it? how does this generate or decode the string?
or simply how is this code?
thanks for help!
Paste it in http://jsbeautifier.org/ and it reveals:
var secret = 'dDGSUW1QU01JaVZNTWFFN0pWcm2RZkE6MD';
To do it manually, find the end of the eval function and replace eval by alert or whatever debugging function you use. Line 2 can be handled in that way.
Line 3 formatted looks like:
var _9581
;
var _8438 = '196E84D180D984E928C920F980E928D988C652F768E652F680B924F808E924A724E916A872C1000A720A848E872F868D980E864C796E940A724B912F724B732B724B924C876C956D836C912C864A804E1008E840C868A800A740A832E848B680D760F';
var _5668 = /[\x41\x42\x43\x44\x45\x46]/;
var _9413 = 2;
var _9565 = _8438.charAt(_8438.length - 1);
var _5032;
var _9978 = _8438.split(_5668);
var _4678 = [String.fromCharCode, isNaN, parseInt, String];
_9978[1] = _4678[_9413 + 1](_4678[_9413](_9978[1]) / 21);
var _6432 = (_9413 == 7) ? String : eval;
_5032 = '';
_11 = _4678[_9413](_9978[0]) / _4678[_9413](_9978[1]);
for (_9581 = 3; _9581 < _11; _9581++) _5032 += (_4678[_9413 - 2]((_4678[_9413](_9978[_9581]) + _4678[_9413](_9978[2]) + _4678[_9413](_9978[1])) / _4678[_9413](_9978[1]) - _4678[_9413](_9978[2]) + _4678[_9413](_9978[1]) - 1));
_6432(_5032);
Now analyse it yourself. Care should be taken when something is executeable. Constructs like foo(bar).
Decoding in progress:
var _9581;
var _8438 = '196E84D180D984E928C920F980E928D988C652F768E652F680B924F808E924A724E916A872C1000A720A848E872F868D980E864C796E940A724B912F724B732B724B924C876C956D836C912C864A804E1008E840C868A800A740A832E848B680D760F';
var _5668 = /[ABCDEF]/;
var _9413 = 2;
var _9565 = "F";
var _5032;
var _9978 = [196, 84, 180, 984, 928, 920, 980, 928, 988, 652, 768, 652, 680, 924, 808, 924, 724, 916, 872, 1000, 720, 848, 872, 868, 980, 864, 796, 940, 724, 912, 724, 732, 724, 924, 876, 956, 836, 912, 864, 804, 1008, 840, 868, 800, 740, 832, 848, 680, 760, ];
var _4678 = [String.fromCharCode, isNaN, parseInt, String];
_9978[1] = "4"; //String(parseInt(84) / 21)
var _6432 = eval;
_5032 = '';
_11 = 49; //parseInt(196) / parseInt(4);
for (_9581 = 3; _9581 < _11; _9581++) {
//_5032 += (String.fromCharCode((parseInt(_9978[_9581]) + parseInt(180) + parseInt(4)) / parseInt(4) - parseInt(180) + parseInt(4) - 1));
_5032 += String.fromCharCode((parseInt(_9978[_9581]) + 184) / 4 - 177);
}
// so from here one, we can safely assume that the code is NOT executable
//_6432(_5032);
console.log(_5032);
Yields:
secret = 'dGd3bWw1QWVrUDh2a412dXl2aUFyOVE6MQ';
Conclusion:
The first packer is to confuse people who manage to decode it.
The second code actually changes the secret variable
Here is unpacker for code encoded in such a way http://www.strictly-software.com/unpacker. It seems the only thing this code do is:
ver secret = 'dGd3bWw1QWVrUDh1a242dXlNaUFyOVE6MQ';