มีหมายเลขจากมากไปน้อยติดต่อกันในหมายเลข


18

2019 ได้มาแล้วและทุกคนอาจสังเกตเห็นถึงความผิดปกติของตัวเลขนี้: ในความเป็นจริงมันประกอบด้วยสองหมายเลขย่อย (20 และ 19) ที่แสดงถึงลำดับของตัวเลขจากมากไปหาน้อย

ท้าทาย

กำหนดตัวเลขxแล้วส่งคืนความยาวของลำดับสูงสุดของตัวเลขเรียงลำดับจากมากไปน้อยซึ่งสามารถสร้างxได้

หมายเหตุ:

  • ตัวเลขย่อยไม่สามารถมีศูนย์นำหน้า (เช่น1009ไม่สามารถแบ่งออก10ได้09)
  • ที่ต่อเนื่องกันและจากมากไปน้อยหมายความว่าตัวเลขในลำดับต้องเท่ากับตัวเลขก่อนหน้า -1 หรือni+1=ni1 (เช่น52ไม่สามารถแบ่งออกเป็น5,2เพราะ5และ2ไม่ต่อเนื่องกันได้2 ≠ 5 - 1)
  • ลำดับจะต้องได้รับโดยใช้เต็มจำนวนเช่นใน7321คุณไม่สามารถทิ้ง7และได้รับลำดับ3, 2,1
  • เพียงหนึ่งลำดับสามารถได้รับจากหมายเลขที่เช่น3211098ไม่สามารถจะแยกออกเป็นสองลำดับ3, 2, 1และ10, 9,8

อินพุต

  • จำนวนเต็ม ( >= 0): สามารถเป็นตัวเลขหรือสตริงหรือรายการของตัวเลข

เอาท์พุต

  • จำนวนเต็มเดียวที่กำหนดจำนวนสูงสุดของการลดจำนวนย่อย (โปรดทราบว่าขอบเขตล่างของตัวเลขนี้1คือจำนวนที่ประกอบขึ้นเองตามลำดับจากความยาวหนึ่ง)

ตัวอย่าง :

2019         --> 20,19           --> output : 2
201200199198 --> 201,200,199,198 --> output : 4
3246         --> 3246            --> output : 1
87654        --> 8,7,6,5,4       --> output : 5
123456       --> 123456          --> output : 1
1009998      --> 100,99,98       --> output : 3
100908       --> 100908          --> output : 1
1110987      --> 11,10,9,8,7     --> output : 5
210          --> 2,1,0           --> output : 3
1            --> 1               --> output : 1
0            --> 0               --> output : 1
312          --> 312             --> output : 1
191          --> 191             --> output : 1

กฎทั่วไป:

  • นี่คือดังนั้นคำตอบที่สั้นที่สุดในจำนวนไบต์ชนะ
    อย่าปล่อยให้ภาษาที่ใช้รหัสกอล์ฟกีดกันคุณจากการโพสต์คำตอบด้วยภาษาที่ไม่ codegolfing พยายามหาคำตอบสั้น ๆ ที่เป็นไปได้สำหรับภาษาโปรแกรม 'ใด ๆ '
  • กฎมาตรฐานใช้สำหรับคำตอบของคุณด้วยกฎ I / O เริ่มต้นดังนั้นคุณจึงได้รับอนุญาตให้ใช้ STDIN / STDOUT ฟังก์ชั่น / วิธีการที่มีพารามิเตอร์ที่เหมาะสมและประเภทผลตอบแทนโปรแกรมเต็มรูปแบบ การโทรของคุณ
  • ช่องโหว่เริ่มต้นเป็นสิ่งต้องห้าม
  • หากเป็นไปได้โปรดเพิ่มลิงก์พร้อมทดสอบรหัสของคุณ (เช่นTIO )
  • นอกจากนี้ขอแนะนำให้เพิ่มคำอธิบายสำหรับคำตอบของคุณ


1
กรณีทดสอบ210 -> 2,1,0ผิด0 -> 0หรือไม่? งานระบุว่า " ตัวเลขย่อยไม่สามารถมีศูนย์นำหน้า " เป็นศูนย์กรณีพิเศษหรือไม่?
ბიმო

