무작위로 단어 철자


16

이 CR 질문에서 영감을 얻었습니다 (CR 찾아보기로 나를 죽이지 마십시오)

투기

단어의 철자가 틀릴 확률은 다음과 같습니다.

  • 시간의 1/3은 출력을 변경하지 않습니다
  • 시간의 1/3은 임의의 문자를 제거합니다
  • 시간의 1/3은 임의의 문자를 복제

입력에서 주어진 문자를 제거 / 복제 할 가능성은 모든 문자에 대해 동일해야합니다.

연속 된 두 문자가 동일하거나 대소 문자를 구분하는 경우 두 문자 중 하나가 수정 될 확률은 문자 하나와 같아야합니다. IE 용 출력 AA(아르 AA하거나 A또는 AAA) 모두 동일한 확률을 가져야한다.


입력은 단순성을 위해 문자 만 포함합니다.

첫 번째 줄이 입력되고 다음 줄은 모두 철자가 틀릴 수 있습니다. 각 줄은 동일한 출력 확률을 가져야하며, 예제에서는 입력이 제외되지만 여전히 1/3의 출력 확률을 가져야합니다.

foo

fo
oo
ffoo
fooo
PPCG

PPC
PPG
PCG
PPPCG
PPCCG
PPCGG

좋은 도전이지만 각 줄의 출력 확률이 같더라도 단어가 동일하게 유지 될 확률이 1/3이 아니라는 것을 의미하지 않습니까?
밸류 잉크

@ValueInk 괜찮습니다. 예제의 결함입니다. 수정하겠습니다
Downgoat

3
각 줄의 출력 확률이 같으면 문자 분포가 균일하지 않습니까? 마찬가지로 foo: 문자를 제거하면 (-f) oo, f (-o) o 및 fo (-o)가 될 수 있습니다. 따라서 fo확률은의 두 배 여야 oo하지만 모든 행의 확률은 동일합니다.
Deusovi

2
@ Deusovi이 단락을 설명하는 전체 단락이 있습니다. 여기에 대비하여 여기에 복사하겠습니다If two consecutive characters are the same (case-sensitive), the probability of one of them being modified should be the same as if they are one character. I.e. the outputs for AA (which are AA or A or AAA) should all have the same probability.
edc65

1
@DJMcMayhem 아니오, 둘 이상의 문자는 일반적으로 미스 펠트입니다.
Downgoat

답변:


5

, 38 27 바이트

a@:`(.)\1*`YRR#aa@y@0X:RR3a

이것은 재미있었습니다 .Pip의 정규 표현식과 변경 가능한 문자열 기능을 사용했습니다. 명령 줄 인수를 통해 입력을받습니다.

설명:

a@:`(.)\1*`                  Split a into runs of identical chars using regex match
           YRR#a             Yank randrange(len(a)) into y (randomly choosing one such run)
                a@y@0        Take the first character of that run
                     X:RR3   Modify in place, string-multiplying by randrange(3):
                               If RR3 is 0, character is deleted
                               If RR3 is 1, no change
                               If RR3 is 2, character is duplicated
                          a  Output the modified a

온라인으로 사용해보십시오!


1
와우 만 ASCII lang을 골프 간결한 필요에 축하
Downgoat

3

루비, 64 55 + 1 ( p플래그) = 56 바이트

입력은 줄 바꿈없이 STDIN 라인입니다.

a=[]
gsub(/(.)\1*/){a<<$&}
a.sample[-1]*=rand 3
$_=a*''

2

CJam (21 바이트)

