รู้จัก mod-folds


18

งาน

กำหนดmod พับเป็นฟังก์ชั่นในรูปแบบf (x) = x% และ1  % และ2  % ... % ในkที่ฉันเป็นจำนวนเต็มบวกและk ≥ 0 (ที่นี่%เป็นตัวดำเนินการโมดูโลด้านซ้าย)

รับรายการnจำนวนเต็มy 0 , …, y n − 1ตรวจสอบว่ามี mod-fold fเพื่อให้y i  = f (i)แต่ละอันหรือไม่

คุณสามารถเลือกและแก้ไขสองเอาต์พุต YและNสำหรับฟังก์ชัน / โปรแกรมของคุณ ถ้ามีอยู่เช่นคุณก็ต้องกลับ / พิมพ์ว่าY ; หากไม่เป็นเช่นนั้นคุณจะต้องส่งคืน / พิมพ์Nทุกครั้ง (สิ่งเหล่านี้อาจเป็นtrue/ falseหรือ1/ 0หรือfalse/ หรือtrueฯลฯ ) กล่าวถึงสิ่งเหล่านี้ในคำตอบของคุณ

การส่งที่สั้นที่สุดในหน่วยไบต์ชนะ

ตัวอย่าง

กำหนดf (x) = x% 7% 3 ค่าเริ่มต้น:

|   x  | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | ...
| f(x) | 0 | 1 | 2 | 0 | 1 | 2 | 0 | 0 | 1 | 2 | ...

ดังนั้น0 1 2 0 1 2 0 0 1 2เมื่อป้อนเข้าสู่โซลูชันของเราเราจะพิมพ์Yเนื่องจากfนี้สร้างลำดับนั้น อย่างไรก็ตาม0 1 0 1 2ในฐานะอินพุตเราจะพิมพ์Nเนื่องจากไม่มีfสร้างลำดับนั้น

กรณีทดสอบ

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

0 1 2 3 4 5              Y    (x)
1                        N
0 0 0                    Y    (x%1)
0 1 2 0 1 2 0 0 1 2      Y    (x%7%3)
0 0 1                    N
0 1 2 3 4 5 6 0 0 1 2    Y    (x%8%7)
0 1 2 0 1 2 0 1 2 3      N
0 2 1 0 2 1 0 2 1        N
0 1 0 0 0 1 0 0 0 0 1    Y    (x%9%4%3%2)

มีการ จำกัด เวลาหรือหน่วยความจำหรือไม่?
Dennis

2
ฉันสามารถส่งออกค่าความจริงและค่าความเท็จแทนได้หรือไม่?
Leun Nun

2
@Leaky ฉันอยากให้คุณไม่ ฉันไม่ใช่แฟนตัวยงของสัจธรรม ฉันพยายามอย่างชัดเจนว่านี่เป็นทางเลือกที่เป็นกลางกว่าซึ่งยังคงให้อิสระคุณ
ลินน์

@ ลินน์เป็นแค่ฉันหรือคุณยังไม่ได้แก้ไขมัน?
Leun Nun

เกี่ยวกับข้อ จำกัด ด้านหน่วยความจำ / เวลา: ฉันไม่คิดว่าฉันจะเพิ่มความท้าทายใด ๆ ลงไป แต่ฉันอาจเรียกใช้คำตอบสั้น ๆ เป็นไบต์ที่สามารถตอบคำถามการทดสอบแต่ละกรณีได้ในระยะเวลาที่เหมาะสม
ลินน์

คำตอบ:


7

Pyth, 14 ไบต์

}Qm%M+RdUQy_Sl

True/Falseผลตอบแทน ลองใช้ออนไลน์: การสาธิตหรือชุดทดสอบ

คำอธิบาย:

}Qm%M+RdUQy_SlQ   implicit Q (=input) at the end
             lQ   length of input list
            S     create the list [1, 2, ..., len]
           _      reverse => [len, ..., 2, 1]
          y       generate all subsets (these are all possible mod-folds)
  m               map each subset d to:
        UQ           take the range [0, 1, ..., len-1]
     +Rd             transform each number into a list by prepending it to d
                     e.g. if mod-fold = [7,3], than it creates:
                        [[0,7,3], [1,7,3], [2,7,3], [3,7,3], ...]
   %M                fold each list by the modulo operator
                  this gives all possible truthy sequences of length len
}Q                so checking if Q appears in the list returns True or False

Pyth, 11 ไบต์

q%M.e+k_tx0

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


4

Python 3, 239 218 ไบต์

from itertools import*
lambda z:z in[[eval(''.join([str(l)]+['%'+str(i[::-1][k])for k in range(len(i))]))for l in range(len(z))]for i in(i for j in(combinations(range(1,len(z)+1),i+1)for i in range(len(z)))for i in j)]

ฟังก์ชั่นที่ไม่ระบุชื่อที่ใช้ใส่ของรายการzและผลตอบแทนTrueหรือFalseสำหรับและYN

วิธีนี้ใช้วิธีการคล้ายกับคำตอบของ @Jakube และถึงแม้ว่ามันจะเป็นแรงเดรัจฉาน แต่ก็วิ่งได้เร็วมาก