2
@BMO: ดีที่นี่หัวข้อจะถูก phylosofical ครับ ... : D กับผม, 0 เป็นตัวเลขที่ไม่มี (ไม่มีประโยชน์) ศูนย์นำดังนั้นใช่ศูนย์เป็นกรณีพิเศษ
digEmAll

2
คุณจะเรียกว่า ... หมายเลขวางตัวเหรอ? xD ขอโทษที่ไม่ตลกแม้แต่น้อย
HyperNeutrino

212019ขออภัยลบความคิดเห็นของฉันที่ฉันถามเกี่ยวกับ ดูเหมือนว่าฉันไม่ได้อ่านกฎทั้งหมด
cyclaminist

คำตอบ:


6

เยลลี่ ,  15  9 ไบต์

แก้ไขข้อผิดพลาดขอบคุณเดนนิส

ŻṚẆDfŒṖẈṀ

ลองออนไลน์! (แม้321ใช้เวลาครึ่งนาทีเนื่องจากรหัสเป็นอย่างน้อยO(N2) )

อย่างไร?

ŻṚẆDfŒṖẈṀ - Link: integer, n
Ż         - [0..n]
 Ṛ        - reverse
  Ẇ       - all contiguous slices (of implicit range(n)) = [[n],...,[2],[1],[0],[n,n-1],...,[2,1],[1,0],...,[n,n-1,n-2,...,2,1,0]]
   D      - to decimal (vectorises)
     ŒṖ   - partitions of (implicit decimal digits of) n
    f     - filter discard from left if in right
       Ẉ  - length of each
        Ṁ - maximum

6

JavaScript (ES6), 56 ไบต์

พอร์ตของคำตอบ Python ของ ArBoนั้นสั้นกว่ามาก อย่างไรก็ตามมันล้มเหลวในบางกรณีทดสอบเนื่องจากการเรียกซ้ำมากเกินไป

f=(n,a=0,c=0,s)=>a<0?f(n,a-~c):n==s?c:f(n,--a,c+1,[s]+a)

ลองออนไลน์!


JavaScript (ES6), 66 ไบต์

รับอินพุตเป็นสตริง

f=(s,n=x='',o=p=n,i=0)=>s[i++]?o==s?i:f(s,--n,o+n,i):f(s,p+s[x++])

ลองออนไลน์!

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

f = (               // f = recursive function taking:
  s,                //   s = input number, as a string
  n =               //   n = counter
  x = '',           //   x = position of the next digit to be added to p
  o = p = n,        //   o = generated output; p = prefix
  i = 0             //   i = number of consecutive descending numbers
) =>                //
  s[i++] ?          // increment i; if s[i] was defined:
    o == s ?        //   if o is matching s:
      i             //     stop recursion and return i
    :               //   else:
      f(            //     do a recursive call with:
        s,          //       s unchanged
        --n,        //       n - 1
        o + n,      //       (n - 1) appended to o
        i           //       i unchanged (but it was incremented above)
      )             //     end of recursive call
  :                 // else:
    f(              //   this is a dead end; try again with one more digit in the prefix:
      s,            //     s unchanged
      p + s[x++]    //     increment x and append the next digit to p
    )               //   end of recursive call

54 ไบต์โดยใช้การเปลี่ยนแปลงกับรหัสของฉัน
ArBo

5

Perl 6 , 43 41 40 ไบต์

-1 ไบต์ต้องขอบคุณ nwellnhof

{/(<-[0]>.*?|0)+<?{[==] 1..*Z+$0}>/;+$0}

ลองออนไลน์!

โซลูชันที่ใช้ Regex ฉันพยายามหาวิธีที่ดีกว่าในการจับคู่จากรายการจากมากไปน้อย แต่ Perl 6 ไม่ได้ทำพาร์ทิชันได้ดี

คำอธิบาย:

