Skip to content

Commit be918c6

Browse files
Merge pull request steam-bell-92#797 from dhruvpatel16120/fix-recognizer-agp
fix: resolve degenerate AGP recognition for zero-ratio sequences (steam-bell-92#637)
2 parents 706d69d + 841de29 commit be918c6

2 files changed

Lines changed: 66 additions & 2 deletions

File tree

math/AP-GP-AGP-HP-Recognizer/AP-GP-AGP-HP-Recognizer.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,17 @@ def agp_candidates(sequence):
9595

9696
def check_agp(sequence):
9797
for ratio in agp_candidates(sequence):
98+
a = sequence[0]
99+
if is_close(ratio, 0.0):
100+
# If common ratio r = 0, all terms from index 1 onwards must be 0
101+
if all(is_close(x, 0.0) for x in sequence[1:]):
102+
return True, ratio
103+
continue
104+
105+
d = (sequence[1] / ratio) - a
98106
valid = True
99-
for i in range(2, len(sequence)):
100-
expected = 2 * ratio * sequence[i - 1] - (ratio * ratio) * sequence[i - 2]
107+
for i in range(len(sequence)):
108+
expected = (a + i * d) * (ratio ** i)
101109
if not is_close(sequence[i], expected, eps=1e-7):
102110
valid = False
103111
break
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import unittest
2+
import subprocess
3+
import os
4+
import sys
5+
6+
class TestProgressionRecognizer(unittest.TestCase):
7+
def setUp(self):
8+
self.script_path = os.path.join("math", "AP-GP-AGP-HP-Recognizer", "AP-GP-AGP-HP-Recognizer.py")
9+
10+
def run_script_with_input(self, user_input):
11+
process = subprocess.Popen(
12+
[sys.executable, self.script_path],
13+
stdin=subprocess.PIPE,
14+
stdout=subprocess.PIPE,
15+
stderr=subprocess.PIPE,
16+
text=True
17+
)
18+
stdout, stderr = process.communicate(input=user_input)
19+
return stdout, stderr
20+
21+
def test_ap_sequence(self):
22+
# Choice 1 for Recognize sequence type, then the AP sequence, then 2 to exit
23+
user_input = "1\n2, 4, 6, 8\n2\n"
24+
stdout, _ = self.run_script_with_input(user_input)
25+
self.assertIn("AP (common difference d = 2)", stdout)
26+
27+
def test_gp_sequence(self):
28+
user_input = "1\n3, 6, 12, 24\n2\n"
29+
stdout, _ = self.run_script_with_input(user_input)
30+
self.assertIn("GP (common ratio r = 2)", stdout)
31+
32+
def test_hp_sequence(self):
33+
user_input = "1\n6, 3, 2, 1.5\n2\n"
34+
stdout, _ = self.run_script_with_input(user_input)
35+
self.assertIn("HP (reciprocal AP difference = 0.166667)", stdout)
36+
37+
def test_agp_sequence(self):
38+
user_input = "1\n2, 8, 24, 64\n2\n"
39+
stdout, _ = self.run_script_with_input(user_input)
40+
self.assertIn("AGP (repetition ratio r = 2)", stdout)
41+
42+
def test_non_agp_zero_sequence(self):
43+
# Sequence like [0, 1, 0, 0] should not be identified as AGP with ratio 0
44+
user_input = "1\n0, 1, 0, 0\n2\n"
45+
stdout, _ = self.run_script_with_input(user_input)
46+
self.assertNotIn("AGP (repetition ratio r = 0)", stdout)
47+
self.assertIn("Not AP, GP, AGP, or HP for the given values", stdout)
48+
49+
def test_valid_agp_zero_sequence(self):
50+
# Sequence like [5, 0, 0, 0] is a valid AGP with ratio 0
51+
user_input = "1\n5, 0, 0, 0\n2\n"
52+
stdout, _ = self.run_script_with_input(user_input)
53+
self.assertIn("AGP (repetition ratio r = 0)", stdout)
54+
55+
if __name__ == "__main__":
56+
unittest.main()

0 commit comments

Comments
 (0)