จัดกลุ่มตัวเลขด้วยผลรวมเดียวกัน


12

งานของคุณคือให้ตารางสี่เหลี่ยมของตัวเลข ( 0-9) เอาท์พุทวิธีหนึ่งที่สามารถจัดกลุ่มตัวเลขดังกล่าว:

  1. แต่ละหลักเป็นส่วนหนึ่งของกลุ่มหนึ่งกลุ่ม
  2. ทุกกลุ่มมีจำนวนหลักเท่ากัน
  3. กลุ่มทั้งหมดถูกล้อมรอบด้วยรูปร่างคล้ายรูปหลายเหลี่ยมหนึ่งอัน (ซึ่งหมายความว่าทุกหลักในกลุ่มอยู่ถัดจาก [ซ้าย, ขวา, ขึ้น, ลง] อย่างน้อยหนึ่งตัวเลขอื่นในกลุ่มเดียวกันเว้นแต่แต่ละกลุ่มจะมี 1 องค์ประกอบ)
  4. ทุกกลุ่มมีผลรวมเท่ากัน

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

อินพุตตัวอย่าง:

stringOfDigits numberOfGroupsสมมติว่ารูปแบบการป้อนข้อมูลของคุณ

ตัวอย่างอินพุตจะเป็น:

156790809 3

ซึ่งจะแปลเป็น (กริดของsqrt(9) * sqrt(9))

1 5 6
7 9 0
8 0 9

ซึ่งคุณจะต้องแบ่งออกเป็น 3 กลุ่มแต่ละกลุ่มควรมี9 / 3 = 3องค์ประกอบที่มีผลรวมเดียวกัน

เอาท์พุท: เอาท์พุทควรเป็นสตริงของตัวเลขโดยมีเว้นวรรคและบรรทัดใหม่สำหรับการจัดรูปแบบโดยแต่ละหลักตามด้วยตัวอักษรที่a-zระบุกลุ่มของมัน ควรมีnumberOfTotalDigits / numberOfGroupsองค์ประกอบที่แน่นอนในแต่ละกลุ่ม คุณจะไม่ต้องแบ่งบางสิ่งออกเป็นมากกว่า 26 กลุ่ม

ตัวอย่างผลลัพธ์:

1a 5a 6b
7c 9a 0b
8c 0c 9b

โปรดทราบว่าการแทนที่ทั้งหมดaด้วยbs และbs ด้วยas นั้นใช้ได้อย่างเท่าเทียมกัน ตราบใดที่แต่ละกลุ่มแสดงด้วยตัวอักษรที่แตกต่างผลลัพธ์ก็จะถูกต้อง

นอกจากนี้ฉันคาดว่าโปรแกรมส่วนใหญ่จะแสดงผลบางอย่างตามบรรทัดของเรื่องนี้เนื่องจากการขึ้นบรรทัดใหม่ / ช่องว่างเป็นตัวเลือก:

1a5a6b7c9a0b8c0c9b

ในกรณีนี้การเพิ่มตัวเลขทั้งหมดของกลุ่มa, bหรือcทำให้ 15. นอกจากนี้ทุกกลุ่มมีความผูกพันตามรูปหลายเหลี่ยมบาง

เอาต์พุตไม่ถูกต้อง:

1a 5a 6b
7c 9a 0c
8c 0b 9b

เนื่องจากกลุ่มไม่รวมรูปหลายเหลี่ยม (โดยเฉพาะกลุ่มที่6bแยกได้และ0cโดดเดี่ยว)

1a 5a 6b
7c 9a 0b
8c 0b 9b

เพราะกลุ่มbมี 4 องค์ประกอบในขณะที่cมีเพียง 2

เป็นต้น

หากไม่มีวิธีแก้ปัญหาที่ถูกต้องโปรแกรมของคุณอาจทำอะไรก็ได้ (เช่นหยุดทำงานผิดพลาดทำงานตลอดไป) แต่ถ้าโปรแกรมของคุณพิมพ์Noneเมื่อไม่มีวิธีแก้ไขที่ถูกต้อง-15ให้คะแนนของคุณ

หากมีมากกว่าหนึ่งวิธีคุณจะต้องพิมพ์วิธีเดียว แต่-20ถ้าโปรแกรมของคุณพิมพ์ทั้งหมดโดยคั่นด้วยตัวคั่นบางตัว

นี่คือรหัสกอล์ฟซึ่งเป็นรหัสที่สั้นที่สุด (พร้อมโบนัส) ชนะ!


