배열 런 찾기


14

배열 내에서 런 찾기

런은 이전 단계에서 일정한 단계로 증가하는 3 개 이상의 숫자로 정의됩니다. 예를 들어 [1,2,3]은 1 단계에서 실행되고 [1,3,5,7]은 2 단계에서 실행되며 [1,2,4,5]는 실행이 아닙니다.

이 런을 "i to j by s"표기법으로 표현할 수 있습니다. 여기서 i는 첫 번째 런 수이고, j는 마지막 런 수이며 s는 단계입니다. 그러나, 단계 1의 실행은 "i 내지 j"로 표현 될 것이다.

따라서 이전에 배열을 사용하면 다음을 얻습니다.

  • [1,2,3]-> "1to3"

  • [1,3,5,7]-> "1to7by2"

  • [1,2,4,5]-> "12 25"

이 문제에서는 여러 번 실행할 수있는 어레이에 대해이 작업을 수행해야합니다.

재귀가있는 예제 파이썬 코드 :

def arr_comp_rec(a, start_index):
    # Early exit and recursion end point
    if start_index == len(a)-1:
        return str(a[-1])
    elif start_index == len(a):
        return ''

    # Keep track of first delta to compare while searching
    first_delta = a[start_index+1] - a[start_index]
    last = True
    for i in range(start_index, len(a)-1):
        delta = a[i+1] - a[i]
        if delta != first_delta:
            last = False
            break
    # If it ran through the for loop, we need to make sure it gets the last value
    if last: i += 1

    if i - start_index > 1:
        # There is more than 2 numbers between the indexes
        if first_delta == 1:
            # We don't need by if step = 1
            return "{}to{} ".format(a[start_index], a[i]) + arr_comp_rec(a, i+1)
        else:
            return "{}to{}by{} ".format(a[start_index], a[i], first_delta) + arr_comp_rec(a, i+1)
    else:
        # There is only one number we can return
        return "{} ".format(a[start_index]) + arr_comp_rec(a, i)

유연한 IO

입력

정렬 된 양의 정수 배열 (중복 없음)

산출

공백으로 구분 된 런의 문자열 또는 런의 문자열 배열

특정 방향으로 탐욕 할 필요가 없습니다

후행 공백을 가질 수 있음

테스트 사례

In: [1000, 1002, 1004, 1006, 1008, 1010]
Out: "1000to1010by2"

In: [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233]
Out: "1to3 5 8 13 21 34 55 89 144 233"

In: [10, 20, 30, 40, 60]
Out: "10to40by10 60"

In: [5, 6, 8, 11, 15, 16, 17]
Out: "5 6 8 11 15to17"

In: [1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 15, 30, 45, 50, 60, 70, 80, 90, 91, 93]
Out: "1to7 9to15by2 30 45 50to90by10 91 93"

이것은 이므로 바이트 수가 가장 적습니다.



2
왼쪽에서 오른쪽으로 탐욕 스럽습니까? (즉 ?가 [4, 5, 6, 7, 9, 11, 13, 15]될 수 없음 4to6 7to15by2)
Jonathan Allan

1
@JonathanAllan 아니오, 반드시 탐욕스럽게 남겨 둘 필요는 없습니다.
WretchedLout

중복 항목이 없을 것이라고 생각합니까?
Shieru Asakoto

1
긍정적 인 정수만. @ Οurous 후행 공백은 허용됩니다.
WretchedLout

답변:


5

젤리 ,  42  40 바이트

Kevin Cruijssen 덕분에 -2 ( ḟ22를 0으로 바꾸지 않고 2를 걸러냅니다 2,0y)

ŒṖIE×LƲ€ḟ2SƊÞṪµ.ịU,Iḟ1ƊQ€Fż“to“by”ṁṖƊ$)K

결과를 인쇄하는 전체 프로그램.
(모나 딕 링크로 정수와 문자가 혼합 된 목록이 생성됩니다)

