*I mean that as we know an int is stored as 4 octets, that means if we want to convert for example 18 to binary, it must contain 32 bits not just 5 bits (18 = 10010 bits)
This works to with any other type, string, float, double etc.... we don't have the number of bits that normally we must have, the majority of type is less. So please can you explain to me, I'm sure I miss something.
*
Imagine you have a device with a long set of wheels, each showing a digit from 0 to 9. You can set each digit to one of those 10 digits, and nothing else. Your task is to agree with your friend a way to pass across a list of numbers.
Your first idea is to simply set the numbers directly, and leave any spare wheels at 0. I give you the numbers 5, 7, and 3, so you set your wheels like this:
5 7 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
You pass it to your friend, and they read off "5", "7", "3".
But now I give you the numbers 18, 2006, and 0. So you set the wheels like this:
1 8 2 0 0 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0
You pass it to your friend, and they're confused. Is that "1", "8", "2006"? Or perhaps it's "18", "200", "6"? They don't know where one number ends and the next begins.
So, you have an idea: if all numbers were the same length, you'd know where they began and ended. Fortunately, in our number system, "1", "01", "001", and "0001" all mean the same thing, so you can agree that all numbers will be 4 digits long. 18, 2006, 0 now looks like this:
0 0 1 8 2 0 0 6 0 0 0 0 0 0 0 0 0 0 0 0
You pass it over, and your friend reads off "0018, that's eighteen; 2006; 0000, that's zero". Success!
Our only problem is that we can't represent a number higher than 9999; so when we choose our fixed width, we need to decide on the highest number we want to be able to represent. More width, higher numbers; but more wasted space when we're working with smaller numbers.
When we talk about "32-bit integers" or "64-bit integers", we're talking about essentially the same idea, but instead of wheels that can be 0 to 9, we have some representation of bits that can be either 0 or 1.
So, to store the number 18, we work out the binary, 10010, and then somewhere in memory we write down a sequence of 32 bits:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0
If we don't need very high numbers, and want to save space, we might make do with 16 bits, or even 8; if memory is cheap enough, we might use 64 bits to be able to have much higher numbers. But everyone reading and writing a particular set of numbers has to agree in advance what width to use.
Things get a bit more complicated if we want negative numbers, or fractions, but the aim is the same: by agreeing in advance how much space each number will take up, we know where one number begins and the next one ends.
Now let's think about strings.
Imagine we extend the earlier task to pass a word to your friend, using the same device. One way to do this is to devise a code where each letter is given a number: 1 for A, 2 for B, and so on. Then a string is just a sequence of numbers, using our earlier fixed length idea; we only need to go up to 26, so we'll choose a fixed width of 2 digits.
So given the word "HELLO", you would pass over 8, 5, 12, 12, 15 like this:
0 8 0 5 1 2 1 2 1 5 0 0 0 0 0 0 0 0 0 0
Your friend reads off the numbers, converts them to letters, and gets the word.
Let's make it more challenging: a word, followed by a number, for instance: "YEAR", 2006; or "AGE", 18.
2 5 0 5 0 1 1 8 2 0 0 6 0 0 0 0 0 0 0 0
0 1 0 7 0 5 0 0 1 8 0 0 0 0 0 0 0 0 0 0
Wait, is that "YEART" and then 600? Or maybe "YEARTF" and a zero? We're back to our original problem of knowing when things begin and end.
One solution is to use our fixed length trick again: let's say every word is five letters long, but if the letter value if "00", ignore it. Because we're not used to putting "zeroes" on the beginning of words, we decide to pad at the end instead of the beginning, but it doesn't really matter.
2 5 0 5 0 1 1 8 0 0 2 0 0 6 0 0 0 0 0 0
0 1 0 7 0 5 0 0 0 0 0 0 1 8 0 0 0 0 0 0
The problem is, words vary in length a lot more than numbers, so we'll often have to have quite a large width, and so waste a lot of space on smaller words.
But there's something interesting about our "00", it's essentially like a space in English writing. When we read, we know that as soon as we get to a space, that's the end of the word, so we can use the same trick with our 00.
2 5 0 5 0 1 1 8 0 0 2 0 0 6 0 0 0 0 0 0
0 1 0 7 0 5 0 0 0 0 1 8 0 0 0 0 0 0 0 0
That means we can write as much text as fits in our total number of spaces; for instance, to write "A", 99; "AA", 99; and "AAAAAAAA", 99:
0 1 0 0 9 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 1 0 0 9 9 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0 9 9
This is one of the common ways strings are stored in computers - in practice, it's not words we want to separate, but whole strings, so the "terminator" (normally all zero bits) is separate from a space (which in ASCII-compatible 8-bit encodings is 00100000),
One problem is that you have to "reserve" that special terminating character - you can't represent a string that has that sequence of bits in it. To get around that, there's a different trick we can use: instead of signalling the end of the string, we can send a separate marker for the length of the string.
For instance, our "YEAR" and "AGE" example becomes:
0 4 2 5 0 5 0 1 1 8 2 0 0 6 0 0 0 0 0 0
0 3 0 1 0 7 0 5 0 0 1 8 0 0 0 0 0 0 0 0
And our As and 9s:
0 1 0 1 9 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 2 0 1 0 1 9 9 0 0 0 0 0 0 0 0 0 0 0 0
0 8 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 9 9
The maximum length of a string is now determined by how wide we made the number representing the length - in this case, we have a two-digit length, so can represent string from 0 to 99 letters long.
I am using the jsonlite package to work with a complex json file in r. To give a sense of its complexity, here is a look into two different data frames it contains
> output$dataobjects[1]$dataobject$scoredresults$x
xid rawscore tscore percentile standarderror omit
1 LAC.1 0 0 0 0 0
2 LAC.5 0 0 0 0 0
3 LAC.9 0 0 0 0 0
4 LAC.14 0 0 0 0 0
> output$dataobjects[1]$dataobject$scoredresults$y
yid rawscore tscore percentile standarderror omit
1 c1.1.1 0 0 0 0 0
2 c1.1.3 0 0 0 0 0
3 c1.1.5 0 0 0 0 0
4 c1.1.6 0 0 0 0 0
I would like to dynamically edit this json based on an external dataframe. Say the dataframe looks like this
id <- c("LAC.1", "LAC.5", "c1.1.1", "c1.1.5", "LAC.14")
rawscore <- c(15, 10, 12, 14, 15)
type <- c("x", "x", "y", "y", "x")
df <- data.frame(id, rawscore, type)
I want to use a for-loop to iterate over the rows in this dataframe, updating the rawscore column in the json as I go. I can't figure out how to move from x to y within the json while staying within the for-loop. Please help!
I have a dataset that looks like:
id s01 s02 s03 s04 s05 s06 s07 s08 s09 s10
U01 1 0 0 0 0 0 0 0 0 1
U02 1 0 0 0 0 0 0 0 0 0
U03 1 0 0 0 0 0 0 0 0 0
U04 0 1 0 0 0 0 0 0 0 0
U05 0 1 0 0 0 0 0 0 0 0
U06 0 0 1 0 0 0 0 0 1 0
U07 0 0 1 0 0 0 0 0 0 0
U08 0 0 1 0 0 0 0 0 0 0
U09 0 0 1 1 0 0 0 0 0 0
U10 0 0 0 1 0 0 0 0 0 0
U11 0 0 0 1 1 0 0 0 0 1
U12 0 0 0 0 1 0 0 0 0 0
U13 0 0 0 0 1 1 0 0 0 0
U14 0 0 0 0 0 1 1 0 0 0
U15 0 0 0 0 0 0 1 0 0 0
U16 0 0 0 0 0 0 1 1 0 0
U17 0 0 0 0 0 1 0 1 0 0
U18 0 0 0 0 0 0 0 1 0 0
U19 0 0 0 0 0 0 0 1 1 0
U20 0 1 0 0 0 0 0 0 1 0
I want to import it into Neo4j, the nodes are U01, U02, U03 ... and s01, s02, ...., s10
The nodes have already been created. Now I want to create relationships based on the values (0/1): if it is 1 then there should be a relationship between the corresponding nodes.
Like for 2nd row and 2nd column the value is 1, so that means I have to create a relationship between U01 and S01 and for 2nd row 3rd column the value is 0, so I will skip that value.
So far, I was trying to come up with a hardcoded solution:
LOAD CSV WITH HEADERS FROM file:///new.csv" AS line FIELDTERMINATOR '\t'
WITH line
MATCH (a:Users)
WHERE a.user_id = line.id
WITH line, a
RETURN CASE
WHEN TOINT(line.s01)=1 THEN CREATE (a)-[:watches]->(c:NewsMedia{ID:"s01"})
WHEN TOINT(line.s02)=1 THEN MERGE (a)-[:watches]->(b:NewsMedia{ID:"s02"})
WHEN TOINT(line.s03)=1 THEN MERGE (a)-[:watches]->(b:NewsMedia{ID:"s03"})
WHEN TOINT(line.s04)=1 THEN MERGE (a)-[:watches]->(b:NewsMedia{ID:"s04"})
WHEN TOINT(line.s05)=1 THEN MERGE (a)-[:watches]->(b:NewsMedia{ID:"s05"})
WHEN TOINT(line.s06)=1 THEN MERGE (a)-[:watches]->(b:NewsMedia{ID:"s06"})
WHEN TOINT(line.s07)=1 THEN MERGE (a)-[:watches]->(b:NewsMedia{ID:"s07"})
WHEN TOINT(line.s08)=1 THEN MERGE (a)-[:watches]->(b:NewsMedia{ID:"s08"})
WHEN TOINT(line.s09)=1 THEN MERGE (a)-[:watches]->(b:NewsMedia{ID:"s09"})
WHEN TOINT(line.s10)=1 THEN MERGE (a)-[:watches]->(b:NewsMedia{ID:"s10"})
END
The error is coming in the conditional MERGE part, can we not create relationships in WHEN <condition> THEN MERGE ()-[]->()
1) I think that you can not use MERGE inside the CASE.
2) To avoid hardcored solution you can use UNWIND and KEYS:
LOAD CSV WITH HEADERS FROM "file:///new.csv" AS line FIELDTERMINATOR '\t'
MERGE (a:Users {user_id: line.id})
WITH a, line
UNWIND KEYS(line) AS bid
WITH a, bid WHERE "id" <> bid AND TOINTEGER(line[bid]) = 1
MERGE (b:NewsMedia {ID: bid})
MERGE (a)-[r:watches]->(b)
RETURN a, r, b
I need to make a function in lisp that counts the pieces in my board. Let me explain my issue/game:
i have a board (10x10) like so:
(
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
)
in this board we will represent two types of pieces:
1 by 1 squares
2 by 2 squares
they will be positioned in my board like so:
(
(0 1 0 0 0 0 0 1 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 1 1 0 0 0 0 0)
(0 0 0 1 1 0 0 1 1 0)
(0 0 0 0 0 0 0 1 1 0)
(0 0 0 0 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
(0 1 0 0 0 0 1 0 0 0)
(0 0 0 0 0 0 0 0 0 0)
)
In this example I have four 1-by-1 squares and two 2-by-2 squares.
I need a function that counts how many of a given type of piece I have in my board.
Example:
(defun countPiece (board pieceType)
; here I am supposed to write my code
)
so if I called (countPiece (testBoard) 'twoByTwoSquare) it returns 2, for example where testboard is a function that returns a sample board with a few pieces positioned
However, I dont know where to begin. any tips?
EDIT: There can never be pieces together horizontally nor vertically. Only in diagonal
I am not fluent in lisp, so my answer will only give you the algorithmic solution:
singlePieces = empty list
doublePieces = empty list
for each row
for each column
if ((value[row][column] = 1)
and ((row - 1, column - 1) not in doublePieces)
and ((row - 1, column) not in doublePieces)
and ((row, column - 1) not in doublePieces)) then
if ((row < rowCount)
and (column < columnCount) and (value[row][column + 1] = 1)
and (value[row + 1][column] = 1)
and (value[row + 1][column + 1]) = 1) then
doublePieces.add(row, column)
else
singlePieces.add(row, column)
end if
end if
end for
end for
Here’s how to do it with lists:
(defun count-pieces (board &aux (small 0) (big 0))
(loop for (row next) on (cons nil board)
for bigs = nil then
(loop for (tl tr) on row
for (bl br) on (or next (mapcar (constantly 0) row)
for n from 0
for nearly-bigp = (every #’plusp (list tl tr bl br))
for bigp = nearly-bigp then (and nearly-bigp (not pbigp))
and pbigp = nil then bigp
for ninbig = (or (member n bigs) (and bigp (member (1+ n) bigs)))
if (and ninbig bigp)
collect n and collect (+ n 1)
and do (incf big)
else if ninbig do (incf small)))
(values big small))
I wrote this on my phone so I’m not sure it works. It’s also a little clumsy. It’s also probably worth using arrays instead of lists and it might be worthwhile to e.g. represent a big piece with -1 or 2 or 3 so that they may be easily distinguished.
I have a matrix of 0's and 1's, say:
0 1 0 0
0 0 1 0
1 0 0 0
I want to generate another matrix that replaces 0's with 1's and 1's with 0's:
1 0 1 1
1 1 0 1
0 1 1 1
Anyone know how to do this in Octave?
b = 1 - a;