การเพิ่มจำนวนทศนิยมของสตริง


14

รับ 2 อินพุตสตริงและเลขทศนิยมส่งออกสตริงที่คูณด้วยตัวเลขนั้น

การจับคือจำนวนสามารถเป็นจำนวนลอยหรือจำนวนเต็ม

คุณควรเอาท์พุตfloor(n)เวลาสตริงและจากนั้นfloor((n-floor(n))*len(string))ตัวอักษรแรกอีกครั้ง

หมายเหตุอื่น ๆ :

  • อินพุตจะไม่ลอยตัวเสมอไปอาจเป็น int ดังนั้น 1.5, 1 และ 1.0 จึงเป็นไปได้ทั้งหมด มันจะอยู่ในฐาน 10 เสมอและหากคุณต้องการข้อยกเว้นโปรดแสดงความคิดเห็น
  • อินพุตสตริงอาจมีช่องว่างคำพูดและอักขระอื่น ๆ ไม่มีการขึ้นบรรทัดใหม่หรือตัวอักษรควบคุม
  • ไม่มีบิวด์อินสำหรับการทำซ้ำสายโดยตรงแม้'a'*5จะอนุญาตให้ใช้การคูณสตริงเช่น python อย่างไรก็ตามอนุญาตให้เพิ่มสตริงได้

กรณีทดสอบ:

เครื่องหมายจุลภาคและช่องว่างแยกอินพุต

test case, 1 -> test case

case, 2.5 -> casecaseca

(will add more later), 0.3333 -> (will(space)

cats >= dogs, 0.5 -> cats >

หมายเหตุสุดท้าย:

ฉันเห็นคำตอบมากมายที่ใช้ฟังก์ชันการคูณสตริงหรือการยกเลิกในตัว สิ่งนี้ไม่ได้รับอนุญาต @ คำตอบของ VTC นั้นถูกต้องเนื่องจากไม่มีการคูณสตริงเฉพาะอินพุตแบบลอย ดังนั้นกฎที่ชัดเจนคือถ้ามันคูณสตริงโดยตรงคุณไม่สามารถทำได้


การใช้ถ้อยคำถูกแก้ไขซ้ำ ๆ (ฉันไม่เห็นการแก้ไขครั้งแรก) ฉันขอแนะนำให้ลบdirectสตริงที่ทำซ้ำ (สิ่งนี้หมายความว่าอย่างไร) แต่ทั้งหมดที่คุณพูดถูก
edc65


@ Sp3000 ใช่ฉันรู้ ฉันคิดว่าความแตกต่างนั้นสำคัญพอสมควร
Rɪᴋᴇʀ

"ไม่มีบิวด์อินสำหรับการทำซ้ำสตริงโดยตรงแม้แต่การคูณสตริงเช่น python 'a' * 5 ได้รับอนุญาต" คุณไม่ได้อธิบายความแตกต่างระหว่างสิ่งเหล่านี้ พวกเขาฟังดูเหมือนฉัน
msh210

@ edc65 ใน Perl คุณสามารถทำรายการซ้ำแล้วเชื่อมโยงองค์ประกอบของรายการนั้นซึ่งไม่ใช่การทำซ้ำสตริงโดยตรง ใน Perl 5: join "", ("case") x 2vs "case" x 2ใน Perl 6 [~] "case" xx 2เทียบกัน"case" x 2
แบรดกิลเบิร์ต b2gills

คำตอบ:


4

เยลลี่ 5 ไบต์

×L}Rị

ไม่ใช้การซ้ำซ้อนในตัว ลองออนไลน์!

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

×L}Rị  Main link. Left input: n (multiplier). Right input: S (string)

 L}    Yield the length of S.
×      Multiply it with n.
   R   Range; turn n×len(S) into [1, ... floor(n×len(S))].
    ị  Retrieve the elements of S at those indices.
       Indices are 1-based and modular in Jelly, so this begins with the first and
       jump back after reaching the last.

7

Java 7, 89

