กราฟิคลำดับนี้หรือไม่


17

ลำดับกราฟิกเป็นลำดับของจำนวนเต็มบวกแต่ละ denoting จำนวนขอบสำหรับโหนดในเป็นกราฟที่เรียบง่าย ตัวอย่างเช่นลำดับ2 1 1หมายถึงกราฟที่มี 3 โหนดหนึ่งมี 2 ขอบและ 2 กับการเชื่อมต่อหนึ่ง

ไม่ใช่ลำดับทั้งหมดที่เป็นลำดับกราฟิก ตัวอย่างเช่น2 1ไม่ใช่ลำดับกราฟิกเนื่องจากไม่มีวิธีการเชื่อมต่อสองโหนดดังนั้นหนึ่งในนั้นมีสองขอบ


งาน

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

คุณต้องแสดงผลว่าลำดับนั้นเป็นลำดับกราฟิกหรือไม่ ค่าความจริงถ้ามันเป็นค่าเท็จอย่างอื่น


เป้าหมาย

นี่คือเป้าหมายคือการลดจำนวนไบต์ในโปรแกรมของคุณ

Testcases

เรียงลำดับมากที่สุดไปหาน้อยที่สุด

                  -> True
3 3 3 2 2 2 1 1 1 -> True
3 3 2 2 1 1       -> True
3 3 2             -> False
8 1 1 1 1 1 1 1 1 -> True
1 1 1 1           -> True
1 1 1             -> False
9 5 4             -> False

เราสามารถสมมติว่ารายการอินพุตจะไม่ว่างเปล่าได้หรือไม่?
ปีเตอร์เทย์เลอร์

@PeterTaylor หากคุณต้องการคุณสามารถใช้สตริงของ0ลำดับที่ว่างเปล่า
โพสต์ Rock Garf Hunter

คำตอบ:


7

Mathematica ขนาด 25 ไบต์

