Using IntegerDigits and ToCharacterCode I convert a String a 2D Table of binary. Now I need to reverse this and all the Mathematica Help claims that FromDigits is the inverse of IntegerDigits. Yet I cannot get back to my original string. Here's my code:
ConvTextTo[s_String]:= IntegerDigits[ToCharacterCode[s],2, 8];
ConvTextFrom[l_List]:= FromCharacterCode[FromDigits[l, 2]];
This returns gibberish and I cannot figure out why. If FromDigits is indeed the inverse of IntegerDigits, why doesn't this work, for god's sake? This is driving me insane, I googled this for an hour now, I cannot find a solution. I've tried whatever I could think of, nothing seems to produce a result.
Thanks for your help.
You did well but there is just a little more to it. First you need to Map (/#) FromDigits onto each binary sequence, representing one character. Second Apply (##) StringJoin to the result of FromCharacterCode to get the original concatenated String. Apply replaces the Head of any expression with a new function that is applied to its arguments.
In general on reducing frustration :-) try giving the problem a rest and coming back to it, or just asking for help as you did. It's an elegant language and will gradually become instinctive.
In[9]:= string1 =
"The quick brown fox jumped over the lazy white dog.";
In[10]:= ToCharacterCode#string1
Out[10]= {84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, \
119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 101, 100, 32, \
111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, \
119, 104, 105, 116, 101, 32, 100, 111, 103, 46}
In[11]:= IntegerDigits[%10, 2, 8]
Out[11]= {{0, 1, 0, 1, 0, 1, 0, 0}, {0, 1, 1, 0, 1, 0, 0, 0}, {0, 1,
1, 0, 0, 1, 0, 1}, {0, 0, 1, 0, 0, 0, 0, 0}, {0, 1, 1, 1, 0, 0, 0,
1}, {0, 1, 1, 1, 0, 1, 0, 1}, {0, 1, 1, 0, 1, 0, 0, 1}, {0, 1, 1, 0,
0, 0, 1, 1}, {0, 1, 1, 0, 1, 0, 1, 1}, {0, 0, 1, 0, 0, 0, 0,
0}, {0, 1, 1, 0, 0, 0, 1, 0}, {0, 1, 1, 1, 0, 0, 1, 0}, {0, 1, 1, 0,
1, 1, 1, 1}, {0, 1, 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, 111, 120, 32, 106, 117, 109, 112, 101, 100, 32, \
111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, \
119, 104, 105, 116, 101, 32, 100, 111, 103, 46}
In[15]:= FromCharacterCode## & /# %14
Out[15]= {"T", "h", "e", " ", "q", "u", "i", "c", "k", " ", "b", "r", \
"o", "w", "n", " ", "f", "o", "x", " ", "j", "u", "m", "p", "e", "d", \
" ", "o", "v", "e", "r", " ", "t", "h", "e", " ", "l", "a", "z", "y", \
" ", "w", "h", "i", "t", "e", " ", "d", "o", "g", "."}
In[16]:= StringJoin ## %15
Out[16]= "The quick brown fox jumped over the lazy white dog."
Related
I have plotted a bipartite graph using igraph. I want to color specific nodes of my interest with different colors.
The code which i have used is
bmat=as.matrix(read.csv(file = "/home/tejaswini/Desktop/Butterflyplantdata.csv" , sep = "\t", header=T, row.names=1))
bmat
library(igraph)
bg=graph_from_incidence_matrix(bmat)
bg
V(bg)$type #Display the vertex types. They are "FALSE" or "TRUE"
library(bipartite)
plotweb(bmat) #the base function for bipartite network plot
Any help is highly appreciated
Here's an igraph-pure solution:
library(igraph)
anemonefish <- matrix(# http://www.ecologia.ib.usp.br/iwdb/data/anemone_fish/anemonefish.txt
c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 0, 0, 0,
1, 1, 1, 1, 1, 0, 1, 1, 0, 0,
0, 0, 1, 1, 1, 0, 1, 0, 0, 0,
1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
1, 1, 0, 1, 1, 0, 0, 0, 0, 0,
1, 0, 1, 0, 0, 1, 0, 1, 0, 0,
0, 1, 0, 1, 1, 0, 0, 0, 0, 0,
1, 1, 1, 0, 0, 1, 0, 0, 0, 0,
0, 0, 1, 1, 0, 1, 0, 0, 0, 0,
1, 0, 1, 0, 0, 1, 0, 0, 0, 0,
1, 0, 1, 1, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 1, 1, 0, 0,
1, 1, 0, 0, 0, 0, 1, 0, 0, 0,
0, 1, 0, 0, 0, 1, 0, 0, 0, 0,
1, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 0, 0, 0, 0, 0, 0,
1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 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),
nrow = 26, byrow = TRUE)
g <- graph_from_incidence_matrix(anemonefish)
plot(g, vertex.label = "", vertex.color = V(g)$type + 1, layout = layout_as_bipartite)
That's if you want to color the nodes by type. If you want some other method of assigning colors, the easiest thing to do is make a node attribute called color:
palette("Tableau 10")
V(g)$color <- sample(1:length(palette()), vcount(g), replace = TRUE)
plot(g, vertex.label = "", layout = layout_as_bipartite)
I read up some data from shared prefs and jsonDecode it into a variable called shapes. When I inspect the shapes in the debugger it looks like the right type. But when I assign it to the following variable "theShapes" I get errors such as Unhandled Exception: type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, List<List<int>>>'
static var theShapes = <String, List<List<int>>>{
'general': [
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
],
....
};
The code used to try to cast the "shapes" variable to the type of "theShapes" is like this at the moment:
theShapes = shapes
.map((key, value) => MapEntry(key, List.castFrom(value).toList()));
here you go
go(String data){
final decodedData = jsonDecode(data);
if(decodedData is Map){
return decodedData.map<String,List<List<int>>>((key, value) => MapEntry(key as String, (value as List).map<List<int>>((e) => (e as List).map<int>((i) => i as int).toList()).toList()));
}
return null;
}
reproducible code
Hello, I used the following code in markdown
<pre><span style="color:blue">Text = ' Sooo SAD I will miss you here in San Diego!!!'</span>, <span style="color:blue">Selected Text='Sooo SAD'</span>, <span style="color:blue">Sentiment = 'negative'</span></pre>
<pre>tokens =</pre>
<pre>input_ids = [0, 2430, 98, 3036, 5074, 939, 40, 2649, 47, 259, 11, 15610, 1597, 2977, 16506, 2, 1, 1...1]</pre>
<pre>attention_masks = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0...0]</pre>
<pre>start_tokens = [0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...0]</pre>
<pre>end_tokens = [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...0]</pre>
<pre>Edit Text = ' Sooo SAD I will miss you here in San Diego!!!' (len=46), Edit Seletected Text='Sooo SAD' (len=8)</pre>
<pre>char = [1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0...0] (len=46, num_1=9)</pre>
<pre>offset = [(0, 3), (3, 5), (5, 9), (9, 11), (11, 16), (16, 21), (21, 25), (25, 30), (30, 33), (33, 37), (37, 41), (41, 43), (43, 46)]</pre>
undesired effect
and generate the following effect
But the too wide line space makes the effect is kind of ugly. Do you know how to redue the line space?
Tried method
I tried this method
<pre style='display:inline'>tokens =</pre> <br>
<pre>input_ids = [0, 2430, 98, 3036, 5074, 939, 40, 2649, 47, 259, 11, 15610, 1597, 2977, 16506, 2, 1, 1...1]</pre>
But it only works for one line, if I want to apply to other lines such as this
<pre style='display:inline'>tokens =</pre> <br>
<pre style='display:inline'>input_ids = [0, 2430, 98, 3036, 5074, 939, 40, 2649, 47, 259, 11, 15610, 1597, 2977, 16506, 2, 1, 1...1]</pre> <br>
<pre>attention_masks = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0...0]</pre>
it would cover part of line content
Question
How to reduce the line space? Thank you
You could use css display and line-height properties on the pre elements and adjust the line-height you want.
pre {
display: inline;
line-height: 0.8em;
}
<pre><span style="color:blue">Text = ' Sooo SAD I will miss you here in San Diego!!!'</span>, <span style="color:blue">Selected Text='Sooo SAD'</span>, <span style="color:blue">Sentiment = 'negative'</span></pre>
<pre>tokens =</pre>
<pre>input_ids = [0, 2430, 98, 3036, 5074, 939, 40, 2649, 47, 259, 11, 15610, 1597, 2977, 16506, 2, 1, 1...1]</pre>
<pre>attention_masks = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0...0]</pre>
<pre>start_tokens = [0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...0]</pre>
<pre>end_tokens = [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...0]</pre>
<pre>Edit Text = ' Sooo SAD I will miss you here in San Diego!!!' (len=46), Edit Seletected Text='Sooo SAD' (len=8)</pre>
<pre>char = [1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0...0] (len=46, num_1=9)</pre>
<pre>offset = [(0, 3), (3, 5), (5, 9), (9, 11), (11, 16), (16, 21), (21, 25), (25, 30), (30, 33), (33, 37), (37, 41), (41, 43), (43, 46)]</pre>
Or reset the default margin.
You can use margin: 0 (or any other value) for pre in CSS:
pre {
margin: 0;
}
<pre><span style="color:blue">Text = ' Sooo SAD I will miss you here in San Diego!!!'</span>, <span style="color:blue">Selected Text='Sooo SAD'</span>, <span style="color:blue">Sentiment = 'negative'</span></pre>
<pre>tokens =</pre>
<pre>input_ids = [0, 2430, 98, 3036, 5074, 939, 40, 2649, 47, 259, 11, 15610, 1597, 2977, 16506, 2, 1, 1...1]</pre>
<pre>attention_masks = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0...0]</pre>
<pre>start_tokens = [0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...0]</pre>
<pre>end_tokens = [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...0]</pre>
<pre>Edit Text = ' Sooo SAD I will miss you here in San Diego!!!' (len=46), Edit Seletected Text='Sooo SAD' (len=8)</pre>
<pre>char = [1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0...0] (len=46, num_1=9)</pre>
<pre>offset = [(0, 3), (3, 5), (5, 9), (9, 11), (11, 16), (16, 21), (21, 25), (25, 30), (30, 33), (33, 37), (37, 41), (41, 43), (43, 46)]</pre>
So I have a little question here, I'm making a small game right now with canvas, probably going to use OpenGL later but not at the moment.
Now my question is, I see a lot of users doing it with a bunch of arrays like this :
var map = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 1, 1, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]
Is this really the best way of doing? This seems a bit overkill when you want to create new maps and I suppose big games that use these kind of pixel art maps are not using something like this, or at least not something like this directly.
So how does this works? Do they create an engine that will output these kind of arrays? Like a little gui where you can create a map and then the software will generate this for you? Or how is this all working
Create an editor that gives you a convenient UI to put together new maps and saves them as files. Then include those files in your project. Add a small component that loads them into an array.
Since last weeks update in Google Spreadsheets, this code is not working properly anymore. Only when a user ENTERS data, timestamp appears, when a user PASTE data, nothing happens.
This is the code we use:
function onEditSwapsheet(e) {
var s = e.source.getActiveSheet(),
sheets = ["5B","10B","15B","20B","25B","30B","35B","40B","50B","60B","70B","80B","90B","100B" ],
watchCols = [6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110,],
offsetCol = [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,],
sheetInd = sheets.indexOf(s.getName()),
colInd = watchCols.indexOf(e.range.columnStart);
if (sheetInd === -1 || colInd === -1 ) return;
e.range.offset(0, offsetCol[colInd])
.setValue(!e.value ? null : Utilities.formatDate(new Date(), "GMT+0100", "HH:mm dd/MM"))
}