re`_,mr_2$=3mr(a.+te~

온라인 데모

해부

r     e# Read a line of input from stdin
e`    e# Run-length encode it
_,mr  e# Select a random index in the RLE array
_     e# Hang on to a copy of that index
2$=   e# Copy the [run-length char] pair from that index
3mr(  e# Select a uniformly random integer from {-1, 0, 1}
a.+   e# Add it to the run-length
t     e# Replace the pair at that index
e~    e# Run-length decode

2

자바 스크립트 (ES6), 107

w=>(r=x=>Math.random()*x|0,a=w.match(/(.)\1*/g),a[p=r(a.length)]=[b=a[p],b+b[0],b.slice(1)][r(3)],a.join``)

덜 골프

w=>(
  a = w.match(/(.)\1*/g),
  r = x => Math.random()*x | 0,
  p = r(a.length),
  b = a[p],
  a[p] = [b, b+b[0], b.slice(1)][r(3)],
  a.join``
)

테스트

f=w=>(r=x=>Math.random()*x|0,a=w.match(/(.)\1*/g),a[p=r(a.length)]=[b=a[p],b+b[0],b.slice(1)][r(3)],a.join``)

function update() { 
  O.innerHTML = Array(99)
  .fill(I.value)
  .map(x=>(
    r=f(x),
    r==x?r:r.length<x.length?'<b>'+r+'</b>':'<i>'+r+'</i>'
  
    )
  ).join` `
}

update()
#O { width:90%; overflow: auto; white-space: pre-wrap}
<input id=I oninput='update()' value='trolley'><pre id=O></pre>


2

자바 7, 189 180 178 바이트

import java.util.*;String c(String i){Random r=new Random();int x=r.nextInt(2),j=r.nextInt(i.length());return x<1?i:i.substring(0,j-(x%2^1))+(x<2?i.charAt(j):"")+i.substring(j);}

언 골프 및 테스트 사례 :

여기에서 시도하십시오.

import java.util.*;
class M{
  static String c(String i){
    Random r = new Random();
    int x = r.nextInt(2),
        j = r.nextInt(i.length());
    return x < 1
            ? i
            : i.substring(0, j - (x%2 ^ 1)) + (x<2?i.charAt(j):"") + i.substring(j);
  }

  public static void main(String[] a){
    for(int i = 0; i < 5; i++){
      System.out.println(c("foo"));
    }
    System.out.println();
    for(int i = 0; i < 5; i++){
      System.out.println(c("PPCG"));
    }
  }
}

가능한 출력 :

foo
fooo
foo
foo
ffoo

PPCCG
PPCG
PPCCG
PPPCG
PPCG


1

Pyth-17 바이트

이것은 실제로 연속 문자가있는 특수 사례를 올바르게 처리합니다.

 XZOKrz8Or_1 2r9K

테스트 스위트 .


이것은 16 바이트입니까? 선행 공간이 정확합니까? 그렇지 않은 경우 15 바이트입니까?
Downgoat

@ Downgoat 아니오, 선행 공간이 정확합니다. 나는 그것의 17 바이트 확신합니다.
Maltysen

1

APL, 21

{⍵/⍨3|1+(?3)×(⍳=?)⍴⍵}

이것은 임의의 위치에 1을 가진 0으로 구성된 벡터를 만드는 것으로 시작합니다. 그런 다음이 값에 1과 3 사이의 난수를 곱합니다. +1과 mod 3은 모든 1과 1, 0 또는 1 또는 2에 위치한 임의의 벡터를 얻습니다.

마지막으로, ⍵ / ⍨는 각 문자는 n 번 쓰여 져야한다고 말하는데, 여기서 n은 벡터의 숫자입니다.

tryapl.org에서 사용해보십시오


0

파이썬 2, 123 바이트

from random import*
R=randint
s=input()
m=n=R(0,len(s)-1)
c=R(0,2)
m=[m,1+[-len(s),m][m>0]][c==1]
n+=c==2
print s[:m]+s[n:]

0

자바 스크립트 (ES6), 103

w=>{w=w.split(''),r=Math.random,x=r(),i=r()*w.length|0;w.splice(i,x<.6,x>.3?w[i]:'');alert(w.join(''))}

0

APL, 27 바이트

{⍵/⍨(?3)⌷1⍪2 0∘.|1+(⍳=?)⍴⍵}

설명:

                        ⍴⍵  ⍝ length of ⍵
                   (⍳=?)    ⍝ bit vector with one random bit on 
                 1+         ⍝ add one to each value (duplicate one character)
           2 0∘.|           ⍝ mod by 2 and 0 (remove instead of duplicate)
         1⍪                 ⍝ add a line of just ones (do nothing)
    (?3)⌷                   ⍝ select a random line from that matrix
 ⍵/⍨                        ⍝ replicate characters in ⍵

테스트:

      {⍵/⍨(?3)⌷1⍪2 0∘.|1+(⍳=?)⍴⍵} ¨ 10/⊂'foo'
 fooo  foo  oo  foo  fo  fo  oo  fo  oo  fooo 
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.