Postscript: Future Bob Playing with Grok3 (ภาคผนวก: Bob ในอนาคตเล่นกับ Grok3)

รีวิวทั้งหมดกลับมาแล้ว และตอนนี้ผมกำลังเตรียมต้นฉบับสุดท้ายเพื่อตีพิมพ์ ตอนนี้เป็นเดือนกุมภาพันธ์ปี 2025 และ Grok3 เพิ่งเปิดตัวออกมา ผมจึงป้อนโค้ดเวอร์ชัน “clean” ด้านบนเข้าไปใน Grok3 แล้วขอให้มันปรับปรุงให้ดีขึ้น หลังจากคิดอยู่พักหนึ่ง มันก็ส่งโค้ดต่อไปนี้กลับมา ซึ่งคอมไพล์ผ่านและผ่านเทสต์ทั้งหมดของผม

package fromRoman;

import java.util.Map;

public class FromRoman {
    private String roman;
    private static final String REGEX =
      "^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$";
    private static final Map<Character, Integer> VALUES = Map.of(
        'I', 1,
        'V', 5,
        'X', 10,
        'L', 50,
        'C', 100,
        'D', 500,
        'M', 1000
    );

    /** Constructs a FromRoman instance,
        validating the input Roman numeral. */
    public FromRoman(String roman) {
        if (!roman.matches(REGEX)) {
            throw new InvalidRomanNumeralException(roman);
        }
        this.roman = roman;
    }

    /** Converts the Roman numeral to an integer. */
    public int toInt() {
        int sum = 0;
        for (int i = 0; i < roman.length(); i++) {
            int currentValue = VALUES.get(roman.charAt(i));
            if (i < roman.length() - 1) {
                int nextValue = VALUES.get(roman.charAt(i + 1));
                if (currentValue < nextValue) {
                    sum -= currentValue;
                } else {
                    sum += currentValue;
                }
            } else {
                sum += currentValue;
            }
        }
        return sum;
    }

    /** Static method to convert a Roman numeral string to an integer. */
    public static int convert(String roman) {
        return new FromRoman(roman).toInt();
    }

    public static
    class InvalidRomanNumeralException extends RuntimeException {
        public InvalidRomanNumeralException(String roman) {
            super("Invalid Roman numeral: " + roman);
        }
    }
}

ผมค่อนข้างพอใจกับการปรับปรุงอัลกอริทึมนี้ มันลดทุกอย่างลงเหลือแค่แก่นแท้จริง ๆ ผมไม่ค่อยชอบการใช้ regular expression เท่าไหร่ แต่ตัวนี้ก็ไม่ยากเกินจะเข้าใจ ผมไม่คิดว่าคอมเมนต์พวกนี้มีประโยชน์อะไร เลยน่าจะลบมันทิ้งไป

จากนั้นผมขอให้มันปรับปรุงเทสต์ของผม นี่คือสิ่งที่มันคิดออกมา:

package fromRoman;

import org.junit.jupiter.api.Test;

import static fromRoman.FromRoman.convert;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class FromRomanTest {
    @Test
    public void valid() {
        // Edge case
        assertThat(convert(""), is(0));
        // Single characters
        assertThat(convert("I"), is(1));
        assertThat(convert("V"), is(5));
        assertThat(convert("X"), is(10));
        assertThat(convert("L"), is(50));
        assertThat(convert("C"), is(100));
        assertThat(convert("D"), is(500));
        assertThat(convert("M"), is(1000));
        // Subtractive notation
        assertThat(convert("IV"), is(4));
        assertThat(convert("IX"), is(9));
        assertThat(convert("XL"), is(40));
        assertThat(convert("XC"), is(90));
        assertThat(convert("CD"), is(400));
        assertThat(convert("CM"), is(900));
        // Complex cases
        assertThat(convert("XIV"), is(14));
        assertThat(convert("CDXLIV"), is(444));
        assertThat(convert("MCMXCIX"), is(1999));
    }

    @Test
    public void invalid() {
        // Too many repeats
        assertInvalid("IIII");
        assertInvalid("VV");
        assertInvalid("MMMM");
        // Invalid subtractive notation
        assertInvalid("VX");  // V can’t precede X
        assertInvalid("IIV"); // Incorrect order
        assertInvalid("IXI"); // Invalid sequence
        // Improper structure
        assertInvalid("CMC"); // Multiple subtractive errors
        assertInvalid("XCL"); // Incorrect order
    }

    private void assertInvalid(String r) {
        assertThrows(FromRoman.InvalidRomanNumeralException.class,
                     () -> convert(r));
    }
}

ผมชอบที่มันจัดระเบียบเทสต์เหล่านี้ได้อย่างเรียบร้อย ผมจำได้ว่าตัวเองเคยดิ้นรนหาการจัดระเบียบแบบนี้ และสุดท้ายก็ตัดสินใจแค่ใส่เทสต์ให้ครอบคลุมไปหมด ต้องขอชมเชย Grok3 ในเรื่องนี้ ในทางกลับกัน เทสต์เหล่านี้ พึ่งพา regular expression อย่างมาก และในมุมมองของผม มันทำหน้าที่ทดสอบ invalid input ได้ไม่ดีพอ ตัวอย่างเช่น มันไม่ได้ตรวจสอบว่า "IM" เป็นค่าที่ไม่ถูกต้อง ผมเลยน่าจะเพิ่มเทสต์อีกสักสองสามตัวสำหรับกรณีแบบนี้