void g(char[]a,float b){for(int i=0,l=a.length;i<(int)(l*b);)System.out.print(a[i++%l]);}

ใช้ถ่าน [] และลอยและส่งออกไปยัง STDOUT การวนลูปแบบพื้นฐาน


3
การตีกอล์ฟที่ดีแม้สำหรับ java : P
Rɪᴋᴇʀ

นี่เป็นข้อเสนอแนะในคำตอบอื่น ๆ ของฉันเช่นกัน แต่ฉันไม่คิดว่าฉันจะทำเช่นนี้ มันดูไม่ถูกต้องสำหรับฉัน
Marky Markov

เอ๊ะยุติธรรมพอ เป็นที่รู้จักที่นี่ แต่ไม่เป็นไร : D
Addison Crump

ฉันแนะนำให้ประกาศภาษาของคุณเป็น Java 7 จากนั้นไม่มีใครสามารถบอกคุณได้ว่าใช้ lambdas
feersum

6

Pyth, 9 8

s@Lz*lzQ

บันทึก 1 ไบต์ขอบคุณ Pietu1998

ใช้floor(n * len(string))ตัวอักษรจากสตริงโดยใช้การจัดทำดัชนีตามวัฏจักร ฉันเชื่อว่าสิ่งนี้จะเทียบเท่าสูตรที่กำหนดไว้เสมอ

ชุดทดสอบ


1
โปรดอย่าใช้สิ่งนี้จากฉันในไม่ช้า xD
Addison Crump

@VoteToClose จริง ๆ แล้วฉันไม่ได้อ่านคำตอบของคุณเลยลูกเสือเกียรติยศ: PI ไม่ได้ตระหนักว่าการทำซ้ำสายอักขระไม่ได้รับอนุญาตนี่เป็นเพียงสั้น ๆ ว่าสิ่งที่ฉันเกิดขึ้นด้วยวิธีนี้ ...
FryAmTheEggman

1
คุณไม่ต้องการแม้แต่วินาทีsเดียว rangeตลกจัง
PurkkaKoodari

1
NOO! ร้องอยู่ที่มุมอ่า
Addison Crump

6

JavaScript (ES6), 50 ไบต์

แก้ไข 2 fไบต์มากขึ้นที่จะรวมความหมายของฟังก์ชั่น น้อยกว่า 1 ไบต์โดยใช้คำแนะนำ @manatwork หมายเหตุ: การใช้~เรามีการวนซ้ำมากกว่าที่จำเป็น แต่นี่เป็นโค้ดกอล์ฟและนับ 1 ไบต์

f=(s,n,l=s.length*n)=>~n?f(s+s,n-1,l):s.slice(0,l)

ทดสอบ

f=(s,n,l=s.length*n)=>~n?f(s+s,n-1,l):s.slice(0,l)

//TEST
console.log=x=>O.textContent+=x+'\n'
;[
 ['test case', 1, 'test case'],
 ['case', 3.5, 'casecasecaseca'],
 ['(will add more later)', 0.3333, '(will '],
 ['cats >= dogs', 0.5, 'cats >']]
.forEach(t=>{
  var s=t[0],n=t[1],x=t[2],r=f(s,n);
  console.log("«"+s+"» "+n+' => «'+r+'» '+(x==r?'OK':'FAIL expected '+x));
 })
<pre id=O></pre>


โอเคขอบคุณ. จนถึงตอนนี้คำตอบส่วนใหญ่ก็ไม่มีปัญหาและมันง่ายมากที่จะแก้ไข ขอบคุณสำหรับการแก้ไข
Rɪᴋᴇʀ

ตัวพิมพ์เล็ก: n>0ในรหัสเทียบกับn>1ในกรณีทดสอบ
จัดการ

@ การจัดการขอบคุณ มันควรใช้วิธีใดวิธีหนึ่ง
edc65

โอ้ จริง แต่ทำไมไม่เป็นเช่นนั้น~n? (จริงๆเป็นเพียงคำถามลองเฉพาะกรณีทดสอบที่ให้มา)
จัดการ

