I want to take the user's input of a positive integer where 1 < a < 10^6 and run a loop on it and then store it in a matrix which gets printed to the screen. However, my code is throwing a syntax error pointing to the letter "e" in while. Does anyone know why this error is appearing?
A = (while (a!=1)
If(rem(a,2)=0
floor(a^(1/2));
Else
floor(a^(3/2));
endwhile)
disp(A);
You're having several different problems in your code:
a while loop doesn't return anything, so you can't assign it to A
syntax is case sensitive, so it's if and else, not If and Else
you're missing a closing brace after the if clause
you're missing an endif
you're assigning to rem, use == to compare for equality
Related
if (a(i,j) gt 0,
z(i,j) eq 0;
else
z(i,j) eq 1;
);
equations
kapasite_asker(i)
kapasite_silah(i,k)
talep_asker1(j)
talep_asker2(j)
talep_silah1(j)
talep_silah2(j)
atama_asker(i)
atama_silah(i)
agirlik(i,j)
risk
amac
;
kapasite_asker(i).. sum(j,a(i,j))=L=y(i)*c(i);
kapasite_silah(i,k).. sum(j,x(i,j,k))=L=m(i,k)*f(i);
talep_asker1(j).. sum(i,z(i,j))=E=1;
talep_asker2(j).. sum(i,a(i,j))=E=g(j);
talep_silah1(j).. sum(i,e(i,j))=E=1;
talep_silah2(j).. sum((i,k),x(i,j,k))=E=sum(k,n(j,k));
agirlik(i,j).. sum(k,x(i,j,k)*v(k))=L=t(i,j);
risk.. sum((i,j),(z(i,j)*r(i,j)+e(i,j)*r(i,j)))=L=25*sum((i,j),z(i,j)+e(i,j));
amac.. sum(i,f(i)+y(i))=E=p;
atama_asker(i).. sum(j,z(i,j))=L=11*y(i);
atama_silah(i).. sum(j,e(i,j))=L=11*f(i);
Error 143
A suffix is missing
Error 141
Symbol declared but no values have been assigned. Check for missing
data definition, assignment, data loading or implicit assignment
via a solve statement.
A wild shot: You may have spurious commas in the explanatory
text of a declaration. Check symbol reference list.
Error 149
Uncontrolled set entered as constant
Error 10
',' expected
Error 140
Unknown symbol
Error 36
'=' or '..' or ':=' or '$=' operator expected
rest of statement ignored
Error 409 in
Unrecognizable item skip to find a new statement
looking for a ';' or a key word to get started again
I'm new to GAMS. The code I wrote gives an error. 'a' in the code is an integer variable and z is a binary variable. How can I fix it? Or how do I connect these two variables? Thanks in advance.
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
Since a and z are both variables, the if-else statement is not good to use for their assignment. You can use big-M method and add two constraints for such a logic:
-Mz <= a <= M(1-z) - eps, where M is a positive scalar, and eps is a sufficient small number by default.
I started working with Ruby today and looked up the appropriate syntax for basic operations. Apparently I wasn't careful enough because the lines calling length below throw the error "dynamic constant assignment". There may be even more mistakes waiting to be uncovered after this is fixed, but for now I will only focus on this error.
def isPalindrome(n)
m = n.to_s;
L = m.length; <-- error
for i in 0..L/2
if(m[i] != m[L-1-i])
return false;
end
end
return true;
end
def nthmax(n, a)
L = a.length; <-- error
if(n > L)
return nil;
end
a.sort;
return a[L-n];
end
Searching the error message leads to posts about how the error is thrown when someone tries to assign a constant a value that isn't actually constant. The question given here is not relevant because I'm not trying to make L a constant (this info was added to clarify that the question, at the time I asked it, was not a duplicate, nor is it a duplicate now).
Turns out that if the 1st letter of a variable is capitalized, it's treated as a constant. "L" should be changed to "l".
You stumbled over an unusual design aspect in Ruby language. Ruby does not have constants, but it calls identifiers that start with upper case latters as constants. You are allowed to modify them, but get a warning (not an error!) if you do this, as it happed in your assignment to L. If you really want to modify a constant without warning, you can do it with
# Don't do this unless you really have to!!!!
self.class.send(:remove_const, :L) # Make it disappear
L = new_value # Recreate it
It is considered bad practice (as you can already guess from the fact that remove_const is now a private method), and you do these tricks mostly if you go into Ruby meta-programming. In your case, I see no reason why you can't use a normal local variable instead.
I read other questions about the "slice bounds of range" here in the stackoverflow, but none of them using the same context from this.
Then, none of the answers helped me.
In my use case, the "golang's syntax" of substring using [] doesn't return an error variable. It launches a runtime error using the "panic" instruction.
My goal is to avoid to reach on the "panic" instruction.
I need to treat this error and provide messages that describe with more details the context around the moment where this error had occurred.
Obs:
The content of the string variable which one I need to get the substring value is totally dynamic and the indexes that I have been using to get the substring value is equally calculated dynamically.
You need to do bounds checking on your indexes:
if j >= 0 && j <= len(str) {
y = str[:j]
}
I'm writing a dice rolling function. In order to add the result of each die, I added to an output variable using a for loop. However, I'm getting an error thrown when I attempt to build;
syntax error: unexpected semicolon or newline, expecting {
This was thrown on the line initializing the for loop.
Here is my code:
for i := 0; i < [0]si; i++ {
output := output + mt.Intn([1]si)
}
si is simply an int array holding 2 values, and mt is the name I gave to math/rand when I imported it.
Your loop has several problems:
The use of square brackets is odd. Outside of type definitions, these go after slice/array names, e.g. x[i] would give you the ith element of the slice x.
There is no reference to i inside the loop body, and thus each iteration of the loop will do the same thing.
You should probably write output = output + ... without the colon. Otherwise, a new variable output is declared during each iteration of the loop, and forgotten immediately after so that the loop has no effect.
The compiler error is probably caused by the first of these problems.
You access an array with varname[idx] in Go, just like in most other languages. It's only when declaring a new array of type si that you use the [size]si prefix syntax. You're getting the error because your code is syntactically invalid. You're trying to compare i to an array of 0 si s.
Given a string in the following format:
"(1 AND (2 OR 3) AND 4)"
What is the fastest way to determine if the number of "open" brackets "(" equal the "close" brackets ")".
NOTE: The string could be several hundred characters longs.
Just use a simple counter that starts with 0.
When you encounter "(", increase it by one. When you encounter ")", decrease by one.
If the counter isn't 0 at the end, you've got a mismatch.
Also, as others have mentioned, if the counter ever becomes negative, this means a situation such as )( has occured. Signal an error and stop further parsing.
Initialise counter to zero.
Iterate through the characters of the string.
a. On an opening parenthesis, increase the counter.
b. On a closing parenthesis, decrease the counter.
c. Error out if the counter is negative.
Error out if the counter is not equal to zero after the loop.
This also catches cases like )(, which do have a matching number of opening and closing parens, but should probably be considered erroneous anyway.
If you're trying to count that the number of ( match the number of ), just run through the string once maintaining a counters, incrementing if you see a ( and decrementing if you see a ). This is O(n) and you can't do better; you have to inspect every character.
However, I suspect you meant to ask a different question. Namely, how do you tell if the ( balance with the ). In this case, you maintain a stack and you push whenever you see a ( and you pop when you see a ). If ever you try to pop when the stack is empty, the parentheses are not balanced. If the stack is not empty when you reach the end of the input string, the parentheses are not balanced.
Of course, you can just mimic this with a counter, but it's more natural to think about from the perspective of a stack.
It's O(n). There is no way around it. Here is a rough idea.
For i=0 to string.length
if string[i] == ')'
add to rightBracketCount
else if string[i] == '('
add to leftBracketCount
end for
compare rightBracketCount to leftBracketCount
:)
You can
int left = "your string".split("(").length()
int right = "your string".split(")").length()
boolean ok = (left == right)
Of course this is stupid, but it is just another way