Why x = y = z does not return false - basic

I have a piece of code that compares three values each defaulting to zero but it does not return false:
x = 0 : y = 0 : z = 0
IF x = y = z THEN PRINT "false"
and I cannot figure out why?

In older dialects of BASIC the following assigned all variables to zero:
a = b = c = 0
but after that each variable has to be set separately:
a = 0 : b = 0 : c = 0
then instead of comparing all values to zero, they were compared in boolean fashion between each: a=b=c would calculate a=b first then its value compared to c..

Related

What is the meaning of "range( i + 1 )"?

I have a question ,what is the meaning of "range( i + 1 )" below ,if I want to show the output which has xyyzzz?
a = ("x", "y", "z")
for i in range(len(a)):
for j in range( i + 1):
print(a[i])
output: x
y
y
z
z
z
I tried to explain this to you with values
i = 0 ---> j = range(1) = 0 : a[0] = x
--------------------------------
i = 1 ----> j =range(2) = (0,1) : a[1] = y
: a[1] = y
-----------------------------------------
i = 2 ----> j = range(3) = (0,1,2) : a[2] = z
: a[2] = z
: a[2] = z
range(n) gives you values from 0 to n-1.
If you are unfamiliar with Python currently, this can be represented in different ways:
In mathematical interval notation, you can represent this as [0,n) . The left value is included, and the right one is excluded.
Taking len(a) to be 3, the above for loops would be written in C as:
for (int i = 0 ; i < 3 ; ++i){
for (int j = 0 ; j <= i ; ++j){ // note the <= . We can also do j < (i+1)
...
}
}
Your code first calculates len(a), which is 3, since this is a container with 3 elements (this specific container is called a tuple). The first for loop goes from 0 to 2, while the second for loop goes from 0 to wherever the counter of the outer for loop is at (less than or equal to). This causes the first value to be printed just once, next twice, and the last one thrice.
(If you want to test your understanding further, try to figure out what would be printed if we had print(a[j]) inside the loops rather than a[i].)
range() is a versatile and powerful thing in Python, it can do much more than just give you values from 0 to n-1. Do read about it if you intend to use Python.

Nested loop in python3

How does a nested loop work?
I tried to make a triangle this is my code:
x = 0
string = ""
while x <= 5:
y = x
while y > 0:
string = string + "*"
y = y - 1
string = string + "\n"
x = x + 1
print(string)
But it came out like this And this is the output:
*
**
***
****
*****
How does a nested loop work in python?
You must debug your code to understand how the nested loop works
for your code, the nested loop will work as following,
for each x where x= 5 the while loop of y will run for 5 times,
then again,
when x=4 the while loop of y will run for 4 times,
and as per this the while loop of x will have 5 iterations and the while loop of y will have about 15 iterations equivalent to number of stars in your pyramid or pattern.

Why is a part of my where clause not used in my conditional function?

I've been practicing Haskell as part of my course at my university, and I made the following function while experimenting with local definitions :
fac1 x | x == 0 = zero
| x == 1 = one
| otherwise = x * fac1 (x-1)
where zero = 0
one = 1
I would expect any call to fac1 result in zero because when x==0, it will multiply by 0. However, it gives me the correct number.
Conversely, writing one = 0 instead of one = 1 results in my results being 0. I would expect the same sort of behavior for zero, but changing the value of it does nothing. I feel like it should be happening since I clearly included a x==0 condition. The x==1 is evaluated, why not x==0?
Can someone explain what error I made?
Your recursion stops on x = 1, not x = 0.
Let's evaluate fac1 2 by hand:
fac1 2
= 2 * fac1 (2 - 1) -- 2 is neither 0 nor 1, so we take the "otherwise" case
= 2 * fac1 1 -- evaluate 2 - 1
= 2 * one -- x == 1 holds, so we return "one"
= 2 * 1 -- one is 1
= 2 -- done

Python - Find a Number in a String List and return String+Number

Need this:
a_1 = 10
a_2 = 15
a_3 = 4
a_4 = 25
x = 3
a_(x) = 4
I tried this:
list = ["a_1", "a_2", "a_3", "a_4"]
x = 3
matching = [s for s in list if (x) in s]
TypeError: 'in ' requires string as left operand, not int
So:
list = ["a_1", "a_2", "a_3", "a_4"]
x = "3"
matching = [s for s in list if (x) in s]
matching = 'a_3'
It worked, but is there a better way?
I really did not know how to ask, all I needed was this:
eval("a_" + str(x))
thx all

Compare 1 string with a cell array of strings with indexes (Matlab)

I have 1 string and 1 cell array of srings :
F = 'ABCD'
R = {'ACBD','CDAB','CABD'};
I would like to compare the string F with all of the strings in R as follows: F(1)='A' and R{1}(1)='A', we will count 1 ( because they have the same value 'A') , F(2)='B' and R{1}(2)='C' we will count 0 ( because they have different values)...and like that until the end of all strings.
We will get same = 2 , dif = 2 for this 'ABCD' and 'ACBD'.
How can I compare F with all the elements in R in the above rule and get the total(same) and total(dif) ?
Assuming all strings in R has the same length as F you can use cellfun:
same = cellfun( #(r) sum(F==r), R )
Results with
2 0 1
That is, the same value per string in R. If you want dif:
dif = numel(F)-same;
If you want the totals:
tot_same = sum(same);
tot_dif = sum(dif);

Resources