สะอึกสตริง


16

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

  1. สร้าง (ไม่จำเป็นต้องเหมือนกัน) จำนวนเต็มสุ่มnระหว่างวันที่ 1 และ 10 รวม
  2. รอnวินาที
  3. พิมพ์ตัวอักษรเริ่มต้น / ถัดไปnตัวอักษรของอินพุตหรือส่วนที่เหลือของอินพุตหากมีน้อยกว่าตัวอักษรnตัว
  4. หากมีอินพุตเหลือสำหรับพิมพ์ให้ย้อนกลับไปยังขั้นตอนที่ 1

กฎระเบียบ

  • อินพุตจะเป็นสตริงที่ไม่ว่างที่มีตัวอักษร ASCII เท่านั้น (32-126)
  • เวลาที่รอไม่ได้จะตรงnวินาที แต่มันต้องอยู่ใน 10% ของn
  • คุณสามารถพิมพ์บรรทัดใหม่ต่อท้ายในแต่ละครั้งที่พิมพ์ส่วนของข้อความ

ตัวอย่าง

พื้นที่ที่นี่หมายถึง 1 วินาที หากอินพุตคือHiccupinator!เอาต์พุตอาจเป็น:

   Hic     cupin a          tor!

เกณฑ์การให้คะแนน

นี่คือเพื่อให้รหัสที่สั้นที่สุดในไบต์ชนะ


ความคิดเห็นไม่ได้มีไว้สำหรับการอภิปรายเพิ่มเติม การสนทนานี้ได้รับการย้ายไปแชท
เดนนิส

เราสามารถใช้ช่องว่างสำหรับภาษาที่ไม่รองรับการรอ / ไม่มีความคิดในเวลาได้หรือไม่?
FliiFe

ฉันพนันได้เลยว่าภาษาใดมีวิธีใช้เวลาโดยไม่สร้างผลลัพธ์ @FliiFe!
โอมาร์

คำตอบ:


9

ข่วน 16 บล็อก + 6 ไบต์

Code

สมมติว่าอินพุตถูกกำหนดเป็นรายการของอักขระ ( ["H","e","l","l","o"," ","W","o","r","l","d"])


สิ่งนี้สามารถลงเล่นกอล์ฟได้ไม่ทางใดทางหนึ่ง?
OldBunny2800

นี่ไม่ใช่วิธีการให้คะแนนที่ถูกต้อง ดูโพสต์เมตา
mbomb007

คุณยินดีที่จะแก้ไขตามมติของชุมชนหรือไม่
OldBunny2800

1
ฉันไม่มีรอยขีดข่วน มันเป็นความรับผิดชอบของคุณเนื่องจากคุณโพสต์คำตอบ ScratchBlocks2 ยังมาพร้อมกับเครื่องกำเนิดไฟฟ้าเพื่อสร้างรหัสข้อความจากโครงการ
mbomb007

5

Pushy , 20 17 16 หรือ 13 ไบต์

มีสองวิธีแก้ไขทั้งนี้ขึ้นอยู่กับสิ่งที่ได้รับอนุญาต

16 ไบต์:

@$LT1U&Wm:v;O"cI

$ pushy hiccup.pshy 'hiccupinator'ให้การขัดแย้งในบรรทัดคำสั่ง: สิ่งนี้จะพิมพ์ขึ้นบรรทัดใหม่ตามหลังแต่ละ 'สะอึก' นี่คือรายละเอียด:

                      % Implicit: input on stack as charcodes
@                     % Reverse input, so chars are pulled from start
 $             I      % While there are items on stack:
   T1U                %   Push a random number, 1-10
      &W              %   Wait that many seconds
  L     m:            %   min(time waited, chars left) times do:
          v;          %     Pull a char from the input.
            O"c       %   Print & delete pulled chars

13 ไบต์:

ในขณะที่การเขียนคำตอบข้างต้นฉันได้คำตอบที่สั้นกว่านี้อย่างมาก:

N@$L1TU&Wm:'.

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

อย่างไรก็ตามในขณะที่ทดสอบสิ่งนี้ฉันสังเกตเห็นข้อบกพร่อง - stdoutเป็นบัฟเฟอร์บรรทัดดังนั้นโปรแกรมจะรอความยาวเต็มแล้วแสดงสตริง hiccuped

