Skip to content

Commit 30937d1

Browse files
committed
feat: resovled
1 parent c2a26e2 commit 30937d1

File tree

1 file changed

+69
-3
lines changed
  • ambiguous_coordinates/src

1 file changed

+69
-3
lines changed

Diff for: ambiguous_coordinates/src/lib.rs

+69-3
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,80 @@ pub struct Solution {}
66

77
impl Solution {
88
pub fn ambiguous_coordinates(s: String) -> Vec<String> {
9-
vec![]
9+
let mut res = vec![];
10+
for i in 2..s.len() - 1 {
11+
let left = &s[1..i];
12+
let right = &s[i..s.len() - 1];
13+
for l in Self::create_possible(left).iter() {
14+
for r in Self::create_possible(right).iter() {
15+
res.push(String::new() + "(" + l + ", " + r + ")");
16+
}
17+
}
18+
}
19+
res
20+
}
21+
22+
fn create_possible(s: &str) -> Vec<String> {
23+
let mut possible = vec![];
24+
for i in 1..=s.len() {
25+
let left = &s[..i];
26+
let right = &s[i..];
27+
if (!left.starts_with("0") || left.len() == 1) && !right.ends_with("0") {
28+
possible
29+
.push(String::new() + left + if right.len() != 0 { "." } else { "" } + right)
30+
}
31+
}
32+
possible
1033
}
1134
}
1235

1336
#[cfg(test)]
1437
mod tests {
38+
use super::*;
39+
40+
fn assert_eq(input: &str, output: &Vec<&str>) {
41+
let mut expected = output
42+
.iter()
43+
.map(|x| x.to_string())
44+
.collect::<Vec<String>>();
45+
expected.sort();
46+
let mut result = Solution::ambiguous_coordinates(input.to_string());
47+
result.sort();
48+
assert_eq!(result, expected);
49+
}
50+
51+
#[test]
52+
fn example_1() {
53+
let input = "(123)";
54+
let output = vec!["(1, 23)", "(12, 3)", "(1.2, 3)", "(1, 2.3)"];
55+
assert_eq(input, &output);
56+
}
57+
58+
#[test]
59+
fn example_2() {
60+
let input = "(00011)";
61+
let output = vec!["(0.001, 1)", "(0, 0.011)"];
62+
assert_eq(input, &output);
63+
}
64+
65+
#[test]
66+
fn example_3() {
67+
let input = "(0123)";
68+
let output = vec![
69+
"(0, 123)",
70+
"(0, 12.3)",
71+
"(0, 1.23)",
72+
"(0.1, 23)",
73+
"(0.1, 2.3)",
74+
"(0.12, 3)",
75+
];
76+
assert_eq(input, &output);
77+
}
78+
1579
#[test]
16-
fn it_works() {
17-
assert_eq!(2 + 2, 4);
80+
fn example_4() {
81+
let input = "(100)";
82+
let output = vec!["(10, 0)"];
83+
assert_eq(input, &output);
1884
}
1985
}

0 commit comments

Comments
 (0)