หยุดชั่วคราวนานเป็นสองเท่า


15

ความท้าทายของคุณคือการพิมพ์อินพุตรอเวลาใด ๆ พิมพ์อินพุตรอสองครั้งที่คุณรอครั้งแรกพิมพ์อินพุตอีกครั้งและอื่น ๆ ความล่าช้าเริ่มต้นจะต้องน้อยกว่า 1 ชั่วโมงและคุณจะต้องมีความแม่นยำ +/- 5% ในความล่าช้าครั้งต่อไป นอกจากนั้นไม่มีข้อ จำกัด เกี่ยวกับเวลาหน่วง

ตัวอย่าง:

อินพุต: hi.

เอาต์พุต: hi(หยุดชั่วคราว 1 มิลลิวินาที) hi(หยุดชั่วคราว 2 มิลลิวินาที) (หยุดชั่วคราว 4 มิลลิวินาที) hi(หยุดชั่วคราวhi8 มิลลิวินาที) (หยุดชั่วคราว 16 มิลลิวินาทีhi) เป็นต้น

อนุญาตด้วย:

hi(หยุด 1 นาที) hi( หยุด2 นาที) hi( หยุดชั่วคราว4 นาที) hi(หยุดชั่วคราว 8 นาที) hi(หยุดชั่วคราว 16 นาที) เป็นต้น

ต้องระบุอินพุตที่จุดเริ่มต้นของโปรแกรม (STDIN, พารามิเตอร์บรรทัดคำสั่ง, พารามิเตอร์ฟังก์ชัน ฯลฯ ) และจะเป็นสตริง

ความล่าช้าเริ่มต้นไม่สามารถเป็น 0


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

1
@ComputSparklePony มันจะต้องส่งออกตราบเท่าที่มันสามารถ (จนกระทั่งความร้อนความตายของจักรวาลคอมพิวเตอร์พัง stackoverflow หน่วยความจำไม่เพียงพอ ฯลฯ )
programmer5000

@CompareSparklePony เฉพาะในกรณีที่เป็นเช่น stackoverflow, หน่วยความจำไม่เพียงพอ ฯลฯ นี่y=x=>(x&&alert(x),y())จะได้รับอนุญาตทางเทคนิคแต่ฉันจะลงคะแนนมัน
programmer5000

@ programmer5000 ขอบคุณเข้าใจแล้ว
สหาย SparklePony

ฉันสามารถพิมพ์บรรทัดใหม่ได้หรือไม่
MD XF

คำตอบ:


12

05AB1E , 6 ไบต์

รหัส:

