Skip to content

Commit f157fc7

Browse files
committed
Test practice-b-*
1 parent 65632fe commit f157fc7

File tree

9 files changed

+305
-91
lines changed

9 files changed

+305
-91
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ jobs:
5757
- name: Checkout
5858
uses: actions/checkout@v1
5959

60+
- name: setup-python
61+
uses: actions/setup-python@v1
62+
with:
63+
python-version: '3.8'
64+
6065
- name: rust-toolchain
6166
uses: actions-rs/toolchain@v1
6267
with:

examples/practice-b-naive.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use maplit::hashset;
44

5-
use std::io;
5+
use std::{io, str};
66

77
fn main() {
88
fn read_line() -> String {
@@ -28,7 +28,7 @@ fn main() {
2828
5 => on_5(query),
2929
_ => unreachable!(),
3030
};
31-
println!("! {}", String::from_utf8(ans).unwrap());
31+
println!("! {}", str::from_utf8(&ans).unwrap());
3232
}
3333

3434
fn on_26(mut query: impl FnMut(u8, u8) -> bool) -> Vec<u8> {

examples/practice-b-proconio.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use maplit::hashset;
44
use proconio::input;
55
use proconio::source::line::LineSource;
66

7-
use std::io;
7+
use std::{io, str};
88

99
fn main() {
1010
let stdin = io::stdin();
@@ -29,7 +29,7 @@ fn main() {
2929
5 => on_5(query),
3030
_ => unreachable!(),
3131
};
32-
println!("! {}", String::from_utf8(ans).unwrap());
32+
println!("! {}", str::from_utf8(&ans).unwrap());
3333
}
3434

3535
fn on_26(mut query: impl FnMut(u8, u8) -> bool) -> Vec<u8> {
@@ -100,6 +100,8 @@ mod tests {
100100
let wr = balls.iter().position(|&b| b == r).unwrap();
101101
wl < wr
102102
});
103+
let ans = str::from_utf8(&ans).unwrap();
104+
let balls = str::from_utf8(&balls).unwrap();
103105
assert_eq!(ans, balls);
104106
assert!(queries <= 7);
105107
}

examples/practice-b-text-io.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use maplit::hashset;
44
use text_io::{read, try_read, try_scan};
55

6-
use std::io;
6+
use std::{io, str};
77

88
#[allow(clippy::try_err)]
99
fn main() {
@@ -28,7 +28,7 @@ fn main() {
2828
5 => on_5(query),
2929
_ => unreachable!(),
3030
};
31-
println!("! {}", String::from_utf8(ans).unwrap());
31+
println!("! {}", str::from_utf8(&ans).unwrap());
3232
}
3333

3434
fn on_26(mut query: impl FnMut(u8, u8) -> bool) -> Vec<u8> {
@@ -99,6 +99,8 @@ mod tests {
9999
let wr = balls.iter().position(|&b| b == r).unwrap();
100100
wl < wr
101101
});
102+
let ans = str::from_utf8(&ans).unwrap();
103+
let balls = str::from_utf8(&balls).unwrap();
102104
assert_eq!(ans, balls);
103105
assert!(queries <= 7);
104106
}

examples/practice-b-whiteread.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
use maplit::hashset;
44
use whiteread::Reader;
55

6+
use std::str;
7+
68
fn main() {
79
let mut rdr = Reader::from_stdin_naive();
810

@@ -18,7 +20,7 @@ fn main() {
1820
5 => on_5(query),
1921
_ => unreachable!(),
2022
};
21-
println!("! {}", String::from_utf8(ans).unwrap());
23+
println!("! {}", str::from_utf8(&ans).unwrap());
2224
}
2325

2426
fn on_26(mut query: impl FnMut(u8, u8) -> bool) -> Vec<u8> {
@@ -89,6 +91,8 @@ mod tests {
8991
let wr = balls.iter().position(|&b| b == r).unwrap();
9092
wl < wr
9193
});
94+
let ans = str::from_utf8(&ans).unwrap();
95+
let balls = str::from_utf8(&balls).unwrap();
9296
assert_eq!(ans, balls);
9397
assert!(queries <= 7);
9498
}

examples/testers/practice-b.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import itertools
2+
import random
3+
import string
4+
from argparse import ArgumentParser
5+
from subprocess import Popen, PIPE
6+
from typing import List
7+
8+
9+
def main() -> None:
10+
parser = ArgumentParser()
11+
parser.add_argument('bin')
12+
13+
binary = parser.parse_args().bin
14+
15+
for _ in range(100):
16+
judge(binary, ''.join(random.sample(string.ascii_uppercase, 26)))
17+
18+
for balls in itertools.permutations(string.ascii_uppercase[:5]):
19+
judge(binary, ''.join(balls))
20+
21+
22+
def judge(binary: str, balls: str) -> None:
23+
n = len(balls)
24+
q = 7 if n == 5 else 100
25+
26+
with Popen([binary], stdin=PIPE, stdout=PIPE) as proc:
27+
def read_words() -> List[str]:
28+
return proc.stdout.readline().decode('utf-8').split()
29+
30+
def on_query(c1: str, c2: str) -> None:
31+
reply = '<' if balls.index(c1) < balls.index(c2) else '>'
32+
proc.stdin.write(f'{reply}\n'.encode('utf-8'))
33+
proc.stdin.flush()
34+
35+
def on_answer(ans: str) -> None:
36+
if ans != balls:
37+
raise Exception('wrong answer')
38+
39+
proc.stdin.write(f'{n} {q}\n'.encode('utf-8'))
40+
proc.stdin.flush()
41+
42+
for _ in range(q):
43+
words = read_words()
44+
if len(words) == 3 and words[0] == '?':
45+
on_query(words[1], words[2])
46+
elif len(words) == 2 and words[0] == '!':
47+
return on_answer(words[1])
48+
else:
49+
raise Exception('invalid')
50+
else:
51+
words = read_words()
52+
if len(words) == 2 and words[0] == '!':
53+
return on_answer(words[1])
54+
raise Exception('answer me')
55+
56+
57+
if __name__ == '__main__':
58+
main()

0 commit comments

Comments
 (0)