Skip to content

Commit f8f2388

Browse files
committed
1行文字列の処理を追加
1 parent ca46805 commit f8f2388

File tree

2 files changed

+63
-29
lines changed

2 files changed

+63
-29
lines changed

atcodertools/fmtprediction/predict_format.py

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
search_formats_with_minimum_vars
55
from atcodertools.fmtprediction.predict_types import predict_types, TypePredictionFailedError
66
from atcodertools.fmtprediction.models.format_prediction_result import FormatPredictionResult
7+
import re
78

89

910
class NoPredictionResultError(Exception):
@@ -16,35 +17,68 @@ def __init__(self, cands):
1617
self.cands = cands
1718

1819

20+
def suspect_single_string(input_format: str, samples):
21+
a = input_format.strip().split()
22+
if len(a) != 1:
23+
return None
24+
input_format = a[0].strip()
25+
for sample in samples:
26+
s = sample.get_input().split()
27+
if len(s) != 1:
28+
return None
29+
i = input_format.find('_')
30+
if i == -1:
31+
return None
32+
pattern = input_format[0:i + 1]
33+
if len([m.start() for m in re.finditer(pattern, input_format)]) < 2:
34+
return None
35+
return input_format[0:i]
36+
37+
1938
def predict_format(content: ProblemContent) -> FormatPredictionResult:
2039
input_format = content.get_input_format()
2140
samples = content.get_samples()
41+
input_format = input_format.replace('\'', 'prime')
2242

2343
if len(samples) == 0:
2444
raise NoPredictionResultError
2545

26-
try:
27-
tokenized_possible_formats = search_formats_with_minimum_vars(
28-
input_format)
29-
except NoFormatFoundError:
30-
raise NoPredictionResultError
46+
for ct in [0, 1]:
47+
tokenized_possible_formats = []
48+
if ct == 0:
49+
try:
50+
tokenized_possible_formats += search_formats_with_minimum_vars(
51+
input_format)
52+
except NoFormatFoundError:
53+
continue
54+
elif ct == 1:
55+
input_format2 = suspect_single_string(input_format, samples)
56+
if input_format2 is not None:
57+
try:
58+
tokenized_possible_formats += search_formats_with_minimum_vars(
59+
input_format2)
60+
except NoFormatFoundError:
61+
raise NoPredictionResultError
3162

32-
output_cands = []
33-
for format in tokenized_possible_formats:
34-
try:
35-
simple_format_array = predict_simple_format(format.var_tokens)
36-
except (TypePredictionFailedError, SimpleFormatPredictionFailedError):
37-
continue
38-
for simple_format in simple_format_array:
63+
output_cands = []
64+
for format in tokenized_possible_formats:
3965
try:
40-
output_cands.append(
41-
FormatPredictionResult.create_typed_format(simple_format, predict_types(simple_format, samples)))
42-
break
66+
simple_format_array = predict_simple_format(format.var_tokens)
4367
except (TypePredictionFailedError, SimpleFormatPredictionFailedError):
44-
pass
68+
continue
69+
for simple_format in simple_format_array:
70+
try:
71+
output_cands.append(
72+
FormatPredictionResult.create_typed_format(simple_format, predict_types(simple_format, samples)))
73+
break
74+
except (TypePredictionFailedError, SimpleFormatPredictionFailedError):
75+
pass
4576

46-
if len(output_cands) > 1:
47-
raise MultiplePredictionResultsError(output_cands)
48-
if len(output_cands) == 0:
49-
raise NoPredictionResultError
50-
return output_cands[0]
77+
if len(output_cands) == 1:
78+
return output_cands[0]
79+
elif len(output_cands) > 1:
80+
raise MultiplePredictionResultsError(output_cands)
81+
elif ct == 0:
82+
continue
83+
else:
84+
raise NoPredictionResultError

