I don't understand the square bracket effect - square-bracket

I just started learning brainfuck and I'm only at the hello world example. I have a question what the square bracket are used for?
Example:
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
This is the program "hello world". I don't understand why there is [ ] I understand - and + but I read somewhere that the brace bracket is:
[ (opening square bracket)
while(mem[ptr]) {
] (closing square bracket)
}
but even if I have this information I don't understand why there is this here.
I'm fluent in php/C/POSIX/java but here I don't understand this.

Every [ has to be matched to another ]
So always think of them in pairs. Here's how you reason them out. When the program encounters a ] it checks the current cell. If the cell has a number larger than 0 in it, the program goes back to the matching [. If the current cell has 0 in it the program continues.
[ and ] are a way to make loops.
Here is a simple example: +++[-] this program first sets the current cell to 3 and then clears it (you will encounter the part [-] quite often, this is the part used to set the cell to 0, whatever its original value was). Then it encounters the first [ and ignores it (you can think of it as just a marker). Then it executes - and the first cell now becomes 2. after that it encounters the ]. The current cell has 2 in it, so since 2 is larger than 0 the program jumps back to the [. it does this twice more until the value in the cell becomes 0. When the cell has 0 in it and the program encounters the ] again, it sees that the current cell has 0 in it, so instead of jumping to the [ it lets the program continue to the right (and since this program has no more instructions after the ], that means that this tiny program will end)

Related

Playwright finds too much elements by text

I tried to find all the elements that has the text "Operation" inside them. I only see one such element visible, but Playwright is giving me 19:
self.page.locator('*', has_text='Operation').click()
When I run this selector in the developer tools:
$x('//*[contains( text(),"Operation" )]')
I see only a single element.
What am I doing wrong here?
You can use the text locator and also add more Texts along with Operation to pinpoint your element more accurately.
page.locator("text=Operation").click()
page.locator("text=Some more words Operation").click()
Or, you can also use the Nth element selector if you know the position of the text occurrence. Starts from 0
page.locator("text=Operation >> nth=0").click()

PineScript is exiting strategy and opening a double sized order on the same bar

Using the pinescript editor in TradingView, I've noticed a slightly strange occurrence. When there are triggers to exit a long trade (trailing stop) and a trigger to enter a new short trade on the same bar, both open, however, the short has 2x the position size than it should.
Here's the relevant code:
// When uptrend, use upper band as a stop market buy
if (uptrend)
strategy.entry("Long", strategy.long, stop = h)
// Exit longs with a trailing stop. Assume longStopPrice is calculated per bar
if (strategy.position_size > 0)
strategy.exit("Trailing Stop", "Long", stop = longStopPrice)
// When downtrend, use lower band as a stop market sell
if (downtrend)
strategy.entry("Short", strategy.short, stop = l)
// Exit shorts with a trailing stop. Assume shortStopPrice is calculated per bar
if (strategy.position_size < 0)
strategy.exit("Trailing Stop", "Short", stop = shortStopPrice)
Here is the output on the UI:
Notice that the long position size is 15.358918 and a few bars later, this is closed with a trailing stop size -15.358918. However a new short trade is also opened on the same bar size -30.70059 (It should not be -30 it should be -15).
A little bit later on this second trade is closed by trailing stop, size +15.34 (but -30 was opened).
The strategy leaves you net short -15 units for the entire duration of the backtest. Ideally i'd like the strategy.enter to only enter a single position size & trailing stop to close the entire position, not to enter a double position & close half.
Any ideas?
Its because your exit condition does not have the same id as the entry condition. For TV, the entry id has still not closed. So, change the exit id from 'Trailing Stop' to 'Long' and 'Short' as those are the ids used during the entry.
Theres some logic behind but this happens when you dont close a position before opening a new one, what helped me was just moving the close strategy above the open, so every loop also checks to confirm to always close a position before opening a new one
Btw, also dont forget that on one way mode position your 2nd trade must have to be 2x the previous one, since lets say you go +$25 long, then -$25 for short, you would end with $0 for the next trade. Then having 2x the previous one, you would go +$25 long, then -$50 for short, you would end with -$25 short for your next trade, then +$50 long again you would end +$25 next trade
add to if downtrend this--
and strategy.position_size == 0

Alignment in flextable correct in Rstudio viewer, incorrect in Word

I am trying to left align some columns in a few of my tables. Most of the time this works no problem, but other times, the columns selected with:
ft <-align(ft, j=1:2, align = "left", part = "body")
ft <-align(ft, j=4:6, align = "left", part = "body")
look correct in the Rstudio viewer. I used a loop to make 39 tables and want to add them to a word document using officer
However, when printed to a word doc using:
doc <- read_docx() %>%
body_add_flextable(tab0)
body_add_break(doc, pos="after")
for (i in 1:38) {
body_add_flextable(doc,get(paste0("tab", i)), align = "left")
body_add_break(doc, pos="after")
}
print(doc,target=documentname)
it center aligns the columns (only changes those that were left aligned). When I click on the text, it says that they are left aligned, but then I center and re-left align and that fixes the issue. I have only noticed the issue which character variables
I tried triming whitespace with
trimws() before I make the flextable with no luck.
I tried other alignments and the issue only seems to occur with left alignment.
I don't believe my code to be wrong, since it works for my other tables. Therefore, I think trying to make reproducible code would be a waste of time.
Therefore, I'm assuming the text itself is causing the issue? Does anyone know what could be wrong with the character variables themselves? Or any other ideas?
Fixed it...
Somewhere within my flextable manipulation this leading white space was added.
So trimming white space before didn't do anything. Had to trim right before naming my table (within a loop). Here is the loop I used to correct it.
for (j in 1: length(ft[["body"]][["content"]][["content"]][["data"]])){
ft[["body"]][["content"]][["content"]][["data"]][[j]][["txt"]] <- trimws(ft[["body"]][["content"]][["content"]][["data"]][[j]][["txt"]])
}