{                                        }  # Anonymous code block
 /                                /;        # Match in the input
   <-[0]>.*?      # Non-greedy number not starting with 0
            |0    # Or 0
  (           )+  # Repeatedly for the rest of the number
                <?{             }>  # Where
                        1..*Z+$0       # Each matched number plus the ascending numbers
                                       # For example 1,2,3 Z+ 9,8,7 is 10,10,10
                   [==]                # Are all equal
                                    +$0  # Return the length of the list


4

Python 3 , 232 228 187 181 180 150 149 ไบต์

-1 ต้องขอบคุณ @ Jonathan Frech

e=enumerate
t=int
h=lambda n,s=1:max([1]+[i-len(n[j:])and h(n[j:],s+1)or s+1for j,_ in e(n)for i,_ in e(n[:j],1)if(t(n[:j])-t(n[j:j+i])==1)*t(n[0])])

ลองออนไลน์!

รหัส ungolfed เริ่มต้น:

def count_consecutives(left, right, so_far=1):
    for i,_ in enumerate(left, start=1):
        left_part_of_right, right_part_of_right = right[:i], right[i:]
        if (int(left) - int(left_part_of_right)) == 1:
            if i == len(right):
                return so_far + 1
            return count_consecutives(left_part_of_right, right_part_of_right, so_far + 1)
    return so_far

def how_many_consecutives(n):
    for i, _ in enumerate(n):
        left, right = n[:i], n[i:]
        for j, _ in enumerate(left, start=1):            
            left_part_of_right = right[:j]
            if int(left) - int(left_part_of_right) == 1 and int(n[i]) > 0:     
                return count_consecutives(left, right)
    return 1

1
s+1 forสามารถs+1for, อาจจะสามารถ(t(n[:j])-t(n[j:j+i])==1)*t(n[0]) t(n[:j])-t(n[j:j+i])==1>=t(n[0])
Jonathan Frech

ดูเหมือนว่าข้อเสนอแนะที่สองไม่ทำงานแม้ว่ามันจะไม่นำอะไรเลยเพราะคุณต้องการพื้นที่ในการแยกนิพจน์ออกจากifกัน
Nishioka

จริง ... ทางเลือกที่149
Jonathan Frech

4

Python 2 , 78 74 73 ไบต์

l=lambda n,a=0,c=0,s="":c*(n==s)or a and l(n,a-1,c+1,s+`a-1`)or l(n,a-~c)

ลองออนไลน์!

-1 ไบต์ขอบคุณ Arnauld

รับอินพุตเป็นสตริง โปรแกรมค่อนข้างเร็วไหลไปสู่ขีด จำกัด การเรียกซ้ำของ Python แต่มันสามารถจบการทดสอบส่วนใหญ่ได้

มันทำงานอย่างไร

l=lambda n,                              # The input number, in the form of a string
         a=0,                            # The program will attempt to reconstruct n by
                                         #  building a string by pasting decreasing
                                         #  numbers, stored in a, after each other.
         c=0,                            # A counter of the amount of numbers
         s="":                           # The current constructed string
              c*(n==s)                   # Return the counter if s matches n
              or                         # Else
              a and l(n,a-1,c+1,s+`a-1`) # If a is not yet zero, paste a-1 after s
              or                         # Else
              l(n,a-~c)                  # Start again, from one higher than last time

1
คำตอบที่ดี! สามารถลงไปa+c+1 a-~c
Arnauld

3

05AB1E , 10 ไบต์

ÝRŒʒJQ}€gà

ช้ามากดังนั้น TIO ด้านล่างใช้สำหรับกรณีทดสอบที่ต่ำกว่า 750 เท่านั้น

ลองมันออนไลน์

คำอธิบาย:

Ý           # Create a list in the range [0, (implicit) input]
            #  i.e. 109 → [0,1,2,...,107,108,109]
 R          # Reverse it
            #  i.e. [0,1,2,...,107,108,109] → [109,108,107,...,2,1,0]
  Œ         # Get all possible sublists of this list
            #  i.e. [109,108,107,...,2,1,0]
            #   → [[109],[109,108],[109,108,107],...,[2,1,0],[1],[1,0],[0]]
   ʒ  }     # Filter it by:
    J       #  Where the sublist joined together
            #   i.e. [10,9] → "109"
            #   i.e. [109,108,107] → "109108107"
     Q      #  Are equal to the (implicit) input
            #   i.e. 109 and "109" → 1 (truthy)
            #   i.e. 109 and "109108107" → 0 (falsey)
       g   # After filtering, take the length of each remaining inner list
            #  i.e. [[109],[[10,9]] → [1,2]
         à  # And only leave the maximum length (which is output implicitly)
            #  i.e. [1,2] → 2

