I have a problem doing one of the questions from my programming lab.
The question was like "Given the variables x, y, and z, each associated with an int, write a fragment of code that assigns the smallest of these to min."
And my work area looks like
if x < y and x < z:
x = min
if y < x and y < z:
y = min
if z < x and z < y:
z = min
When I turned in, the feedback said:
Remarks:
⇒ Unexpected identifiers: and
More Hints:
⇒ Solutions with your approach don't usually use: <
although I tried may ways to figure it out but none of them worked.
Plz help.
Thx.
For the first reply, the system said:
Remarks:
⇒ Unexpected identifiers: and
More Hints:
⇒ Solutions with your approach don't usually use: <
Problems Detected:
⇒ Exception occurred(, TypeError('unorderable types: int() < builtin_function_or_method()',), )
⇒ Exception occurred(, TypeError('unorderable types: int() < builtin_function_or_method()',), )
⇒ Exception occurred(, TypeError('unorderable types: int() < builtin_function_or_method()',), )
⇒ Exception occurred(, TypeError('unorderable types: int() < builtin_function_or_method()',), )
⇒ min does not contain the correct value
⇒ y was modified
⇒ z was modified
For the second reply, the system said:
Remarks:
⇒ Unexpected identifiers: and, def, minist
⇒ You have to use the min variable .
⇒ You should use an assignment operator (=) in this exercise.
More Hints:
⇒ You almost certainly should be using: =
⇒ You almost certainly should be using: min
⇒ I haven't yet seen a correct solution that uses: , (comma)
I see three issues with the code as you've shown it in the question.
The first is that your indentation is a bit messed up. You probably want all your if statements to be at the same level, with the corresponding assignment statements indented under each one:
if something:
foo = bar
if something_else:
foo = baz
# etc
You could also use elif statements instead of the second and third if, since you never expect to have more than one of the conditions true at the same time.
The second issue is that you're doing your assignment statements backwards. If you want to assign the value in a variable x to a new variable named min, you should put min on the left side and x on the right:
min = x
This is part of the reason you're getting lots of confusing errors. Before your code runs, min is the name of a builtin function (which I suspect you'll learn about later in your class). When you did x = min you were replacing the old x value with a reference to the function.
The last thing is a logic issue. Your comparisons are all using the < operator to test if one value is smaller than the others. The issue is what happens if two of the values are equal. If x and y have the same value, both x < y and y < x will be False. That's not very good for your code, and so you probably want to use the <= operator instead. It tests if the left side is less than or equal to the right side. With this version, if you're using elifs, you can actually do away with the last condition and just use else, since if neither x or y is less than or equal to the other values, z must be the smallest value by process of elimination.
Anyway, here's the code with all three issues fixed:
if x <= y and x <= z:
min = x
elif y <= x and y <= z:
min = y
else: # no "z <= x and z <= y" check needed here, will always be true if reached
min = z
Maybe the tab problem.
You can try this:
if x < y and x < z:
x = min
if y < x and y < z:
y = min
if z < x and z < y:
z = min
I make a sample:
#-*- coding:utf-8 -*-
def minist(x,y,z):
if x < y and x < z:
return x
if y < x and y < z:
return y
if z < x and z < y:
return z
x = 1
y = 2
z = 3
test = -1
test = minist(x,y,z)
print(test)
This sample will print 1.
Related
I'm exercising opreators in C++ and I don't understand the output of the code bellow
int x = 21, z = 33, y = 43;
cout << (!(z < y&& x < z) || !(x = z - y)) << endl;
I wrote it with the thought to be true and I understand it as "it's not the case z is less than y and x is less than z (which is false) or it's not the case x is equal to the difference of z and y (which is true)" so I expected output 1 (=true) and I'm confused that's not the case. Can you explain me where I'm making a mistake?
edit: Thanks for the answers, it's funny how I made such trivial mistakes I actually read about.
The part that you misinterpreted:
!(x = z - y))
x = z - y is assignment. It yields -10 as result. -10 is not 0, hence negating it yields false.
Now, first part of the expression:
!(z < y&& x < z)
!(33 < 43 && 21 < 33)
!(true && true)
!(true)
false
Putting it together:
(false || false) == false
This = is an assignment operator. It assigns values.
This == is a comparison operator. It is used to compare two values.
int value = 5, value2 = 12;
if(value == value2)
{
// do something if value and value2 are EQUAL (which they are not)
}
See this link for more information on operators in C++.
I have a doubt about this code:
int i, x, z = 4, y = 1;
i = x = z += y++;
Which is the value of i? Can we know that value or not?
First of all, have you tested if this compiles at all?
If not, then why?
Unless you write some code that invokes UB, compiler, regarding basic syntax of the language, has most of the answers you'll ever need. Please test it yourself, and if it's still not clear, or behaves weirdly, then it's something worth asking.
In this case, it's valid, so let's go through it.
#include <iostream>
int main() {
int i, x, z = 4, y = 1;
i = x = z += y++;
std::cout << "i=" << i << std::endl;
}
I compiled, ran it, and here's the result:
$ g++ test.cpp -o test
$ ./test
i=5
So, what is actually going on in this line?
i = x = z += y++;
First of all, it's easier to understand when you add parentheses so it's perfectly obvious what is evaluated and when:
i = (x = (z += (y++)));
i is a result of assignment to x;
x is a result of addition assignment to z;
z is a result of z + (y post increment);
You can then work your way backwards through the list, and will arrive at the correct result:
y++ evaluates to simply y, because post increment affects only value of y, not the result of expression itself;
z is then z + y, which is 4 + 1, or 5;
intermediate expression becomes i = x = z; (or, form that's too obvious, i = (x = z);), and that means that i, just like x is 5.
Whatever you do, please don't actually write code like this, while it's easy to deconstruct, it's not something that is easy to understand at a first glance, and that, more often than not, causes confusion.
Can we know that value or not?
Yes.
Which is the value of i?
5
since you add y to i, where their values are 4 (since i gets assigned the value of z, which is 4) and 1 respectively.
y is incremented after z has been initialized. It's incremented in the following statement, as #john commented.
As #LightnessInOrbit commented, this is not good code. Forget about it and move on.
Yes the value of i is 5.
Just trace the code.
y++ post increement i,e first assign the value then increement so Y= 4. Next
z += y shorthand operation i,e.., z= z + y ,initially z=4 so 5 = 4+ 1 so Z=5
x = z i.e, x = 5 Next
i = x i.e, i = 5.
Before reading this question please consider that it is intended for use with the Z3 solver tool and it's c++ api (everything is redefined so it's not normal c++ syntax)
Can someone explain how do I mix boolean logic with integers (programing wise)?
Example:
y = (x > 10 and x < 100) //y hsould be true or false (boolean)
z = (y == true and k > 20 and k < 200)
m = (z or w) //suppose w takes true of false (boolean)
I tried with the examples given in the c++ file but I can't figure out how it works when mixing integer arithmetic and boolean.
Writing answer assuming you a beginner of c++.
May be you are looking for this.
bool y,z,m,w;
int x, k;
y = (x>10 && x<100);
z = (y == true && k > 20 && k < 200);
m = (z || w);
Let see what this line means:
y = (x>10 && x<100);
here if x is greater than 10 x>10 results true. In the same way if x is less than 100 x<100 results true. if both of them are true, the right side results true, which will be assigned to y.
|| means or.
I have two variables: x>= 0 and y binary (either 0 or 1), and I have a constant z >= 0. How can I use linear constraints to describe the following condition:
If x = z then y = 1 else y = 0.
I tried to solve this problem by defining another binary variable i and a large-enough positive constant U and adding constraints
y - U * i = 0;
x - U * (1 - i) = z;
Is this correct?
Really there are two classes of constraints that you are asking about:
If y=1, then x=z. For some large constant M, you could add the following two constraints to achieve this:
x-z <= M*(1-y)
z-x <= M*(1-y)
If y=1 then these constraints are equivalent to x-z <= 0 and z-x <= 0, meaning x=z, and if y=0, then these constraints are x-z <= M and z-x <= M, which should not be binding if we selected a sufficiently large M value.
If y=0 then x != z. Technically you could enforce this constraint by adding another binary variable q that controls whether x > z (q=1) or x < z (q=0). Then you could add the following constraints, where m is some small positive value and M is some large positive value:
x-z >= mq - M(1-q)
x-z <= Mq - m(1-q)
If q=1 then these constraints bound x-z to the range [m, M], and if q=0 then these constraints bound x-z to the range [-M, -m].
In practice when using a solver this usually will not actually ensure x != z, because small bounds violations are typically allowed. Therefore, instead of using these constraints I would suggest not adding any constraints to your model to enforce this rule. Then, if you get a final solution with y=0 and x=z, you could adjust x to take value x+epsilon or x-epsilon for some infinitesimally small value of epsilon.
So I change the conditional constraints to
if x = z then y = 0 else y = 1
Then the answer will be
x - z <= M * y;
x - z >= m * y;
where M is a large enough positive number, m is a small enough positive number.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Int y = 0
Int x = 5
y = x++
As compared to doing something like
Int y = 0
Int x = 5
y = ++x
Particularly, I'm asking how the position of the post increment generally affects the output
When the operator is before variable, it does (and supposed to when one writes own operators) perform an operation and then return result.
int x = 1 ;
int y = x++ ; // y = 1, x = 2
int z = ++x ; // x = 3, z = 3
I would suggest using temporary programs and outputs to see how things work.
Of course a good book is the best ;)
x++ increments x after the operation.
Int y = 0 Int x = 5 y = x++
Y would be equal to 5 while also setting x to equal 6.
Int y = 0 Int x = 5 y = ++x
Y would be equal to 6, X would also be equal to 6.
In the first case, y = x++, x is post-incremented. That is to say x is increased in value after its value is assigned to y.
y = 5 and x = 6.
In the second case, y = ++x, x is pre-incremented. x is increased in value before its value is assigned to y.
y = 6 and x = 6
If the prefix/postfix operator is part of an expression or function evaluation, it determines the order in which the variable is modified, either before the evaluation (prefix) or after the evaluation (postfix).
In this first case,y is assigned before x is incremented.
int y = 0;
int x = 5;
y = x++;
In this second case, x is incremented first, then y is assigned.
int y = 0;
int x = 5;
y = ++x;
Beware: if used in an array index on both sides of an equation, the behavior may be undefined.
pre-increment (++X): means that the value of X will be incremented before it gets assigned.
in your example, value of Y will be 6 because X gets incremented first (5+1) then gets assigned to Y.
post-increment (X++): means that the value of X will be incremented after it gets assigned.
in your example, value of Y will be 5 because X gets incremented after it is assigned to Y.
You can compare these as:
++X = in-place increment
X++ = increment happens in a temporary location (Temp=5+1) which gets assigned to X (X=Temp) after Y is set to its current value (5).
Y = X++ can be represented as:
> Temp = X+1
> Y = X
> X = Temp