孩子们洗牌


12

洗牌对孩子来说很难,因此他们必须想出办法,尽可能简单地获得合理的洗牌。

一种取得相当好的结果的方法是:

  1. 取出顶部卡并将其随机插入卡座中
  2. 取出底部卡并将其插入卡座中的随机位置
  3. 继续,直到您认为足够好为止。

请注意,切勿将卡插入顶部或底部,应将其放置卡座中的某个位置。


取而代之的洗牌中,我们将洗牌字母数字字符:0-9A-Ja-jq-zQ-Z

从下面显示的字符串开始,然后按照上述方法对字符进行随机排列。您可以选择是否要继续无限次随机播放或随机播放100张卡片(顶部100张卡片,底部100张卡片)。

0123456789abcdefghijqrstuvwxyzABCDEFGHIJQRSTUVWXYZ

挑战在于显示正在改组的字符。每个“随机播放”(取出并插入卡)应花费0.25至0.35秒。

下面的gif显示了示例输出:

在此处输入图片说明


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


“你为什么不有a-t,而不是a-jq-z?” 因为这将说明卡片套装,而不仅仅是字符。是的,有5套西装。


注意:我已决定停止在 -challenges 上使用选中标记。这里这里的相关元文章。


怎么有5套西装?
TrojanByAccident

1
@TrojanByAccident五个集卡(ASCII字符)的西装是0-9A-Ja-jq-zQ-Z,根据这个问题。
mbomb007'1

有50张卡,而不是52张。也许孩子丢了一些。
杰森

@ mbomb007我在问有5套卡片。除非我缺少任何东西,否则只有锹,棍棒,红心和钻石。那是
4。– TrojanByAccident

2
@TrojanByAccident这不使用卡。它使用ASCII而不是卡。这是ASCII的五种形式。除了改组卡片,我们将改组字母数字字符
mbomb007'1

Answers:


5

的JavaScript(ES6),192个 188 185字节

document.write('<pre id=o>')
s='ABCDEFGHIJQRSTUVWXYZ'
a=[0,...123456789+s.toLowerCase()+s]
setInterval('o.innerText=a.join``;a.splice(Math.random(s^=1)*48+1,0,s?a.pop():a.shift())',250)

编辑:由于@L.Serné,节省了4个字节。@Arnauld节省了3个字节。


我认为,如果e^=1Math.random调用的空括号内移动,则可以节省一些字节。您也可以将textContent更改为innerHTML,因为您没有传递任何特殊字符。您也可以etoLowerCase通话中将其设置为0 。
路加福音

你真的不需要e。您可以使用s。(因为('some_string'^1) === 1
Arnauld

4

MATL,62 58 56字节

@Luis节省了2个字节

4Y2'A':'J'tk'Q':'Z'tk&h`48YrQJh&)@o?l&)wb}wO&)]&htD3&XxT

此版本将无限期运行。在MATL Online上尝试在线演示,MATL Online是一个支持动态输出的实验性在线解释器。如果不首先杀死它,它将运行30秒(在线版本强加了一个硬限制)。

说明

4Y2     % Predefined literal for the characters '0'...'9'
'A':'J' % Create an array of the characters 'A'...'J'
tk      % Duplicate and make lowercase
'Q':'Z' % Create an array of the characters 'Q'...'Z'
tk      % Duplicate and make lowercase
&h      % Horizontally concatenate everything
`       % Do...while loop
  48YrQ % Determine a random integer between 2 and 49 
  Jh&)  % Split the string at the selected location
  @o?   % If this is an odd time through the loop
    l&) % Grab the first character
    wb  % Move it to the middle of the stack of three strings
  }     % Else...
    wO&)% Grab the last character and move it to the middle of the stack
  ]     % End of if statment
  &h    % Horizontally concatenate all strings on the stack
  tD    % Duplicate and display the current string
  3&Xx  % Pause for 0.3 seconds and clear the display
  T     % Push a literal TRUE to the stack to make this an infinite loop
        % Implicit end of while loop

4

Perl,117个字节

@F=(0..9,a..j,"q"..z,A..J,Q..Z);{print"\r",@F;splice@F,++$|+rand(@F-2),0,++$v%2?shift@F:pop@F;select$,,$,,$,,.3;redo}