2
กอล์ฟรหัสที่มา - ที่เพิ่ม 1 ไบต์ในการเขียนโปรแกรมของคุณจะไปจากn!ที่จะn lg nเพียงแค่จะไม่คุ้มค่า
corsiKa

3

Pyth, 16 ไบต์

lef!.EhM.+vMT./z

ลองออนไลน์ได้ที่นี่หรือตรวจสอบทุกกรณีการทดสอบในครั้งเดียวที่นี่

lef!.EhM.+vMT./z   Implicit: z=input as string
             ./z   Get all divisions of z into disjoint substrings
  f                Filter the above, as T, keeping those where the following is truthy:
          vMT        Parse each substring as an int
        .+           Get difference between each pair
      hM             Increment each
   !.E               Are all elements 0? { NOT(ANY(...)) }
 e                 Take the last element of the filtered divisions
                     Divisions are generated with fewest substrings first, so last remaining division is also the longest
l                  Length of the above, implicit print

3

เยลลี่ 11 ไบต์

ŒṖḌ’Dɗ\ƑƇẈṀ

O(n0.3)

ลองออนไลน์!

มันทำงานอย่างไร

ŒṖḌ’Dɗ\ƑƇẈṀ  Main link. Argument: n (integer)

ŒṖ           Yield all partitions of n's digit list in base 10.
        Ƈ    Comb; keep only partitions for which the link to the left returns 1.
       Ƒ       Fixed; yield 1 if calling the link to the left returns its argument.
      \          Cumulatively reduce the partition by the link to the left.
     ɗ             Combine the three links to the left into a dyadic chain.
  Ḍ                  Undecimal; convert a digit list into an integer.
   ’                 Decrement the result.
    D                Decimal; convert the integer back to a digit list.

3

ถ่าน 26 ไบต์

F⊕LθF⊕Lθ⊞υ⭆κ⁻I…θιλI﹪⌕υθ⊕Lθ

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

F⊕Lθ

วนiซ้ำตั้งแต่ 0 ถึงความยาวของอินพุต

F⊕Lθ

วนkซ้ำตั้งแต่ 0 ถึงความยาวของอินพุต

⊞υ⭆κ⁻I…θ⊕ιλ

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

I﹪⌕υθ⊕Lθ

ค้นหาตำแหน่งของสำเนาที่ตรงกันแรกของอินพุตและลด modulo 1 มากกว่าความยาวของอินพุต

ตัวอย่าง: สำหรับอินพุตของ2019สตริงต่อไปนี้ถูกสร้าง:

 0
 1  0
 2  0-1
 3  0-1-2
 4  0-1-2-3
 5  
 6  2
 7  21
 8  210
 9  210-1
10  
11  20
12  2019
13  201918
14  20191817
15  
16  201
17  201200
18  201200199
19  201200199198
20  
21  2019
22  20192018
23  201920182017
24  2019201820172016

2019 พบได้ที่ดัชนี 12 ซึ่งลดโมดูโล 5 เพื่อให้ 2 คำตอบที่ต้องการ


3

Haskell, 87 ไบต์

