I recently tried to do the online programming challenge Texture Analysis on open.kattis.com; however, despite my answers for the sample cases being correct in my personal tests, I keep getting the second sample case wrong, the status of the submission says: "wrong answer". What am I doing wrong with my code?
Link to the kattis problem. My code:
use std::io::{self, BufRead};
fn main() {
let mut line: i8 = 0;
let stdin = io::stdin();
for input in stdin.lock().lines().map(|l| l.unwrap()) {
line += 1;
if input == "END" {
break;
}
let pattern = input
.split("*")
.filter(|&i| i.contains("."))
.collect::<Vec<&str>>();
let c = pattern
.iter()
.all(|j| j.len() == pattern[0].len());
if (c && pattern.len() != 1 ) || pattern.len() == 0 {
println!("{} EVEN", line);
} else {
println!("{} NOT EVEN", line);
}
}
}
Fixed commented example:
use std::io::{self, BufRead};
fn main() {
// line needs to store values up to 2000
// it cannot be an i8, must be an u16 at minimum
// Rust's default for integer literals is i32 which we use here
let mut line = 0;
let stdin = io::stdin();
for input in stdin.lock().lines().map(|l| l.unwrap()) {
line += 1;
if input == "END" {
break;
}
// not possible to construct NON-EVEN input in <= 3 chars
if input.len() <= 3 {
println!("{} EVEN", line);
continue;
}
// ignore first & last * when splitting input
let pattern = &input[1..input.len()-1]
.split("*")
// do not filter out empty strings
.collect::<Vec<&str>>();
let space_len = pattern[0].len();
let is_even = pattern
.iter()
.all(|j| j.len() == space_len);
if is_even {
println!("{} EVEN", line);
} else {
println!("{} NOT EVEN", line);
}
}
}