要运行它:

perl -e '@F=(0..9,a..j,"q"..z,A..J,Q..Z);{print"\r",@F;splice@F,++$|+rand(@F-2),0,++$v%2?shift@F:pop@F;select$,,$,,$,,.3;redo}'

说明:
- @F=(0..9,a..j,"q"..z,A..J,Q..Z)创建初始卡座,并将其存储在中@F
- 永远{...;redo}执行...
- splice@F,++$|+rand(@F-2),0,++$v%2?shift@F:pop@F或者从卡座中删除第一个/最后一个元素,然后将其插入随机位置(在递增时$|,因此不会缓冲打印内容),
- print"\r",@F打印卡座,
- select$,,$,,$,,.3睡眠0.3秒(Perl的sleep睡眠时间不能少于1秒),


数字范围是0..9,不是1..9,您的初始牌组也处于
混乱状态

确实,@ ardnew,谢谢。编写此代码时,我一定很累。无论如何,它是固定的:)
Dada

4

Python 3中,199个 196 192 186字节

TuukkaX节省了4个字节,FlipTack节省了6个字节!

import time,random
s="abcdefghijqrstuvwxyz";s="0123456789"+s+s.upper()
f=0
while 1:print(end="\r"+s);time.sleep(.3);s,e=s[1^f:50-f],s[f and-1];i=random.randint(1,49);s=s[:i]+e+s[i:];f^=1

使用Python 3的print功能来抑制换行符,比Python 2的短sys.stdout.write

使用触发器变量在移动顶部和底部卡片之间切换。

取消高尔夫:

from random import randint
from time import sleep

string = "abcdefghijqrstuvwxyz"
string = "0123456789" + string + string.upper()
flip_flop = 0
while True:
    print("\r"+string,end="")
    sleep(0.3)
    string,char = string[not flip_flop:50-flip_flop], string[flip_flop and -1]
    position = randint(1,49)
    string = string[:position] + char + string[position:]
    f = not f

import random,time更短吗?
FlipTack

@FlipTack是的,短6个字节,谢谢!
busukxuan

@ mbomb007谢谢,完成了:-)
busukxuan

3

C,290个 285字节

#include<stdio.h>
#include<time.h>
#define S(s,a,b){int p=rand()%48+1;clock_t c=clock()+300;while(c>clock());int x=d[s];for(int i=s;i a p;)d[i b]=d[i];d[p]=x;printf(d);}
main(){char d[]="0123456789abcdefghijqrstuvwxyzABCDEFGHIJQRSTUVWXYZ\r";srand(time(0));for(;;){S(0,<,++)S(49,>,--)}}

取消高尔夫:

#include<stdio.h> // variadic(printf) functions without a valid prototype = UB
#include<time.h>  // required for the implementation-defined clock_t type
// note that <stdlib.h> isnt required for srand()/rand() because they are
//  validly declared implicitly
#define S(s,a,b) // macro function
{
    int p=rand()%48+1;     // get a random position within the array
    clock_t c=clock()+300; // get the time 300 milliseconds from now
    while(c>clock());      // wait for that time
    int x=d[s];            // save the s'th character in a tempvar
    for(int i=s;i a p;)    // shift everything in the array from p
        d[i b]=d[i];       // a determines the comparison: </>
                           // b determines the operation: ++/--
    d[p]=x;                // put the tempvar in its new position
    printf(d);             // print the modified string
} // note: the \r at the end makes it so the next printf overwrites it

main() // main entry point
{      // deck to shuffle below
    char d[]="0123456789abcdefghijqrstuvwxyzABCDEFGHIJQRSTUVWXYZ\r";
    srand(time(0)); // seed the random number generator
    for(;;)         // infinite loop
    {
        S(0,<,++)   // shuffle from the start of the deck
        S(49,>,--)  // shuffle from the end of the deck
    }
}

2

迅捷,288字节