tests/resources/test_fmtprediction/answer.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ abc075-C OK [(Singular: N),(Si
314314
abc075-D OK [(Singular: N),(Singular: K),(Parallel: x,y | 1 to N)] [('N', <class 'int'>), ('K', <class 'int'>), ('x', <class 'int'>), ('y', <class 'int'>)]
315315
abc076-A OK [(Singular: R),(Singular: G)] [('R', <class 'int'>), ('G', <class 'int'>)]
316316
abc076-B OK [(Singular: N),(Singular: K)] [('N', <class 'int'>), ('K', <class 'int'>)]
317-
abc076-C No result
317+
abc076-C OK [(Singular: Sprime),(Singular: T)] [('Sprime', <class 'str'>), ('T', <class 'str'>)]
318318
abc076-D OK [(Singular: N),(Parallel: t | 1 to N),(Parallel: v | 1 to N)] [('N', <class 'int'>), ('t', <class 'int'>), ('v', <class 'int'>)]
319319
abc077-A OK [(Parallel: C | 1 to 2)] [('C', <class 'str'>)]
320320
abc077-B OK [(Singular: N)] [('N', <class 'int'>)]
@@ -556,7 +556,7 @@ agc014-D OK [(Singular: N),(Pa
556556
agc014-E OK [(Singular: N),(Parallel: a,b | 1 to N-1),(Parallel: c,d | 1 to N-1)] [('N', <class 'int'>), ('a', <class 'int'>), ('b', <class 'int'>), ('c', <class 'int'>), ('d', <class 'int'>)]
557557
agc014-F OK [(Singular: N),(Parallel: p | 1 to N)] [('N', <class 'int'>), ('p', <class 'int'>)]
558558
agc015-A OK [(Singular: N),(Singular: A),(Singular: B)] [('N', <class 'int'>), ('A', <class 'int'>), ('B', <class 'int'>)]
559-
agc015-B No result
559+
agc015-B OK [(Singular: S)] [('S', <class 'str'>)]
560560
agc015-C No result
561561
agc015-D OK [(Singular: A),(Singular: B)] [('A', <class 'int'>), ('B', <class 'int'>)]
562562
agc015-E OK [(Singular: N),(Parallel: X,V | 1 to N)] [('N', <class 'int'>), ('X', <class 'int'>), ('V', <class 'int'>)]
@@ -688,8 +688,8 @@ arc006-C OK [(Singular: N),(Pa
688688
arc006-D OK [(Singular: H),(Singular: W),(Parallel: c | 0 to H-1)] [('H', <class 'int'>), ('W', <class 'int'>), ('c', <class 'str'>)]
689689
arc007-A OK [(Singular: X),(Singular: s)] [('X', <class 'str'>), ('s', <class 'str'>)]
690690
arc007-B OK [(Singular: N),(Singular: M),(Parallel: disk | 0 to M-1)] [('N', <class 'int'>), ('M', <class 'int'>), ('disk', <class 'int'>)]
691-
arc007-C No result
692-
arc007-D No result
691+
arc007-C OK [(Singular: c)] [('c', <class 'str'>)]
692+
arc007-D OK [(Singular: c)] [('c', <class 'str'>)]
693693
arc008-A OK [(Singular: N)] [('N', <class 'int'>)]
694694
arc008-B OK [(Singular: N),(Singular: M),(Singular: name),(Singular: kit)] [('N', <class 'int'>), ('M', <class 'int'>), ('name', <class 'str'>), ('kit', <class 'str'>)]
695695
arc008-C OK [(Singular: N),(Parallel: x,y,t,r | 0 to N-1)] [('N', <class 'int'>), ('x', <class 'int'>), ('y', <class 'int'>), ('t', <class 'int'>), ('r', <class 'int'>)]
@@ -879,14 +879,14 @@ arc054-D OK [(Singular: N),(Pa
879879
arc055-A OK [(Singular: N)] [('N', <class 'int'>)]
880880
arc055-B OK [(Singular: N),(Singular: K)] [('N', <class 'int'>), ('K', <class 'int'>)]
881881
arc055-C OK [(Singular: S)] [('S', <class 'str'>)]
882-
arc055-D No result
882+
arc055-D OK [(Singular: d)] [('d', <class 'int'>)]
883883
arc056-A OK [(Singular: A),(Singular: B),(Singular: K),(Singular: L)] [('A', <class 'int'>), ('B', <class 'int'>), ('K', <class 'int'>), ('L', <class 'int'>)]
884884
arc056-B OK [(Singular: N),(Singular: M),(Singular: S),(Parallel: u,v | 1 to M)] [('N', <class 'int'>), ('M', <class 'int'>), ('S', <class 'int'>), ('u', <class 'int'>), ('v', <class 'int'>)]
885885
arc056-C OK [(Singular: N),(Singular: K),(TwoDimensional: w)] [('N', <class 'int'>), ('K', <class 'int'>), ('w', <class 'int'>)]
886886
arc056-D No result
887887
arc057-A OK [(Singular: A),(Singular: K)] [('A', <class 'int'>), ('K', <class 'int'>)]
888888
arc057-B OK [(Singular: N),(Singular: K),(Parallel: a | 1 to N)] [('N', <class 'int'>), ('K', <class 'int'>), ('a', <class 'int'>)]
889-
arc057-C No result
889+
arc057-C OK [(Singular: a)] [('a', <class 'str'>)]
890890
arc057-D OK [(Singular: N),(Parallel: A | 1 to N-1)] [('N', <class 'int'>), ('A', <class 'int'>)]
891891
arc058-C OK [(Singular: N),(Singular: K),(Parallel: D | 1 to K)] [('N', <class 'int'>), ('K', <class 'int'>), ('D', <class 'int'>)]
892892
arc058-D OK [(Singular: H),(Singular: W),(Singular: A),(Singular: B)] [('H', <class 'int'>), ('W', <class 'int'>), ('A', <class 'int'>), ('B', <class 'int'>)]
@@ -1523,7 +1523,7 @@ ddcc2019-qual-B OK [(Singular: N)] [(
15231523
ddcc2019-qual-C OK [(Singular: N)] [('N', <class 'int'>)]
15241524
ddcc2019-qual-D OK [(Parallel: a | 2 to 30)] [('a', <class 'int'>)]
15251525
digitalarts2012-A No result
1526-
digitalarts2012-B No result
1526+
digitalarts2012-B OK [(Singular: c)] [('c', <class 'str'>)]
15271527
discovery2016-final-A OK [(Singular: N),(Parallel: Id | 1 to N)] [('N', <class 'int'>), ('Id', <class 'int'>)]
15281528
discovery2016-final-B OK [(Singular: N),(Singular: X),(Parallel: T | 1 to N),(Parallel: A | 1 to N)] [('N', <class 'int'>), ('X', <class 'int'>), ('T', <class 'int'>), ('A', <class 'int'>)]
15291529
discovery2016-final-C OK [(Singular: S),(Singular: K)] [('S', <class 'str'>), ('K', <class 'int'>)]
@@ -2003,7 +2003,7 @@ tenka1-2012-qualA-B No result
20032003
tenka1-2012-qualA-C OK [(Singular: N),(Singular: M),(Parallel: a,b | 1 to M),(Singular: s)] [('N', <class 'int'>), ('M', <class 'int'>), ('a', <class 'int'>), ('b', <class 'int'>), ('s', <class 'str'>)]
20042004
tenka1-2012-qualA-D OK [(Singular: N),(Parallel: c | 0 to N-1)] [('N', <class 'int'>), ('c', <class 'str'>)]
20052005
tenka1-2012-qualB-A OK [(Singular: a),(Singular: b),(Singular: c)] [('a', <class 'int'>), ('b', <class 'int'>), ('c', <class 'int'>)]
2006-
tenka1-2012-qualB-B No result
2006+
tenka1-2012-qualB-B OK [(Singular: c)] [('c', <class 'str'>)]
20072007
tenka1-2012-qualB-C OK [(Singular: N),(Parallel: Ts,Te | 1 to N)] [('N', <class 'int'>), ('Ts', <class 'str'>), ('Te', <class 'str'>)]
20082008
tenka1-2012-qualB-D No result
20092009
tenka1-2012-qualC-A OK [(Singular: n)] [('n', <class 'int'>)]

0 commit comments

Comments
 (0)