ในการส่งออกที่ไม่ถูกต้องครั้งแรกผมคิดว่าคุณหมายถึงจะถูกแยกออกไม่ได้6b 0b
เลเวลริเวอร์เซนต์

โปรแกรมของเรามีความสำคัญหรือไม่ ถ้ามันช้าเกินไปที่จะตรวจสอบว่ามันใช้งานได้ดี?
Beta Decay

156790889 3ดูเหมือนว่าควรจะเป็น156790809 3
isaacg

คำตอบ:


10

Pyth , 122 - 20 - 15 = 87

=Z/lzQ=ks^lz.5Jm]dUzL[-bk+bk?tb%bkb?hb%hbkb)FNJIgNZB~Jm+NksmybN;|jbS{msm+@zk@S*Z<GQxsdkUzfqSsTUz^fqsmv@*ZzbY/smvdzQJQ"None

การเปลี่ยนแปลง:

  • 130 -> 120: เปลี่ยนเป็นอินพุตที่คั่นด้วยบรรทัดใหม่

  • 120 -> 134: แก้ไขข้อผิดพลาดที่เกี่ยวข้องกับกลุ่มที่มีขนาดไม่เท่ากับความยาวด้านของเมทริกซ์

  • 134 -> 120: พิมพ์โซลูชันทั้งหมดรวมถึงรายการที่เทียบเท่าภายใต้การเปลี่ยนชื่อกลุ่ม

  • 120 -> 122: แก้ไขข้อผิดพลาดที่จะสร้างเฉพาะเส้นทางแทนที่จะเป็นกลุ่มกฎหมายทั้งหมด

ทดสอบการทำงาน:

pyth programs/sum_group.pyth <<< '156790809
3'
1a5a6b7c9a0b8c0c9b
1a5a6c7b9a0c8b0b9c
1b5b6a7c9b0a8c0c9a
1b5b6c7a9b0c8a0a9c
1c5c6a7b9c0a8b0b9a
1c5c6b7a9c0b8a0a9b


pyth programs/sum_group.pyth <<< '156790808
3'
None

pyth programs/sum_group.pyth <<< '1111     
2'
1a1a1b1b
1a1b1a1b
1b1a1b1a
1b1b1a1a

คำอธิบาย:

Pyth code           (Pseudo)-Python code              Comments

(implicit)          z = input()                       z is the digit string
(implicit)          Q = eval(input())                 S is the number of groups
(implicit)          G = 'abcdefghijklmnopqrstuvwxyz'
=Z/lzQ              Z = len(z)/Q                      Z is the size of each group.
=ks^lz.5            k = int(len(z) ** .5)             k is the side length of the matrix.
Jm]dUz              J = map(lambda d:[d], range(len(z))) Locations are encoded as numbers.
L                   def y(b): return                  y will be the transition function.
 [-bQ                         [b-k,                   Move up - the row above is k less.
  +bQ                          b+k,                   Move down - the row below is k more.
  ?tb%bkb                      b-1 if b%k else b      Move left, unless at the left edge.
  ?hb%hbkb)                    b+1 if (b+1)%k else b] Move right, unless at right edge.
FNJ                 for N in J:                       This constructs the list of all
   IgNZB                       if N[Z-1]: break       Z-length connected groups.
   ~Jm+Nk                      J+=map(lambda k: N+[k],  Append to J the group of N +
      smybN                          sum(map(lambda b:  anything reachable from
                                     y(b),N)))        anywhere in N.
   ;                (end for)
