I don't understand how 's' is being used in '(+s[i])'. What is the '+' for? Also I do not understand the use of the || symbol in this way, what is it doing?
var draw = function(s){
...
if (+s[i]) a = (a+90)%360||360; // Right
else a = (a-90||360); // Left
In the code below I do not understand what 'while (n--)' does?
var getS = function(n){
var s = '';
while (n--) s += getNext(0);
return s;
};
If you want to look at this code in context go to http://fractal.qfox.nl/ and press F12 to get the developer tools up and look for dragon.js in the scripts. Please feel entirely free to post a complete translation to C# as well if you fancy the challenge.
Putting + in front of an expression coerces it into a number, e.g. from a string.
The || operator has the value of its left side if that can convert to true, otherwise the value of its right side. And so a||b would mean "use a if it's not null, false, zero or an empty string, otherwise use b".
And n-- will have boolean value false when n reaches zero.
if (+s[i]) is checking if s[i] exists and is a number != 0. In C# it would be the same as
int n;
if (int.TryParse(s[i], out n) && n != 0) { }
a = (a-90||360); is basically saying if leftside of || is null, undefined, false or zero, then take rightside. In C# it would look something like
a = (a-90 > 0)? a-90 : 360;
but a would have to be declared prior to that line.
while (n--){ } keeps repeating itself until n is 0. n must be declared prior to running that code though such as var n = 10;. In C# it would be
int n = 10;
while (n >= 0)
{
n--;
}
Related
I have been googling for two days now, but can't figure this out and it seems to be basic.
Within the void OnTick(), I would like to create a variable int a;. Lets say it starts out with no value int a;, then I test condition if a is NULL or || equals 1 like this if (a == NULL || a == 1) which should always return true the first time the if statement runs due to NULL. I then assign a value to the variable a = 0;, so now a should equal 0.
This should trigger the else if(a == 0) the next time OnTick() is called, at this point I assign a = 1; resulting in the if being triggered next time round, etc and infinitum, constantly checking and assigning values switching between 0 and 1.
void OnTick()
int a;
if (PositionsTotal() < 1)
{
if(a == NULL || a == 1)
{
a = 0;
}
else if(a == 0)
{
a = 1;
}
}
I do not know what is going on, but during Testing in Strategy tester, I am getting a long string of numbers which switches between negatives and positives always with the same number -123456789 or 123456789, always 9 digits long.
Or during Debugging from Meta Editor. A random positive 3 digit number which never changes!
So far I have had both 303 and 597.
Beyond frustrating and most likely answered somewhere else.
Just not sure how to phrase the search term.
Thanks for you help.
I think the problem is that you declare your variable a inside the method OnTick, so it starts with an arbitrary value every time you call this method. Try declaring it outside this method.
So I have some code centered around a do while loop.
string token;
var count = 0;
var checkDataLength= data.Length == 16;
do
{
count = 0;
token = GenerateToken(data, start, end);
if (checkDataLength)
{
if (Mod120Check(token))
{
count += 1;
}
}
if (IsCompliant(token))
{
count += 1;
}
}
while (count > 0);
return token;
Basically, I am generating a token and for this token to be valid, has to FAIL the Mod120Check and the IsCompliant check. I cannot change how those methods return the data.
While the above code works, I feel that its ugly and I was wondering if anyone had a better way of doing it?
Thanks
Try this:
do
{
token = GenerateToken(data, start, end);
}
while (checkDataLength && Mod120Check(token) || IsCompliant(token))
Just moving your conditions to the while.
(!) Notice that IsCompliant(token) will only be called if checkDataLength && Mod120Check(token) states false. It shouldn't cause any colateral effects, but it could, depending on what your IsCompliant method does.
You're right, it is ugly. You are using count in an unexpected way (it gets reset to zero at the top of every loop, and can go positive for two different reasons). When I see count, I expect something to start at zero and count up (or start high and count done). Try this instead:
Change var count = 0; to var goodEnough = false; up at the top
Remove the count = 0; statement
Change the two count += 1; statements to goodEnough = true;
Change the while (count > 0); to while (!goodEnough);
This emphasizes that you are starting in a "not good enough" state, and you will loop until some condition makes it good enough to continue past the loop.
This question already has answers here:
How do Prefix (++x) and Postfix (x++) operations work?
(7 answers)
Closed 6 years ago.
While I am testing post increment operator in a simple console application, I realized that I did not understand full concept. It seems weird to me:
int i = 0;
bool b = i++ == i;
Console.WriteLine(b);
The output has been false. I have expected that it would be true. AFAIK, at line 2, because of the post increment, compiler does comparison and assigned b to true, after i incremented by one. But obviously I am wrong.
After that I modify the code like that:
int i = 0;
bool b = i == i++;
Console.WriteLine(b);
This time output has been true. What did change from first sample?
Suppose i has the value 0 initially, as it does in your examples.
i++ == i reads i (0), increments i, reads i again (1), and compares the two values: 0 == 1.
i == i++ reads i (0), reads i again (0), increments i, and compares the two values: 0 == 0.
The increment happens immediately after reading the old value.
Answering to your first snippet of code:
Here, bool b = i++ == i;is 0 == 1and this is because as you know i++ is a post increment so i remains 0 at i++ but after that part is finished executing and it is being compared to the right hand side which is i , by this time the value has change to 1 due to that previous post increment. This is why you are getting False when doing : bool b = i++ == i;.
Like #hvd said: The increment happens immediately after reading the old value.
The order of evaluation of the postfix and the equality operator is from left to right, so the code behaves as explained in the code comments.
int i = 0;
bool b = i++ == i;
// 1.) i++ returns old value i.e. 0
// 2.) but after that it increments and becomes 1
// 3.) hence, bool b = 0 == 1; --> evaluates to false
Console.WriteLine(b); // prints false
int i = 0;
bool b = i == i++;
// 1.) i returns old value i.e. 0
// 2.) i++ returns old value i.e. 0, as this is the end of the statement after that it would increment
// 3.) hence, bool b = 0 == 0; --> evaluates to true
Console.WriteLine(b); // prints true
I'm trying to get away with a slick one liner as I feel it is probably possible.
I'll put my code below and then try to explain a little more what I'm trying to achieve.
for (int p = 0; p < 2; p++)
{
foreach (string player in players[p])
{
if (PlayerSkills[player].streak_count *>* 0) //This line
PlayerSkills[player].streak_count++;
else
PlayerSkills[player].streak_count = 0;
}
}
*(p==0 ? >:<) the comparison operator is chosen depending on p.
Of course what I've written is rubbish. But basically I want to use >0 when p==0, and <0 when p>>0. Is there a nice way to achieve this?
Well, you should use what is most readable, even if it is not as consice. That said...
// Invert the count for all but the first player and check for a positive number
if (PlayerSkills[player].streak_count * (p==0 ? 1 : -1) > 0)
I don't know about slick, but the following and/or combination is one line:
if ((p == 0 && PlayerSkills[player].streak_count > 0)
|| PlayerSkills[player].streak_count < 0)
...
This will only ever do the array index once (due to the p==0 condition occurring first) and so is equivalent to the "ternary" you wrote (albeit a bit more verbose).
p > 0 ? whenGreaterThanZero : whenZeroOrLess ;
E.g.
int p = 1; bool test = p > 0 ? true : false ;
Lets test = True
Sorry for such a basic question regarding lists, but do we have this feature in C#?
e.g. imagine this Python List:
a = ['a','b,'c']
print a[0:1]
>>>>['a','b']
Is there something like this in C#? I currently have the necessity to test some object properties in pairs. edit: pairs are always of two :P
Imagine a larger (python) list:
a = ['a','a','b','c','d','d']
I need to test for example if a[0] = a[1], and if a[1] = a[2] etc.
How this can be done in C#?
Oh, and a last question: what is the tag (here) i can use to mark some parts of my post as code?
You can use LINQ to create a lazily-evaluated copy of a segment of a list. What you can't do without extra code (as far as I'm aware) is take a "view" on an arbitrary IList<T>. There's no particular reason why this shouldn't be feasible, however. You'd probably want it to be a fixed size (i.e. prohibit changes via Add/Remove) and you could also make it optionally read-only - basically you'd just proxy various calls on to the original list.
Sounds like it might be quite useful, and pretty easy to code... let me know if you'd like me to do this.
Out of interest, does a Python slice genuinely represent a view, or does it take a copy? If you change the contents of the original list later, does that change the contents of the slice? If you really want a copy, the the LINQ solutions using Skip/Take/ToList are absolutely fine. I do like the idea of a cheap view onto a collection though...
I've been looking for something like Python-Slicing in C# with no luck.
I finally wrote the following string extensions to mimic the python slicing:
static class StringExtensions
{
public static string Slice(this string input, string option)
{
var opts = option.Trim().Split(':').Select(s => s.Length > 0 ? (int?)int.Parse(s) : null).ToArray();
if (opts.Length == 1)
return input[opts[0].Value].ToString(); // only one index
if (opts.Length == 2)
return Slice(input, opts[0], opts[1], 1); // start and end
if (opts.Length == 3)
return Slice(input, opts[0], opts[1], opts[2]); // start, end and step
throw new NotImplementedException();
}
public static string Slice(this string input, int? start, int? end, int? step)
{
int len = input.Length;
if (!step.HasValue)
step = 1;
if (!start.HasValue)
start = (step.Value > 0) ? 0 : len-1;
else if (start < 0)
start += len;
if (!end.HasValue)
end = (step.Value > 0) ? len : -1;
else if (end < 0)
end += len;
string s = "";
if (step < 0)
for (int i = start.Value; i > end.Value && i >= 0; i+=step.Value)
s += input[i];
else
for (int i = start.Value; i < end.Value && i < len; i+=step.Value)
s += input[i];
return s;
}
}
Examples of how to use it:
"Hello".Slice("::-1"); // returns "olleH"
"Hello".Slice("2:-1"); // returns "ll"