Post Reply 
HHC 2021 Programming Contests - Surprise !
10-06-2021, 05:24 PM
Post: #23
RE: HHC 2021 Programming Contests - Surprise !
Just for fun, I did a 71B BASIC implementation of the RPN problem. This should work on a stock 71B with no additional LEX files or modules.

Rather than searching upward one by one, I took the approach of simply "mirroring" the input number to create a palindrome, checking to see if the result is greater than the input, and if it's not, incrementing the value of the left half by 1 and building a palindrome from that instead. Had to do a few tricks to correctly handle cases with input of all 9. It also works correctly for an input of 0, returning 1.

Roughly half of the program is a string-reverse function, and there's room to shave a few more bytes off of this by combining some lines. It's very fast, though. Smile It may look like there's a loop at first glance, but the backward jump should only occur either 0 or 1 times. There is a FOR loop in the string reversal function, of course.

Code:
0010 INPUT X
0020 X$=STR$(X) @ L=LEN(X$) @ N=CEIL(L/2) @ M=FLOOR(L/2)
0030 A$=X$[1,N]
0035 B$=FNR$(A$[1,M])
0040 P=VAL(A$[1,N]&B$) @ IF P>X THEN GOTO 100
0050 A$=STR$(VAL(A$)+1) @ IF LEN(A$)>N THEN M=M+1
0060 GOTO 35
0100 DISP P @ END 
1000 DEF FNR$(S$)
1001 FNR$="" @ IF LEN(S$)=0 THEN END 
1005 DIM T$[LEN(S$)]
1010 T$=""
1020 FOR I=LEN(S$) TO 1 STEP -1
1030 T$=T$&S$[I,I]
1040 NEXT I @ FNR$=T$
1050 END DEF
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: HHC 2021 Programming Contests - Surprise ! - Dave Britten - 10-06-2021 05:24 PM



User(s) browsing this thread: 1 Guest(s)