|                   or                                Print first truthy thing between
 S{                 sorted(set(                       Unique elements in sorted order of
   ms               map(lambda b:sum(                 Map+sum over allowable combinations
     m+@zd          map(lambda d:z[d]+                Character in original digit string
       @S*Z<GQ      sorted(G[:Q]*Z)[                  Repeated and sorted early alphabet
        xsbd        sum(b).index(d)],                 At index of number in sum of groups
      Uz                range(len(z)))                Over possible indexes.
   f                filter(lambda T:                  To generate allowable combinations, 
                                                      we will filter all groups of Q paths.
    qSsTUz          sorted(sum(T)) == range(len(z))   Ensure all locations are visited.
    ^                                                 Combinations of
     f              filter(lambda Y:                  Filter over connected Z-length groups
      qsm           equal(sum(map(lambda k:           Sum of the values of the group
         v@*ZzkY    eval((z*Z)[k]),Y)                 In the original digit string
       /smvbzQ      sum(map(lambda b:eval(b),z))/Q    must equal the sum of all values in z
                                                      divided by the number of groups.
      J             J                                 Filter over connected Z-length groups
     Q              Q                                 Combinations of length Q
 "None              "None"                            If the above was empty, print "None"

9
"Pyth"? คุณสะกดคำว่า "base64"
Ingo Bürk

4

JavaScript (ES6) 361 (376-15) 372

(อาจจะยังสามารถเล่นกอล์ฟได้อีกเล็กน้อย)

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

F=(g,n,l=g.length,i=w=Math.sqrt(l),o=s=h='',
  R=(g,p,k,j=l/n,t=s/n,v=0,h=String.fromCharCode(97+k))=>(
    t-=g[p],!(t<0)&&(
      g=[...g],g[p]=h,
      S=f=>g.some((c,p)=>c<':'&&f(p)),
      --j?S(p=>(g[p+1]==h|g[p-1]==h|g[p+w+1]==h|g[p-w-1]==h)?v=R(g,p,k,j,t):0)
      :t?0:k?S(p=>v=R(g,p,k-1)):v=g
    ),v
  )
)=>([for(c of g)(s-=-c,h+=--i?c:(i=w,c+':'))],h=R(g=h,-1,n,1))?h.map((c,p)=>o+=c!=':'?g[p]+c:'')&&o:'None'

Ungolfed & อธิบาย

F=(g,n)=> 
{
  var l = g.length, // string size, group size is l/n 
      w = Math.sqrt(l), // width of grid
      s,i,h,o;

  // Build a new string in h, adding rows delimiters that will act as boundary markers  
  // At the same time calculate the total sum of all digits
  h='',  // Init string
  s = 0, // Init sum 
  i = w, // Init running counter for delimiters
  [for(c of g)(
    s -= -c, // compute sum using minus to avoid string concatenation
    h += --i ? c : (i=w, c+':') // add current char + delimiter when needed
  )];


  // Recursive search
  // Paramaters:
  // g : current grid array, during search used digits are replaced with group letters
  // p : current position
  // k : current group id (start at n, decreaseing)
  // j : current group size, start at l/n decreasing, at 0 goto next group id
  // t : current group sum value, start at s/n decreasing

  var R=(g,p,k,j,t)=> 
  {
    var v = 0, // init value to return is 0
        h = String.fromCharCode(97+k); // group letter from group

    t-=g[p]; // subtract current digit

    if (t<0) // exceed the sum value, return 0 to stop search and backtrak
      return 0;

    g=[...g]; // build a new array from orginal parameter
    g[p] = h; // mark current position

    // Utility function  to scan grid array
    // call worker function  f only for digit elements
    //   skipping group markers, row delimieters and out of grid values (that are undefined)  
    // Using .some will return ealry if f returns truthy  
    var S=f=>g.some((c,p)=>c<':'&&f(p));

    if (--j) // decrement current group size, if 0 then group completed
    { // if not 0
      // Scan grid to find cells adiacent to current group and call R for each 
      S( p => {
        if (g[p+1]==h|g[p-1]==h|g[p+w+1]==h|g[p-w-1]==h) // check if adiacent to a mark valued h
        {
          return v=R(g,p,k,j,t) // set v value and returns it
        }
      })
      // here v could be 0 or a full grid 
    }
    else
    {
      // special case: at first call, t is be NaN because p -1 (outside the grid)
      // to start a full grid serach
      if (t) // check if current reached 0
        return 0; // if not, return 0 to stop search and backtrak


      if (k) // check if current group completed
      {
        // if not at last group, recursive call to R to check next group 
        S( p => {
          // exec the call for each valid cell still in grid
          // params j and t start again at init values
          return v=R(g,p,k-1,l/n,s/n) // set value v and returns it
        })
        // here v could be 0 or a full grid 
      }
      else
      {
        return g; // all groups checked, search OK, return grid with all groups marked
      }
    }
    return v
  };
  g = h; // set g = h, so g has the row boundaries and all the digits

  h=R(h,-1,n,1); // first call with position -1 to and group size 1 to start a full grid search

  if (h) // h is the grid with group marked if search ok, else h is 0
  {
    o = ''; // init output string
    // build output string merging the group marks in h and the original digits in g
    h.map( (c,p) => o += c>':' ? g[p]+c: '') // cut delimiter ':'
    return o;
  }
  return 'None';
}

ทดสอบในคอนโซล FireFox / FireBug

F("156790809",3) เอาท์พุต 1c5c6b7a9c0b8a0a9b

F("156790819",3) เอาท์พุต None

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