ฉันได้แก้ไขสิ่งต่อไปนี้แล้ว การคอมมิชชันล่าสุดโดยการเพิ่มวิ.flush()- นี่คือเทคนิคไม่เพิ่มคุณสมบัติใหม่ให้กับภาษาเพียงแก้ไขข้อบกพร่อง แต่ฉันเข้าใจว่าถ้าคุณไม่นำคำตอบนี้มาพิจารณา :)

รายละเอียดมีดังนี้:

        % Implicit: input on stack as charcodes
N       % Set trailing newlines to False
@       % Reverse stack (so the charcodes are pulled off in order)
$       % While there are items left to print:
L       %    Push stack length
1TU     %    Push a random number 1-10
&W      %    Wait that amount of time
m:      %    min(time waited, chars left) times do:
'.      %      Pop and print last char

อนุสัญญาที่ PPCG คือภาษาที่กำหนดโดยการดำเนินการ (ข้อบกพร่องและทั้งหมด) เนื่องจากการกระทำดังกล่าวทำให้เกิดการท้าทายส่วนนั้นจึงไม่สามารถแข่งขันได้
Luis Mendo

@LuisMendo ok ขอบคุณสำหรับการชี้แจง :)
FlipTack

คำตอบที่ดี BTW :-)
Luis Mendo

4

Javascript (ES6) 91 89 ไบต์

f=s=>s&&setTimeout(_=>console.log(s.slice(0,n))|f(s.slice(n)),(n=1+Math.random()*10)<<10)

console.log(2 + f.toString().length); 
f('Hello sweet world!')                                       

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

ละเมิดความอดทน 10% สำหรับช่วงเวลารอโดยรอเป็นn<<10 === 1024*nมิลลิวินาที

เนื่องจากคุณบอกว่าเวลารอต้องอยู่ภายใน 10% ของnฉันจึงตัดสินใจบันทึกหนึ่งไบต์และรอ 999 มิลลิวินาทีมากกว่า 1 วินาที

ฉันไม่ต้องการความบ้าที่ 999 มิลลิวินาทีอีกต่อไปขอบคุณ @ETHProductions


1
อืมไม่แน่ใจว่าDate ใหม่ ()% 10นับเป็น "random" โดยการวัดใด ๆ
zeppelin

@zeppelin Fair point ตามนิยามมาตรฐานจะไม่นับรวม ( meta.codegolf.stackexchange.com/a/1325/56071 ) ฉันจะเปลี่ยนมันตาม
Lmis

คุณสามารถบันทึกไบต์หนึ่งคู่ได้โดยลบ "| 0"
zeppelin

2
คุณรู้ไหมคุณสามารถแสดง 1,000 ในสามไบต์เช่นกัน: 1e3;-)
ETHproductions

1
> (1 + 0.099999 * 10) * 999> 1997 จริง แต่คุณสามารถแทนที่ * 999 ด้วย << 10 เพื่อหลีกเลี่ยงปัญหานี้: (1 + 0.099999 * 10) << 10 => 1024, (1 + 0.99999999 * 10) << 10 => 10240
zeppelin

4

Python 2, 93 92 ไบต์

import random,time
def F(s):
 if s:n=random.randint(1,10);time.sleep(n);print s[:n];F(s[n:])

-1 ไบต์ขอบคุณ Flp.Tkc

ฉันแน่ใจว่ามีวิธีที่จะทำให้สั้นลงrandom.randintและtime.sleepแต่from random,time import*ไม่ทำงาน ...


1
from random,time import*ไม่ทำงานเนื่องจาก Python ไม่ทราบว่าคุณต้องการนำเข้าไลบรารีจากโมดูลใด
Erik the Outgolfer

Python 3 มีความยาวหนึ่งไบต์ แทรก '(' ระหว่างการพิมพ์และ 'i' และ ')' ก่อนวงเล็บ
จอร์จ

1
ปรับให้เป็น minipy (Python 3): while v1:n=ri(1,10);_i("time").sleep(n);p(v1[:n]);v1=v1[n:];(รับอินพุตจากบรรทัดคำสั่ง)
แยกผลไม้