maximum.map length.(0#)
a#(b:c)=[a:x|c==[]||b>0,x<-b#c,a==x!!0+1]++(10*a+b)#c
a#b=[[a]]

อินพุตคือรายการของตัวเลข

ลองออนไลน์!

ฟังก์ชัน#สร้างรายการแยกที่เป็นไปได้ทั้งหมดโดยดูที่ทั้งสอง

  • การเตรียมหมายเลขปัจจุบันaให้กับการแยกทั้งหมดที่ส่งคืนโดยการเรียกซ้ำด้วยส่วนที่เหลือของอินพุต ( x<-b#c) แต่เฉพาะในกรณีที่หมายเลขถัดไปไม่ใช่ศูนย์ ( b>0) (หรือเป็นตัวเลขสุดท้ายในอินพุต ( c==[])) และaเป็นหมายเลขที่มากกว่าครั้งแรก จำนวนของการแบ่งก่อนหน้านั้นx( a==x!!0+1)

และ

  • ต่อท้ายตัวเลขถัดไปbจากรายการอินพุตไปยังหมายเลขปัจจุบันaและดำเนินต่อไปกับส่วนที่เหลือของอินพุต ( (10*a+b)#c)

กรณีฐานคือเมื่อรายการอินพุตว่างเปล่า (เช่นไม่ตรงกับรูปแบบ(b:c)) การเรียกซ้ำเริ่มต้นด้วยหมายเลขปัจจุบันaคือ0( (0#)) ซึ่งไม่เคยกระทบกับสาขาแรก ( aเตรียมการแยกก่อนหน้านี้ทั้งหมด) เนื่องจากจะไม่มีค่ามากกว่าจำนวนของการแยกใด ๆ

ใช้ความยาวของแต่ละการแยกและค้นหาค่าสูงสุด ( maximum.map length)

ตัวแปรที่มีขนาด 87 ไบต์ด้วย:

fst.maximum.(0#)
a#(b:c)=[(r+1,a)|c==[]||b>0,(r,x)<-b#c,a==x+1]++(10*a+b)#c
a#b=[(1,a)]

ซึ่งโดยทั่วไปจะทำงานในลักษณะเดียวกัน แต่แทนที่จะรักษาแยกทั้งในรายการก็เพียง แต่ช่วยให้คู่(r,x)ของความยาวของการแยกจำนวนครั้งแรกในการแยกrx


3

Python 3 , 302 282 271 ไบต์

-10 ไบต์ขอบคุณคำแนะนำโดย @ElPedro

รับอินพุตเป็นสตริง โดยทั่วไปจะเพิ่มจำนวนชิ้นใหญ่ขึ้นจากด้านซ้ายและดูว่าสำหรับส่วนของหมายเลขนั้นสามารถสร้างลำดับโดยใช้ตัวเลขทั้งหมดได้หรือไม่

R=range
I=int
L=len
def g(n,m,t=1):
 for i in R(1,L(m)+1):
  if I(m)==I(n[:i])+1:
   if i==L(n):return-~t
   return g(n[i:],n[:i],t+1)
 return 1
def f(n):
 for i in R(L(n)):
  x=n[:i]
  for j in R(1,L(x)+1):
   if (I(x)==I(n[i:i+j])+1)*I(n[i]):return g(n[i:],x)
 return 1

ลองออนไลน์!


1
เนื่องจากคุณใช้range3 ครั้งคุณสามารถกำหนดR=rangeนอกฟังก์ชั่นทั้งสองแล้วใช้R(whatever)แทนrange(whatever)การบันทึก 4 ไบต์
ElPedro

3

Japt , 27 ไบต์

ò pÊÔpÊqÊfl²i1Uì q"l?"¹ÌèÊÉ

ลองออนไลน์! หรือตรวจสอบกรณีทดสอบส่วนใหญ่

มันไม่ได้คะแนนดีนัก แต่ใช้วิธีการเฉพาะและอาจมีห้องสำหรับตีกอล์ฟเพิ่มมากขึ้น นอกจากนี้ยังทำงานได้ดีพอที่กรณีทดสอบทั้งหมดนอกเหนือจากการ201200199198หลีกเลี่ยงการหมดเวลา

คำอธิบาย:

ò                              #Get the range [0...input]
  pÊ                           #Add an "l" to the end
    Ô                          #Reverse it
     pÊ                        #Add an "l" to the end
       qÊ                      #Add an "l" between each number and turn to a string
         f            ¹        #Find the substrings that match this regex:
          l²                   # The string "ll"
            i1                 # With this inserted between the "l"s:
              Uì               #  All the digits of the input
                 q"l?"         #  With optional spaces between each one
                       Ì       #Get the last match
                        èÊ     #Count the number of "l"s
                          É    #Subtract 1

ฉันคิดว่ามันใช้ได้กับ 27.
Shaggy


@Shaggy ทั้งสองล้มเหลวในการป้อนข้อมูล21201เพราะพวกเขาไม่ได้บังคับให้ลำดับท้ายจัดอย่างถูกต้อง (จากรุ่นเดิมของฉันบรรทัด "ลงท้ายด้วยเครื่องหมายจุลภาค") นี้หรือนี้ผลงานทางเลือก
Kamil Drakari

อาโอเค. ในกรณีนี้: 26 ไบต์
Shaggy

@Shaggy That และ 28 byte solution ฉันล้มเหลว210เพราะไม่มีตัวคั่นหลังจาก 0 นี่คือ 28 byte ที่แน่นอนที่ทำงานได้
Kamil Drakari

2

Haskell, 65 ไบต์

f i=[y|x<-[0..],y<-[1..length i],i==(show=<<[x+y-1,x+y-2..x])]!!0

อินพุตเป็นสตริง

ลองออนไลน์!

แตกต่างอย่างสิ้นเชิงของฉันคำตอบอื่นแรงเดรัจฉานที่เรียบง่ายที่พยายามรายการทั้งหมดของตัวเลขจากมากไปหาน้อยติดต่อกันจนกว่าจะพบหนึ่งที่เท่ากับรายการอินพุต

ถ้าเรา จำกัด จำนวนการป้อนข้อมูลเพื่อจำนวนเต็ม 64 บิตเราสามารถบันทึก 6 ไบต์โดยบ่วงyผ่าน[1..19]เพราะที่ใหญ่ที่สุด 64 บิตจำนวนเต็มมี 19 หลักและมีความจำเป็นที่จะต้องทดสอบรายการที่มีองค์ประกอบอื่น ๆ ไม่มี

Haskell, 59 ไบต์

f i=[y|x<-[0..],y<-[1..19],i==(show=<<[x+y-1,x+y-2..x])]!!0

ลองออนไลน์!



2

Dyalog APL, 138 ไบต์

บิตของคำ แต่มันทำงานได้อย่างรวดเร็วสำหรับจำนวนมากเกินไป หากคุณลองออนไลน์ให้ใส่คำนำหน้า dfn ด้วย⎕←และป้อนข้อมูลทางด้านขวาเป็นรายการตัวเลข

{⌈/⍵((≢⊂)×1∧.=2-/10⊥¨⊂)⍨⍤1⊢1,{⍬≡1↓⍵:↑⍬1⋄0=⊃⍵:0,∇1↓⍵⋄↑,0 1∘.,⊂⍤1∇1↓⍵}1↓⍵}

คำอธิบาย

ขั้นแรกให้ dfn ด้านในทางด้านขวาซึ่งสร้างรายการวิธีที่เป็นไปได้ที่จะแบ่งพาร์ติชัน (ด้วย) รายการของตัวเลขซ้ำ ตัวอย่างเช่นผลตอบแทนเวกเตอร์ที่ซ้อนกัน1 0 1 0 ⊂ 2 0 1 9(2 0)(1 9)

{
   ⍬≡1↓⍵: ↑⍬1       ⍝ Edge case: If ⍵ is singleton list, return the column matrix (0 1)
   0=⊃⍵: 0,∇1↓⍵     ⍝ If head of ⍵ is 0, return 0 catenated to this dfn called on tail ⍵
   ↑,0 1∘.,⊂⍤1∇1↓⍵  ⍝ Finds 1 cat recursive call on tail ⍵ and 0 cat recursive call on ⍵. 
}                    ⍝ Makes a matrix with a row for each possibility.

เราใช้1,เพื่อเพิ่มคอลัมน์ 1s ที่จุดเริ่มต้นและท้ายด้วยเมทริกซ์ของพาร์ติชันที่ถูกต้องสำหรับ⍵

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

((≢⊂)×1∧.=2-/10⊥¨⊂)⍨     ⍝ ⍨ swaps left and right arguments of the train.
                  ⊂       ⍝ Partition ⍵ according to ⍺. 
             10⊥¨         ⍝ Decode each partition (turns strings of digits into numbers)
          2-/             ⍝ Difference between adjacent cells
      1∧.=                ⍝ All equal 1?
   ⊂                      ⍝ Partition ⍵ according to ⍺ again
  ≢                       ⍝ Number of cells (ie number of partitions)
     ×                    ⍝ Multiply.

หากพาร์ติชันสร้างลำดับของตัวเลขจากมากไปน้อยรถไฟจะคืนค่าความยาวของลำดับ มิฉะนั้นศูนย์

⍤1⊢ใช้การฝึกฟังก์ชั่นระหว่างอินพุตของผู้ใช้และแต่ละแถวของเมทริกซ์พาร์ติชันโดยคืนค่าสำหรับแต่ละแถวของเมทริกซ์ มีความจำเป็นต้องกระจ่างระหว่างถูกดำเนินการและอาร์กิวเมนต์ฟังก์ชั่นที่ได้รับของ

⌈/ ค้นหาสูงสุด

สามารถหาอัลกอริธึมที่สั้นกว่าได้ แต่ฉันต้องการลองวิธีนี้ซึ่งเป็นวิธีที่ตรงที่สุดและเป็นไปได้ที่ฉันคิดได้


ยินดีต้อนรับสู่ PPCG! นี่เป็นโพสต์แรกที่น่าประทับใจ!
Rɪᴋᴇʀ

1

TSQL, 169 ไบต์

หมายเหตุ: สามารถดำเนินการได้เฉพาะเมื่ออินพุตสามารถแปลงเป็นจำนวนเต็มได้

SQL แบบเรียกซ้ำใช้สำหรับการวนซ้ำ

แข็งแรงเล่นกอล์ฟ:

DECLARE @ varchar(max) = '1211109876';

WITH C as(SELECT left(@,row_number()over(order by 1/0))+0t,@+null z,0i
FROM spt_values UNION ALL
SELECT t-1,concat(z,t),i+1FROM C WHERE i<9)SELECT
max(i)FROM C WHERE z=@

Ungolfed:

DECLARE @ varchar(max) = '1211109876';

WITH C as
(
  SELECT
    left(@,row_number()over(order by 1/0))+0t,
    @+null z,
    0i
  FROM
    spt_values
  UNION ALL
  SELECT
    t-1,
    concat(z,t),
    i+1
  FROM C
  WHERE i<9
)
SELECT max(i)
FROM C
WHERE z=@

ลองดู


0

R , 101 ไบต์

function(a,N=nchar(a)){for(x in 1:N)F=max(F,which(Reduce(paste0,seq(substr(a,1,x),,-1,N),a=T)==a));F}

ลองออนไลน์!

มากกว่า 2 สัปดาห์ที่ผ่านมาโดยไม่มีคำตอบ R ดังนั้นฉันตัดสินใจที่จะโพสต์ของตัวเอง :)

รหัสนี้ค่อนข้างเร็วเนื่องจากใช้วิธีการ จำกัด แบบเดรัจฉาน

รหัสที่ไม่ได้ควบคุมและคำอธิบาย:

function(a){                  # get string a as input (e.g. "2019")

  N = nchar(a)                # set N = length of a (e.g. 4)
  Y = 0                       # initialize Y = 0 (in the actual code we abuse F)

  for(x in 1:N){              # for x in 1 ... N    

    S = substr(a,1,x)         # get the first x characters of a (e.g. "20" for x=2)

    Q = seq(S,,-1,N)          # create a decreasing sequence (step = -1) 
                              # of length N starting from S converted into integer
                              # (e.g. Q = c(20,19,18,17) for x=2)

    R = Reduce(paste0,Q,a=T)  # concatenate all the increasing sub-sequences of Q
                              # (e.g. R = c("20","2019","201918","20191817") for x=2)

    I = which(R == a)         # Get the index where R == a, if none return empty vector
                              # (e.g. I = 2 for x=2)

    Y = max(Y,I)              # store the maximum index found into Y
  }
  return(Y)                   # return Y
}
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.