[Code Challenge] Are they opposite?
Hello, community
To celebrate that this week is "International Week of Deaf People 2022" , https://wfdeaf.org/iwdeaf2022/
Could we do a small challenge?
The statement is:
Task
Give you two strings: s1 and s2. If they are opposite, return true; otherwise, return false. Note: The result should be a boolean value, instead of a string.
The opposite means: All letters of the two strings are the same, but the case is opposite. you can assume that the string only contains letters or it's a empty string. Also take note of the edge case - if both strings are empty then you should return false/False.
Examples (input -> output)
"ab","AB" -> true "aB","Ab" -> true "aBcd","AbCD" -> true "AB","Ab" -> false "","" -> false
🧏🧑💻📍 This programming exercise has been taken from: https://www.codewars.com/kata/574b1916a3ebd6e4fa0012e7
We hope you enjoy this challenge
Thanks for your time
Comments
ClassMethod Opposite(s1 As %String = "", s2 As %String = "") As %Boolean
{
write s1_", "_s2_" --> "
if (($l(s1) '= $l(s2)) || (s1 = "") || (s1'?.1A.A) || (s2'?.1A.A)) { return 0 }
for cnt=1:1:$l(s1) {
if (($zabs($a($e(s1,cnt)) - $a($e(s2,cnt))) '= 32)) { return 0 }
}
return 1
}The spirit of the time... we have to save water, energy, etc. Maybe we should save bytes too...
// instead of this lineif (($zabs($a($e(s1,cnt)) - $a($e(s2,cnt))) '= 32)) { return0 }
// try this onereturn:$zabs($a(s1,cnt)-$a(s2,cnt))-320That's is easy for the ASCII character set... (you didn't give any restrictions)
ClassMethod CheckOpposite(s1,s2) As%Boolean
{
q$zb(s1," ",6)=s2
}