บางครั้งมันมีประโยชน์ในการรันปัญหาคณิตศาสตร์ด้วยอินพุตหลายตัว เป้าหมายของความท้าทายนี้คือการสร้างโปรแกรมที่ช่วยให้งานนี้ง่ายขึ้น
นิพจน์ที่สร้างตัวเลข
คุณต้องสนับสนุนการแสดงออก 3 ประเภท:
- ตัวสร้างตัวเลขเดี่ยว: ง่ายมากเพียงแค่ตัวอักษร
- เครื่องกำเนิดหลายหมายเลข: ตาดซับซ้อนมากขึ้น Thees ถูกล้อมรอบด้วยวงเล็บเหลี่ยม (
[]
) ตัวเลขคั่นด้วยเครื่องหมายจุลภาค (,
) ในนิพจน์[-1,2,3.26]
ตัวอย่าง - Range generator: อันนี้ล้อมรอบด้วยเครื่องหมายปีกกา (
{}
) มันจะมี 3 ตัวเลขคั่นด้วยเครื่องหมายจุลภาค{start,stop,step}
รูปแบบของการแสดงออกนี้อยู่start
และstop
รวม
กฎสำหรับการประเมินผล
- คุณต้องสนับสนุนคำสั่งของการดำเนินงาน ( https://en.wikipedia.org/wiki/Order_of_operations#Definition )
- คุณไม่จำเป็นต้องสนับสนุนวงเล็บ
- จำนวนช่องว่างใด ๆ ที่อาจเกิดขึ้นในการแสดงออก
- คุณต้องสนับสนุนตัวเลขทศนิยม (สิ่งที่แม่นยำค่าเริ่มต้นของภาษาของคุณจะดี)
- หารด้วย
0
ผลลัพธ์ในNaN
(ไม่ใช่ตัวเลข)
โปรแกรมของคุณต้องรองรับการคูณ ( *
), การหาร ( /
), การเพิ่ม ( +
) และการลบ ( -
)
เอาท์พุต
เอาต์พุตแต่ละบรรทัดเป็นหนึ่งในการรวมกันของเครื่องกำเนิดไฟฟ้า รูปแบบคือนิพจน์ (ที่มีจำนวนจริงแทนลงไป) ตามด้วยเครื่องหมายเท่ากับ ( =
) และผลลัพธ์ของการประเมิน การรวมกันทั้งหมดของเครื่องกำเนิดไฟฟ้าจะต้องเป็นตัวแทนในการส่งออก
ตัวอย่าง
( >>>
หมายถึงอินพุต)
>>>3 * [3,2]
3 * 3 = 9
3 * 2 = 6
>>>{1,2,3}
1 = 1 <-- this is because 1 + 3 > the end
>>>{0,2,1} + {0,1,1}
0 + 0 = 0
1 + 0 = 1
2 + 0 = 2
0 + 1 = 1
1 + 1 = 2
2 + 1 = 3
>>>6/[2,3]
6/2 = 3
6/3 = 2
>>>{1.5,2.5,0.5}
1.5 = 1.5
2 = 2
2.5 = 2.5
>>>3-{6,5,-1}
3-6 = -3
3-5 = -2
>>>5/{-1,1,1}
5/-1 = -5
5/0 = NaN
5/1 = 5
>>>4.4 / [1,2.2] + {0,2,1}
4.4 / 1 + 0 = 4.4
4.4 / 1 + 1 = 5.4
4.4 / 1 + 2 = 6.4
4.4 / 2.2 + 0 = 2
4.4 / 2.2 + 1 = 3
4.4 / 2.2 + 2 = 4
>>> [1,2] / 0 + 5
1 / 0 + 5 = NaN
2 / 0 + 5 = NaN
โปรแกรมต้องสั้นเพื่อให้สามารถจดจำและใช้งานได้ทุกที่
ขอบคุณ @PeterTaylor และ @geokavel ที่ช่วยฉันด้วยการโพสต์นี้ในกล่องทราย
x/0
ส่งผลให้มีการประเมินผลทันทีให้กับ NaN หรือไม่หรือฉันต้องถือว่า NaN เป็นค่าหรือไม่?