온라인으로 사용해보십시오!
(가장 큰 테스트 사례가 60 초 이내에 완료 되기에는 너무 비효율적이므로 제거했습니다[1,2,3,4].)

어떻게?

ŒṖIE×LƲ€ḟ2SƊÞṪµ.ịU,Iḟ1ƊQ€Fż“to“by”ṁṖƊ$)K - Main Link: list of numbers
ŒṖ                                       - all partitions
           ƊÞ                            - sort by last three links as a monad:
      Ʋ€                                 -   last four links as a monad for €ach:
  I                                      -     incremental differences (of the part)
   E                                     -     all equal?
     L                                   -     length (of the part)
    ×                                    -     multiply
        ḟ2                               -   filter discard twos
          S                              -   sum
             Ṫ                           - tail (gets us the desired partition of the input)
              µ                       )  - perform this monadic chain for €ach:
               .                         -   literal 0.5
                ị                        -   index into (the part) - getting [tail,head]
                 U                       -   upend - getting [head,tail]
                      Ɗ                  -   last three links as a monad:
                   I                     -     incremental differences (of the part)
                     1                   -     literal one
                    ḟ                    -     filter discard (remove the ones)
                  ,                      -   pair -> [[head,tail],[deltasWithoutOnes]]
                       Q€                -   de-duplicate €ach -> [[head,tail],[delta]] or [[head,tail],[]] or [[loneValue],[]]
                         F               -   flatten -> [head,tail,delta] or [head,tail] or [loneValue]
                                     $   -   last two links as a monad:
                                    Ɗ    -     last three links as a monad:
                           “to“by”       -       literal list [['t', 'o'], ['b', 'y']]
                                   Ṗ     -       pop (get flattened result without rightmost entry)
                                  ṁ      -       mould ["to","by"] like that (i.e. ["to","by"] or ["to"] or [])
                         ż               -     zip together      
                                       K - join with spaces
                                         - implicit print

단계 = 1 인 경우 'by'를 사용하면 안됩니다.
WretchedLout

oh Gawwwd : p 고마워요!
Jonathan Allan

2,0ySƲÞḟ2SƊÞ-2 바이트 로 골프를 칠 수 있습니다 .
Kevin Cruijssen

@KevinCruijssen 매우 사실, 멋진 골프, 감사합니다
Jonathan Allan

1
@KevinCruijssen 참고로 나는 그것이 왜 지금 있었는지 기억합니다. 원래 원래 P는 합계가 아닌 제품으로 정렬을 시도 S했지만 0이 필요했을 것입니다.
Jonathan Allan

6

스위프트, 246 바이트

func f(_ a:[Int]){
var c=a.count,m=a+a,z=0,r:String=""
for i in 1..<c{m[i]-=a[i-1];if m[i]==m[i-1]{m[i-1]=0}};m[0]=1
for i in 0..<c{if m[i]==0 {z=1}else if z==0{r+=" \(a[i])"}else{r+="to\(a[i])"+(m[i]>1 ? "by\(m[i])":"");z=0;m[i+1]=1}}
print(r)
}

온라인으로 사용해보십시오!


4
프로그래밍 퍼즐 및 코드 골프에 오신 것을 환영합니다! 아주 좋은 첫 번째 대답. 스위프트 커뮤니티가 정말 작기 때문에 계속 머무르기를 바랍니다.
Mr. Xcoder



3

JavaScript (ES6), 129 바이트

문자열 배열을 반환합니다.

a=>(a.map((n,i)=>n+[n-a[i+1]||''])+'').replace(/(\d+)(-\d+)(?:,\d+\2)+,(\d+)/g,(_,a,b,c)=>a+'to'+(~b?c+'by'+-b:c)).split(/-\d+,/)

온라인으로 사용해보십시오!

어떻게?

1 단계

