สร้างสายพิทเบสบอล


11

เป้าหมาย

เขียนโปรแกรมหรือฟังก์ชั่นที่ใช้เป็นจำนวนเต็มบวกnและสุ่มสร้างซีรีส์ทางกฎหมายของสนาม (ต่อจากนี้ไปเรียกว่าสตริงลาด) nของความยาว

อินพุต

จำนวนเต็มบวกที่ไม่เป็นศูนย์n<= 100

เอาท์พุต

nกลับสตริงแบบสุ่มหรือรายการของตัวละครที่เป็นตัวแทนของความเป็นไปได้สตริงสนามที่ถูกต้องของความยาว ตัวละครที่ใช้จะเป็น:

  • B - บอล หากคุณสะสม 4 สิ่งเหล่านี้แป้งจะถูกเดินและตีบอลจนหมด
  • S - สไตรค์ หากคุณสะสม 3 สิ่งเหล่านี้แป้งจะหมดและลูกบอลหมด
  • F - เหม็น จะเพิ่มจำนวนการโจมตี แต่ไม่สามารถทำการโจมตีได้ คือคุณไม่สามารถมี Foul เป็น pitch สุดท้ายในสตริงที่ถูกต้อง การฟาล์วที่ผ่านมาสองครั้ง / การฟาล์วจะไม่เพิ่มจำนวนการตี (การปะทะมี 2 ครั้ง ณ จุดนั้นและครั้งที่ 3 จะพาเขาออกไป)
  • H - Hit ปะทะได้ตีลูกเข้าสู่การเล่นและเสร็จสิ้นการตี

(สิ่งนี้ง่ายเล็กน้อย แต่คุณไม่ต้องเป็นห่วง)

สายพิทช์ที่ถูกต้องคือสตริงที่จบในการนัดหยุดงานการเดินหรือการตี

นั่นคือสตริงพิทช์ไม่ถูกต้องเช่นกัน

  • สนามเพิ่มเติมหลังจากบอลลูกที่ 4, นัดที่ 3 หรือ Hit
  • ถูกยกเลิกก่อนที่จะสร้างลูกที่ 4, การโจมตีครั้งที่ 3 หรือการเข้าชม

กฎระเบียบ

  • โปรแกรมของคุณจะต้องสามารถสร้างผลลัพธ์ที่เป็นไปได้ทั้งหมดสำหรับการป้อนข้อมูลที่กำหนด
  • โปรแกรมของคุณไม่จำเป็นต้องสุ่มอย่างสม่ำเสมอ แต่ยังคงต้องปฏิบัติตามกฎก่อนหน้า
  • นี่คือรหัสกอล์ฟ

ตัวอย่าง

Input => Possible Outputs
1 => [H] #Can only end with a hit
2 => [S,H], [B,H], [F,H] #Can only end with a hit
3 => [S,S,S], [F,F,S], [B,B,H], ... #Can now strike-out, otherwise must end with a hit
4 => [B,B,B,B], [S,B,S,S], [B,F,S,S], [B,B,B,H], ... #Can now be walked, struck-out, or get a hit
6 => [S,B,S,B,B,H], [F,F,F,F,F,S], ... #Can now have a full-count (3 balls, 2 strikes) before finishing 

Input => Invalid Outputs
1 => [S], [B]    #Not enough for a strike-out/walk
2 => [S,S]       #Not enough for a strike-out/walk
2 => [H,H]       #Batter has already scored a hit
3 => [S,S,F]     #Fouls will not cause a strike-out
4 => [S,S,S,H]   #Batter has already struck out
5 => [B,B,B,B,B] #Batter has already walked

1
ดังนั้นเราต้องสามารถผลิตได้ทุกที่ตั้งแต่ 1 ถึงอินฟินิตี้ F หรือไม่?
Quintec

สตริงจะมีความยาวไม่เกิน 100 อักขระ การทำผิดกติกาเป็นสิ่งที่อนุญาตให้ใช้สายFS
Veskah

โอ้เข้าใจแล้วไม่ได้รับมัน
Quintec

@Quintec ได้รับรางวัลให้เป็นบิตที่ชัดเจนยิ่งขึ้นในกรณีที่
Veskah

คำตอบ:


4

Python 2 , 128 ไบต์

