หมายเลข sporeball

Sep 19 2020

พื้นหลัง

เราได้ศึกษาอย่างน้อยห้า ที่แตกต่างกัน ชนิด ของ ตัวเลขที่อยู่บนพื้นฐานของรหัสของผู้ใช้ที่แตกต่างกันในเว็บไซต์นี้ ทำไมไม่ศึกษาอื่น

ID ผู้ใช้ของฉันคือ\$91030_{10}\$. การแทนค่าฐานสองคือ\$10110001110010110_2\$การแสดงที่มีคุณสมบัติที่น่าสนใจเป็นของตัวเอง:

  • ทางเลือกหนึ่งสำหรับการรันเลขฐานสองหลักที่ยาวที่สุดคือ\$0011100\$.
  • หากคุณลบตัวเลขจำนวนนี้ออกรายการหลักที่เหลือ ( \$1011010110\$) สามารถแบ่งออกเป็นสองส่วนที่เหมือนกัน

ฉันกำหนดหมายเลขสปอร์บอลเป็นจำนวนเต็มบวกใด ๆ โดยที่อย่างน้อยหนึ่งในตัวเลือกสำหรับการรันเลขฐานสองที่ยาวที่สุดในการแทนเลขฐานสองสามารถลบออกเพื่อให้รายการหลักที่เหลือสามารถแบ่งออกเป็นสองส่วนที่เหมือนกันได้

งาน

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

คำชี้แจงบางประการที่ควรทราบ:

  • ควรละเว้นศูนย์นำหน้า
  • แน่นอนหนึ่งวิ่ง palindromic ของตัวเลขที่ควรจะออกก่อนที่จะตรวจสอบว่าตัวเลขที่เหลือสามารถแบ่งออกเป็นครึ่งหนึ่งเหมือนกัน
    • หากหนึ่งในตัวเลือกสำหรับการรัน palindromic ที่ยาวที่สุดเกิดขึ้นหลายครั้งให้ลบเพียงเหตุการณ์เดียวแทนที่จะเป็นทั้งหมดก่อนที่จะตรวจสอบว่าตัวเลขที่เหลือสามารถแบ่งออกเป็นครึ่งเดียวกันได้หรือไม่
  • ตัวเลขหลักเดียวคือ palindromic
  • สตริงว่าง""ไม่ใช่ palindromic และไม่สามารถแบ่งออกเป็นสองส่วนที่เหมือนกันได้

โปรดจำไว้ว่าอาจมีการรันเลขหลักที่ยาวที่สุดมากกว่าหนึ่งหลัก:

  • เลขฐานสองของ\$12_{10}\$( \$1100\$) มีสองตัวเลือกสำหรับการรัน palindromic ที่ยาวที่สุด ( \$11\$และ\$00\$). ไม่ว่าตัวเลขใดจะถูกลบออกตัวเลขที่เหลือจะสามารถแบ่งออกเป็นครึ่งเดียวกันได้ ดังนั้น\$12_{10}\$ คือหมายเลขสปอร์บอล
  • เลขฐานสองของ\$20_{10}\$( \$10100\$) มีสองตัวเลือกสำหรับการรัน palindromic ที่ยาวที่สุด ( \$010\$และ\$101\$). การลบ\$010\$ออกจากตัวเลข\$10\$ซึ่งไม่สามารถแบ่งออกเป็นครึ่งเดียวกันได้ อย่างไรก็ตามการลบ\$101\$ออกจากตัวเลข\$00\$ซึ่งสามารถ ดังนั้น\$20_{10}\$ คือหมายเลขสปอร์บอล

มี 153 หมายเลข sporeball ต่ำกว่า 1,000:

12 20 23 24 26 28 29 39 48 57 60 68 71 84 87 96 100 106 108 110 111 113 117 123 124 132
135 154 166 178 180 183 192 204 207 210 222 225 237 240 243 252 260 263 277 282 287 295
314 326 334 336 337 340 343 348 351 354 370 372 375 384 392 394 396 399 404 412 418 426
428 431 432 446 449 457 469 476 477 479 480 483 484 490 491 496 497 501 503 508 516 519
533 538 543 562 600 610 612 615 634 646 652 660 663 664 670 673 676 691 700 703 706 718
720 735 742 754 756 759 768 778 780 783 792 804 816 821 826 828 831 834 858 870 874 876
879 894 897 918 921 922 924 927 933 957 960 963 972 978 987 993 999

กฎ

  • นี่คือโค้ดกอล์ฟคำตอบที่สั้นที่สุดในหน่วยไบต์จึงชนะ
  • ใช้กฎ I / O มาตรฐาน