import Darwin
var o=Array("0123456789abcdefghijqrstuvwxyzABCDEFGHIJQRSTUVWXYZ".characters)
while true{
print(String(o),terminator:"\r")
fflush(__stdoutp);{o.insert(o.removeLast(),at:$0())
o.insert(o.removeFirst(),at:$0()+1)}({Int(arc4random_uniform(UInt32(o.count-1)))})
usleep(300000)
}

在Swift中打高尔夫球一直是一个挑战,因为它的卖点之一就是表现力。


2

红宝石(138119字节)

f=0;a=[*0..9,*?a..?j,*?q..?z,*?A..?J,*?Q..?Z];loop{$><<a*''+?\r;a.insert(rand(48),f>0? a.shift : a.pop);f^=1;sleep 0.3}

不如@PaulPrestidge短,但至少我理解。


1

红宝石, 111 101个字符

s=[*0..9,*?a..?j,*?q..?z,*?A..?J,*?Q..?Z,?\r]*''
loop{$><<s;s[1+rand(48),0]=s.slice!$.^=-2;sleep 0.3}

无限循环。


1

Noodel,非竞争性41 个字节

"Q…Z"A…J"q…z"a…j"0…9⁵⁺ḷçṛ47⁺1ɱɲOṃḃɲ49ḅṙḍq

尝试一下:)

怎么运行的

"Q…Z"A…J"q…z"a…j"0…9⁵⁺                    # Creates the string literal to be shuffled.
                      ḷçṛ47⁺1ɱɲO      ṙḍq # The main "infinite" loop that applies the animation.
                                ṃḃɲ49ḅ    # Sub-loop that acts like an if-statement that only runs every odd iteration of the loop.

"Q…Z                                      # Pushes the string literal "QRSTUVWXYZ".
    "A…J                                  # Pushes the string literal "ABCDEFGHIJ".
        "q…z                              # Pushes the string literal "qrstuvwxyz".
            "a…j                          # Pushes the string literal "abcdefghij".
                "0…9                      # Pushes the string literal "0123456789".
                    ⁵⁺                    # Add the five strings on the stack to make one string.
                      ḷ                   # Unconditionally loop the following code.
                       ç                  # Copy what is on the top of the stack, clear the screen, and then print the copy.
                        ṛ47               # Create a random integer from 0 to 47.
                           ⁺1             # Increment the number to get 1 - 48 such that will not be the top or bottom of the deck.
                             ɱ            # Push the number of times that the unconditional loop has ran.
                              ɲO          # Consume the counter and push on zero if is even and one if is odd.
                                ṃ         # Conditional loop that only passes if the top of the stack is truthy (if the counter is odd).
                                 ḃ        # Throws away the top of the stack.
                                  ɲ49     # Pushes the literal 49 in order to represent the top of the deck.
                                     ḅ    # Ends the conditional loop.
                                      ṙ   # Relocate an element in the string by using the two numbers on the stack (either 0 or 49 to the random number).
                                       ḍq # Delay for a quarter of second. (End of unconditional loop)

<div id="noodel" code='"Q…Z"A…J"q…z"a…j"0…9⁵⁺ḷçṛ47⁺1ɱɲOṃḃɲ49ḅṙḍq' input="" cols="50" rows="2"></div>

<script src="https://tkellehe.github.io/noodel/release/noodel-0.0.js"></script>
<script src="https://tkellehe.github.io/noodel/ppcg.min.js"></script>


为什么不竞争?
Stewie Griffin

@StewieGriffin我直到挑战之后才完成js解析器的发布。所有功能都在此之前存在,但我不知道让Noodel参与竞争是否对我正确。因此,我采取了安全的路线:)
tkellehe

@ mbomb007,谢谢您解决此问题。我没有意识到它被放在最上面。
tkellehe

0

bash,170个字节

r(){((r=RANDOM%48+1));echo -n $c^;sleep .3;}
c=0123456789abcdefghijqrstuvwxyzABCDEFGHIJQRSTUVWXYZ
while r
do
c=${c:1:r}${c:0:1}${c:r+1}
r
c=${c:0:r}${c:49}${c:r:-1}
done

这里的“ ^”(第一行)表示ctrl-mctrl-v enter根据您的编辑器的工作方式(假设您的编辑器工作),在命令行中作为编辑器或在编辑器中输入

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.