from random import*
def g(n):
 x=i=S=0;r=''
 while(S>2)+x<3>=i-S:x=randint(0,3);r+='BFSH'[x];S+=x>0;i+=1
 return(i==n)*r or g(n)

ลองออนไลน์!

สร้างสตริงพิทช์แบบสุ่มจนกระทั่งทำแป้งเสร็จแล้วเอาท์พุทถ้ามันออกมาเป็นความยาวที่เหมาะสมและลองอีกครั้งตั้งแต่เริ่มต้น


Python 2 , 136 ไบต์

from random import*
def g(n):
 B=H=F=S=0;r=''
 while(F+S<3or'S'>x)>B/4+H:x=choice('BHFS');r+=x;exec x+"+=1"
 return(len(r)==n)*r or g(n)

ลองออนไลน์!


พอร์ตของเควินทำให้สิ่งนี้ทำให้ฉันตระหนักว่าการหยุดพักเพื่อจำนวนที่สูงขึ้น n=8สามารถสร้างห่วงโซ่ของFs ในตอนท้าย
Veskah

2
@Veskah จับที่ดี ฉันไม่คิดว่าจะนับการตี (นับการฟาล์ว) อาจสูงถึง 6 และเปลี่ยนS/3เป็นการ(S>2)แก้ไข
xnor

4

05AB1E ,  44  50 44 ไบต์

ตัดออก&nbsp;44&nbsp;แล้วไม่ 44 :)