คุณสามารถเขียน 1 ไบต์นี้ให้สั้นลงเพื่อใช้เป็นฟังก์ชันเรียกซ้ำ: import random,timeจากนั้นจึงขึ้นdef F(s):บรรทัดใหม่if s:n=random.randint(1,10);time.sleep(n);print s[:n];F(s[n:])
FlipTack

3

Perl 6 , 62 ไบต์

{$_=$^a;while $_ {sleep my \t=(1..10).roll;put s/.**{0..t}//}}

ขยาย

{ # block lambda with parameter 「$a」

  $_ = $^a; # declare parameter, and store it in 「$_」
            # ( the input is read-only by default )

  while $_ {
    # generate random number and sleep for that many seconds
    sleep my \t=(1..10).roll;

    put
      s/              # substitution on 「$_」 ( returns matched text )
        . ** { 0..t } # match at most 「t」 characters
      //              # replace it with nothing
  }
}

1

แบตช์ 131 ไบต์

@set/ps=
:l
@set/an=%random%%%10+1
@timeout/t>nul %n%
@call echo(%%s:~0,%n%%%
@call set s=%%s:~%n%%%
@if not "%s%"==2" goto l

การใช้set/pn=<nulจะให้ผลที่ดีกว่ายกเว้นว่าจะมีการเว้นวรรค


1

Pyth, 16 ไบต์

Wz.d_JhOT<zJ=>zJ

คุณสามารถลองออนไลน์ได้ แต่ใช้งานไม่ได้เนื่องจากล่ามออนไลน์แสดงเฉพาะเอาต์พุตเมื่อโปรแกรมเสร็จสิ้น

คำอธิบาย

Wz         While z (the input) is not empty:
     hOT   Get a random number between 1-10 (inclusive)
    J      Set the variable J to that number
 .d_       Sleep for that number of seconds
 <zJ       Get and implicitly print the first J characters of the input
  >zJ      Get all characters of z at and after index J
 =         Set z to that string

1

MATL 19 ไบต์

`10YrtY.ynhX<:&)wDt

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

ลองออนไลน์! คอมไพเลอร์ออนไลน์จะค่อย ๆ สร้างผลลัพธ์ด้วยการหยุดชั่วคราว

`         % Do...while loop
  10Yr    %   Random integer from 1 to 10
  tY.     %   Duplicate. Pause that many seconds
  y       %   Duplicate the second-top element. This is the remaining string; or it
          %   takes the input implicitly in the first iteration
  n       %   Number of elements
  hX<     %   Minimum of the number of elements and the random number
  :       %   Range from 1 to that
  &)      %   Apply as index. Push the substring as given by the index and the
          %   remaining substring
  w       %   Swap
  D       %   Display
  t       %   Duplicate the remaining substring. This is used as loop condition:
          %   if non-empty execute next iteration
          % End loop implicitly

1

BaCon , 93 ไบต์

ทางออกในภาษาเบสิก ฟังก์ชัน RANDOM () สร้างตัวเลขระหว่าง 0 ถึง n-1 ดังนั้นเราต้องใช้ RANDOM (11) เพื่อรับตัวเลขตั้งแต่ 0 ถึง 10

INPUT s$
WHILE LEN(s$)>0
n=RANDOM(11)
SLEEP n*1000
?LEFT$(s$,n),SPC$(n);
s$=MID$(s$,n+1)
WEND

ตัวอย่างเซสชันบรรทัดแรกคืออินพุตวินาทีเอาต์พุต:

Hiccupinator!
Hiccupi       nato    r!

2
หากสิ่งที่คุณพูดนั้นเป็นความจริงฟังก์ชั่นการสุ่มของคุณควรเป็นn=RANDOM(10)+1รหัสของคุณจะสร้างตัวเลขตั้งแต่ 0-10 รวมไม่ใช่ 1-10
Octopus

1
@ Octopus มันไม่สำคัญเพราะมันจะนอนไม่นานและไม่มีผลลัพธ์ในกรณีนั้น
Neil

แก้ไขข้อผิดพลาดในคำอธิบายของฉัน
ปีเตอร์

1

Perl, 42 ไบต์

41 รหัสไบต์ + 1 -nสำหรับ

$|=$-=--$-||sleep 1+rand 10,print for/./g

ผมต้องบังคับ Perl $|เพื่อการส่งออกล้างเป็นมันก็ไม่ได้แสดงอะไรจนสิ้นสุดในตอนแรกจึงตั้งค่า เราใช้$-เพื่อติดตามจำนวนตัวอักษรprintเนื่องจากไม่สามารถลบได้ (ดังนั้นฉันจึงสามารถใช้--$-และมันจะงี่เง่าเมื่อมันว่างเปล่า) และมันก็ยังfloors แม้ว่าจะเป็นเพราะฉันใช้ผลตอบแทนsleepสำหรับตอนนี้ที่ไม่ ไม่เป็นไร

การใช้

perl -ne '$|=$-=--$-||sleep 1+rand 10,print for/./g' <<< 'Hello, World!'
    Hell      o, Wor     ld!
# spaces showing delay!

0

Ruby, 56 ไบต์

f=->s{n=sleep rand 1..10;print s.slice!0,n;f[s]if s!=""}

แลมบ์ดาแบบเรียกซ้ำ f["Hello, World!"]โทรเช่น


0

><> (Fish) 103 88 Bytes

5>:?vl1-?!v+40.    >~
   1x2v   
>^  0  |:!/>:?!v1-b2.
^-1}< <     |~!/:?!^1-i:1+?!;of3.

Online interpreter found here!

First attempt at this problem (not golfed).

It waits a certain amount of loops(n) as fish doesn't have a timer that is accessible (Execution in ticks).

Edit 1: Moved last line across to the top (last 2 characters and re-used the starting values. (saving of 15 bytes).


0

Bash, 78 bytes

As nobody has posted a Bash solution yet, here is one. Straightforward, yet small enough.

Golfed

H() { N=$(($RANDOM%10+1));sleep $N;echo ${1:0:$N};S=${1:$N};[ "$S" ] && H $S;}

Test

>H "It's the Hiccupinator"
It's the
Hiccupi
n
ator

0

PHP, 81 bytes

for(;''<$s=&$argv[1];$s=$f($s,$n))echo($f=substr)($s,0,sleep($n=rand(1,10))?:$n);

use like:

php -r "for(;''<$s=&$argv[1];$s=$f($s,$n))echo($f=substr)($s,0,sleep($n=rand(1,10))?:$n);" "Hiccupinator!"

0

C++14, 202 bytes

#import<thread>
void f(auto c){if(c.size()<1)return;int n=(uintptr_t(&c)%99)/10+1;std::this_thread::sleep_for(std::chrono::seconds(n));std::cout<<c.substr(0,n)<<std::endl;f(n<c.size()?c.substr(n):"");}

Requires input to be a std::string

Ungolfed and usage:

#include<iostream>
#include<string>

#import <thread>

void f(auto c){
  if (c.size() < 1) return;
  int n=(uintptr_t(&c) % 99) / 10 + 1;
  std::this_thread::sleep_for(std::chrono::seconds(n));
  std::cout << c.substr(0,n) << std::endl;
  f(n < c.size() ? c.substr(n) : "");
}

int main(){
  std::string s="abcdefghijklmnopqrstuvwxyz";
  f(s);
}

using namespace std; should save 5 bytes from all of those std::s
Alfie Goodacre

@AlfieGoodacre the 5th std:: is only in the usage code, in the golfed one there are only 4
Karl Napf

Ah so it's identical!
Alfie Goodacre

0

C#, 205 bytes

void X(string s){Random r=new Random();int n=r.Next(1,11);while(n<s.Length){Console.WriteLine(s.Substring(0,n));s.Remove(0,n);n*=1000;System.Threading.Thread.Sleep(n);n=r.Next(1,11);}Console.WriteLine(s);}

I'm sure this can be destroyed, I haven't really optimised it at all as it stands.

Un-golfed:

void X(string s)
{
    Random r = new Random();
    int n = r.Next(1,11);
    while(n < s.Length)
    {
        Console.WriteLine(s.Substring(0,n));
        s.Remove(0,n);
        n *= 1000;
        System.Threading.Thread.Sleep(n);
        n = r.Next(1,11);
    }
    Console.WriteLine(s);
}


0

C, 149 bytes, not tested

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int f(char *s){int n;while(*s){sleep(n=rand()%10+1);for(;*s&&n--;s++)printf("%.*s",1,s);}}

to run, add

int main(){f("Programming Puzzles & CodeGolf");}

then compile and execute


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