3
@ edc65 มีคำfจำกัดความใดในโซลูชันของคุณ คุณหายไปf=เหรอ
andlrc

4

Vitsy, 9 ไบต์

คาดหวังคำว่าเป็นอาร์กิวเมนต์และจำนวนที่จะคูณด้วยผ่าน STDIN

zlW*\[DO{]
z          Grab all string argument input.
 l         Get the length of the stack.
  W        Parse STDIN.
   *       Multiply the top two items (length of string and the number of repetitions)
    \[   ] Do the stuff in the loop.
      DO{  Output one char at a time, making sure to duplicate first.

ลองออนไลน์!


ทรูกับคำพูดของคุณคุณตอบได้อย่างรวดเร็ว
Rɪᴋᴇʀ

@RikerW Martin ออก FGITW'd ฉัน
Addison Crump

ทำไมคุณถึงทำเช่นGrab all string argument input.นั้นParse STDIN.อีกครั้ง?
Rɪᴋᴇʀ

@RikerW ข้อโต้แย้งที่เป็นสองเท่าจะถูกวิเคราะห์โดยอัตโนมัติผลักพวกเขาไปยังกองซ้อนทันที การจัดการที่ใช้ไบต์มากกว่าที่คุ้มค่า
Addison Crump

ตกลง. ที่เหมาะสมมากขึ้นในขณะนี้
Rɪᴋᴇʀ

3

CJam, 10 ไบต์

l_,l~*,\f=

สตริงจะถูกส่งไปที่บรรทัดแรกของ STDIN ซึ่งเป็นทศนิยมบนบรรทัดที่สอง

ทดสอบที่นี่

คำอธิบาย

l    e# Read string.
_,   e# Duplicate and get its length.
l~   e# Read second line and evaluate.
*    e# Multiply them. If the result, N, was floored it would give us the number of
     e# characters in the required output.
,    e# Get range [0 1 ... ⌊N⌋-1].
\f=  e# For each character in that range, fetch the corresponding character from the
     e# string using cyclic indexing.

3

Python 2, 71 ไบต์

lambda s,x:"".join(s for i in range(int(x)))+s[:int(len(s)*(x-int(x)))]

ลองที่นี่!

สร้างแลมบ์ดาที่ไม่มีชื่อซึ่งรับค่าสตริงเป็นอาร์กิวเมนต์แรกและลอยเป็นวินาที ส่งคืนสตริงซ้ำ

นี่อาจเป็น 46 ถ้าอนุญาตการสร้างสายอักขระซ้ำภายใน :(


1
เศร้ามาก กฎการคูณสตริงดังกล่าว +1 A + สำหรับ effurt
Addison Crump


3

Perl 6 ,  46 41   39 ไบต์

{([~] $^a xx$^b)~$a.substr(0,$a.chars*($b%1))}    # 46 bytes
{substr ([~] $^a xx$^b+1),0,$a.chars*$^b}         # 41 bytes
{substr ([~] $^a xx$^b+1),0,$a.comb*$b}           # 39 bytes

Perl 6 มีทั้งผู้ประกอบการสตริงซ้ำและผู้ประกอบการรายชื่อซ้ำxxx

เนื่องจากกฎไม่อนุญาตการทำซ้ำสตริงเราจึงทำซ้ำราวกับว่ามันเป็นรายการองค์ประกอบเดียวแทน จากนั้นรายการจะถูกรวมเข้าด้วยกันและสตริงย่อยของมันจะถูกส่งกลับ

การใช้งาน:

# give it a lexical name
my &code = {substr ([~] $^a xx$^b+1),0,$a.chars*$^b}
#          {substr ($^a x$^b+1),0,$a.chars*$^b}

say code('test case', 1).perl;                  # "test case"
say code('case', 2.5).perl;                     # "casecaseca"
say code('(will add more later)', 0.3333).perl; # "(will "
say code('cats >= dogs', 0.5).perl;             # "cats >"

substr ([~] $^a xx$^b+1),0,$a.comb*$b}บันทึกสองตัวอักษร
raiph

2

osascript, 173 ไบต์

โอ้วันนี้มันแย่กว่าที่ฉันคิด

on run a
set x to a's item 1's characters
set y to a's item 2
set o to""
set i to 1
set z to x's items's number
repeat y*z
set o to o&x's item i
set i to i mod z+1
end
o
end

ส่งคืนค่าของสตริงซึ่งเป็นอีกคำตอบหนึ่งที่ใช้การจัดทำดัชนีตามวัฏจักร "string" "repetitions"คาดว่าการป้อนข้อมูลเป็น


Oh my days, this is worse than I thought.จริงมากจริง ๆ
Rɪᴋᴇʀ

มีชุดคำสั่งหลายชุดพร้อมกันหรือไม่? คือset x,y to a's itemsอะไร
Rɪᴋᴇʀ

@RikerW ฉันไม่คิดอย่างนั้น ถ้ามีฉันก็หายไปอย่างจริงจัง
Addison Crump

2

Haskell, 44 ไบต์

c x=x++c x
s#n=take(floor$n*sum[1|a<-s])$c s

ตัวอย่างการใช้: "(will add more later)" # 0.3333->"(will " ->

How it works: c concatenates infinite copies of the string x. It behaves like the built-in cycle. sum[1|a<-s] is a custom length function that works with Haskell's strict type system as it returns a Double (the built-in length returns an Int which cannot be multiplied with n). # takes floor (n * length(s)) characters from the cycled string s.


2

PHP 5, 96 87

9 bytes saved thanks to @manatwork

<?for($i=$z=0;$i++<floor(strlen($a=$argv[1])*$argv[2]);$z++)echo$a[$z]?:$a[$z=0‌​];

Pretty straight forward looping answer.

Ungolfed

<?
$a=$argv[1];
$z=0;
for($i=0; $i < floor(strlen($a)*$argv[2]); $i++) {
    // if the string offset is not set
    // then reset $z back to 0 so we can
    // echo the beginning of ths string again
    @$a[$z] ?: $z=0;
    echo $a[$z];
    $z++;
}

Not sure when should that error suppression help, for me seems to work without @ too: <?for($i=$z=0;$i++<floor(strlen($a=$argv[1])*$argv[2]);$z++)echo$a[$z]?:$a[$z=0];
manatwork

I was getting a notice on case #2 which caused the output to render incorrectly, which is when I added in the suppression. (running in CLI mode)
Samsquanch

“ PHP 5.3 หรือใหม่กว่าค่าเริ่มต้นคือ E_ALL & ~ E_NOTICE & ~ E_STRICT & ~ E_DEPRECATED” - error_reportingดังนั้นเราจึงชอบที่จะใช้โซลูชั่นพื้นฐานของเราในการกำหนดค่าเริ่มต้นและไม่สนใจเกี่ยวกับประกาศและนิสัยที่ดีอื่น ๆ ยกตัวอย่างเช่นการละเว้นการเริ่มต้นของและ$z $i
จัดการ

โอ้เท่ ขอบคุณสำหรับข้อมูล!
Samsquanch

2

R, 59 ไบต์

function(s,l)cat(rawToChar(array(charToRaw(s),nchar(s)*l)))

เป็นฟังก์ชั่นที่ไม่มีชื่อ สิ่งนี้ใช้ charToRaw เพื่อแยกสตริงเป็นเวกเตอร์ของ raws สิ่งนี้ถูกเติมลงในอาร์เรย์ที่มีความยาว * l แปลงกลับเป็นอักขระถ่านและเอาต์พุต
ฉันกำลังจะใช้ strsplit แต่ท้ายที่สุดมันก็นานขึ้น

ทดสอบ

> f=
+ function(s,l)cat(rawToChar(array(charToRaw(s),nchar(s)*l)))
> f('test case', 1) # -> test case
test case
> f('case', 2.5) # -> casecaseca
casecaseca
> f('(will add more later)', 0.3333) # -> (will(space)
(will 
> f('cats >= dogs', 0.5) # -> cats >
cats >
> 

2

Perl, 51 + 3 = 54 ไบต์

$l=<>*y///c;for$i(1..$l){push@a,/./g}say@a[0..$l-1]

ต้องการ: -n, -lและ-M5.010| -E:

 $ perl -nlE'$l=<>*y///c;for$i(1..$l){push@a,/./g}say@a[0..$l-1]' <<< $'test case\n1'
 test case
 $ perl -nlE'$l=<>*y///c;for$i(1..$l){push@a,/./g}say@a[0..$l-1]' <<< $'case\n2.5'
 casecaseca
 $ perl -nlE'$l=<>*y///c;for$i(1..$l){push@a,/./g}say@a[0..$l-1]' <<< $'(will add more later)\n0.3333'
 (will 
 $ perl -nlE'$l=<>*y///c;for$i(1..$l){push@a,/./g}say@a[0..$l-1]' <<< $'cats >= dogs\n0.5'
 cats >

คำอธิบาย:

$l=<>*y///c;              # Calculate output length (eg. 2.5 * input length)
for$i(1..$l){push@a,/./g} # Push a lot of chars from input into @a
say@a[0..$l-1]            # Slice @a according to output length

1

c (มาโครตัวประมวลผลล่วงหน้า), 71

j,l;
#define f(s,m) l=strlen(s);for(j=0;j<(int)(l*m);)putchar(s[j++%l])

ไม่ยุ่งยากมากที่นี่ เพียงแค่ต้องให้แน่ใจว่าl*mถูกโยนไปยังก่อนที่จะเปรียบเทียบกับintj

ลองออนไลน์


1

Oracle SQL 11.2 154 152 ไบต์

WITH v(s,i)AS(SELECT SUBSTR(:1,1,FLOOR(FLOOR((:2-FLOOR(:2))*LENGTH(:1)))),1 FROM DUAL UNION ALL SELECT :1||s,i+1 FROM v WHERE i<=:2)SELECT MAX(s)FROM v;

ยกเลิกแข็งแรงเล่นกอล์ฟ

WITH v(s,i) AS
(
  SELECT SUBSTR(:1,1,FLOOR(FLOOR((:2-FLOOR(:2))*LENGTH(:1)))),1 FROM DUAL 
  UNION ALL 
  SELECT :1||s,i+1 FROM v WHERE i<=:2
)
SELECT MAX(s) FROM v;

ฉันไปวนซ้ำด้วยการเริ่มต้นเลือกการดูแลส่วนทศนิยม

บันทึก 2 ไบต์ด้วย @MickyT


คุณสามารถบันทึกคู่ได้โดยการลบช่องว่างหลังจากเครื่องหมาย) ในส่วนคำสั่ง WITH และตัวเลือกสุดท้าย
MickyT

การประหยัดอีกอย่างคือแทนที่FLOOR(FLOOR((:2-FLOOR(:2))*LENGTH(:1)))ด้วยMOD(:2,1)*LENGTH(:1)
MickyT

และอันสุดท้าย :) คุณสามารถใช้LPADแทนSUBSTR
MickyT

1

อย่างจริงจัง 24 ไบต์

,╗,mi@≈╜n╜l(*≈r`╜E`MΣ)kΣ

ลองออนไลน์!

คำอธิบาย:

,╗,mi@≈╜n╜l(*≈r`╜E`MΣ)kΣ
,╗                        get first input (string) and push it to register 0
  ,mi@≈                   get input 2 (x), push frac(x) (f), int(x) (n)
       ╜n                 push n copies of the string
         ╜l(*≈            push length of string, multiply by f, floor (substring length) (z)
              r`╜E`MΣ     push s[:z]
                     )kΣ  move fractional part of string to bottom, concat entire stack

1

Pyth, 9 bytes

V*Elzp@zN

Basically just doing

             z = input()
V*Elz        for N in range(evaluatedInput()*len(z)):    # flooring is automatic
     p@zN        print(z[N], end="")                     # modular indexing
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.