Python 3: While loop not returning False - if-statement

I'm making a 3 in a row game for practice using the concepts I have learned in class so far: while loops, booleans, and if-statements.
For some reason, my function doesn't seem to be returning False even though I give an argument in which it should. Instead I get a 'executing command, please wait for result' message, where nothing comes up after that. Initially I thought it was caught in an infinite loop somewhere, but I don't think it should as I gave limits to all my variables and added the +=1 at the end for all of the while loops.
Any help will be much appreciated!
Here is a snippet of my code below:
def is_winner(grid):
x = 0
y = 0
while x <=3:
x +=1
while y <= 4:
if grid[x][y] == grid[x + 1][y] ==grid[x + 2][y]:
y += 1
return True
while x <= 3:
x +=1
while y <= 3:
if grid[x][y] == grid[x + 1][y - 1] == grid[x + 2][y - 2]:
y +=1
return True
else:
return False

You only ever increment y if the if statements are True. If they never are, you entered into an infinite loop:
while x <=3:
while y <= 4:
x +=1
if grid[x][y] == grid[x + 1][y] ==grid[x + 2][y]]:
y += 1
Your inner while loop never completes, so your outer while loop is never tested.
Change this to:
while x <=3 and y <= 4:
x +=1
if grid[x][y] == grid[x + 1][y] ==grid[x + 2][y]]:
y += 1

Related

Why is the output 0 (=false)?

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++.

Fast way of finding all numbers X that sum with themselves with one digit removed to get N?

I'm working on an assignment that gives an integer N and tasks us to find all possible combinations of X, Y such that X + Y = N and Y = X with one digit removed. For example, 302 would have the following solutions:
251 + 51 = 302
275 + 27 = 302
276 + 26 = 302
281 + 21 = 302
301 + 01 = 302
My code to accomplish this can find all of the correct answers, but it runs too slowly for very large numbers (it takes roughly 8 seconds for the largest possible number, 10^9, when I would like for the entire algorithm of up to 100 of these cases to complete in under 3 seconds).
Here's some code describing my current solution:
//Only need to consider cases where x > y.
for(int x = n * 0.5; x <= n; x++)
{
//Only considers cases where y's rightmost digit could align with x.
int y = n - x,
y_rightmost = y % 10;
if(y_rightmost == x % 10 || y_rightmost == (x % 100) / 10)
{
//Determines the number of digits in x and y without division. places[] = {1, 10, 100, 1000, ... 1000000000}
int x_numDigits = 0,
y_numDigits = 0;
while(x >= places[x_numDigits])
{
if(y >= places[x_numDigits])
y_numDigits++;
x_numDigits++;
}
//y must have less digits than x to be a possible solution.
if(y_numDigits < x_numDigits)
{
if(func(x, y))
{
//x and y are a solution.
}
}
}
Where func is a function to determine if x and y only have a one digit difference. Here's my current method for calculating that:
bool func(int x, int y)
{
int diff = 0;
while(y > 0)
{
if(x % 10 != y % 10)
{
//If the rightmost digits do not match, move x to the left once and check again.
x /= 10;
diff++;
if(diff > 1)
return false;
}
else
{
//If they matched, both move to the next digit.
x /= 10;
y /= 10;
}
}
//If the last digit in x is the only difference or x is composed of 0's led by 1 number, then x, y is a solution.
if((x < 10 && diff == 0) || (x % 10 == 0))
return true;
else
return false;
}
This is the fastest solution that I've been able to find so far (other methods I tried included converting X and Y into strings and using a custom subsequence function, along with dividing X into a prefix and suffix without each digit from the right to the left and seeing if any of these summed to Y, but neither worked as quickly). However, it still doesn't scale as well as I need it to with larger numbers, and I'm struggling to think of any other ways to optimize the code or underlying mathematical reasoning. Any advice would be greatly appreciated.
Consider solving a simpler solution first:
Finding X and Y such that X + Y = N
In pseudo-code you steps should look like this:
loop through the array and with every given item do the next:
add this number to Set and check whether there is N - item
This will work as O(n) complexity for unique array.
So improve it to work with duplicated numbers by looping through an array first and adding counter of duplicates for every number. Use some kind of Dictionary for c++ or extend Set. And every time you find the necessary number check for counter.
After doing that you will just have to write this "digit check" function and apply it when finding the value in Set.

Setting a variable equal to 0 or 1 using an IF statement in SAS

I've been searching the documentation and experimenting myself but I can't figure out whether or not you can set a variable using an IF statement. For instance
x = if y >= 1;
would set x equal to 1 if y is greater than or equal to 1 and 0 otherwise. Is this possible in SAS? Do you have to do
if y >= 1 then x = 1; else x = 0;
Almost there... just remove the if :
x = (y >= 1) ;
Remember, all evaluations equate to either true (1) or false (0), so you can simplify lots of code in this manner, especially with the addition of ifn and ifc.
x = (y >= 1) ;
z = (index(name,'Dave') > 0) ;
q = ifc(x and not z,'This','That') ;
Or mixing boolean & regular algebra :
points = ((product = 'SHOES') * 100 * sale_price) + ((product = 'HATS') * 200 * sale_price) ;

Tracking number of recursive calls of Fibonacci number without using global variables in Python 2.7

`
def fib(x):
"""assume x an int >= 0
Returns Fibnacci of x"""
global numcalls
numcalls += 1
if x == 0 or x == 1:
return 1
else:
return fib(x-1) + fib(x-2)
def testfib(n):
for i in range(n+1):
global numcalls
numcalls = 0
print 'fib of', i, '=', fib(i)
print 'fib called', numcalls,'times.'
This is the version of using the global variable to track the numbers of recursive calls of Fibonacci,my question is how to write a version without using global variables?
`
You should change fib so that it returns a tuple of (fib(n), numcalls).
def fib(x):
"""assume x an int >= 0
Returns tuple of (Fibonacci of x, number of calls)"""
if x == 0 or x == 1:
return 1, 1
else:
f1, n1 = fib(x-1)
f2, n2 = fib(x-2)
return f1 + f2, n1 + n2 + 1

Mixing integer arithmetic with boolean - Z3 prover

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.