Check if a string has the same amount of 'x' and 'o'. The method must return a Boolean value and must not be case-sensitive. The string can contain any char.
Thank you for that! I had not noticed it. Yes, there are some awesome string methods in Python, but I didn't use any of them here because I wanted to finish the count(s) in one pass of the string. Doing a .lower() and then counting might involve two passes of the same string.
I suggested it to reduce your conditions, lowering the string before checking would have allowed you to only check for o and x for example. This way you would have one pass on the string and a more concise condition :)
Do you mean lowering the string before the loop or lowering the character in the loop before checking whether it is an 'x' or 'o'?
Lowering the string before the loop would involve one complete pass of the string. And then you would have to iterate through the string, again, to count the frequencies.
Lowering just the character inside the loop, is just equivalent to the if condition, right? I am probably saving on a function call by not calling .lower() on the character.
fnmain(){println!("{} the same amount",ifhas_same_amount("$XxkkLLOoox-","x","o"){"Has"}else{"Not has"})}fnhas_same_amount(str:&str,ch1:&str,ch2:&str)->bool{letstr=str.to_lowercase();str.matches(ch1).count()==str.matches(ch2).count()}
funcIsBalanced(dicmap[rune]int,sstring)bool{b:=0for_,r:=rangestrings.ToLower(s){i,ok:=dic[r]ifok==false{continue}b+=i}returnb==0}funcmain(){//because the world is bigger than Latinvarstep=map[rune]int{'x':1,'🗴':1,'o':-1,'ອ':-1}table:=map[string]bool{"":true,//0x == 0o"x0x0":true,"ອXອXອ":false,"H🗴H🗴H(╯° °)╯︵ ┻━┻)HoHo":true,}forinput,exp:=rangetable{fmt.Printf("'%s', exp: '%v', got: '%v'\n",input,exp,IsBalanced(step,input))}}
Python solution -
Thank you @Pierre for pointing out the error.
Hey @ganeshtata !
I think you missed an uppercase in your
elif
:)instead of
To dig a little deeper, I suggest you to see some very useful string methods in Python !
Thank you for that! I had not noticed it. Yes, there are some awesome string methods in Python, but I didn't use any of them here because I wanted to finish the count(s) in one pass of the string. Doing a
.lower()
and then counting might involve two passes of the same string.I suggested it to reduce your conditions, lowering the string before checking would have allowed you to only check for
o
andx
for example. This way you would have one pass on the string and a more concise condition :)Do you mean lowering the string before the loop or lowering the character in the loop before checking whether it is an 'x' or 'o'?
.lower()
on the character.Oh right, nevermind !
In Haskell, with what I thought was kind of a neat method:
As a javascript one-liner:
A simple solution with Rust.
Python to the rescue !
output
Time for a Go kata you say?
play.golang.org/p/n8q9BYrsW4a
Rust Solution.
Ruby Solution!