우리는 먼저 각 숫자에 선행으로 구성되는 접미사를 붙이고 '-'마지막 숫자는 변경되지 않은 것을 제외하고 다음 숫자와의 차이를 붙 입니다. 이 새로운 배열은 문자열로 강제 변환됩니다.

(a.map((n, i) => n + [n - a[i + 1] || '']) + '')

예:

Input : [ 1, 2, 3, 5, 9, 11, 13, 20 ]
Output: "1-1,2-1,3-2,5-4,9-2,11-2,13-7,20"

2 단계

결과 문자열에서 모든 실행을 식별하고 적절한 표기법으로 대체합니다.

.replace(
  /(\d+)(-\d+)(?:,\d+\2)+,(\d+)/g,
  (_, a, b, c) => a + 'to' + (~b ? c + 'by' + -b : c)
)

예:

Input : "1-1,2-1,3-2,5-4,9-2,11-2,13-7,20"
Output: "1to3-2,5-4,9to13by2-7,20"

3 단계

마지막으로 후행 쉼표를 포함하여 나머지 접미사로 문자열을 분할합니다.

.split(/-\d+,/)

예:

Input : "1to3-2,5-4,9to13by2-7,20"
Output: [ '1to3', '5', '9to13by2', '20' ]

2

루비 , 125 118 바이트

->a{i=y=0;a.chunk{|x|-y+y=x}.map{|z,w|2-i<(s=w.size)?"#{w[i*s]}to#{w[~i=0]}"+"by#{z}"*(z<=>1)+' ':' '*i+w*' '*i=1}*''}

온라인으로 사용해보십시오!

설명

Ruby 's Enumerable에는 chunk여기서 필요한 것을 정확하게 수행 하는 유용한 방법이 있습니다.이 경우 블록에서 동일한 반환 값을 연속으로 실행하여 항목을 그룹화합니다 (이 경우 현재 x( y) 값 과 이전 ( ) 값 의 차이 ).

주의 할 점은 이러한 전략이 런의 첫 번째 요소를 캡처하지 않는다는 것입니다. 예를 들어 여기에서는 두 개의 마지막 요소 만 함께 그룹화됩니다.

Input: [5, 6, 8, 11, 15, 16, 17]
Grouped: [[5, [5]], [1, [6]], [2, [8]], [3, [11]], [4, [15]], [1, [16, 17]]]

따라서 올바른 형식의 문자열에 매핑하는 동안 새로운 잠재적 실행 (> 항목이 1 개인 청크)이 발생하면 이전 항목이 단일인지 ( i=1) 이미 다른 실행에 이미 사용 되었는지 ( ) 추적해야합니다 i=0. 사용하지 않는 단일 항목이 있으면 실행의 시작점이되고 청크 크기 임계 값을 3에서 2로 낮 춥니 다.


2

R , 180 175 바이트

r=rle(c(0,diff(a<-scan())));for(j in 1:sum(1|r$l)){l=r$l[j];v=r$v[j];i=T+l-1;cat("if"(l>2-F,paste0(a[T][!F],"to",a[i],"by"[v>1],v[v>1]," "),c("",a[T:i])[-3^F]));T=i+1;F=l<3-F}

온라인으로 사용해보십시오!

개념적으로 이것은 기술적으로는 약간 다르지만 Ruby 답변 의 포트입니다 .

JayCe가 5 바이트를 절약했습니다.


나는 무언가를 원했지만 rle너무 게으른 ... sum(1|x)대신에 1 바이트를 절약 할 수 있습니다 length(x): TIO
JayCe

실제로 cat 175 바이트에 대해 1 을 가질 수 있습니다 . TIO
JayCe

아, 감사합니다!
Kirill L.

2

R , 238217 바이트

-19 바이트에 대해 @digEmAll에게 감사합니다.