<<Combinatorica`
GraphicQ

ใช่อีกตัวใน (ใช้อินพุตเป็นรายการของจำนวนเต็มบวก) ต้องโหลดCombinatoricaแพ็กเกจ


7

Python 2 (รหัสทางออก), 53 ไบต์

l=input()
while any(l):l.sort();l[~l[-1]]-=1;l[-1]-=1

ลองออนไลน์!

ส่งออกผ่านรหัสทางออก

ใช้อัลกอริทึม Havel-Hakimi รุ่นหนึ่ง ลดลงซ้ำ ๆ ทั้งองค์ประกอบที่ใหญ่ที่สุดkและองค์ประกอบที่kใหญ่ที่สุด (ไม่นับkตัวเอง) ซึ่งสอดคล้องกับการกำหนดขอบระหว่างจุดยอดสองจุดด้วยองศาเหล่านั้น สิ้นสุดลงเมื่อรายการกลายเป็นศูนย์ทั้งหมด มิฉะนั้นหากมีดัชนีอยู่นอกขอบเขตล้มเหลวโดยมีข้อผิดพลาด ค่าลบใด ๆ ที่สร้างขึ้นในที่สุดก็นำไปสู่ข้อผิดพลาดนอกขอบเขต


5

CJam (20 ไบต์)

{{W%(Wa*.+$_0a<!}g!}

ชุดทดสอบออนไลน์รวมถึงการทดสอบพิเศษอีกสองชุดที่ฉันเพิ่มเข้าไปเพื่อจับข้อบกพร่องในความพยายามของฉัน

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

อาร์เรย์อินพุตอาจไม่ว่างเปล่า แต่อาจมีศูนย์ตามคำตอบของ OP สำหรับการสืบค้นของฉันในหัวข้ออินพุตว่าง

การผ่า

นี้ต่อไปนี้คำตอบของ OP ในการดำเนินการตามขั้นตอนวิธีการ Havel-Hakimi

{          e# Define a block
  {        e#   Do-while loop (which is the reason the array must be non-empty)
           e#     NB At this point the array is assumed to be non-empty and sorted
    W%     e#     Reverse
    (Wa*.+ e#     Pop the first element and subtract 1 from that many subsequent
           e#     elements. If there aren't enough, it adds -1s to the end. That's
           e#     the reason for using W (i.e. -1) and .+ instead of 1 and .-
    $      e#     Sort, restoring that part of the invariant
    _0a<!  e#     Continue looping if array >= [0]
           e#     Equivalently, break out of the loop if it starts with a negative
           e#     number or is empty
  }g
  !        e#   Logical not, so that an empty array becomes truthy and an array
           e#   with a negative number becomes falsy
}

2

Python 2 , 108 ไบต์

นี่คือการใช้งานของฉันใน Python ฉันแน่ใจว่ามันจะถูกตีโดยนักกอล์ฟหรือนักคณิตศาสตร์ที่มีประสบการณ์มากกว่า มันใช้อัลกอริทึม Havel-Hakimi

def f(x):p=x[0]+1;x=sorted(x+[0]*p)[::-1];return~x[-1]and(p<2or f(sorted([a-1for a in x[1:p]]+x[p:])[::-1]))

ลองออนไลน์!


[2,1,1]ผลตอบแทนTrueแต่[1,1,2]ผลตอบแทน0- แก้ไข: เพิ่งเห็นว่าข้อมูลจำเพาะของคุณบอกว่าคุณสามารถสันนิษฐานได้ว่ามันเรียงลำดับแล้ว (ฉันเคยเห็นกรณีทดสอบ9 4 5)
Jonathan Allan

2

Haskell , 102 98 95 94 ไบต์

import Data.List
f(x:r)=length r>=x&&x>=0&&(f.reverse.sort$take x(pred<$>r)++drop x r)
f x=1<3

ลองออนไลน์! การใช้: f [3,3,2,2,1,1]ผลตอบแทนTrueหรือFalseหรือสมมติว่าอินพุตไม่มีศูนย์และเรียงลำดับจากมากไปน้อยตามที่อนุญาตในการท้าทาย

คำอธิบาย:

import Data.List          -- import needed for sort
f (x:r) =                 -- x is the first list element, r the rest list
  length r >= x           -- the rest list r must be longer or equal x
  && x >= 0               -- and x must not be negative
  && (f .                 -- and the recursive call of f
      reverse . sort $    --    with the descendingly sorted list
      take x(pred<$>r)    --    of the first x elements of r subtracted by 1
      ++ drop x r         --    and the rest of r
     )                    -- must be true
f [] = True               -- if the list is empty, return True

แก้ไข: ดูเหมือนว่าจะเป็นไปตาม Havel-Hakimi ที่กล่าวถึงในคำตอบอื่น ๆ แต่ฉันไม่ทราบอัลกอริทึมนี้เมื่อเขียนคำตอบ


length r < xไม่ถูกต้องตามที่[1,0]จะได้ผลจริง แต่ไม่มีกราฟง่าย ๆ ที่มี 2 โหนดที่มีขอบหนึ่งและศูนย์
Jonathan Allan

@JanathanAllan คุณพูดถูก แต่ความท้าทายระบุว่า "คุณอาจคิดว่าไม่มีเลขศูนย์ในอินพุต"
Laikoni

โอ้ใช่แล้วดูเหมือนว่าจะเป็นการตัดสินใจที่แปลกเพราะมันไม่เหมาะกับคำจำกัดความ
Jonathan Allan

@JanathanAllan ฉันเปลี่ยนมันเพื่อจัดการกับกรณีเหล่านั้นเช่นกันและแม้กระทั่งบันทึก 4 ไบต์ด้วยการทำเช่นนั้น
Laikoni

ดีแล้ว! : D
Jonathan Allan

2

เยลลี่ 12 ไบต์

ṢṚḢ-€+ƊƊƬ>-Ȧ

ลิงค์ monadic ยอมรับรายการที่ให้ผล 10ถ้าคำตอบที่มีความสอดคล้องเป็นอย่างอื่น

ลองออนไลน์!หรือดูการทดสอบในตัว

อย่างไร?

ṢṚḢ-€+ƊƊƬ>-Ȧ - Link: list of integers
        Ƭ    - collect up while results change:
       Ɗ     -   last three links as a monad i.e. f(L):
Ṣ            -     sort                      [min(L),...,max(L)]
 Ṛ           -     reverse                   [max(L),...,min(L)]
      Ɗ      -     last three links as a monad i.e. f([a,b,c,...,x]):
  Ḣ          -       pop head                          a
   -€        -       -1 for each                       [-1,-1,...,-1] (length a)
     +       -       add to head result (vectorises)   [b-1,c-1,...,x-1,-1,-1,...]
         >-  - greater than -1? (vectorises)
           Ȧ - Any and all? (0 if empty or contains a 0 when flattened, else 1)

1

05AB1E , 26 25 ไบต์

D0*«¹v{R¬U¦X¹gn‚£`s<ì}0QP