How to clear the output in a cell in itorch notebook?

In itorch notebook, Is there a way to clear the output of a cell with code?
What I would like to do is to dynamically display the progress, like a loading bar.
The reason for this is when a lot of computation is involved, I would like to track how far the progress has gotten, and whether it froze or not.
So basically I would like to print the number of iteration I'm in in percentage form and overwrite it in the next iteration.
Any ideas?
You can use the carriage return character (\r) to overwrite the same line in a cell.
For example:
io.write("Hello") -- No newline
io.write("\rWorld")
just displays
World
You can use this to write several times the progress of your process, without adding multiple lines.

Using CGContext, how to draw a line which starts with an unpainted dash segment?

Using CGContext, drawing a dashed line starting with a painted segment is easy:
using (var ctx = UIGraphics.GetCurrentContext())
{
ctx.BeginPath();
//... draw a path here
var pattern = new float[] { 2, 2 };
ctx.SetLineDash(0, pattern);
ctx.DrawPath(CGPathDrawingMode.Stroke);
}
This draws a line looking like this "xx--xx--xx--xx--", where "x" means painted, and "-" means not painted.
Now I want to draw a line starting with an unpainted segment, that is: "--xx--xx--xx--xx". Is there an elegant way to achieve this? E.g. by telling iOS to start with an unpainted segment?
Given our example, a line starting with unpainted segments can be achieved using the phase parameter:
using (var ctx = UIGraphics.GetCurrentContext())
{
ctx.BeginPath();
//... draw a path here
var pattern = new float[] { 2, 2 };
ctx.SetLineDash(2, pattern);
ctx.DrawPath(CGPathDrawingMode.Stroke);
}
However, for more complex patterns (something like { 2, 4, 3, 7, 5, 6 }) using the phase parameter becomes more cumbersome. The pattern probably needs to be shifted in addition to the phase parameter.
To wrap up: Is it possible to elegantly create a line dash starting with an unpainted segment, without using the phase parameter?
Note: The code examples are written in C# (MonoTouch), but the question applies to native iOS code as well.
Simple solution
Using only the line dash feature, the given problem cannot be solved without the phase parameter. It can be solved by re-ordering the dash array, and setting a phase.
To start with an unpainted segment, move the last dash array entry to the front, and set a phase with the same value. E.g. when the pattern -xx-xx-xx-xx-xx... is desired, the dash array 1, 2 with phase 0 would lead to a pattern like x--x--x--x--.... The dash array 2, 1 with phase 2 would lead to the pattern -xx-xx-xx-xx-xx..., as desired.
Extended solution with adapted pattern repeating
It seems iOS always alternates between painted and unpainted segments, and does not reset when the dash array is repeated. So e.g. the dash array 1, 2, 1 would result in a line pattern looking like x--x-xx-x--x.... In our case, we wanted the whole pattern to repeat. That is, with the given dash array, the line pattern should look like x--xx--xx--xx....
To achieve that, we need to ensure that the dash array always has an even number of elements. In addition to re-ordering the dash array and setting a phase, we need to adapt and remove elements. There are four cases to consider.
1. Start with painted segment, even number of segments.
Nothing to do here. Use dash array as is. Phase is 0.
2. Start with unpainted segment, even number of segments.
Move the last dash array entry to the front, and set a phase with the same value. Refer to the simple example above.
3. Start with painted segment, odd number of segments.
Add the last dash array entry to the first entry. Remove the last entry. Set a phase equal to the original last dash array entry.
E.g. the dash array 1, 2, 1 would normally lead to a pattern x--x-xx-.... The dash array 2, 2 with a phase of 1 leads to the desired pattern x--xx--xx--....
4. Start with unpainted segment, odd number of segments.
Add the first dash array entry to the last entry. Remove the first entry. Set a phase equal to the sum of all entries minus the original first dash array entry (this essentially lets the pattern start near the end, exactly where we added the original first entry to the last entry).
E.g. the dash array 1, 2, 1 would normally lead to a pattern x--x-xx-.... The pattern 2, 2 with a phase of 3 leads to the desired pattern -xx--xx--xx....
Alternatives
An alternative which does not use line dash feature could be the one described by #user2320861. #user2320861 suggests to paint over a solid line with a patterned line, using the background color. This should work well when the background color is known, and a uniform background is used.
A variant for diverse backgrounds (e.g. images) could be to set a clipping mask (CGContextClipToMask) by drawing a patterned line. Then a solid line could be drawn with this clipping mask.

Resources