ผลิตภัณฑ์ในช่วงพิเศษและรวม


18

แรงบันดาลใจจากคำถามนี้โดย@ CᴏɴᴏʀO'Bʀɪᴇɴ

นำมาจากคำถาม:

งานของคุณง่าย: ให้สองจำนวนเต็ม a และ b เอาท์พุท ∏ [a, b]; นั่นคือผลคูณของช่วงระหว่าง a และ b คุณอาจใช้ a และ b ในรูปแบบที่สมเหตุสมผลไม่ว่าจะเป็นข้อโต้แย้งกับฟังก์ชั่นการป้อนข้อมูล, STDIN, และอื่น ๆ คุณอาจส่งออกในรูปแบบที่เหมาะสมเช่นค่าตอบแทน (สำหรับฟังก์ชั่น) หรือ STDOUT a จะน้อยกว่า b เสมอ

โปรดทราบว่าท้ายที่สุดอาจเป็นแบบเอกสิทธิ์หรือรวมถึง b ฉันไม่จู้จี้จุกจิก ^ _ ^

ความแตกต่างสำหรับความท้าทายนี้คือเราจะพิถีพิถันกับประเภทพิสัย ป้อนข้อมูลคือสตริงของรูปแบบ[a,b], (a,b], [a,b)หรือ(a,b)ที่[]เป็นขอบเขตครอบคลุมและ()เป็นเขตแดน แต่เพียงผู้เดียว ให้ขอบเขตที่ชัดเจนให้ผลิตภัณฑ์ของช่วง นอกจากนี้ช่วงข้อมูลเข้าจะรวมอย่างน้อย 1 หมายเลขซึ่งหมายความว่าช่วงเช่น(3,4)ไม่ถูกต้องและไม่จำเป็นต้องทดสอบ

กรณีทดสอบ

[a,b) => result
[2,5) => 24
[5,10) => 15120
[-4,3) => 0
[0,3) => 0
[-4,0) => 24

[a,b] => result
[2,5] => 120
[5,10] => 151200
[-4,3] => 0
[0,3] => 0
[-4,-1] => 24

(a,b] => result
(2,5] => 60
(5,10] => 30240
(-4,3] => 0
(0,3] => 6
(-4,-1] => -6

(a,b) => result
(2,5) => 12
(5,10) => 3024
(-4,3) => 0
(0,3) => 2
(-4,0) => -6

นี่คือดังนั้นโปรแกรมที่สั้นที่สุดในหน่วยไบต์ชนะ


ลีดเดอร์บอร์ด

ส่วนย่อยของสแต็กที่ด้านล่างของโพสต์นี้สร้างแคตตาล็อกจากคำตอบ a) เป็นรายการของวิธีแก้ปัญหาที่สั้นที่สุดต่อภาษาและ b) เป็นลีดเดอร์บอร์ดโดยรวม

เพื่อให้แน่ใจว่าคำตอบของคุณปรากฏขึ้นโปรดเริ่มคำตอบด้วยหัวข้อโดยใช้เทมเพลต Markdown ต่อไปนี้:

## Language Name, N bytes

ที่Nมีขนาดของส่งของคุณ หากคุณปรับปรุงคะแนนของคุณคุณสามารถเก็บคะแนนเก่าไว้ในพาดหัวโดยการตีพวกเขาผ่าน ตัวอย่างเช่น

## Ruby, <s>104</s> <s>101</s> 96 bytes

หากคุณต้องการรวมหลายตัวเลขไว้ในส่วนหัวของคุณ (เช่นเนื่องจากคะแนนของคุณคือผลรวมของไฟล์สองไฟล์หรือคุณต้องการแสดงรายการบทลงโทษการตั้งค่าสถานะของล่ามแยกต่างหาก) ตรวจสอบให้แน่ใจว่าคะแนนจริงเป็นตัวเลขสุดท้ายในส่วนหัว:

## Perl, 43 + 2 (-p flag) = 45 bytes

คุณยังสามารถตั้งชื่อภาษาให้เป็นลิงค์ซึ่งจะปรากฏในตัวอย่างข้อมูล:

## [><>](http://esolangs.org/wiki/Fish), 121 bytes

คำตอบ:


7

LabVIEW, 38 LabVIEW Primitives

"เล็กน้อย" แก้ไขตอนนี้ตั้งช่วงโดยการสแกนหา () และ [] และเพิ่มดัชนีไปยังตัวเลข

เป็นครั้งแรก


5
ด้วยการมีภาษาที่ต้องใช้ gif แฟนซีคุณจะได้รับตัวแทนทันที GG +1
Addison Crump

3

Python 2, 72 ไบต์

lambda s:reduce(int.__mul__,range(*eval(s[1:-1]+'+'+`']'in s`))[s<'[':])

ในการแยกตัวเลขที่เราประเมินs[1:-1]สตริงการป้อนข้อมูลที่มีจุดสิ้นสุดจะถูกลบออกซึ่งจะให้สิ่งอันดับ ความคิดคือการรับrangetuple นี้และใช้ผลิตภัณฑ์

lambda s:reduce(int.__mul__,range(*eval(s[1:-1]))

เหลวไหลเกิดขึ้นเพื่อปรับจุดปลาย ปลายทางบนเป็นเรื่องง่ายเพียงแค่ตัดองค์ประกอบแรกถ้าการป้อนข้อมูลเริ่มต้นด้วยการทำตามที่([s<'[':]

ปลายทางอื่น ๆ นั้นซับซ้อนกว่า Python ไม่มีวิธีที่สะอาดในการลบองค์ประกอบสุดท้ายของรายการแบบมีเงื่อนไขเนื่องจากl[:0]ลบทุกสิ่ง ดังนั้นเราจึงทำสิ่งที่แปลก เราปรับเปลี่ยนสตริง tuple ก่อนที่มันจะได้รับการประเมินเพื่อตะปูบนสาย"+True"หรือ"+False"ขึ้นอยู่กับว่า s สิ้นสุดในหรือ] )ผลที่ได้คือสิ่งที่ต้องการ3,7จะกลายเป็นอย่างใดอย่างหนึ่ง3,7+Falseซึ่งเป็น3,7หรือซึ่งเป็น3,7+True3,8

สำรองที่สวยกว่า 72:

lambda s:eval("reduce(int.__mul__,range((s<'[')+%s+(']'in s)))"%s[1:-1])

3

Minecraft 15w35a + ขนาดโปรแกรม 638 รวม (ดูด้านล่าง)

เหมือนกับคำตอบของฉันที่นี่แต่ถูกแก้ไข เนื่องจาก Minecraft ไม่มีการป้อนข้อมูลแบบสตริงฉันจึงมีอิสระในการเก็บรักษาคะแนนกระดานคะแนน หากเป็นปัญหาให้ลองพิจารณาคำตอบนี้โดยไม่มีข้อ จำกัด

enter image description here

สิ่งนี้คำนวณPI a,bด้วยการรวม / เอกสิทธิ์ที่ระบุโดยคันโยกทั้งสอง enter image description hereการป้อนข้อมูลจะได้รับจากการใช้สองคำสั่งนี้และ/scoreboard players set A A {num} /scoreboard players set B A {num}จำไว้ว่าให้ใช้/scoreboard objectives add A dummyก่อนใส่

คะแนนโดยใช้: {program size} + ( 2 * {input command} ) + {scoreboard command} = 538 + ( 2 * 33 ) + 34 = 638 .

รหัสนี้สอดคล้องกับ psuedocode ต่อไปนี้:

R = 1
T = A
loop:
  R *= A
  A += 1
  if A == B:
    if A.exclusive:
      R /= T
    if B.exclusive:
      R /= B
    print R
    end program

ดาวน์โหลดโลกที่นี่


2

Pyth, 20 ไบต์

*FPW}\)ztW}\(z}FvtPz

ลองใช้ออนไลน์: การสาธิตหรือชุดทดสอบ

คำอธิบาย:

*FPW}\)ztW}\(z}FvtPz   implicit: z = input string
                 tPz   remove the first and last character of z
                v      evaluate, returns a tuple of numbers
              }F       inclusive range
        tW             remove the first number, if
          }\(z            "(" in z
  PW                   remove the last number, if
    }\)z                  ")" in z
*F                     compute the product of the remaining numbers

2

Ruby, 79 77 ไบต์

->s{a,b=s.scan /\-?\d+/;(a.to_i+(s[?[]?0:1)..b.to_i-(s[?]]?0:1)).reduce 1,:*}

79 ไบต์

->s{a,b=s.scan(/\-?\d+/).map &:to_i;((s[?[]?a:a+1)..(s[?]]?b:b-1)).reduce 1,:*}

Ungolfed:

-> s {
  a,b=s.scan /\-?\d+/    # Extracts integers from the input string, s
  (
    a.to_i+(s[?[]?0:1).. # Increase start of the range by 1 if s contains `(`
    b.to_i-(s[?]]?0:1)   # Decrease end of the range by 1 if s contains `)`
  ).reduce 1,:*
}

การใช้งาน:

->s{a,b=s.scan /\-?\d+/;(a.to_i+(s[?[]?0:1)..b.to_i-(s[?]]?0:1)).reduce 1,:*}["(2,5]"]
=> 60

2

อย่างจริงจัง 31 ไบต์

,#d@p@',@s`εj≈`Mi(@)']=+)'(=+xπ

รับอินพุตเป็นสตริง (อยู่ในเครื่องหมายคำพูดคู่)

ลองออนไลน์ (ต้องป้อนข้อมูลด้วยตนเอง)

คำอธิบาย:

,#d@p@                             get input, take first and last character off and push them individually
      ',@s`εj≈`Mi                  split on commas, map: join on empty, cast to int; explode list
                 (@)']=+)'(=+      increment start and end if braces are ( and ] respectively (since range does [a,b))
                             xπ    make range, push product

1

Python 3, 104

y,r=input().split(',')
t=int(y[1:])+(y[0]<')')
for x in range(t+1,int(r[:-1])+(r[-1]>'[')):t*=x
print(t)

รับอินพุตจาก stdin


จริง ๆ แล้วเราโพสต์คำตอบของเราในวินาทีเดียวกัน Oo
Eumel

@Eumel นั่นควรเป็นตรา
Morgan Thrapp

ไม่จริงโพสต์ที่ Meta ตอนนี้ ^^
Eumel

@Eumel: จริงๆแล้วคุณโพสต์ของคุณ 1 วินาทีก่อนที่ Morgan Thrapp
ev3commander

จริงๆเหรอ? มันแสดงคำตอบเมื่อไม่กี่วินาทีก่อนทั้งสองคำตอบ
Eumel

1

MATLAB, 86 70 ไบต์

s=sscanf(input(''),'%c%d,%d%c');a=s<42;disp(prod(a(1)+s(2):s(3)-a(4)))

วิธีนี้ใช้ได้กับอ็อกเทฟ คุณสามารถลองออนไลน์ได้ที่นี่ ฉันได้เพิ่มรหัสเป็นสคริปต์ไปยังพื้นที่ทำงานที่ดังนั้นคุณก็สามารถป้อนที่พร้อมท์แล้วใส่ของคุณเช่นproductRange'(2,5]'


ดังนั้นรหัสแรกสแกนอินพุตเพื่อดึงวงเล็บและตัวเลขเข้าด้วยกัน:

s=sscanf(input(''),'%c%d,%d%c');

ส่งคืนอาร์เรย์ที่สร้าง[bracket, number, number, bracket]ขึ้น

อาร์เรย์จะถูกเปรียบเทียบกับ42จริง ๆ แล้วจำนวนใด ๆ ระหว่าง 42 และ 90 จะรวมกัน นี่เป็นตัวกำหนดประเภทของวงเล็บเหลี่ยมให้ 1 ถ้ามันเป็นวงเล็บพิเศษและ 0 ถ้าวงเล็บรวม

a=s<42;

ในที่สุดเราจะแสดงผลิตภัณฑ์ของช่วงที่ต้องการ:

disp(prod(a(1)+s(2):s(3)-a(4)))

ผลิตภัณฑ์มีจำนวนที่จ้องมองด้วยหมายเลขแรกs(2)บวกกับประเภทวงเล็บแรกa(1)(ซึ่งคือ 1 ถ้าเป็นวงเล็บพิเศษ) จนถึงและรวมถึงตัวเลขที่สองs(3)ลบด้วยวงเล็บประเภทที่a(4)สอง สิ่งนี้จะช่วยให้ช่วงรวม / เอกสิทธิ์ที่ถูกต้อง


1

Julia, 75 ไบต์

s->prod((x=map(parse,split(s[2:end-1],",")))[1]+(s[1]<41):x[2]-(s[end]<42))

นี่คือฟังก์ชันที่ไม่ระบุชื่อที่ยอมรับสตริงและส่งคืนจำนวนเต็ม f=s->...เรียกว่าให้มันชื่อเช่น

Ungolfed:

function f(s::AbstractString)
    # Extract the numbers in the input
    x = map(parse, split(s[2:end-1], ","))

    # Construct a range, incrementing or decrementing the endpoints
    # based on the ASCII value of the surrounding bracket
    r = x[1]+(s[1] == 40):x[2]-(s[end] == 41)

    # Return the product over the range
    return prod(r)
end

1

Mathematica, 128 ไบต์

1##&@@Range[(t=ToExpression)[""<>Rest@#]+Boole[#[[1]]=="("],t[""<>Most@#2]-Boole[Last@#2==")"]]&@@Characters/@#~StringSplit~","&

นี่ยาวเกินไป ... กำลังคิดวิธีแก้ปัญหาStringReplace+RegularExpression


0

PowerShell, 146 104 ไบต์

param($i)$a,$b=$i.trim("[]()")-split',';($a,(1+$a))[$i[0]-eq'(']..($b,(+$b-1))[$i[-1]-eq')']-join'*'|iex

เลื่อนออกไป 42 ไบต์โดยการเปลี่ยนวิธีการแยกตัวเลขจากอินพุต แอ่ว!

param($i)                          # Takes input string as $i
$a,$b=$i.trim("[]()")-split','     # Trims the []() off $i, splits on comma,
                                   # stores the left in $a and the right in $b

($a,(1+$a))[$i[0]-eq'(']..($b,(+$b-1))[$i[-1]-eq')']-join'*'|iex
# Index into a dynamic array of either $a or $a+1 depending upon if the first
# character of our input string is a ( or not
# .. ranges that together with
# The same thing applied to $b, depending if the last character is ) or not
# Then that's joined with asterisks before
# Being executed (i.e., eval'd)


0

Perl 6 , 60 bytes

{s/\((\-?\d+)/[$0^/;s/(\-?\d+)\)/^$0]/;s/\,/../;[*] EVAL $_}

มีข้อผิดพลาดเล็กน้อยเนื่องจากวิธีการที่คุณจะเขียน(2,5]ตัวอย่างใน Perl 6 จะเป็น2^..5( [2^..5]ยังใช้งานได้)
ดังนั้นฉันต้องสลับ(2กับ[2^และ,ด้วย..แล้วฉันต้องEVALเป็น Range


การใช้งาน:

# give it a name
my &code = {...}

# the `$ =` is so that it gets a scalar instead of a constant

say code $ = '(2,5)'; # 12
say code $ = '[2,5)'; # 24
say code $ = '(2,5]'; # 60
say code $ = '[2,5]'; # 120

say code $ = '(-4,0)' # -6
say code $ = '[-4,0)' # 24
say code $ = '(-4,0]' # 0
say code $ = '[-4,0]' # 0

say code $ = '(-4,-1)' # 6
say code $ = '[-4,-1)' # -24
say code $ = '(-4,-1]' # -6
say code $ = '[-4,-1]' # 24

# this is perfectly cromulent,
# as it returns the identity of `*`
say code $ = '(3,4)'; # 1

0

CJam, 34 ไบต์

r)\(@+"[()]"2/\.#\',/:i.+~1$-,f+:*

ลองออนไลน์

คำอธิบาย:

r       Read input.
)       Split off last character.
\       Swap rest of input to top.
(       Split off first character.
@       Rotate last character to top.
+       Concatenate first and last character, which are the two braces.
"[()]"  Push string with all possible braces.
2/      Split it into start and end braces.
\       Swap braces from input to top.
.#      Apply find operator to vector elements, getting the position of each brace
        from input in corresponding list of possible braces. The lists of braces
        are ordered so that the position of each can be used as an offset for the
        start/end value of the interval.
\       Swap remaining input, which is a string with two numbers separated by
        a comma, to top.
',/     Split it at comma.
:i      Convert the two values from string to integer.
.+      Element-wise addition to add the offsets based on the brace types.
~       Unwrap the final start/end values for the interval.
1$      Copy start value to top.
-       Subtract it from end value.
,       Build 0-based list of values with correct length.
f+      Add the start value to all values.
:*      Reduce with multiplication.

0

JavaScript (ES6), 90 ไบต์

s=>eval(`for(n=s.match(/-*\\d+/g),i=n[0],c=s[0]<"["||i;++i<+n[1]+(s.slice(-1)>")");)c*=i`)

คำอธิบาย

s=>
  eval(`                    // use eval to allow for loop without return or {}
    for(
      n=s.match(/-*\\d+/g), // n = array of input numbers [ a, b ]
      i=n[0],               // i = current number to multiply the result by
      c=s[0]<"["||i;        // c = result, initialise to a if inclusive else 1
      ++i<+n[1]             // iterate from a to b
        +(s.slice(-1)>")"); // if the end is inclusive, increment 1 more time
    )
      c*=i                  // multiply result
  `)                        // implicit: return c

ทดสอบ


0

R, 102 104 ไบต์

f=function(s){x=scan(t=gsub('\\(|\\[|,|\\)|\\]',' ',s))+c(grepl('^\\(',s),-(grepl('\\)$',s)));prod(x[1]:x[2])}

Ungolfed

f=function(s){
    # remove delimiting punctuation from input string, parse and return an atomic vector
    x=scan(t=gsub('\\(|\\[|,|\\)|\\]',' ',s)) +
    # add /subtract from the range dependent on the `[)` pre/suf-fixes
    c(grepl('^\\(',s),-(grepl('\\)$',s)))
    # get the product of the appropriate range of numbers
    prod(x[1]:x[2])
}

แก้ไขเพื่ออนุญาตให้มีตัวเลขติดลบ [ที่มีค่าใช้จ่ายเพิ่มอีก 2 ตัวอักษร


ภาษา?
ThisSuitIsBlackNot

@ThisSuitIsBlackNot - R(และแก้ไขคำตอบ)
mnel

0

JavaScript (ES6), 79

เป็นวิธีการที่ไม่ระบุชื่อ

r=>eval("[a,b,e]=r.match(/-?\\d+|.$/g);c=a-=-(r<'@');for(b-=e<'@';a++<b;)c*=a")

ตัวอย่างการทดสอบ

F=r=>eval("[a,b,e]=r.match(/-?\\d+|.$/g);c=a-=-(r<'@');for(b-=e<'@';a++<b;)c*=a")

// TEST
console.log=x=>O.innerHTML+=x+'\n'

;[
 ['[2,5)',24],['[5,10)',15120],['[-4,3)',0],['[0,3)',0],['[-4,0)',24],
 ['[2,5]',120],['[5,10]',151200],['[-4,3]',0],['[0,3]',0],['[-4,-1]',24],
 ['(2,5]',60],['(5,10]',30240],['(-4,3]',0],['(0,3]',6],['(-4,-1]',-6],
 ['(2,5)',12],['(5,10)',3024],['(-4,3)',0],['(0,3)',2],['(-4,0)',-6]
].forEach(t=>{
  r=F(t[0]),k=t[1],console.log(t[0]+' -> '+r+' (check '+k+ (k==r?' ok)':' fail)'))
})
<pre id=O></pre>

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.