[=No.W

คำอธิบาย:

[        # Start an infinite loop
 =       # Print the top of the stack without popping
  No     # Compute 2 ** (iteration index)
    .W   # Wait that many milliseconds

ลองออนไลน์!


หากคุณเริ่มต้นด้วยการรอ 1 วินาทีที่คุณสามารถใช้แทนw .W
Riley

@Riley ฉันไม่คิดว่ามันจะทำงานได้ wรอหนึ่งวินาทีไม่ว่าจะเกิดอะไรขึ้นและ.Wจะปรากฏขึ้นและรอหลายมิลลิวินาทีนั้น
สหาย SparklePony

@ComporSparklePony คุณพูดถูก Gwมันจะต้องมีการ
Riley

ฉันไม่รู้ว่ามันจำเป็นต้องมีการสะท้อนของปัญหาใด ๆ ในรหัสหรือไม่ แต่ในขณะที่ฉันเรียกใช้ตัวอย่างที่เชื่อมโยงแล้วเอ็นจิ้นให้ฉัน: "คำเตือน: คำขอเกินเวลา จำกัด 60 วินาทีและถูกยกเลิก"
doppelgreener

@doppelgreener ใช่ล่ามออนไลน์อาจไม่ใช่ตัวเลือกที่ดีที่สุดในกรณีนี้ ออฟไลน์ทำงานเหมือนมีเสน่ห์
Adnan


5

Python 3, 60 56 ไบต์

import time
def f(x,i=1):print(x);time.sleep(i);f(x,i*2)

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

  • lambda แบบเรียกซ้ำเปลี่ยนเป็นฟังก์ชันแบบเรียกซ้ำ (-4 ไบต์)

คุณสามารถบันทึกไบต์บนprintคำสั่งได้โดยเปลี่ยนไปใช้ Python 2 :)
numbermaniac

1
@ ตัวเลขช่างใช่ แต่ฉันต้องเปลี่ยนไปใช้ Python 2: P
L3viathan

5

MATL , 8 ไบต์

`GD@WY.T

การหยุดครั้งแรกคือ 2 วินาที

ลองมันที่MATL ออนไลน์ หรือดูเวอร์ชันที่แก้ไขซึ่งแสดงเวลาที่ผ่านไปนับตั้งแต่โปรแกรมเริ่มทำงาน (หากล่ามไม่ทำงานโปรดรีเฟรชหน้านี้แล้วลองอีกครั้ง)

หรือดู gif:

enter image description here

คำอธิบาย

`     % Do...while
  G   %   Push input
  D   %   Display
  @   %   Push iteration index (1-based)
  W   %   2 raised to that
  Y.  %   Pause for that time
  T   %   Push true. This will be used as loop confition
      % End (implicit). The top of the stack is true, which produces an infinite loop 

@ programmer5000 ไม่เป็นที่ต้องการ: ล่ามออนไลน์ทำงานให้คุณได้หรือไม่?
Luis Mendo

ใช่มันเป็นเพราะอะไร
programmer5000

@ programmer5000 ขอบคุณ เพียงเพื่อตรวจสอบ บางครั้งมีปัญหาการหมดเวลา
Luis Mendo

5

Mathematica 34 32 30 29 Bytes

โซลูชันดั้งเดิม 34 ไบต์:

For[x=.1,1<2,Pause[x*=2];Print@#]&

กำจัด 2 ไบต์ด้วยสิ่งที่ต้องทำ

x=1;Do[Pause[x*=2];Print@#,∞]&

กำจัด Byte อีกหนึ่งตัวด้วยโซลูชันแบบเรียกซ้ำของ @ MartinEnder

±n_:=#0[Print@n;Pause@#;2#]&@1

@ngenisis ใช้การเรียกซ้ำ REPRepeat เพื่อลบล้างไบต์อื่น

1//.n_:>(Print@#;Pause@n;2n)&

4
True1>0เป็น แต่บางสิ่งเช่นนี้สั้นกว่านิดหน่อย:±n_:=#0[Print@n;Pause@#;2#]&@1
Martin Ender

ฉันใส่1<2ความคิดเห็นของคุณก่อน อย่างไรก็ตามโซลูชันแบบเรียกซ้ำของคุณจะบันทึกไบต์ ขอบคุณ @MartinEnder
Kelly Lowder

±คือหนึ่งไบต์ในการเข้ารหัส CP-1252 (การเข้ารหัส Windows เริ่มต้น)
JungHwan Min

3
ยิ่งสั้นกว่า:1//.n_:>(Print@#;Pause@n;2n)&
ngenisis

5

คู่, 42 41 ไบต์

x=input('');p=1;while p*=2,pause(p),x,end

ที่บันทึกไว้หนึ่งขอบคุณไบต์ rahnema1, จะสั้นกว่าp*=2p=p*2

ฉันไม่อยากจะเชื่อเลยว่าฉันไม่สามารถลงสนามได้ แต่จริงๆแล้วมันไม่ง่ายเลย

  • อินพุตต้องอยู่ในช่วงเริ่มต้นดังนั้นส่วนแรกจะเป็นไปไม่ได้ที่จะหลีกเลี่ยง
  • ฉันต้องการตัวเลขที่เพิ่มเป็นสองเท่าและต้องเริ่มต้นที่ด้านหน้าของลูป
    • มันเป็นไปได้ที่จะใช้อินพุตเป็นเงื่อนไขสำหรับลูป แต่จากนั้นฉันจะต้องมีที่p*=2อื่น
    • การหยุดชั่วคราวไม่มีค่าตอบแทนมิฉะนั้นอาจเป็นได้ while pause(p*=2)

2
เคล็ดลับที่ฉันเรียนรู้จาก rahnema1: input(0)ทำงาน
Luis Mendo

1
@LuisMendo น่าเสียดายที่เล่ห์เหลี่ยมไม่ทำงานในอ็อคเทฟรุ่นล่าสุด :(
rahnema1

4

Java (OpenJDK 8) , 113 ไบต์

interface M{static void main(String[]a)throws Exception{for(int i=1;;Thread.sleep(i*=2))System.out.print(a[0]);}}

ลองออนไลน์!

-60 ไบต์ขอบคุณ Leaky Nun!


2
+1 สำหรับ "อย่าใช้ Java สำหรับการเล่นกอล์ฟมันเป็นความคิดที่ไม่ดี" คุณสามารถเพิ่มลิงค์ TIO ได้หรือไม่?
programmer5000

@ programmer5000 แน่นอน แต่มันใช้งานไม่ได้เพราะ TIO รอรหัสให้เสร็จ
HyperNeutrino

2
ทำไมต้องใช้อินเตอร์เฟสแทนคลาส?
rightfold

2
@rightfold อินเตอร์เฟซที่ช่วยให้คุณสามารถละเว้นในpublic public static void main
Leun Nun

1
@ColeJohnson จำเป็นต้องมีอาร์กิวเมนต์
Leun Nun

4

R, 50 48 ไบต์

function(x,i=1)repeat{cat(x);Sys.sleep(i);i=i*2}

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

-2 ไบต์ต้องขอบคุณ pajonk

ลองออนไลน์!


ทำไมไม่ลองi=1ใช้i=i*2ตอนท้ายและนอนหลับiล่ะ?
pajonk

นั่นเป็นความคิดที่ดี ... ฉันจะเปลี่ยนสิ่งนั้น
จูเซปเป้

4

Ruby, 34 28 23 22 (+2 สำหรับ-n) = 24 ไบต์

บันทึกได้ 3 ไบต์ด้วย Value Ink!

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

loop{print;sleep$.*=2}

เริ่มที่2แล้ว4เป็นต้น

คำอธิบาย

-n                       # read a line from STDIN
  loop{                } # while(true):
       print;            # print that line
             sleep$.*=2  # multiply $. by 2, then sleep that many seconds. 
                         # $. is a Ruby special variable that starts at 1.

มันจะนอนหลับหนึ่งวินาทีก่อนที่จะอ่านอินพุต แต่คุณสามารถป้อนได้แล้ว
John Dvorak

การเริ่มต้นโปรแกรม Ruby ด้วยการ-nตั้งค่าสถานะให้คุณข้ามการgetsโทรเริ่มต้นเนื่องจากการตั้งค่าสถานะจะจัดการให้คุณ
Value Ink

printไม่มีข้อโต้แย้งเทียบเท่ากับputs$_- บันทึกหนึ่งไบต์
daniero

4

อลิซ 16 ไบต์

1/?!\v
T\io/>2*.

ลองออนไลน์! (ไม่มากที่จะเห็นที่นั่นแน่นอน แต่คุณสามารถตรวจสอบความถี่ที่พิมพ์ภายในหนึ่งนาที)

คำอธิบาย

1    Push 1 to the stack. The initial pause duration in milliseconds.
/    Reflect to SE. Switch to Ordinal.
i    Read all input.
!    Store it on the tape.
/    Reflect to E. Switch to Cardinal.
>    Move east (does nothing but it's the entry of the main loop).
2*   Double the pause duration.
.    Duplicate it.
     The IP wraps around to the first column.
T    Sleep for that many milliseconds.
\    Reflect to NE. Switch to Ordinal.
?    Retrieve the input from the tape.
o    Print it.
\    Reflect to E. Switch to Cardinal.
v    Move south.
>    Move east. Run another iteration of the main loop.

4

R, 44 43 ไบต์

ข้ามไป 44 ยังคงเป็นปกติ 44; (

คำตอบนี้มีวิธีแก้ปัญหาที่ดีอยู่แล้ว แต่เราสามารถบันทึกได้มากขึ้น

function(x)repeat{cat(x);Sys.sleep(T<-T*2)}

xฟังก์ชั่นการถ่ายที่ไม่ระบุชื่อจริงอะไรพิมพ์เป็นอาร์กิวเมนต์ เริ่มที่ 2 วินาทีและเพิ่มเป็นสองเท่าทุกครั้งหลังจากนั้น ละเมิดสิทธิมนุษยชนความจริงที่ว่าTเป็นค่าเริ่มต้นกำหนดให้เป็นที่ประเมินTRUE1

ตราบใดที่ความคิดเห็นนี้ยังคงได้รับไฟเขียวจาก OP เราสามารถทำให้มันสั้นลงได้ แต่ฉันไม่คิดว่ามันจะอยู่ในจิตวิญญาณของความท้าทาย ไม่อนุญาตให้รอเป็นจำนวน 0 อีกต่อไป

function(x)repeat cat(x)

2
ดูคุณเหยียดหยามคนจนTแบบนั้น ในรุ่นที่สั้นกว่าของคำตอบคุณไม่จำเป็นต้องใช้เครื่องมือจัดฟันเพียงช่องว่าง
Giuseppe

1
เฮ้ถ้าTไม่ชอบก็Tสามารถยืนหยัดเพื่อตัวเองได้ นอกจากนี้ยังพบดี :)
JAD

3

Cubix , 30 ไบต์

/(?:u<q.;1A>?ou2$/r;w;q^_q.\*/

ลองที่นี่

แผนที่นี้บนก้อนที่มีความยาวด้านข้าง 3

      / ( ?              # The top face does the delay.  It takes the stack element with the
      : u <              # delay value, duplicates and decrements it to 0.  When 0 is hit the
      q . ;              # IP moves into the sequence which doubles the delay value.
1 A > ? o u 2 $ / r ; w  # Initiates the stack with one and the input.  For input hi this
; q ^ _ q . \ * / . . .  # gives us 1, -1, 10, 105, 104.  There is a little loop that prints 
. . . . . . . . . . . .  # each item in the stack dropping it to the bottom until -1 is hit.
      . . .              # Then the delay sequence is started om the top face
      . . .
      . . .

โอ้ว้าวนี่เป็นภาษาที่เรียบร้อยแล้ว
Skidsdev


3

PHP, 31 ไบต์

for(;;sleep(2**$i++))echo$argn;
for(;;sleep(1<<$i++))echo$argn;

นอน 1, 2, 4, 8, ... วินาที เรียกใช้เป็นท่อด้วยphp -nR '<code>'

จะทำงานจนกว่าการพิมพ์ 63 (บนเครื่อง 64 บิต) หลังจากนั้นจะไม่มีการรออีกต่อไป
รุ่นที่ 1 จะให้คำเตือนsleep() expects parameter 1 to be integer, float given,
รุ่นที่ 2 sleep(): Number of seconds must be greater than or equal to 0จะให้ผลผลิตหนึ่งเตือน

ใส่@ก่อนsleepเพื่อปิดเสียงคำเตือน




2

CJam, 26 bytes

qKes{es1$-Y$<{W$o;2*es}|}h

Doesn't work properly on TIO.

The first pause is 20 milliseconds.

Explanation

q                           e# Push the input.
 K                          e# Push 20 (the pause time).
  es                        e# Push the time (number of milliseconds since the Unix epoch).
    {                       e# Do:
     es1$-                  e#  Subtract the stored time from the current time.
          Y$<{              e#  If it's not less than the pause time:
              W$o           e#   Print the input.
                 ;2*es      e#   Delete the stored time, multiply the pause time by 2, push
                            e#     the new time.
                      }|    e#  (end if)
                        }h  e# While the top of stack (not popped) is truthy.
                            e#  (It always is since the time is a positive integer)

2

C, 51 bytes

main(c,v)char**v;{puts(v[1]);sleep(c);main(2*c,v);}

C, 35 bytes as a function

c=1;f(n){puts(n);sleep(c*=2);f(n);}

Takes input as a command line argument.


2

Batch, 62 bytes

@set/at=%2+0,t+=t+!t
@echo %1
@timeout/t>nul %t%
@%0 %1 %t%

This turned out to be a byte shorter than explicitly doubling t in a loop:

@set t=1
:g
@echo %1
@timeout/t>nul %t%
@set/at*=2
@goto g



2

Python 2, 54 bytes

Uses a lengthy calculation instead of timing libraries.

def f(x,a=1):
 while 1:a*=2;exec'v=9**9**6;'*a;print x

I now notice that my answer is similar to yours. I had not read your answer when I posted, but if you would like to incorporate my solution into yours I'll happily remove my answer.
Tim

2

PowerShell, 35 33 30 29 Bytes

With a helpful hint from whatever and Joey

%{for($a=1){$_;sleep($a*=2)}}

Explanation

%{          # Foreach
for($a=1){  # empty for loop makes this infinite and sets $a
$_;         # prints current foreach item
sleep($a*=2)# Start-Sleep alias for $a seconds, reassign $a to itself times 2           
}}          # close while and foreach

Executed with:

"hi"|%{for($a=1){$_;sleep($a*=2)}}

1
you should be able to use an empty for instead of the while: %{$a=1;for(){$_;sleep($a*=2)}}``
whatever

Thanks! I had tried using a for loop before but I put for(;;). Didn't even try to remove the semi-colons.
SomeShinyMonica

1
Put the $a=1 as the initialization into the for to save another byte (for($a=1){...}). Also, I'm not sure whether to count the %, as the actual routine you're running is just a script block. (My challenges tend to be rather strict about requiring a program, sidestepping such ponderings, but for anything goes questions I'm still not quite sure how to count various ways of using PowerShell.)
Joey

@Joey, sweet that does work. Thanks for the tip
SomeShinyMonica

2

Python 3, 49 bytes

b=input();x=6**6
while 1:print(b);exec("x+=1;"*x)

Uses the slight delay of the += operation and executes it x times. x doubles by adding one to itself as many times as the value of x.

It starts at 6^6 (46656) to stick to the maximum of 5% variation in the delay.


Clever, but this is a memory hog.
eush77

@eush77 yes, on my tablet it terminated after just 7 iterations of the loop! I expect it would last a few longer on my desktop.
Tim

1

Perl 6, 39 bytes

print(once slurp),.&sleep for 1,2,4...* 

Try it (print overridden to add timing information)

Expanded:

  print(        # print to $*OUT
    once slurp  # slurp from $*IN, but only once
  ), 
  .&sleep       # then call sleep as if it was a method on $_

for             # do that for (sets $_ to each of the following)

  1, 2, 4 ... * # deductive sequence generator

1

JS (ES6), 44 42 40 38 36 bytes

Crossed out 44 is still 44

i=1,y=x=>setTimeout(y,i*=2,alert(x))

Don't like alert bombs?

i=1,y=x=>setTimeout(y,i*=2,console.log(x))

Technically correct, but loophole-abusing:

y=x=>(x&&alert(x),y())

-3 bytes thanks to Cyoce, -2 thanks to Business Cat, -2 thanks to Neil


2
I don't seem to be able to test this properly, but you could probably do i=1,y=x=>(alert(x),setTimeout(y,i*=2)) to save a couple bytes
Business Cat

1
I went ahead and edited in a credit message for Cyoce; if you want to change it, feel free to edit/rollback.
HyperNeutrino

1
How about i=1,y=x=>setTimeout(y,i*=2,console.log(x))?
Neil

1

Common Lisp, 49 bytes

(do((a(read))(i 1(* 2 i)))(())(print a)(sleep i))

first delay should be 1 second.


1
You have 321 rep!
programmer5000

@programmer5000 you have 3683 rep!
Cyoce



1

Racket, 51 bytes

(λ(s)(do([n 1(* n 2)])(#f)(displayln s)(sleep n)))

Example

➜  ~  racket -e '((λ(s)(do([n 1(* n 2)])(#f)(displayln s)(sleep n)))"Hello")'
Hello
Hello
Hello
Hello
Hello
^Cuser break
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.