function(v,R=rle(diff(v)),L=R$l,S=sum(L|1),Y=0)if(!S)cat(v)else for(i in 1:S){D=R$v[i]
N=L[i]-F
if(N>1){N=N+1
cat(v[Y+1],'to',v[Y+N],'by'[D>1],D[D>1],' ',sep='')
F=1}else{N=N+(i==S)
if(N>0)cat(v[Y+1:N],'')
F=0}
Y=Y+N}

온라인으로 사용해보십시오!


이미 초기화되어 있기 때문에 F대신 사용 하십시오. 몇 바이트를 절약해야한다고 생각합니다. n0
주세페

이것은 좌절입니다. 당신은 사용하여 거의 다 왔어 split, diff하고 rle. 불행히도 달리기에 대한 탐욕스러운 검색은 많은 문제를 의미합니다.
제이

214 바이트 거의 유일한 문제는 때때로
잘못된

'by'[D>1]좋은 트릭입니다.
제이

이것은 잘 작동 것 같다 217 바이트
digEmAll






1

gvm (커밋 2612106) ) 바이트 코드, 108 바이트

한 줄에 배열의 크기를 예상 한 다음 멤버를 한 줄에 기대합니다.

16 진 덤프 :

> hexdump -C findruns.bin
00000000  e1 0a 00 10 00 e1 0b 00  ff c8 92 00 f4 f7 10 00  |................|
00000010  01 b0 20 03 00 ff 0a 01  a2 01 c8 92 00 f4 01 c0  |.. .............|
00000020  03 00 ff 0a 02 d0 72 01  0a 03 c8 92 00 f4 05 b0  |......r.........|
00000030  20 a2 02 c0 02 02 6a 03  8b 00 ff f6 06 b0 20 a2  | .....j....... .|
00000040  02 f4 ce 0a 02 c8 92 00  f6 07 6a 03 8b 00 ff f6  |..........j.....|
00000050  f2 b9 66 01 a2 02 00 01  8a 03 f6 05 b9 69 01 a2  |..f..........i..|
00000060  03 92 00 f4 ac c0 74 6f  00 62 79 00              |......to.by.|
0000006c

시운전 :

> echo -e "7\n5\n6\n8\n11\n15\n16\n17\n" | ./gvm findruns.bin
5 6 8 11 15to17
> echo -e "20\n1\n2\n3\n4\n5\n6\n7\n9\n11\n13\n15\n30\n45\n50\n60\n70\n80\n90\n91\n93\n" | ./gvm findruns.bin
1to7 9to15by2 30 45 50to90by10 91 93

이로부터 수동 조립 :

0100  e1        rud                     ; read length of array
0101  0a 00     sta     $00             ; -> to $00
0103  10 00     ldx     #$00            ; loop counter
                  readloop:
0105  e1        rud                     ; read unsigned number
0106  0b 00 ff  sta     $ff00,x         ; store in array at ff00
0109  c8        inx                     ; next index
010a  92 00     cpx     $00             ; length reached?
010c  f4 f7     bne     readloop        ; no -> read next number
010e  10 00     ldx     #$00            ; loop counter
0110  01                                ; 'lda $20b0', to skip next instruction
                  runloop:
0111  b0 20     wch     #' '            ; write space character
0113  03 00 ff  lda     $ff00,x         ; load next number from array
0116  0a 01     sta     $01             ; -> to $01
0118  a2 01     wud     $01             ; and output
011a  c8        inx                     ; next index
011b  92 00     cpx     $00             ; length reached?
011d  f4 01     bne     compare         ; if not calculate difference
011f  c0        hlt                     ; done
                  compare:
0120  03 00 ff  lda     $ff00,x         ; load next number from array
0123  0a 02     sta     $02             ; -> to $01
0125  d0        sec                     ; calculate ...
0126  72 01     sbc     $01             ; ... difference ...
0128  0a 03     sta     $03             ; ... to $03
012a  c8        inx                     ; next index
012b  92 00     cpx     $00             ; length reached?
012d  f4 05     bne     checkrun        ; if not check whether we have a run
012f  b0 20     wch     #' '            ; output space
0131  a2 02     wud     $02             ; output number
0133  c0        hlt                     ; done
                  checkrun:
0134  02 02     lda     $02             ; calculate next ...
0136  6a 03     adc     $03             ; ... expected number in run
0138  8b 00 ff  cmp     $ff00,x         ; compare with real next number
013b  f6 06     beq     haverun         ; ok -> found a run
013d  b0 20     wch     #' '            ; otherwise output space ...
013f  a2 02     wud     $02             ; ... and number
0141  f4 ce     bne     runloop         ; and repeat searching for runs
                  haverun:
0143  0a 02     sta     $02             ; store number to $02
0145  c8        inx                     ; next index
0146  92 00     cpx     $00             ; length reached?
0148  f6 07     beq     outputrun       ; yes -> output this run
014a  6a 03     adc     $03             ; calculate next expected number
014c  8b 00 ff  cmp     $ff00,x         ; compare with real next number
014f  f6 f2     beq     haverun         ; ok -> continue parsing run
                  outputrun:
0151  b9 66 01  wtx     str_to          ; write "to"
0154  a2 02     wud     $02             ; write end number of run
0156  00 01     lda     #$01            ; compare #1 with ...
0158  8a 03     cmp     $03             ; ... step size
015a  f6 05     beq     skip_by         ; equal, then skip output of "by"
015c  b9 69 01  wtx     str_by          ; output "by"
015f  a2 03     wud     $03             ; output step size
                  skip_by:
0161  92 00     cpx     $00             ; length of array reached?
0163  f4 ac     bne     runloop         ; no -> repeat searching for runs
0165  c0        hlt                     ; done
                  str_to:
0166  74 6f 00                          ; "to"
                  str_by:
0169  62 79 00                          ; "by"
016c

1

05AB1E (레거시) , 49 50 바이트

.œʒε¥Ë}P}Σ€g2KO>}¤εD©g≠i¬s¤„toý¬®¥0èDU≠i„byX««]˜ðý

너무 길지만 이미 작동하고있어 기쁩니다. 이 도전은 생각보다 훨씬 어렵습니다. 의심의 여지없이 더 골프를 타실 수 있습니다.
Σ€g2KO>}¤의 포트 2,0ySƲÞṪ 에서 @JonathanAllan 의 젤리 답변 (감사합니다!).

온라인으로 사용해보십시오. (참고 : 큰 테스트 사례의 시간이 초과되었습니다.)

0정렬시 항상 후행 위치에 배치 되므로 버그 수정으로 +1 바이트 .