[õ0U.µ["BFSH"3ÝΩ©è«®ĀX+U¼X2›®+3@¾X-3›~#}I¾Q#

คำตอบ Python 2ของพอร์ตของ@xnor ดังนั้นโปรดอัปเกรดเขาเช่นกันถ้าคุณชอบคำตอบนี้!
+6 ไบต์เนื่องจากการแก้ไขข้อผิดพลาดและหลังจากนั้น -6 ไบต์อีกครั้งต้องขอบคุณ@xnorด้วยการแก้ไขวิธีที่มีประสิทธิภาพมากขึ้นเมื่อเปรียบเทียบกับการแก้ไขชั่วคราวของฉันตามที่ฉันคาดหวัง ;)

ลองมันออนไลน์หรือตรวจสอบบางส่วนเอาท์พุทแบบสุ่ม

คำอธิบาย:

[                # Start an infinite loop:
 õ               #  (Re)set the result-string to an empty string ""
 0U              #  (Re)set variable `X` to 0
               #  Reset the counter_variable to 0
   [             #  Start an inner infinite loop:
    "BFSH"       #   Push string "BFSH"
          3ÝΩ    #   Push a random integer in the range [0,3]
             ©   #   Store this random integer in variable `r` (without popping)
              è  #   Index it into the string "BFSH"
               « #   Append it to the result-string
    ®Ā           #   If `r` is NOT 0:
      X+U        #    Increase `X` by 1
    ¼            #   Increase the counter_variable by 1
    X2›®+        #   Calculate `X`>2 (1 if truthy; 0 if falsey) + `r`
         3@      #   Check if this is larger than or equal to 3
    ¾X-          #   Calculate counter_variable - `X`
       3        #   Check if this is larger than 3
    ~            #   If either of the two checks above is truhy:
     #           #    Stop the inner infinite loop
   }             #  After the inner infinite loop:
    I¾Q          #  If the input and counter_variable are equal:
       #         #   Stop the outer infinite loop
                 # (and output the result-string at the top of the stack implicitly)

1
@Veskah ฉันได้ทำการแก้ไขแบบตรงไปข้างหน้าในตอนนี้ ฉันมีความรู้สึกที่xnorสามารถทำการแก้ไขที่สั้นกว่าได้ดังนั้นฉันอาจจะทำการพอร์ตโปรแกรมแก้ไขของเขาเพื่อบันทึกไบต์ในภายหลัง :)
Kevin Cruijssen

1
คุณสามารถแก้ไขได้เช่นผมโดยการเปลี่ยนไปX/3 X>2
xnor

@xnor ขอบคุณกลับไปที่ 44 ไบต์อีกครั้ง ฉันรู้ว่าคุณจะพบบางสิ่งที่สั้นกว่านี้ ; p
Kevin Cruijssen

3

R , 148 ไบต์

function(n){`~`=paste0
`*`=sample
o=""
while(nchar(o)<n-1){a=c("B"[T<4],"F","S"[F<2])*1
F=F+(a>"E")
T=T+(a<"F")
o=o~a}
o~c("B"[T>3],"H","S"[F>1])*1}

ลองออนไลน์!

สร้างสตริงโดยใช้การรวมตามเงื่อนไขในชุดข้อมูลการสุ่มตัวอย่างเพื่อให้แน่ใจว่าผลลัพธ์เป็นลำดับพิชที่เป็นไปได้

การสุ่มตัวอย่างการปฏิเสธอาจเป็นไปได้ (เช่นเดียวกับคำตอบของงูเหลือม xnor ) จะสั้นกว่า

function(n){`~`=paste0		# alias
`*`=sample			# alias
o=""				# empty string for output
while(nchar(o)<n-1){		# do n-1 times:
a=c("B"[T<4],"F","S"[F<2])*1	# sample 1 from the string "BFS", conditionally including B or S if the ball/strike count is 3/2	
F=F+(a>"E")			# increment F (strike count) if sampled character is F or S
T=T+(a<"F")			# increment T (ball count) if sampled character is B
o=o~a}				# append a to output

o~c("B"[T>3],"H","S"[F>1])*1}	# append the sampled "BHS", conditionally including B or S if the ball/strike count is 3/2.

การอ้างอิง "F และ S"แบบสุ่มที่เก็บไว้ในหัวของฉันทุกครั้งที่ฉันพิมพ์จดหมายฉบับใดฉบับหนึ่ง ...



2

Pyth, 53 ไบต์

u+GO?H+W<K/G\B3+W<Jl@"SF"G2\F\S\B+WqK3+WgJ2\H\S\B_UQ[

ลองออนไลน์ได้ที่นี่

รู้สึกเช่นนี้นานเกินไปฉันคิดว่าอาจต้องมีวิธีการอื่น

u+GO?H+W<K/G\B3+W<Jl@"SF"G2\F\S\B+WqK3+WgJ2\H\S\B_UQ[   Implicit: Q=eval(input())
                                                 _UQ    Reversed range from Q-1 to 0
u                                                   [   Reduce the above, with initial value G=[], next value as H:
                    @"SF"G                                Keep elements of G which are in "SF"
                   l                                      Length of the above
                  J                                       Store in J - this is the number of strikes and fouls so far
          /G\B                                            Count number of "B"s in G
         K                                                Store in K - this is the number of balls so far
    ?H                                                    If H is not 0 (i.e. not final pitch):
                           \F                               Start with "F" (foul is always available in non-final pitch)
                W<J       2                                 If J<2...
               +             \S                             ... append "S"
       W<K    3                                             If K<3...
      +                        \B                           ... append "B"
                                                          Else:
                                           \H               Start with "H" (hit is always available in final pitch)
                                       WgJ2                 If J >= 2...
                                      +      \S             ... append "S"
                                  WqK3                      If K == 3...
                                 +             \B           ... append "B"
   O                                                      Choose one element at random from the available options
 +G                                                       Append the above to G
                                                        Implicit print the result of the reduce operation

2

JavaScript (ES6),  107 106  99 ไบต์

f=(n,k=p=s=0,o='')=>p&&p>2|k-s>3|s>2&p<2?k-n?f(n):o:f(n,k+1,o+'FSBH'[p=Math.random()*4|0,s+=p<2,p])

ลองออนไลน์!

แสดงความคิดเห็น

f = (                       // f = recursive function taking:
  n,                        //   n = requested length
  k =                       //   k = pitch counter, initialized to 0
  p =                       //   p = last pitch
  s = 0,                    //   s = sum of strikes and fouls
  o = ''                    //   o = output string
) =>                        //
  p &&                      // if the last pitch was not a foul
  p > 2 |                   // AND the last pitch was a hit
  k - s > 3 |               //     OR we have 4 balls (or 3 balls + 1 hit)
  s > 2 & p < 2 ?           //     OR more than 2 strikes or fouls, ending with a strike:
    k - n ?                 //   if k is not equal to n:
      f(n)                  //     valid series but bad timing: try again from scratch
    :                       //   else:
      o                     //     success: return o
  :                         // else:
    f(                      //   do a recursive call:
      n,                    //     n is unchanged
      k + 1,                //     increment k
      o + 'FSBH'            //     append the pitch letter to o
        [ p = Math.random() //     pick a new random pitch
              * 4 | 0,      //     in [0..3]
          s += p < 2,       //     increment s if the pitch is a foul or a strike
          p ]               //     actual index in 'FSBH'
    )                       //   end of recursive call

2

หมึก , 120 119 116 117 ไบต์

=f(n)
->g(n,3,2)
=g(n,b,s)
~n--
{n:{~{b:b->g(n,b-1,s)}|{s:s->g(n,b,s-1)}|}f->g(n,b,s-(s>0))|{~{b:h|b}|{s:h|s}|h}}->->

ลองออนไลน์!

อาจยังเล่นกอล์ฟได้

Ungolfed (ฟอร์แมตใหม่อย่างอ่อนโยน)

=f(length) // Define a stitch f, with one parameter which specifies the length of the created string. This is the intended entry point.
->g(length,3,2) // Instantly divert to g, defined below, with some extra parameters

=g(length,balls_left,strikes_left) // Define a stitch g, with three parameters.
~ length--                         // Decrement remaining length
{
    - length: // If this is not to be the last character in the string
              // randomly do one of the following:
              // 1. If balls_left is nonzero, print a b and recurse
              // 2. If strikes_left is nonzero, print an s and recurse
              // 3. Do nothing
              // If we did not divert earlier, print an f and recurse.
        {~{balls_left:b->g(length,balls_left-1,strikes_left)}|{strikes_left:s->g(length,balls_left,strikes_left-1)}|}f->g(length,balls_left,strikes_left-(strikes_left>0)) 
    - else: // Randomly do one of the following
            // 1. If a ball would result in a walk, print a b, otherwise an h.
            // 2. If a strike would result in a strikeout, print an s, otherwise an h.
            // 3. Just print an h.
            // And finally, halt.
        {~{balls_left:h|b}|{strikes_left:h|s}|h}}->->

การแก้ไข

  1. ที่บันทึกไว้ไบต์โดยจบด้วยแทน->->->END
  2. บันทึกสามไบต์ด้วยการลดลงnก่อนหน้านี้
  3. แก้ไขข้อผิดพลาดที่ทำให้เกิดการปะทะในสถานที่ที่ไม่ถูกต้องขอบคุณ @veskah สำหรับการตรวจจับ (+1 ไบต์)

1
จากการเขียนและเอาท์พุทดูเหมือนว่ามันจะไม่คำนึงถึงการผิดกติกาที่เพิ่มจำนวนการนัดหยุดงานอย่างถูกต้อง
Veskah

1
@veskah ด่างดีควรได้รับการแก้ไขแล้วขอบคุณ
Sara J


1

ถ่าน , 57 ไบต์

≔⁰η≔⁰ζF⊖N«≔‽⁺²‹ζ³ι¿›ι¹≦⊕ζ≦⊕η§SFB∨ι›η²»⊞υHF›η¹⊞υSF›ζ²⊞υB‽υ

ลองออนไลน์! การเชื่อมโยงคือการใช้รหัสเวอร์ชันอย่างละเอียด คำอธิบาย:

≔⁰η≔⁰ζ

เริ่มต้นด้วย 0 ลูกและ 0 นัด

F⊖N«

วนรอบการส่งมอบทั้งหมดยกเว้นครั้งสุดท้าย

≔‽⁺²‹ζ³ι

หากลูกบอลน้อยกว่าสามลูกให้สร้างตัวเลขสุ่มจาก 0 ถึง 2 มิฉะนั้นเพียงแค่โยนเหรียญระหว่าง 0 และ 1

¿›ι¹≦⊕ζ≦⊕η

ค่าสุ่ม 2 คือลูกบอลมิฉะนั้นจะเพิ่มจำนวนการนัดหยุดงาน

§SFB∨ι›η²»

แผนที่ค่า 0 ถึง 2 สำหรับการนัดหยุดงานการฟาล์วและลูกบอลยกเว้นว่าจะมีการนัดหยุดงานสามครั้งการทำฟาล์วจะถูกพิมพ์แทน (ไม่รวมสี่ลูกด้านบน)

⊞υHF›η¹⊞υSF›ζ²⊞υB‽υ

ตรวจสอบว่าการนัดหยุดงานหรือลูกจะได้รับการปะทะและเลือกจากผู้ที่ตีหรือตามความเหมาะสม



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