Xs and Os, Nobody Knows (Code Challenge)
- Page Owner: Not Set
- Last Reviewed: 2019-08-19
For the first coding challenge, I picked a fairly simple one (https://edabit.com/challenge/RG5NJWDa7pZGFkhTA)
Create a function that takes a string, checks if it has the same number of x's and o's and returns either true or false.
If you're using JS, and name your function "XO", here's some test cases you can use
XO("ooxx") === true);
XO("xooxx") === false);
XO("ooxXm") === true);
XO("zpzpzpp") === true);
XO("zzoo") === false);
XO("Xo") === true);
XO("x") === false);
XO("o") === false);
XO("xxxoo") === false);
XO("") === true);
To Add a spoiler to your code use Prefix it with >!
Additional Posts
Here's my solution in python!
def XO(input):
num_of_x = 0
num_of_o = 0
for let in input:
if let == 'x' or let == 'X':
num_of_x = num_of_x + 1
if let == 'o' or let == 'O':
num_of_o = num_of_o + 1
return num_of_o == num_of_x
print(XO("ooxx"))
print(XO("xooxx"))
print(XO("ooxXm"))
print(XO("zpzpzpp"))
print(XO("zzoo"))
print(XO("Xo"))
print(XO("x"))
print(XO("o"))
print(XO("xxxoo"))
print(XO(""))
Comments
- is
leta keyword (or otherwise special) in Python? - @BobDavidson Yes, it is. I was not aware of that before I began and I actually used it as a variable name. It was short for 'letter' in this case. I then realized you can use it as a way to define locally scoped variables for subroutines
Written C# because thats all that you need for anything in the world.
void Main()
{
XO("ooxx").Dump();
XO("xooxx").Dump();
XO("ooxXm").Dump();
XO("zpzpzpp").Dump();
XO("zzoo").Dump();
XO("Xo").Dump();
XO("x").Dump();
XO("o").Dump();
XO("xxxoo").Dump();
XO("").Dump();
}
public bool XO(string input)
{
int x = 0;
int o = 0;
foreach(var character in input.ToLower()){
if(character == 'x'){
x++;
}
else if(character == 'o'){
o++;
}
}
return x == o;
}
Comments
- Bonus points for posting the entire LINQPad script.
I used JS and did mine in code golf style.
XO = a => (a.match(/x/gi)||[]).length == (a.match(/o/gi)||[]).length
console.log(XO("ooxx") === true);
console.log(XO("xooxx") === false);
console.log(XO("ooxXm") === true);
console.log(XO("zpzpzpp") === true);
console.log(XO("zzoo") === false);
console.log(XO("Xo") === true);
console.log(XO("x") === false);
console.log(XO("o") === false);
console.log(XO("xxxoo") === false);
console.log(XO("") === true);
Comments
- I would look at this and have no idea what is happening. :(
- Nice
Okay, here it is in Rust. The lack of case sensitivity threw me for a loop. Let me know if I can make it even better:
fn main() {
xo("ooxx");
xo("xooxx");
xo("ooxXm");
xo("zpzpzpp");
xo("zzoo");
xo("Xo");
xo("x");
xo("o");
xo("xxxoo");
xo("");
}
fn xo(string: &str) {
let lower_string = string.to_lowercase();
let x_count = lower_string.matches("x").count();
let o_count = lower_string.matches("o").count();
let comp = x_count == o_count;
println!("{}", comp);
}
Comments
- I like this. It's very clean and obvious what it's doing. One question, though. Is there a reason for the
_prefix on_lower_string? I think the_prefix tells the compiler not to worry that you're not using a value, but you are using the value, so it might not be necessary? - @BobDavidson When I built the code the compiler said I should use the underscore at the beginning. I just removed the underscore and it's fine with it so I've removed it. Make up your mind, compiler.
Here's my take in Rust. File this one in the "over-engineering" pile, but I do love me some match.
pub fn execute() {
let example_data = [
("ooxx", true),
("xooxx", false),
("ooxXm", true),
("zpzpzpp", true),
("zzoo", false),
("Xo", true),
("x", false),
("o", false),
("xxxoo", false),
("", true)
];
for (test_value, expected_result) in &example_data {
let actual_result = xo(test_value);
if actual_result != expected_result.clone() {
println!("Failed: {}. Expected: {}", test_value, actual_result);
panic!();
}
}
println!("Tests Passed");
}
fn xo(value : &str) -> bool {
return value.chars().map(|c| match c {
'o' | 'O' => 1,
'x' | 'X' => -1,
_ => 0
}).sum::<i32>() == 0;
}
Comments
- I like this use of math in the map.
- Arg, that's pretty much what I tried to do but I couldn't figure it out. Nice.
I tried to code golf this in python and got the function down to 88 chars (note python 2.7 allows print without parens and its one letter shorter than 'return' 8) )
def X(p):
x=o=0
for l in p.lower():
if l=='x':x+=1
if l=='o':o+=1
print o==x
X("ooxx")
X("xooxx")
X("ooxXm")
X("zpzpzpp")
X("zzoo")
X("Xo")
X("x")
X("o")
X("xxxoo")
X("")
Here's my take on the challenge in JS. Thanks to John for suggesting I apply a bit of what I learned at my conference. I also made a simple UI for fun to test it out:
function exsAndOhs(string) {
let characters = Array.from(string);
let lowercaseCharacters = characters.map(character => {
return character.toLowerCase();
});
let numberOfExs = lowercaseCharacters.filter(x => x === 'x').length;
let numberOfOhs = lowercaseCharacters.filter(o => o === 'o').length;
return numberOfExs === numberOfOhs;
}
Comments
- I like the UI :)
C#
public bool XO(string value)
{
return value.ToLower().Count(c => c == 'x') == value.ToLower().Count(c => c == 'o');
}
I decided to spice it up and make capitol letters worth twice as much as lower case. I know, wild right?
public bool weightedXO(string input)
{
int sum = 0;
foreach(var i in input){
if(i == 'x'){
sum += 1;
}
else if(i == 'X'){
sum += 2;
}
else if(i == 'o'){
sum -= 1;
}
else if (i == 'O'){
sum -= 2;
}
}
return sum == 0;
}
So this way, xXoo isn't equal. But xXooo would be.
Comments
- +1 for spice!