설명:

                       # Get all partions of the (implicit) input-list
                         #  i.e. [1,2,3,11,18,20,22,24,32,33,34]
                         #   → [[[1],[2],[3],[11],[18],[20],[22],[24],[32],[33],[34]],
                         #      [[1],[2],[3],[11],[18],[20],[22],[24],[32],[33,34]],
                         #      [[1],[2],[3],[11],[18],[20],[22],[24],[32,33],[34]],
                         #      ...]
  ʒ     }                # Filter this list by:
   ε  }                  #  Map the current sub-list by:
    ¥Ë                   #   Take the deltas, and check if all are equal
                         #    i.e. [1,2,3] → [1,1] → 1
                         #    i.e. [1,2,3,11] → [1,1,8] → 0
       P                 #  Check if all sub-list have equal deltas
  Σ      }               # Now that we've filtered the list, sort it by:
   g                    #  Take the length of each sub-list
                         #   i.e. [[1,2,3],[11,18],[20,22,24],[32,33],[34]]
                         #    → [3,2,3,2,1]
                         #   i.e. [[1,2,3],[11],[18,20,22,24],[32,33,34]]
                         #    → [3,1,4,3]
     2K                  #  Remove all 2s
                         #   i.e. [3,2,3,2,1] → ['3','3','1']
       O                 #  And take the sum
                         #   i.e. ['3','3','1'] → 7
                         #   i.e. [3,1,4,3] → 11
        >                #  And increase the sum by 1 (0 is always trailing when sorting)
          ¤              # And then take the last item of this sorted list
                         #  i.e. for input [1,2,3,11,18,20,22,24,32,33,34]
                         #   → [[1,2,3],[11],[18,20,22,24],[32,33,34]]
  ε                      # Now map each of the sub-lists to:
   D©                    #  Save the current sub-list in the register
     gi                 #  If its length is not 1:
                         #    i.e. [11] → 1 → 0 (falsey)
                         #    i.e. [18,20,22,24] → 4 → 1 (truthy)
        ¬s¤              #   Take the head and tail of the sub-list
                         #    i.e. [18,20,22,24] → 18 and 24
           toý          #   And join them with "to"
                         #    i.e. 18 and 24 → ['18to24', '18to20to22to24']
               ¬         #   (head to remove some access waste we no longer need)
                         #    i.e. ['18to24', '18to20to22to24'] → '18to24'
        ®                #   Get the sub-list from the register again
         ¥               #   Take its deltas
                         #    i.e. [18,20,22,24] → [2,2,2]
          0è             #   Get the first item (should all be the same delta)
                         #    i.e. [2,2,2] → 2
            DU           #   Save it in variable `X`
              i         #   If the delta is not 1:
                         #     i.e. 2 → 1 (truthy)
                byX«    #    Merge "by" with `X`
                         #     i.e. 2 → 'by2'
                     «   #    And merge it with the earlier "#to#"
                         #     i.e. '18to24' and 'by2' → '18to24by2'
                      ]  # Close the mapping and both if-statements
˜                        # Flatten the list
                         #  i.e. ['1to3',[11],'18to24by2','32to34']
                         #   → ['1to3',11,'18to24by2','32to34']
 ðý                      # And join by spaces (which we implicitly output as result)
                         #  i.e. ['1to3',11,'18to24by2','32to34']
                         #   → '1to3 11 18to24by2 32to34'

0

펄 5 , 154 바이트

{my(@r,$r);@r&&(@$r<2||$$r[1]-$$r[0]==$_-$$r[-1])?push@$r,$_:push@r,$r=[$_]for@_;join" ",map@$_<3?@$_:("$$_[0]to$$_[-1]by".($$_[1]-$$_[0]))=~s/by1$//r,@r}

공백, 줄 바꿈, # 주석 및 sub by:

sub by {
  my(@r,$r);
  @r &&                               # if at least one run candidate exists and...
   ( @$r<2                            # ...just one elem so far
     || $$r[1]-$$r[0] == $_-$$r[-1] ) # ...or diff is same
    ? push @$r, $_                    # then add elem to curr. run candidate
    : push @r, $r=[$_]                # else start new run cand. with curr elem
        for @_;
  join " ",
    map @$_<3                                         # is it a run?
    ? @$_                                             # no, just output the numbers
    : ("$$_[0]to$$_[-1]by".($$_[1]-$$_[0]))=~s/by1$//r, # yes, make run, delete by1
    @r                                                # loop run candidates
}

온라인으로 사용해보십시오!

... OP에서 테스트를 통과했습니다.


0

레티 나 0.8.2 , 77 바이트

\d+
$*
(1+)(?= \1(1+))
$1:$2
1:(1+) (1+:\1 )+(1+)
1to$3by$1
:1+|by1\b

1+
$.&

온라인으로 사용해보십시오! 링크에는 테스트 사례가 포함됩니다. 설명:

\d+
$*

단항으로 변환합니다.

(1+)(?= \1(1+))
$1:$2

연속적인 차이를 계산합니다.

1:(1+) (1+:\1 )+(1+)
1to$3by$1

실행을 to...by구문으로 변환 합니다.

:1+|by1\b

변환되지 않은 차이점을 제거하십시오 by1.

1+
$.&

십진수로 변환합니다.

당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.