คำตอบ

9 UnrelatedString Sep 19 2020 at 18:45

Brachylog 26 ไบต์

{ḃ~c₃↺{↔?¬Ė}ʰ↻c}ᶠlᵒlᵍh∋~jz

ลองออนไลน์!

ช้าอย่างน่ากลัวสำหรับกรณีทดสอบที่เป็นเท็จขนาดใหญ่ แต่ตรวจสอบความจริงได้อย่างรวดเร็วอย่างน่าประหลาดใจ โซลูชันดั้งเดิมของฉันพบผลลบเท็จเช่นเดียวกับคำตอบที่ถูกลบของ xash แต่โชคดีที่กระบวนการแก้ไขที่ช่วยให้ฉันกำจัด 2 ไบต์

{              }ᶠ             Find every possible result from:
 ḃ                             take the binary digits of the input,
  ~c₃                          split them into three (possibly empty) partitions,
     ↺{    }ʰ↻                 for the middle partition:
       ↔                        reversed it is
        ?                       itself
         ¬Ė                     which is not the empty list;
          Ė                     replace it with the empty list.
              c                and re-concatenate the partitions.
                 lᵒ           Sort the results by length,
                   lᵍ         group them by length,
                     h        and take the first group (that with minimal length).
                      ∋       Some element of that group
                       ~j     is something concatenated with itself
                         z    which is not the empty list.

แทนที่จะขยายความยาวของสตริงย่อย palindromic ≜ก็แค่ลดความยาวของทุกอย่างที่เหลือซึ่งเป็นวิธีการที่ผมเพียงขึ้นมาด้วยเนื่องจากวิธีการเริ่มต้นของฉันอาศัยบน

8 Lyxal Sep 19 2020 at 11:24

05AB1E , 47 45 43 42 40 41 40 31 ไบต์

b©ŒʒÂQ}é.γg}θε®sõ.;D2ä1ìËsgĀ*}à

ลองออนไลน์!

-2 ขอบคุณ @ovs!
-1 ขอบคุณ @ovs!
-1 (lol) ขอบคุณการแก้ไขข้อผิดพลาด
-1 ขอบคุณ @ovs (อีกครั้ง!)
+1 เนื่องจากการชี้แจงความท้าทาย :-(
แต่ -1 ขอบคุณ @Kevin!
และอีกอย่างมาก -9 ขอบคุณ @Kevin!

อย่ารังเกียจฉัน ... เพียงแค่โพสต์คำตอบที่ยาวเกินไปใน 05AB1E ซึ่งอาจจะ ถูกตีโดยใครก็ตามที่มีประสบการณ์กับ 05AB1E

ÂQเคล็ดลับที่จะดูว่าสตริงเป็น palindrome ถูกนำมาจากนี้ 05AB1E คำตอบเคล็ดลับโดยเควิน

อธิบาย (เก่า)

bDV.œ˜ʒÂQ} ЀgàUʒgXQ}εYsõ:Ðg;ôËsgD0ÊsÈ**}à
bDV                                             # Get the binary representation of the input, and assign variable Y to that value while still keeping a copy on the stack
   .œ                                           # Push all partitions of that binary representation
     ˜                                          # Flatten said list and
      ʒ                                         #  Select items where:
       ÂQ}                                      #      They are a palindrome

            Ð                                   # and push three copies of it to the stack.
             €g                                 # For one of those copies, push the length of each item
               àU                               # Find the maximum length and assign it to variable Y
                 ʒgXQ}                          # From the list of palindromic partitions, select the ones which are of the maximum length
                      ε                         # And from that list:
                       Ysõ:                     #   Replace the occurrence of that number in variable Y with nothing THEN
                           Ð                    #   Triplicate it THEN
                            g;ô                 #   Split it in half THEN
                               Ë                #   See if all elements are equal AND
                                sgD0ÊsÈ**       #   Ensure the length of Y with the item removed isn't 0 and isn't odd
                                         }à     # Close the map, and take the maximum of the list and implicitly print the result
5 Jonah Sep 19 2020 at 13:35

J , 80 78 75 66 61 57 ไบต์

1 e.#\,@((#<.[-:[:,~,~inv)\.*[:(*i.@#=+./"{i:1:)(-:|.)\)]

ลองออนไลน์!

-3 ไบต์ขอบคุณ Marshall

-9 ไบต์ขอบคุณ xash

ยากกว่าที่ฉันคิดไว้

ในที่สุดก็เป็นขนาดที่น่านับถือแม้ว่าจะยังสูงสำหรับ J.

