打ic一串


16

您面临的挑战是编写一个打乱字符串的程序或函数。它应该以字符串作为输入(通过任何标准方法),然后执行以下步骤:

  1. 生成一个(不一定是一致的)1到10之间(含1和10 )的随机整数n
  2. 等待n秒。
  3. 打印输入的前/后n个字符,如果少于n 字符,则打印其余的输入。
  4. 如果还有要打印的输入,请返回步骤1。

规则

  • 输入将始终是仅包含ASCII字符(32-126)的非空字符串。
  • 等待时间不必精确地是n秒,但是必须在n的 10%之内。
  • 每次打印一段文本时,您都可以打印尾随换行符。

此处的空格表示1秒。如果输入为Hiccupinator!,则输出可能为:

   Hic     cupin a          tor!

计分

这是,因此最短的代码以字节为单位获胜


评论不作进一步讨论;此对话已转移至聊天
丹尼斯

我们可以为不支持等待/没有时间概念的语言使用空格吗?
FliiFe

我敢打赌,任何一种语言都可以浪费时间而不产生输出,@ FliiFe!
奥马尔

Answers:


9

临时,16块+ 6字节

码

假设输入已经定义为字符列表(["H","e","l","l","o"," ","W","o","r","l","d"]


可以以任何方式打高尔夫球吗?
OldBunny2800 '16

这不是有效的评分方法。请参阅meta post
mbomb007 '16

您是否愿意根据社区共识进行修复?
OldBunny2800 '16

1
我没有Scratch。自发布答案以来,这是您的责任。ScratchBlocks2甚至带有生成器,可以从项目中创建文本代码。
mbomb007 '16

5

Pushy20 17 16或13个字节

根据所允许的内容,有两种解决方案。

16个字节:

@$LT1U&Wm:v;O"cI

在命令行上输入参数:$ pushy hiccup.pshy 'hiccupinator'。每次“打h”后,该行将打印尾随换行符。细目如下:

                      % 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行缓冲,因此程序将等待整个长度,然后显示打string的字符串。

我已经在最新的提交中修复了这个问题通过添加一个简单的方法.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好的,谢谢您的澄清:)
FlipTack

好的回答顺便说一句:-)
路易斯·门多

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!')                                       

@zeppelin节省了2个字节

通过等待滥用等待时间的10%容忍度 n<<10 === 1024*n毫秒为单位。

既然您说等待时间必须在n的 10%之内,所以我决定保存一个字节并等待999毫秒而不是1秒。

感谢@ETHProductions,我不再需要999毫秒的傻了


1
嗯,不确定新的Date()%10是否以任何方式算作“随机”。
zeppelin

@zeppelin公平点,根据标准定义,不包括在内。(meta.codegolf.stackexchange.com/a/1325/56071)。我将相应地对其进行更改。
Lmis

您还可以节省一对字节,通过删除“| 0”
齐柏林

2
您知道,您也可以用三个字节表示1000:1e3;-)
ETHproductions 2016年

1
>(1 + 0.099999 * 10)* 999> 1997是,但是您可以将* 999替换为<< 10,以解决此问题:(1 + 0.099999 * 10)<< 10 => 1024,(1 + 0.99999999 * 10)<< 10 => 10240
齐柏林飞艇

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.randintand time.sleep,但是from random,time import*不起作用...


1
from random,time import*不起作用,因为Python不知道您要从哪个模块导入库。
暴民埃里克

Python 3长了一个字节。在括号前插入'('在print和'i'以及')'之间
乔治

1
将其改编为minipy(Python 3):(while v1:n=ri(1,10);_i("time").sleep(n);p(v1[:n]);v1=v1[n:];从命令行args获取输入)
Esolanging Fruit

您可以将这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

培根,93字节

BASIC中的解决方案。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没关系,因为在这种情况下它不会休眠并且不会产生任何输出。
尼尔

修正了我的解释中的错字。
彼得

1

Perl,42个字节

41个字节的代码+ 1 for -n

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

我不得不强迫Perl刷新输出,因为直到一开始它都没有显示任何内容,因此进行了设置$|。我们使用它$-来跟踪字符数,print因为它不能为负数(因此我可以使用,--$-并且当它为空时它将是虚假的),并且floor是s,尽管因为我使用的是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!=""}

递归lambda。打电话喜欢f["Hello, World!"]


0

> <>(鱼)103 88字节

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

在这里找到在线口译员!

首先尝试解决此问题(不打高尔夫球)。

它等待一定数量的循环(n),因为鱼​​没有可访问的计时器(以秒为单位执行)。

编辑1:将最后一行移到顶部(最后2个字符,并重新使用了起始值。(节省15个字节)。


0

Bash,78个字节

由于尚无人发布Bash解决方案,因此这里是一个。直截了当,但足够小。

打高尔夫球

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

测试

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

0

PHP,81字节

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

用途像:

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字节

#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):"");}

要求输入为 std::string

脱胶和用法:

#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;应保存所有的5个字节std::小号
阿尔菲古达克

@AlfieGoodacre的第五std::只在使用码,在一个golfed仅存在4
卡尔NAPF

啊,一样!
Alfie Goodacre

0

C#,205个字节

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);}

我确信这是可以销毁的,就目前而言,我还没有真正对其进行优化。

未打高尔夫球:

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

PHP,74字节

for($s=$argv[1];$s[$p+=$n]>"";print substr($s,$p,$n))sleep($n=rand(1,10));

用运行php -r 'code' "string"


0

C,149字节,未经测试

#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);}}

运行,添加

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

然后编译并执行


0

Python 3,99个字符

i=input()
import os,time
while len(i):n=1+ord(os.urandom(1))%10;time.sleep(n);print(i[:n]);i=i[n:]
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.