เพิ่มขนาดตัวเลข
Intro
ระบุจำนวนเต็ม\$< 100,\$ ขยาย / ย่อการแสดงตัวเลขในภาษาอังกฤษให้มีอักขระมากที่สุดเท่าที่เป็นค่า
แซนด์บ็อกซ์
กฎ
รับหมายเลข\$n\$และรับเป็นคำจากพจนานุกรมนี้ คุณสามารถใช้พจนานุกรมนี้ในแบบที่คุณต้องการ (รายการ, อ่านจากลิงค์, บรรทัดคำสั่ง arg, STDIN, ตัวแปรและอื่น ๆ )
นี่คือไฟล์ที่เป็น JSON Array (ได้รับความอนุเคราะห์จาก Lyxal)
จากนั้นทำสิ่งต่อไปนี้ด้วยสตริง:
- หากความยาวของสตริงต่ำกว่าตัวเลขให้ทำซ้ำอักขระบางตัวในตำแหน่งจนกว่าความยาวจะตรงกับตัวเลข
- ไม่ควรใช้อักขระตัวแรกและตัวสุดท้ายซ้ำและจำนวนการซ้ำของอักขระอื่น ๆ ควรแตกต่างกันอย่างน้อยหนึ่งตัว (ดังนั้นคุณสามารถทำซ้ำบางตัวได้ 5 ครั้งและอื่น ๆ 6 ครั้งเช่นไม่สำคัญว่าอักขระใด เป๊ะ).
- หากความยาวของสตริงมากกว่าตัวเลขให้นำอักขระใด ๆ ออกยกเว้นตัวแรกและตัวสุดท้ายเพื่อให้ความยาวตรงกับตัวเลข 1 และ 0 เป็นข้อยกเว้นเนื่องจากมีขนาดเล็กเกินไป
- ต้องรักษาลำดับของตัวอักษรในสตริง
- ตัวอย่าง:
50 → f[ift]y → 3 letters must be duplicated 16 times
61 → s[ixtyon]e → 5 letters must be duplicated 10 times, 1 character must be duplicated 9 times
การรันทีละขั้นตอน
ยก 11 เป็นตัวอย่าง
(จัดรูปแบบเป็นword → length)
eleven → 6
^
elleven → 7
^
elleeven → 8
^
elleevven → 9
^
elleevveen → 10
^
ellleevveen → 11 (end)
ตัวอย่าง
2 → to
3 → the or tre or tee
4 → four
5 → fiive or fivve
7 → seevven or sevveen or seeveen
10 → teeeeeeeen
Special cases:
0 → (Any null value/no output)
1 → o
เกณฑ์การชนะ
นี่คือรหัสกอล์ฟ รหัสที่สั้นที่สุดในแต่ละภาษาชนะ
เงินรางวัล Lo ™ อ้างสิทธิ์โดย Dominic Van Essen!
คำตอบแรกของ Husk สำหรับคำถามนี้จะได้รับค่าตอบแทน +50 จากฉันนอกเหนือจากค่าหัวของ Zgarb หากเป็นไปตามเกณฑ์ที่ระบุไว้ที่นี่ในส่วนความพยายามเพิ่มเติม
คำตอบ
R , (110105 96) 96 95 ไบต์
หวังว่าจะตอบคำถามที่ถูกต้องตอนนี้ ...
function(n,a)cat(substring(b<-a[n+1],r<-sort(c(1,l<-nchar(b),rep(3:l-1,n))[1:n]*!!n),r),sep='')
ลองออนไลน์!
ดู๊! อ่านคำถามแล้วอึ้ง!
ตกลงความพยายามครั้งแรก (หลายครั้ง) ไม่ได้ตอบคำถามที่ถูกต้องเพราะฉันอ่านคำท้าทายไม่ถูกต้องและไม่ได้สังเกตว่าไม่ควรใช้อักขระตัวแรก + ตัวสุดท้ายซ้ำ บังเอิญแม้ว่าการเขียนซ้ำที่ใกล้เสร็จสมบูรณ์จะออกมาในจำนวนไบต์เท่ากัน!
อย่างไร? รหัสที่ไม่ได้รับการแก้ไข:
size_up=
function(n,a){ # n is number, a is array of text strings
b=a[n+1] # get the text string for this number
l=nchar(b) # get the number of characters
r=sort( # r = sort the indices of characters to output:
c(1,l, # we need index 1 and the last index (l)...
rep(2:(l-1),n)) # then plenty of repeats of 2..(l-1) ...
[1:n]) # and, from this, we'll use the first n.
if(n) # only output anything if n>0 ...
cat(substring(b,r,r),sep='')
# ...in which case concatenate the indexed
} # characters of the text string b.
Haskell , 59 ไบต์
d!n|h:t<-d!!n=take n$h:[t!!div(i*length t-i)(n-1)|i<-[1..]]
ลองออนไลน์!
การใช้งาน: ["zero","one",...]!n.
ตัวอย่างเช่นสำหรับอินพุต11มันจะส่งออก'e'ตามด้วย"leven"ดัชนีที่: $$ \ left \ lfloor \ frac {4} {10} \ right \ rfloor, \ left \ lfloor \ frac {8} {10} \ right \ rfloor \ จุด \ left \ lfloor \ frac {40} {10} \ ขวา \ rfloor $$"lleevvveen"ซึ่งเป็น (โดยสัญชาตญาณสูตรจะขยายดัชนี [0..4] จาก"leven"เป็น [0..10])
take n$ใช้เพื่อรองรับกรณี 0 (มิฉะนั้นผลลัพธ์คือ"z") แต่ยังให้เราเขียน[1..]แทน[1..n-1]ดังนั้นต้นทุนสุทธิจึงเป็นเพียง 4 ไบต์แทนที่จะเป็น 7
แกลบ , 28 27 25 22 21 ไบต์
แก้ไข: -2 ไบต์ขอบคุณ Zgarb, เพิ่มอีก -3 ไบต์ขอบคุณ Jo King และจากนั้นเพิ่มอีก -1 ไบต์ขอบคุณ Zgarb อีกครั้ง (4 ไบต์สุดท้ายเหล่านี้ใช้เวลาสักครู่ในการแก้ไขและทำความเข้าใจฉันได้เพิ่มคำอธิบายแล้ว แต่ มันไม่ใช่เรื่องง่ายสำหรับแกลบมือใหม่อย่างผม ... )
SṀ!(O↑¹:1S:ȯ*¹…2←L)!→
ลองออนไลน์!
ครั้งแรกของฉันแกลบคำตอบแรงบันดาลใจจาก 'ภาษาของเดือน
พอร์ตของวิธีการในคำตอบของฉัน R
ฉันมีความสุขมากที่ได้คิดหาวิธีเขียนโปรแกรมการทำงาน (ไชโย!) ดังนั้นมันจึงแทบจะไม่แน่นอนเท่าที่ควร ...
อย่างไร? (แสดงความคิดเห็นหลังจากตีกอล์ฟ Zgarb & Jo King's: ตอนนี้ค่อนข้างซับซ้อน ... ):
ก่อนอื่นเราจะใส่อาร์กิวเมนต์โดยนัยทั้งหมด (เป็นตัวยก⁰และ²):
SṀ!(O↑²:1S:ȯ*²…2←L)!→²⁰
ตอนนี้รหัสแสดงความคิดเห็น:
S # S is a 'hook' combinator to recycle argument x: Sfgx == fx(gx)
Ṁ! # f is M! = map the list-index function across a list of indexes, with the list to index given by argument x
(O↑²:1S:ȯ*²…2←L) # g is (O↑²:1S:ȯ*²…2←L) = function to generate the list of indexes from argument x (see below)
!→²⁰ # x is !→²⁰ = the text name of the number (for instance, "twenty"; see below)
# Explanation of g:
S # S 'hook' combinator again, so Sfgy here == fy(gy)
: # f is : = prefix with y
ȯ # g is given by ȯ*²…2←
# ȯ is 3-function combinator: ȯfgh == single function fgh
← # subtract 1
…2 # and construct a series from 2..this value,
*² # and repeat this series n times, with n given by argument 2 (first given program argument);
# so ȯ*²…2← is the function that repeats 2..x, n times, for argument y
L # y is the length of the argument x
# So: up to here, we have :Lx *²…2←Lx
:1 # Now prefix with 1...
↑² # then select the first n elements, with n given by argument 2 (first given program argument)...
O # and sort the result. This is the list of indexes from x.
# Explanation of x:
! ⁰ # element of argument 1 (second given), indexed by...
→² # argument 2 (first given) +1
เยลลี่ 20 ไบต์
ị©L⁸,_2œs/Ẉ⁸>Ø.¤j®x"
Dyadic Link ยอมรับหมายเลขทางด้านซ้ายและรายการชื่อตัวเลขทางด้านขวา (ตามลำดับดัชนี Jelly) ซึ่งให้รายการอักขระ
ลองออนไลน์! หรือดูทั้งหมด 100 .
อย่างไร?
ị©L⁸,_2œs/Ẉ⁸>Ø.¤j®x" - Link: N, Words
ị - (N) index into (Words) -> the word
© - copy for later
L - length
⁸ - chain's left argument -> N
, - pair -> [N, length(Word)]
_2 - subtract two -> [N-2, length(Word)-2]
/ - reduce using:
œs - split (implicit range [1..N-2]) into (length(Word)-2)
approximately equal parts
Ẉ - length of each -> sizes (call this [s1, s2, ...])
¤ - nilad followed by link(s) as a nilad:
⁸ - chain's left argument -> N
Ø. - bits -> [0,1]
> - greater than? -> [N>0, N>1]
j - join -> [N>0, s1, s2, ..., N>1]
® - recall from earlier -> the word
" - zip with:
x - repeat
J , 40 39 ไบต์
[{.[(]#~1:0 _1}2+/@}.($[:}:@}.@=#\))>@{
ลองออนไลน์!
Python 3 , 98 ไบต์
def f(n,a):x,*s,y=a[n];l=len(s);d=n-2;a[0]=(x+"".join(s[i]*(d//l+(i<d%l))for i in range(l))+y)[:n]
ลองออนไลน์!
-8 ไบต์ขอบคุณ OVs
-2 ไบต์ขอบคุณ pxeger
(-9 ไบต์ระหว่างทั้งสองเนื่องจากหนึ่งในไบต์ที่บันทึกไว้ทับซ้อนกันระหว่างการเพิ่มประสิทธิภาพทั้งสอง)
-9 ไบต์มากขึ้นขอบคุณ ovs
JavaScript (ES6), 78 73 ไบต์
ความคาด(n)(dictionary)หวัง ส่งคืนสตริงว่างสำหรับศูนย์
n=>d=>(s=d[n--],g=k=>k<n?s[~k?k/~-n*(s.length-2)+1|0||2:0]+g(k+1):'')(-1)
ลองออนไลน์!
K (oK) , 50 46 40 ไบต์
-4 ไบต์ขอบคุณ ovs
-6 ไบต์ขอบคุณ coltim
{x#,/(1,(+/'(-2+#y@x;0N)#2_x#1),1)#'y@x}
ลองออนไลน์!
สนิม 203 ไบต์
|i,a|if i<2{a[i].truncate(i)}else
if let[f,m@..,l]=&*a[i].clone(){let mut v:Vec<_>=(0..m.len()).cycle().take(i-2).collect();v.sort();a[i]=v.iter().map(|&j|m[j]).collect();a[i].insert(0,*f);a[i].push(*l)}
ลองออนไลน์
fn(usize,&mut [Vec<char>])ปิดประเภท ผลลัพธ์ถูกเขียนในรูปแบบa[i].
APL (Dyalog Unicode) , 39 37 36 ไบต์
-2 ไบต์ขอบคุณRazetime !
-1 ไบต์ขอบคุณJo King !
สมมติ⎕IO←0.
⊃∘⌷{⍵↑⍺/⍨1,⍨1,(⍵-2)(⌊÷+|⍨>∘⍳⊢)≢2↓⍺}⊣
ลองออนไลน์!
คำอธิบาย
ฟังก์ชันนี้ใช้พจนานุกรมเป็นอาร์กิวเมนต์ด้านขวาและจำนวนเต็มเป็นอาร์กิวเมนต์ด้านซ้าย
⊃∘⌷เป็นฟังก์ชันที่รับคำที่ดัชนีด้านซ้ายในพจนานุกรมด้านขวา
⊣คือฟังก์ชันระบุตัวตนด้านซ้าย
ฟังก์ชั่นภายใน{ ... }แล้วเรียกว่ามีคำว่าเป็นอาร์กิวเมนต์ซ้ายและจำนวนเต็มเป็นอาร์กิวเมนต์ขวา⍺⍵
≢2↓⍺คือความยาวของคำที่ไม่มีอักขระสองตัวแรก ( a)
⍵-2เป็นเพียงอินพุตจำนวนเต็มลบ2( b)
กับทั้งสองมีปากเสียงฟังก์ชั่น⌊÷+|⍨>∘⍳⊢เรียกว่า: สร้างเวกเตอร์ดัชนี
⍳⊢ คำนวณ เปรียบเทียบสิ่งนี้กับเวกเตอร์ดัชนีซึ่งส่งผลให้เวกเตอร์บูลีนมี's and ' s เป็นชั้นของ. เพิ่มสิ่งนี้ให้กับเวกเตอร์บูลีน0 1 ... a-1
|⍨b mod a
>b mod a 1a - b mod a 0
⌊÷b÷a
+
1,prepends 1, ผนวก1,⍨ เลือกหลายรายการจากคำ x, ในขณะที่ระบุ ตัวอย่าง: . นี่คือการสับเปลี่ยน ( ) ที่นี่เพื่อหลีกเลี่ยงวงเล็บ จากนั้นรับจำนวนอักขระที่ต้องการ นี้เป็นสิ่งจำเป็นสำหรับและ1
⍺/vv1 3 2 2 2 1/'eleven'≡'ellleevveen'⍨
⍵↑01
ถ่าน 29 ไบต์
NθF⊕θSηFθ§η∧ι⊖÷×⊕⁻ιθ⁻²Lη∨⁻²θ¹
ลองออนไลน์! ลิงก์คือรหัสเวอร์ชันที่ละเอียด ป้อนข้อมูลเป็นตัวเลขแล้วตามด้วยพจนานุกรม การลบF⊕θSηผลลัพธ์ในโปรแกรมที่ปรับขนาดบรรทัดที่สองเป็นความยาวที่กำหนดในบรรทัดแรก น่ารำคาญ2เป็นตัวเลขที่ยากที่สุดในการประมวลผลเนื่องจากทั้งอักขระตัวแรกและตัวสุดท้ายเป็นตัวพิมพ์เล็ก คำอธิบาย:
Nθ
อินพุตn.
F⊕θSη
nอ่านในพจนานุกรมจนถึงรายการสำหรับ
Fθ
วนซ้ำnอักขระ ( ⭆θยังใช้งานได้)
§η
แสดงอักขระของคำในพจนานุกรมที่กำหนดโดยดัชนีที่คำนวณ
∧ι
0สำหรับตัวอักษรตัวแรกของการส่งออกดัชนีคำนวณอยู่เสมอ
⊖÷×⊕⁻ιθ⁻²Lη∨⁻²θ¹
มิฉะนั้นให้ปรับขนาดระยะทางไปยังจุดสิ้นสุดของสตริงจาก0..n-2ถึง0..l-2( lความยาวของคำในพจนานุกรมอยู่ที่ไหน) โดยปัดขึ้น หมายเหตุว่าผลนี้0/0สำหรับn=2ดังนั้นส่วนที่มีการบังคับให้1ในกรณีนี้ ตัวอย่างสำหรับn=5, five:
i=0 o=0
i=1 o=3-ceil((4-1)*(4-2)/(5-2))=3-ceil(3*2/3)=3-2=1
i=2 o=3-ceil((4-2)*(4-2)/(5-2))=3-ceil(2*2/3)=3-2=1
i=3 o=3-ceil((4-3)*(4-2)/(5-2))=3-ceil(1*2/3)=3-1=2
i=4 o=3-ceil((4-4)*(4-2)/(5-2))=3-ceil(0*2/3)=3-0=3
ดังนั้นดัชนีการส่งออกจะมีผลในการ0, 1, 1, 2, 3 fiive(ในความเป็นจริงการคำนวณทำได้โดยใช้ดัชนีเชิงลบดังนั้นดัชนีที่แท้จริงคือ0, -3, -3, -2, -1ซึ่งหมายความว่าพวกเขาจะถูกปัดเศษลงแทนที่จะเป็นขึ้น)
Perl 5 , 77 ไบต์
sub{@w=@{$d[$n=pop]};$h=$n<4?2:(@w-2)/($n-2);join'',@w[map.99+$h*$_,0..$n-1]}
ลองออนไลน์!
Scala, 93 ไบต์
i=>a=>{val f+:m:+l=a(i);(f+:Seq.fill(i)(m.indices).flatten.take(i-2).sorted.map(m):+l)take i}
ลองออนไลน์!
Int=>Seq[Seq[Char]]=>Seq[Char]กำหนดหน้าที่ของประเภท
คำอธิบาย:
i => a => { // define a curried lambda function with two arguments
val f+:m:+l = a(i) // destructure the string to extract the first and last letter
( // return...
f +: // the first letter prepended to
Seq.fill(i)(m.indices) // the numbers from 0 to i-2, all repeated i times
// for "seven", this is 7 instances of [0,1,2]
.flatten // flatten this list
.take(i-2) // take the first i-2 numbers from the list
.sorted // sort them
.map(m) // and use them as indices for the middle part of the string
:+l // append l
)
take i // to handle 0 and 1, take i letters from the result
}
05AB1E , 20 ไบต์
è⍍₂и¾šηε{®ª}0šδè¹ù
รับรายการคำเป็นอินพุตเพิ่มเติมที่สอง
แสดงผลเป็นรายการอักขระ
ลองออนไลน์หรือยืนยันทั้งหมด\$[0,99]\$กรณีทดสอบ
คำอธิบาย:
è # Index the (implicit) input-integer into the (implicit) string-list
ā # Push a list in the range [1,string-length] (without popping)
¨¨ # Remove the last two values to make the range [1,length-2]
₂и # Repeat this list 26 times: [1,2,3] → [1,2,3,1,2,3,1,2,3,...]
¾š # Prepend a 0 to this list
η # Take all prefixes
ε # Map each prefix-list to:
{ # Sort the list
®ª # And append a -1
}0š # After the map: prepend a 0 to the list of lists
δ # Map over each list:
è # Index it in the string that's still on the stack
# (modulair 0-based, so the -1 indexes into the last character)
¹ù # Keep the list of characters of a length equal to the first input
# (after which the result is output implicitly as result)
หาก outputting ผลอนันต์ขี้เกียจจะได้รับอนุญาตที่₂иอาจจะÞแทน -1 ไบต์: ลองออนไลน์
แฟกเตอร์ 201 ไบต์
: s ( n d -- s ) dupd 2dup nth length 2 - pick 1 - 1 max dup [ / ] dip swap
<repetition> dup first [ + ] accumulate [ 1 + >integer ] map nip [ nth ] dip
dupd swap nths [ 1 head ] dip append swap head ;
ลองออนไลน์!
Japt , 25 ไบต์
gV
_uUÊ-2 Ä}hV[0UÊÉ] Í®gU
ลองมัน
Input :
U = dictionary
V = number
gV - get element from dictionary
hV[0UÊÉ] - build a list of V indexes,
starting with [0 , last idx]
and calling the following function on last element to generate next items.
_uUÊ-2 Ä} - returns number modulo( literal length -2) + 1
Example : 11 - eleven
[0,5] -> 5%4+1
[0,5,2] -> 2%4+1
[0,5,2,3]
[0,5,2,3,4,1,..]
ͮgU - sort and maps to literal
-P flag to join result