Lists compared in a if function - python-3.8

enter image description here
>>> if LIST[2] == "a" or "A":
print("Yes")
Yes
>>> LIST[2]
'l'
Can someone please explain this?
I'm trying to compare items in a list to initiate a function. But Python is telling me that "l" == "a" or "A"
Is it something to do with the or in the code.

You're correct that it has something to do with the or. I assume you want to see if LIST[2] is equal to either "a" or "A". The correct way to do this is
if LIST[2] == "a" or LIST[2] == "A" or if LIST[2] in "aA"
So here's whats actually happening in your code:
if LIST[2] == "a" or "A": is the same as if (LIST[2] == "a") or "A". (LIST[2] == "a") is false. But or with strings is still a valid operation! x or "A" will evaluate to x if x is true or "A" if x is false. Since (LIST[2] == "a") is false, the or gives "A" resulting in if "A":. Python considers all non-empty strings to be true, and the empty string "" to be false. So, "A" is considered true, and the statement inside the if is executed.

Related

How to change every letter in a word using dictionary?

So basically the title I want to change the word letters to another word but it says I have to specify the parameter
fun main() {
val dictionary = mapOf( "c" to "b", "a" to "d" , "r" to "e"//here is the letters i want to change
)
val letters = "car"//the word i want to change
for (me in letters){
println(dictionary[me])
}
}
i want for "car" to be "bde"
Notice that your map has strings as keys, because you used string literals in these places:
val dictionary = mapOf( "c" to "b", "a" to "d" , "r" to "e")
*** *** ***
However, when you access the map, you are accessing it using me, which is a Char you got from the string letters. This causes the error.
I would suggest that you change the map to use Char keys instead. Change the string literals to character literals:
val dictionary = mapOf( 'c' to "b", 'a' to "d" , 'r' to "e")
*** *** ***
Notice that the letters are now surrounded with single quotes.
Though it is not required to solve this particular problem, you can also change the map's values to character literals too.
After that, you should use print instead of println to print the map values out, so that they are all printed on the same line:
for (me in letters){
print(dictionary[me] ?: me.toString())
}
Note that I added the ?: me.toString() part so that if no replacement was found in the map, the original letter would be printed.
If you want a single string as the result, rather than having it printed out,
val result = letters.map { dictionary[it] ?: it.toString() }.joinToString("")

What is the concise way to write following code segment in Kotlin

I am learning Kotlin and I have written following code snippets in Kotlin.
Rather than using if-else condition is there any concise way to write following code?
fun test(a: int, b: int): Coding {
return Coding().apply{
if(a>b){
comment = "first value greater than second value"
value = a
}else{
comment = "second value greater than or equal to first value"
value = b
}
}
}
comment = if (a > b) "first value grater than second value" else "second value grater than or equal to first value"
value = max(a, b)
You can replace with a when expression as below;
when {
a > b -> {
comment = "first value grater than second value"
value = a
}
else -> {
comment = "second value grater than or equal to first value"
value = b
}
}
But as per docs also, if the condition is a simple binary condition, use if statements. If you have to handle more than three conditions, prefer when. More info https://kotlinlang.org/docs/coding-conventions.html#if-versus-when
Based on #Laksitha and #Twistleton answers a combination of both suggestions:
val (value, comment) = when {
a > b -> a to "first value greater than second value"
else -> b to "second value greater than or equal to first value"
}
As a Kotlin one-liner with an if expression:
val a = 5
val b = 9
val (value, comment) = if (a > b) Pair(a, "first value greater than second value") else Pair(b, "second value greater than or equal to first value")
Pair represents a generic pair of two values.

Understanding the match operator

Hi I am confused about the usage of the match operator. I have come across a snippet of code that looks nothing like the explanation on the documentation: https://docs.mulesoft.com/mule-runtime/3.9/dataweave-operators#match
%function testMatch(key)
(key match {
x when x is :null -> false,
x when x == "A" -> true,
x when x == "B" -> false,
x when x == "J" -> true,
x when x == "K" -> false,
x when x == "L" -> true,
default -> false
})
Please help understand the meaning of this syntax for match
Great question! The match keyword serves two purposes in DataWeave, and it depends on its placement. Match is either used for regex, or pattern matching.
match for Regex
If match has a string on the left-hand side (lhs) and a regex on the rhs, it will operate according to the following docs. Basically, it's doing regex matching:
Match returns an array that contains the entire matching expression, followed by all of the capture groups that match the provided regex.
match for Pattern Matching
If match has anything that evaluates to a value (i.e., does not evaluate to a function) on the lhs and an open bracket on the rhs, match is now doing pattern matching. You can find that docs for that here. I cover this pretty extensively in the talk I did, you can find the slides for that here.
For the example you provided (nice formatting, btw):
%function testMatch(key)
(key match {
x when x is :null -> false,
x when x == "A" -> true,
x when x == "B" -> false,
x when x == "J" -> true,
x when x == "K" -> false,
x when x == "L" -> true,
default -> false
})
match is checking if its input, x, is null, A, B, J, K, or L. If it matches any of those, DW will evaluate what's on the rhs of the arrow, and return immediately. If nothing matches, it will return what is on the rhs of the arrow for default.

