เยลลี่ , 18 13 10 ไบต์
×⁵RDP$€io-
ลองออนไลน์!
วิธีแก้ปัญหา 13 ไบต์:
×⁵RDP$€iµ’¹¬?
ลองออนไลน์!
คำอธิบายด้วยอินพุตN
:
׳R create a range from 1 to N * 100 (replace ³ with ⁵ for a faster execution time)
DP$ define a function ($) to get the product of the digits of a number,
€ apply that function to each element in the list,
iµ get the index of the input N in the list (or 0 if it's not there), and yeild it as the input to the next link,
’¹¬? conditional: if the answer is 0, then subtract one to make it -1, otherwise, yeild the answer
โซลูชัน 18 ไบต์:
D×/
×⁵RÇ€i
Ç⁾-1¹¬?
ลองออนไลน์!
D×/ product of digits function, takes input M
D split M into digits,
×/ reduce by multiplication (return product of digits)
×⁵RÇ€i range / index function
×⁵R make a range from 1 to N*10,
Ç€ run the above function on each of the range elements,
i get the index of N in the result, or 0 if it's not there
Ç⁾-1¹¬? main function:
Ç ¬? run the line above and check if the answer is null (0)
⁾-1 if it is, return "-1",
¹ otherwise, return the answer (identity function).
ลิงก์สุดท้ายใช้เพื่อแทนที่ 0 เท่านั้น (ค่า falsey เริ่มต้นของ Jelly เนื่องจากรายการทั้งหมดเป็นดัชนีเดียว) ด้วย -1 หากคุณพิจารณา 0 ค่า falsey ตกลงโครงการคือ8 ไบต์
1
การให้1
เป็นกรณีทดสอบที่สำคัญ