ลองออนไลน์!

คำอธิบาย

D0*«                       # extend the input list with as many zeroes as it has elements
    ¹v                     # len(input) times do:
      {R                   # sort in descending order
        ¬U¦X               # extract the first element of the list
            ¹gn‚           # pair it with len(input)^2
                £          # partition the list in 2 parts, the first the size of the 
                           # extracted element, the second containing the rest of the list
                 `         # split these list to stack (the second on top)
                  s<       # decrement the elements of the first list by 1
                    ì      # prepend it to the rest of the list
                     }     # end loop
                      0Q   # compare each element in the resulting list with 0
                        P  # reduce list by multiplication

1

JavaScript (ES6), 82 80 76 ไบต์

f=([$,..._])=>1/$?_.length>=$&$>=0&f(_.map(a=>a-($-->0)).sort((a,b)=>b-a)):1

ขอบคุณ ETHproductions สำหรับการบันทึกจำนวนมาก!

การใช้

f=([$,..._])=>1/$?_.length>=$&$>=0&f(_.map(a=>a-($-->0)).sort((a,b)=>b-a)):1
f([3,3,3,2,2,2,1,1,1])

เอาท์พุต

1

คุณสามารถแทนที่map((a,b)=>b<$?a-1:a)ด้วยmap(a=>a-($-->0))เพื่อบันทึก 4 ไบต์
Arnauld

1

R , 20 ไบต์

igraph::is_graphical

Mathematica ไม่ใช่ภาษาเดียวที่มีในตัว! ;-)

igraphแพคเกจจะต้องติดตั้ง รับอินพุตเป็นเวกเตอร์ของจำนวนเต็ม



0

05AB1E , 19 ไบต์

[D{RćD1‹#Å0<0ζO})dW

คำตอบของเยลลี่พอร์ตของJonathanAllanดังนั้นอย่าลืมโหวตให้เขา !!

ลองมันออนไลน์หรือตรวจสอบกรณีทดสอบทั้งหมด

คำอธิบาย:

[            # Start an infinite loop:
 D           #  Duplicate the current list
             #  (which is the implicit input-list in the first iteration)
  {R         #  Sort it from highest to lowest
    ć        #  Extract the head; pop and push the remainder and head
     D1     #  If the head is 0 or negative:
        #    #   Stop the infinite loop
     Å0<     #  Create a list of the head amount of -1
        0ζ   #  Zip/transpose it with the remainder list, with 0 as filler
          O  #  Sum each pair
})           # After the loop: wrap everything on the stack into a list
  d          # Check for each value if it's non-negative (>= 0)
             # (resulting in 1/0 for truthy/falsey respectively)
   W         # Get the flattened minimum (so basically check if none are falsey)
             # (which is output implicitly as result)

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