Repository navigation
Fail mirrored runs copied from the reversed read - #852
hankliuliu wants to merge 6 commits into
Conversation
e69ec3c to
0bdce44
Compare
haruuu1214
left a comment
There was a problem hiding this comment.
Thanks, the new case clearly blocks the reversed-copy common-substring shortcut, and running both tie-break variants makes the regression convincing. Using checkAnswer instead of assertRejects is also the right choice here, because a palindrome answer can have several valid strings.
Two small comments:
- Line 1056:
assert.equal(answer, copied)needs a failure message like the other assertions. - Lines 1038-1041: the check that
expectedpasses the checker could move into a general test over allpalindromejudges.
0bdce44 to
f67b2d6
Compare
f67b2d6 to
4a5c445
Compare
|
Some review comments on this pull request are not settled yet. Please refine the change per the review comments listed below, reply on each thread with what you changed or why you did not, and mark each thread resolved yourself once it is settled (Resolve conversation, at the bottom of the thread). The pull request stays blocked while review threads are open. Not addressed at the current head:
Checked at 9c2d717. |
All five Mirrored Tag Segment cases happen to share a palindromic longest common substring with their reversed copy, so the shortcut the LeetCode 5 editorial warns about, returning that substring with no mirror check, passed 5/5. The added read ends with the reverse of its non-palindromic prefix and its longest mirrored stretch is only three characters, so every common substring of length four or more now fails on length and palindromicity alike. The browser regression runs the shortcut and a centre-expansion reference against the cases, so a later edit cannot quietly let the shortcut back in. Close sysprog21#850 Change-Id: I11a38840cd414fb4b10fc8c971d1a9a41f5d7ae3
No case's longest mirrored stretch had an even length above two, so a solution that expands odd centres but keeps an adjacent equal pair as it stands passed all six cases: the pair is the full answer for "cbbd" and too short to win anywhere else. The whole of "abba" mirrors, so the bare pair "bb" now fails on length, and the regression pins the case by label beside the reversed-copy one. Change-Id: I3492b4a06baf344efd058389ac32018f8886f1ba
The palindrome checker reads only the length of its case's expected value, so a judge could carry a wrong expected answer, pass every run, and show that value as the answer in the results panel. Running each case's expected through its own checker catches that for every judge at once, including the checkers that accept more than one answer. The check proves membership, not optimality: the shortcut regression beside it is what pins how long a longest mirrored stretch has to be. Change-Id: I0aaa7eed76187aa1d48d864c8d49d9ba6306db7c
Four more scans agreed with a longest mirrored stretch on every read so far. An expansion whose centres skip the ends never sees the pair at the very start, so "aab", whose only stretch past one character is that pair, rejects it. Every read was lowercase letters, so a scan comparing case-folded characters passed although the contract promises exact comparison, and so did one that drops everything but letters; "aA" rejects the first, and "ab1ba", mirrored through its digit, rejects the second. The first stretch that grows past one character, like the last, is a longest one in every current read, so "abacdedcfgf" puts its only five-character stretch between a shorter one on each side and rejects both picks. The regression writes centre expansion once and states each scan as the one thing it changes, so the copies cannot drift apart, and runs each by name against its pinned case. Change-Id: Ib026eca612139649d943073b69be66fc2c97d4f1
Odd and even centres never competed in any read: wherever the longest stretch was even, no odd one grew past one character, and the reverse. A scan that settles odd centres first and tries even ones only when nothing grew passed every case, and so did its mirror image. "abacca" holds the even "acca" beside the odd "aba", and "aabcb" the odd "bcb" beside the even "aa", so each read rejects one order. Both scans are run by name beside the others. Change-Id: I7ad86960b89ed7352a38bc0187c8526683a01f2e
9c2d717 to
2612c5f
Compare
Every longest stretch so far was lowercase letters and digits, and no read ended in its only even pair, so two more scans passed all thirteen cases. One drops everything but lowercase letters and digits before expanding, the counterpart of the letters-only scan, although the contract compares upper and lower case exactly: "abBba" mirrors through its capital, and that scan returns "abba". The other stops its even centres one short and never tries the pair at the very end of a read, so "baa", whose only stretch past one character is that pair, rejects it as "aab" rejects a scan missing the pair at the start. Centre expansion now takes the centres it visits as a predicate, so the scan skipping the ends and the one skipping the last pair are each one call, and both new scans run by name beside the others. Change-Id: Ie66656d7e98c51c8c459aad030b36929ccd775f1
| "expected": "acca" | ||
| }, | ||
| { | ||
| "label": "odd mirror beats even mirror: s=\"aabcb\"", |
There was a problem hiding this comment.
No read here has three equal characters in a row, so a scan that tries only one kind of centre at each position, the even one when the next character matches and the odd one otherwise, passes every case. On baaab it expands the pair at the first aa, then the pair at the second, and returns aa, never trying the odd centre on the middle a that gives baaab. A read such as s="baaab" expecting baaab rejects it; its label would join the ones tests/web/contract.rs pins, and the scan would run by name in the mirrored tag test like the others.
jserv
left a comment
There was a problem hiding this comment.
Rebase the current branch onto the upstream default branch and rework the series into functionally minimal commits, folding similar ones and enforcing the project's commit message rules.
Problem and behavior
In Mirrored Tag Segment (
longest-palindromic-substring, LeetCode 5), several wrong scans passed all five judge cases, because every read happened to give each of them a longest mirrored stretch. The one the issue reports returns the longest common substring of the read and its reversed copy without checking that the result is itself mirrored, the approach the LeetCode editorial for problem 5 warns about. The others expand odd centres but keep an adjacent equal pair as it stands, skip the centres at the ends of the read or stop the even centres one short of the last pair, compare characters case-folded or drop everything but letters, or everything but lowercase letters and digits, although the contract promises exact matching of digits and letters, keep the first or the last stretch that grows past one character, or settle one kind of centre, odd or even, before trying the other. The judge is what the interviewer and the report trust, so a full green run credited solutions a human interviewer would reject. Each of these now fails a case named for its read, and a correct solution passes all fifteen.mirrored ends: s="abacdfgdcaba"abacdordcabawhole string even: s="abba"bbmatching prefix: s="aab"amatching suffix: s="baa"bmixed case: s="aA"aAdigit at the centre: s="ab1ba"abbacapital at the centre: s="abBba"abbamiddle mirror: s="abacdedcfgf"abaorfgfeven mirror beats odd mirror: s="abacca"abaodd mirror beats even mirror: s="aabcb"aaApproach
Each added read gives one family a wrong answer, so each case is needed, and none depends on which tied stretch an implementation keeps. The browser regression in
tests/browser/compiler-explorer.test.jswrites centre expansion once and states each scan built on it as the one thing it changes (the centres it visits, how two characters compare, which stretch it keeps, which kind of centre it tries first), so the copies cannot drift apart; the reversed-copy shortcut and the unexpanded pair stay loops of their own, since neither is centre expansion with one option changed. Every scan runs by name against the case pinned for it, with the exact string it returns and thepalindromechecker's refusal asserted, and centre expansion runs through the checker on every case. Thepalindromechecker reads only the length of a case'sexpected, so a wrong recorded answer could pass every run and be shown as the answer;tests/browser/judge-case-counts.test.js, beside the other bank-wide judge invariant, now runs every judge case'sexpectedthrough its own checker, which proves membership while the shortcut test pins the length. Labels describe the read, not the shortcut, and passrules.check_labels;tests/web/contract.rspins all ten;web/judges/mirrored-tag-segment.jsonand the digest intests/golden/task-posed.jsonare regenerated, not hand-edited. The page example names case 4, so it still shows the same read. The cases arrive in five commits and the bank-wide check in a sixth, each under the interactive-review threshold.Validation
The branch sits on current
main(9817ef6)../scripts/test.shran to the end twice on this head. The second run passed every gate; its browser lane passed 3809 of 3843 with 34 skipped forjavacandjava, and it ended:The first run failed one browser test,
explicit Start mixes tab audio and the existing mic into a downloadable WebMintests/browser/local-recorder.test.js, where the 440 Hz level of the recording came out at 0.0065 against a floor of 0.01. It passed 6 runs of 6 on its own and 12 of 12 run four at a time, both on this head and on 2612c5f, and this change touches no recording code, so I left it out of this pull request. Not checked here: desktop clippy and tests, and the Java harness runs.Beyond the scans the test names, 29 single-change wrong variants of centre expansion, the DP table and brute force (centre ranges and loop bounds, slice ends, character filters, comparisons, which stretch is kept, centre order) were run against the fifteen cases, and each fails at least one; the variants that still pass are equivalent to a correct scan, such as skipping the odd centres at the ends, which only ever yield one character. Dropping either new case, or making its read lowercase, fails the suite by the case's name, and the centre-visiting predicate gives the same answer as the scan it replaced on every read over
abA1up to six characters and on 30,000 random reads overabAB1up to nine.Close #850
Summary by cubic
Adds ten
longest-palindromic-substringjudge cases so wrong scans that previously passed every existing case now fail. The judge gave a full green run to solutions an interviewer would reject: returning the longest common substring of a read and its reversed copy without checking the result is mirrored, keeping an adjacent equal pair instead of expanding it, skipping the centres at the read's ends, comparing characters case-folded, dropping everything but letters or dropping capitals too, stopping even centres one short of the last pair, keeping the first or last stretch that grows past one character, and settling odd or even centres before trying the other. Each scan now fails a case named for its read, and a correct centre-expansion solution passes all fifteen cases.Regression coverage
expectedvalue through its own checker, so a wrong recorded answer can no longer slip into any judge.tests/web/contract.rs; the regenerated judge file and golden digest follow automatically. Closes Mirrored Tag Segment accepts reversed-copy common-substring solutions #850.Written for commit 8706f02. Summary will update on new commits.