Jวิธีอื่น 73 ไบต์

1 e.1}.((((<:@[,(-:|.)\#(#<.]-:[:,~,~inv)\.)~{.))^:(0<{.@]*1=#@])^:_#)@#:

ลองออนไลน์!

อันนี้ใช้ do .. ใน^:(while)^:_ขณะที่เริ่มต้นด้วยการค้นหา palindrome ที่ยาวที่สุดเท่าที่จะเป็นไปได้และหยุดทันทีที่พบว่ามีความยาวที่กำหนดส่งคืนบูลีนบอกคุณว่าส่วนเติมเต็มสำหรับ palindrome นั้นเป็นสตริงสองเท่า

4 JonathanAllan Sep 19 2020 at 23:15

เยลลี่ 31 ไบต์

ḊḢŒḂḤœP⁸F
BØ2jŒṖḟ€€2Ç€LÐṂŒHE$ƇẸ

ลองออนไลน์! หรือดูได้ถึง 600 (มากถึง 1,000 ช้าเกินไป)

อย่างไร?

BØ2jŒṖḟ€€2Ç€LÐṂŒHE$ƇẸ - Main Link: n
B                     - convert (n) to a binary list
 Ø2                   - [2,2]
   j                  - join ([2,2]) with (B(n))
    ŒṖ                - partitions (none with empty parts, hence the Ø2j and ḟ€€2)
      ḟ€€2            - remove any 2s from each part of each
          ǀ          - call Link 1 for each
                        (removes second part if it's palindromic & flattens)
            LÐṂ       - keep only those with minimal length
                   Ƈ  - filter keep those for which:
                  $   -   last two links as a monad:
               ŒH     -     split into two
                 E    -     all equal?
                    Ẹ - any truthy?

ḊḢŒḂḤœP⁸F - Link 1: list of parts
Ḋ         - deueue
 Ḣ        - head -> second part
  ŒḂ      - is palindromic? (1 if so, else 0)
    Ḥ     - double
       ⁸  - the list of parts
     œP   - partition at index
            (0œP[4,5,6,7] -> [[4,5,6,7]] while 2œP[4,5,6,7] -> [[4],[6,7]])
        F - flatten
4 att Sep 19 2020 at 11:59

ภาษา Wolfram (Mathematica) , 128 ... 103101ไบต์

FreeQ[MinimalBy[$@@d~Drop~#&/@SequencePosition[d=#~IntegerDigits~2,_?PalindromeQ],Length],a__~$~a__]&

ลองออนไลน์!

ส่งคืนFalseหากตัวเลขไม่ใช่หมายเลขสปอร์บอลหรือTrueอย่างอื่น

d=#~IntegerDigits~2                     (* get digits of input, base 2. *)
SequencePosition[ % ,_?PalindromeQ]     (* get positions of palindromic runs *)
d~Drop~#/@ %                            (* and remove them, *)
$@@ % (* placing the remaining digits in $ *)
MinimalBy[ % ,Length]                   (* keep the shortest remaining digit lists *)
FreeQ[ % ,a__~$~a__]                    (* and check if they have identical halves. *)

$@@จำเป็นในการจัดการกรณีเช่น\$38=100110_2\$ซึ่งการลบทั้งสองยาวที่สุด palindromes 1001, มีผลเหมือนกัน011010

3 Arnauld Sep 19 2020 at 11:29

JavaScript (ES6) 175 ไบต์

n=>(m=g=(s,p='',q=p)=>s?g(s.slice(1),p+s[0],q,s==[...s].reverse(L=s.length).join``?o=(L<=m?o:!(m=L))|L==m&/^(.+)\1$/.test(p+q):0,g(s.slice(0,-1),p,s[L-1]+q)):o)(n.toString(2))

ลองออนไลน์!

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

n => (                    // n = input
  m =                     // initialize m to a non-numeric value
  g = (                   // g is a recursive function taking:
    s,                    //   s = middle part of the string (the palindromic one)
    p = '', q = p         //   p = left part, q = right part
  ) =>                    //
    s ?                   // if s is not empty:
      g(                  //   outer recursive call:
        s.slice(1),       //     with the first character of s removed ...
        p + s[0],         //     ... and appended to p
        q,                //     with q unchanged
        s == [...s]       //     split s
        .reverse(         //     reverse it
          L = s.length    //     set L = length of s (argument ignored by reverse)
        ).join`` ?        //     join again; if s is a palindrome:
          o =             //       update o:
            ( L <= m ?    //         if L is not higher than m:
                o         //           yield o
              :           //         else:
                !(m = L)  //           update m to L and yield 0
            ) | L == m &  //         bitwise OR with 1 if L = m (current max.)
            /^(.+)\1$/    //         and the concatenation of p and q can be
            .test(p + q)  //         split into 2 identical halves
        :                 //     else:
          0,              //       abort
        g(                //     inner recursive call:
          s.slice(0, -1), //       with the last character of s removed
          p,              //       with p unchanged
          s[L - 1] + q    //       with the last character of s prepended to q
        )                 //     end of inner recursive call
      )                   //   end of outer recursive call
    :                     // else:
      o                   //   return o
)(n.toString(2))          // initial call to g with s = binary string for n
3 Neil Sep 19 2020 at 15:45

Retina 126 ไบต์

.+
*
+`^(_*)\1(_?)(?!^|_)
$1$.2
Lv$`(.)+.?(?<-1>\1)+(?(1)(?!))|. $`$' N$`
$.& +m`^((.)*)¶(?<-2>.)*(?(2)(?!)).+$
$1 0m`^(.+)\1$

ลองออนไลน์! คำอธิบาย:

.+
*

แปลงอินพุตเป็นยูนารี

+`^(_*)\1(_?)(?!^|_)
$1$.2

แปลงเป็นไบนารี

Lv$`(.)+.?(?<-1>\1)+(?(1)(?!))|. $`$'

ค้นหาและลบ palindromes

N$`
$.&

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

+m`^((.)*)¶(?<-2>.)*(?(2)(?!)).+$
$1

ลบผลลัพธ์ที่ยาวกว่าทั้งหมด

0m`^(.+)\1$

ตรวจสอบว่าแยกได้หรือไม่

3 Neil Sep 19 2020 at 17:33

ถ่าน 53 ไบต์

≔⍘N²θF⊕LθFιF⁼✂θκι¹⮌✂θκι⊞υ⁺…θκ✂θι¿⌊υ⊙υ∧⁼Lι⌊EυLλ⁼ιײ∕ι²

ลองออนไลน์! ลิงก์คือรหัสเวอร์ชันที่ละเอียด เอาต์พุตเป็นบูลีนของถ่านเช่น-สำหรับหมายเลขสปอร์บอลว่างเปล่าถ้าไม่มี คำอธิบาย:

≔⍘N²θ

แปลงอินพุตเป็นฐาน 2

F⊕LθFι

วนซ้ำสตริงย่อยที่ไม่สำคัญทั้งหมดของอินพุต

F⁼✂θκι¹⮌✂θκι

หากสตริงย่อยนี้เท่ากับย้อนกลับ ...

⊞υ⁺…θκ✂θι

... จากนั้นดันตัวเลขที่เหลือไปยังรายการว่างที่กำหนดไว้ล่วงหน้า

¿⌊υ

หากหมายเลขเดิมไม่ใช่ palindromic ...

⊙υ∧⁼Lι⌊EυLλ⁼ιײ∕ι²

... จากนั้นแสดงผลลัพธ์ว่าผลลัพธ์ใดมีความยาวน้อยที่สุดและมีความยาวเท่ากันครึ่งหนึ่งและเพิ่มขึ้นเป็นสองเท่าอีกครั้ง

2 GalenIvanov Sep 21 2020 at 14:24

ปัจจัย , 209 197 ไบต์

: s ( n -- ? ) >bin dup all-subseqs [ dup reverse = ] filter
dup [ last length ] dip [ length over = ] filter nip
[ split1 append [ ""= not ] keep dup length 2/ cut = and ]
with [ or ] map-reduce ;

ลองออนไลน์!

2 Zgarb Sep 21 2020 at 15:59

แกลบ 28 ไบต์

▲foE½†!ḋ¹ṠM-ö→kLfoS=↔m!ḋ¹Qŀḋ

ลองออนไลน์! ส่งคืนรายการว่าง (ซึ่งเป็นเท็จใน Husk) หรือรายการที่ไม่ว่างเปล่า (ซึ่งเป็นความจริง)

คำอธิบาย

ซ้ำแล้วซ้ำอีกḋรู้สึกสิ้นเปลือง แต่ฉันไม่แน่ใจว่าจะกำจัดมันอย่างไร

Input is a number, say n=357
▲f(E½)†!ḋ¹ṠM-(→kLf(S=↔m!ḋ¹)Q)ŀḋ   Parentheses added for clarity.
                              ḋ   Binary digits: D=[1,0,1,1,0,0,1,0,1]
                             ŀ    Indices: I=[1,2,3,4,5,6,7,8,9]
             (→kLf(S=↔m!ḋ¹)Q)     Get indices of longest palindromic runs.
                           Q      Slices: [[1],[2],[1,2],..,[1,2,..,9]]
                 f                Filter by condition:
                  (S=↔m!ḋ¹)       Is a palindrome in D.
                      m           Map
                       !          indexing into
                        ḋ¹        D (recomputed).
                   S=             That equals
                     ↔            its reverse.
               kL                 Classify (into separate lists) by length.
              →                   Get the last one: [[2,3,4,5],[4,5,6,7]]
          ṠM-                     Remove each from I: [[1,6,7,8,9],[1,2,3,8,9]]
      †                           Deep map
       !ḋ¹                        indexing into D (recomputed again): [[1,0,1,0,1],[1,0,1,0,1]]
 f                                Filter by condition:
  (E½)                            Splits into identical halves.
    ½                             Split into halves (if length is odd, first part is longer): [[1,0,1],[0,1]]
   E                              All elements are equal: 0
                                  Result is []
▲                                 Maximum, or [] if the argument is empty: []
                                  The final result is nonempty iff the last filter keeps a nonempty list.
2 AZTECCO Sep 21 2020 at 04:56

Japt , 33 42 ไบต์

s2
ã fêS üÊo
Vc@ðXãjXVÎlîòZÊ/2Ãd_Ê©ZÎ¥Zo

ลองมัน

s2          - convert input to binary string

ã           - substrings
  fêS       - filter palindrome
      üÊo   - take last group by length

Vc@ðXÃ      - find indexes of each palindrome in input
£jXVÎlà     - map those indexes by removing n(=palindr.length) characters from input at index
®òZÊ/2Ã     - split all results
d_          - return true if any : 
  Ê©          - exists and..
    ZÎ¥Zo     - are ==
  • คงที่: ตอนนี้ใช้ได้กับตัวเลขที่มีการรัน palindromic ที่ยาวที่สุดเหมือนกันมากขึ้นเช่น 2405 => 100101100101

  • ทดสอบ : คำนวณคำศัพท์ 1,000 คำแรกและตรวจสอบว่าผลลัพธ์เหมือนกับกรณีทดสอบหรือไม่

1 ovs Sep 19 2020 at 15:40

งูหลาม 3.8 (ก่อนเผยแพร่) , 154 152 ไบต์

def f(n):s=f'{n:b}';k=len(s);return max((b-a,(r:=s[:a]+s[b:])[:(h:=k-b+a>>1)]==r[h:]>'')for a in range(k)for b in range(a,k+1)if(p:=s[a:b])==p[::-1])[1]

ลองออนไลน์!

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

s=f'{n:b}'                    # convert n to a binary string
k=len(s)                      # and take the length
return max( ... )[1]          # the second element from the maximum of 
  (b-a,                       #   tuples of palindrome length b-a ...
             [:(h:=k-b+a>>1)] #   ... and is the first half
  (r:=s[:a]+s[b:])            #       of the binary string without the palindrome
    ==r[h:]                   #       equal to the second half
    >'')                      #       and not equal to the empty string
  for a in range(k)           #   for palindrome starting positions a in [0, 1, ..., k-1]
  for b in range(a,k+1)       #   for palindrome end indices b in [1, 2, ..., k-a]
  if(p:=s[a:b])==p[::-1])     #   if this is an actual palindrome

หากมีหลาย palindromes ของความยาวสูงสุดเดียวกันเลือกอันดับที่มีมูลค่าสูงสุดเป็นอันดับสองที่maxTrue>False

1 user Sep 20 2020 at 07:00

Dotty, 201 ไบต์

s=>((for{j<-1 to s.size
i<-0 to j-1
x=s.slice(i,j)if x==x.reverse}yield(i,j))groupBy(_-_)minBy(_._1)_2)exists{(i,j)=>val x=s.slice(0,i)+s.substring(j)
x!=""&&x.slice(0,x.size/2)==x.substring(x.size/2)}

ลองออนไลน์ (ใน Scastie)

อินพุตต้องเป็นสตริงไบนารีอยู่แล้ว

Dotty, 226 ไบต์

x=>{val s=x.toBinaryString
((for{j<-1 to s.size
i<-0 to j-1
x=s.slice(i,j)if x==x.reverse}yield(i,j))groupBy(_-_)minBy(_._1)_2)exists{(i,j)=>val x=s.slice(0,i)+s.substring(j)
x!=""&&x.slice(0,x.size/2)==x.substring(x.size/2)}}

ลองออนไลน์ (ใน Scastie)

อินพุตคือIntไฟล์.