J - 63 ตัวอักษร
โปรแกรมอ่านตัวเลขแล้วตามด้วยประโยค
;(*&.".&(1!:1]1)^:(0{i)(<@);.1~1,2~:/\i=.e.&'0123456789')1!:1]1
อธิบายโดยการระเบิด:
;(*&.".&(1!:1]1)^:(0{i)(<@);.1~1,2~:/\i=.e.&'0123456789')1!:1]1
1!:1]1 NB. read sentence
e.&'0123456789' NB. is digit? bool vector
i=. NB. save to i
2 /\ NB. between adjacent chars:
~: NB. true if not equal
1, NB. pad to sentence length
( ;.1~ ) NB. cut the sentence
^:(0{i) NB. if cut is digits:
*&.". NB. multiply as number
&(1!:1]1) NB. with user input
; NB. rejoin sentence
หากเราใช้ไลบรารี่ PCRE ของ J และทำให้ประโยคเป็นอันดับแรกเราสามารถเคาะได้ถึง54 ตัวอักษร :
;_2(,*&.".&(1!:1]1)&.>)/\'\d+'(rxmatches rxcut])1!:1]1
อธิบายโดยการระเบิด:
1!:1]1 NB. read in sentence
'\d+'(rxmatches ) NB. all /\d+/ matches
( rxcut]) NB. cut by those matches
_2 \ NB. on each nonmatch-match pair:
( &.>)/ NB. take the match
*&.". NB. multiply as number
&(1!:1]1) NB. by user input
(, ) NB. prepend nonmatch
; NB. rejoin sentence
J แย่มากในเรื่องนี้ฉันจะพูดอะไรดี มันไม่สะดวกเพราะเจไม่ได้ให้ความสำคัญ
ตัวอย่างบางส่วน:
;(*&.".&(1!:1]1)^:(0{i)(<@);.1~1,2~:/\i=.e.&'0123456789')1!:1]1
3
This 1 is22a 3352sentence 50
This 3 is66a 10056sentence 150
;(*&.".&(1!:1]1)^:(0{i)(<@);.1~1,2~:/\i=.e.&'0123456789')1!:1]1
100
N0 R3GUL4R 3XPR35510N5 1N J M4K35 M3 54D ALS0
N0 R300GUL400R 300XPR3551000N500 100N J M400K3500 M300 5400D ALS0
0!:0 <'system\main\regex.ijs' NB. this is usually preloaded by J on startup anyway
;_2(,*&.".&(1!:1]1)&.>)/\'\d+'(rxmatches rxcut])1!:1]1
TH4T'5 M4RG1N411Y B3TT3R
0
TH0T'0 M0RG0N0Y B0TT0R