from itertools import*               Import everything from the Python module for
                                     iterable generation
lambda z                             Anonymous function with input list z
combinations(range(1,len(z)+1),i+1)  Yield all sorted i+1 length subsets of the range
                                     [1,len(z)]...
...for i in range(len(z))            ...for all possible subset lengths
(i for j in(...)for i in j)          Flatten, yielding an iterator containing all possible
                                     mod-fold values as separate lists
...for i in...                       For all possible mod-fold values...
...for k in range(len(i))            ...for all mod-fold values indices k...
...for l in range(len(z))            ...for all function domain values in [0,len(z)-1]...
[str(l)]+['%'+str(i[::-1][k])...]    ...create a list containing each character of the
                                     expression representing the function defined by the
                                     mod-fold values (reversed such that the divisors
                                     decrease in magnitude) applied to the domain value...
 eval(''.join(...))                  ...concatenate to string and evaluate...
 [...]                               ...and pack all the values for that particular
                                     function as a list
 [...]                               Pack all lists representing all functions into a list
 ...:z in...                         If z is in this list, it must be a valid mod-fold, so
                                     return True. Else, return False

ลองใช้กับ Ideone


4

Python 2, 69 ไบต์

f=lambda a,i=0:i/len(a)or a[i]in[a[i-1]+1,i,0][i<=max(a)::2]*f(a,i+1)

การใช้/TrueFalse

คำตอบสำหรับสิ่งที่เป็นลักษณะของชุดดัดแปลงที่พับได้นั้นน่าสนใจน้อยกว่าตอนแรก มันเป็นชุดของรูปแบบ 0, 1, ... , M - 1, 0, 1, ... x 1 , 0, 1, ... , x 2 , ... เช่นนั้นสำหรับฉันทั้งหมด, 0 <= x i <M ลำดับดังกล่าวสามารถผลิตได้โดยเชนเชนของดัชนีทั้งหมด (0-based) ของศูนย์ในอาร์เรย์โดยไม่รวมลำดับแรก


3

เยลลี่ , 19 15 14 ไบต์

LṗLUZ’1¦%/sLe@

ส่งคืน1สำหรับความจริง, 0สำหรับความเท็จ ลองออนไลน์!

อัลกอริทึมคือO (n n )โดยที่nคือความยาวของรายการทำให้ช้าเกินไปและใช้หน่วยความจำมากสำหรับกรณีทดสอบส่วนใหญ่

รุ่นปรับเปลี่ยน - ซึ่งแทนที่สองLด้วย5- สามารถใช้ในการตรวจสอบกรณีทดสอบทั้งหมด โปรดทราบว่ารุ่นที่แก้ไขนี้จะไม่ทำงานสำหรับรายการที่มีความยาวโดยพลการ

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

LṗLUZ’1¦%/sLe@  Main link. Argument: A (array of integers)

L L             Yield the length l of A.
 ṗ              Take the l-th Cartesian power of [1, ..., l], i.e., construct
                all arrays of length l that consist of elements of [1, ..., l].
   U            Upend/reverse each array. This way, the first l arrays start
                with [1, ..., l], as do the next l arrays, etc.
    Z           Zip/transpose the array of arrays.
     ’1¦        Decrement the first array to map [1, ..., l] to [0, ..., l - 1].
        %/      Reduce the array's columns by modulus/residue.
          sL    Split the result into chunks of length l.
            e@  Verify if A belongs to the resulting array.

คุณสามารถเพิ่มคำอธิบายได้ไหม? ในฐานะคนที่ไม่ได้ใช้เจลลี่ (ฉัน) ฉันไม่รู้เลยว่ามันทำงานอย่างไร
สตีเวนเอช.

ฉันจะเพิ่มทันทีที่ฉันเล่นกอล์ฟเสร็จ ยังมีบางสิ่งที่ฉันต้องการลองก่อน
Dennis

ฉันได้เพิ่มคำอธิบายไปแล้ว
Dennis

3

JavaScript (ES6), 98ไบต์

a=>a.every((n,i)=>n?n<(l+=p==i)&&n==p++:p=1,l=p=1)

บันทึก 48 ไบต์โดยเปลี่ยนเป็นการค้นพบของ @ Feersum ค่าใด ๆ ที่กำหนดnในอาร์เรย์จะมีค่าเป็นศูนย์ซึ่งในกรณีนี้การคาดคะเนครั้งต่อไปpคือ 1 หรือเท่ากับการทำนายถัดไปซึ่งในกรณีนี้pจะเพิ่มขึ้น นอกจากนี้เรายังวัดความยาวlของลำดับเริ่มต้นโดยเปรียบเทียบpกับiเนื่องจากnจะต้องน้อยกว่าlตลอดเวลา


2

Python 2, 103 99 ไบต์

f=lambda l,r:r==x or l and f(l-1,[t%l for t in r])|f(l-1,r)
x=input();l=len(x);print+f(l,range(l))

พิมพ์1สำหรับความจริงและ0สำหรับความเท็จ ทดสอบบนIdeone

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