Nested pair array in kotlin

I want to use nested pair in kotlin, such as "a" to {"b" to "c"}
I have tried :
"teachers" to {"a" to "c"; "c" to "d"}
but when I debug this, the data type is:
(teachers, () -> kotlin.Pair<kotlin.String, kotlin.String>)
how to use this?
if don't use
"a" to mapOf("a" to "b"...)
Is it possible?
{ A -> B } is an anonymous function whose parameter is A and body is B.
{ B } is a short form of { () -> B }.
In addition,
A; B
is the same as
A
B
Therefore {"a" to "c"; "c" to "d"} means a function whose parameter list is () (zero parameters) and body is
Pair("a", "c")
Pair("c", "d")
which is equivalent to something like ::noName with
fun noName() {
Pair("a", "c")
return Pair("c", "d")
}
Anyway, what are the braces in your code? They do not mean "pairs." I think you meant to represent a map (or dictionary) in python, but PAIRS ARE NOT MAPS and vice versa.
Nested pair is something like this: "a" to ("b" to "c") which is equivalent to Pair("a", Pair("b", "c"))
If you want to make a map in Kotlin, you should use function mapOf().
If you want to make an array of pairs in Kotlin, you can do so like arrayOf("a" to "b", "c" to "d").
Also, arrays are not maps and vice versa.
This is an example of (a pair of (a string) and (an array of (pairs of strings))).
"a" to arrayOf("b" to "c", "d" to "e")

Why does Perl 6's Map give me a value in one case and a list in another?

I'm playing with Map and I get a result I don't understand.
First, I construct the Map. No big whoop:
> my $m = Map.new: '1' => :1st, '2' => :2nd;
Map.new(("1" => :st(1),"2" => :nd(2)))
I access a single element by the literal key and get back a Pair:
> $m<1>.^name
Pair
> $m<<1>>.^name
Pair
That's all fine.
If I try it with the key in a variable, I get back a List instead:
> my $n = 1
1
> $m<<$n>>.^name
List
That list has the right value, but why do I get a List in that case and not the $m<<1>> case?
And, once I have the list, I seem unable to chain another subscript to it:
> $m<<$n>>.[0]
===SORRY!=== Error while compiling:
Unable to parse quote-words subscript; couldn't find right double-angle quote
at line 2
When you access an associative value like this, the compiler can tell that it need only ever return one value.
$m< 1 >
$m<< 1 >>
In Perl 6, a singular value will in many cases behave just like a list of one value.
42.elems == 1 # True
42.[0] =:= 42 # True
In the following case, the compiler can't immediately tell that it will only produce one value:
my $n = 1;
$m<< $n >>;
As it could produce 2 values:
my $o = '1 2';
$m<< $o >>;
If you want the string to be a single key, you have to use quotation marks.
$m<< "$o" >>
Or use the more appropriate {}
$m{ $n }
The $m<1> is just a combination of two features.
Quotewords: ( qw<> and qqww<<>> )
< a b c > eqv ("a", "b", "c")
< "a b" c > eqv (「"a」, 「b"」, "c") # three strings
<< a b c >> eqv ("a", "b", "c")
<< "a b" c >> eqv ("a b", "c") # two strings
Associative indexing:
%h< a b c > eqv %h{ < a b c > }
%h<< "a b" c >> eqv %h{ << "a b" c >> }
Also I now get back different values.
$m< 1 >.WHAT =:= Pair
$m<< 1 >>.WHAT =:= Pair
$m<< $n >>.WHAT =:= Pair # different
$m<< $o >>.WHAT =:= List
The reason $m<<$n>>.[0] doesn't work is the compiler thinks you are using a hyper postfix >>.[0].
There are a couple ways of working around that.
Actually using a hyper postfix
$m<<$n>>>>.[0]
$m<<$n>>».[0]
Use an unspace. (can never be inside of an operator so will split them up)
$m<<$n>>\.[0]
$m<<$n>>\ .[0]
I think this is a bug, as it doesn't make much sense to be matching a hyper postfix inside of a quotewords statement.
(It doesn't affect $m<<1>>.elems)