14
\$\begingroup\$

Challenge:

Read input (within visible ASCII range) and output with a few modifications:

  1. In each set of 10 characters of the input randomly (50/50):
    • replace one character* (with a random** one within visible ASCII range) (ex. lumberjack becomes lumbeZjack)
    • or remove one character (ex. lumberjack becomes lmberjack)

* If the set is of less than 10 characters, you don't have to modify it, but you can.
** The character can be the same as the one input, as long as it's still random.

Example:

Input: Go home cat! You're drunk!
Output: Go hom cat! YouLre drunk!
(just a example, since output could be random, don't use as a test case)

Rules:

\$\endgroup\$
9
  • 5
    \$\begingroup\$ @n4melyh4xor also you could post future challenges on the Sandbox. There you will get some feedback about your challenge. \$\endgroup\$
    – Rod
    Dec 16, 2016 at 16:57
  • 1
    \$\begingroup\$ What if the replacement char is the same as the char to be replaced? \$\endgroup\$ Dec 16, 2016 at 17:16
  • \$\begingroup\$ @BradGilbertb2gills, I'm not that picky, it's fine. \$\endgroup\$ Dec 16, 2016 at 17:21
  • \$\begingroup\$ Closely related \$\endgroup\$ Dec 16, 2016 at 17:27
  • 1
    \$\begingroup\$ Does it have to be uniformly random, or just each character just need a non-zero chance of appearing? \$\endgroup\$
    – FlipTack
    Dec 31, 2016 at 12:44

14 Answers 14

4
\$\begingroup\$

Pyth, 27 25 bytes

VczTpXNOT?<JOr\ \ÞKC127JK

Test suite available here.

Thanks to Maltysen for shaving off 2 bytes.

Explanation

VczTpXNOT?<JOr\ \ÞKC127JK    z autoinitalizes to input, T autoinitializes to 10 
 czT                         chop input into strings of length 10, returned as list of strings
V                            for each string N in this list:
            Or\ \Þ            randomly pick a char between ' ' (32) and 'Þ' (222)
           J                  and assign it to variable J
                  KC127       assign the DEL char to variable K
         ?<J      K           if J < K:
     XNOT              J       replace a random character in N with J
         ?<J      K           else:
     XNOT               K      replace a random character in N with K
    p                         print this string with no trailing newline

As is often the case, I feel that this is a bit of a naive method and may be improved upon. Usually I find something obvious while typing up the explanation but nothing jumped out at me this time.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Nice use of the delete char! One tip is that range operates on string too \$\endgroup\$
    – Maltysen
    Dec 17, 2016 at 0:44
  • \$\begingroup\$ @Maltysen Thanks! I have thought about your comment but I am not entirely sure where I could use it to save space. OC\¾ is the same number of bytes as O190, and assigning K to the delete char doesn't save any space either. \$\endgroup\$ Dec 19, 2016 at 14:53
  • \$\begingroup\$ @Maltysen So it took me a little bit but I realized what you meant and I've shaved off 2 bytes. Thanks again! \$\endgroup\$ Dec 20, 2016 at 15:29
3
\$\begingroup\$

*><>, 44 46 52 50 bytes

rl5(?voooo/!|Ou+1Ox:@=?~o~oooo!
ol5(?v" ":/
o;!?l<

Try it here!

This uses any ascii character near/above space for the random characters. This always edits the 6th character, unless it's the end of a string and that string's length isn't a multiple of 10. This has a 50% chance to remove the 7th character instead of editing the 6th.

Input

The IEEE Standard for Floating-Point Arithmetic (IEEE 754) is a technical standard for floating-point computation established in 1985 by the Institute of Electrical and Electronics Engineers (IEEE). The standard addressed many problems found in the diverse floating point implementations that made them difficult to use reliably and portably. Many hardware floating point units now use the IEEE 754 standard.

Output

The IEE Standardfor Float$ng-Point Aithmetic (EEE 754) i a technicl standar! for floa!ing-point!computati#n establised in 1985by the Insitute of !lectrical#and Electrnics Engi!eers (IEE%). The st!ndard add!essed man! problems#found in !he divers! floating oint impl!mentation" that mad# them dif!icult to ue reliabl# and port!bly. Many!hardware foating po#nt units %ow use th! IEEE 754"standard.

Edit: This answer probably isn't always in the visible ascii range, editing... Fixed.

Edit2: Didn't see there needs to be a 50/50 chance to remove a character, editing again... I believe everything is in order now :).

\$\endgroup\$
2
  • \$\begingroup\$ One more thing, someone suggested a random character so it's random now. \$\endgroup\$ Dec 16, 2016 at 17:13
  • \$\begingroup\$ @n4melyh4xor, that was me! It is random :). \$\endgroup\$ Dec 16, 2016 at 17:16
2
\$\begingroup\$

Perl 6,  78  67 bytes

{[~] map {~S/.**{(^.chars).pick}<(./{(' '..'~').pick x Bool.pick}/},.comb(10)}
{[~] .comb(10)».&{~S/.**{10.rand}<(./{(' '..'~').pick x 2.rand}/}}

Try it

Explanation:

{
  [~]                  # reduce with string concatenation operator

  .comb(10)\           # take input and break it into chunks of up-to 10 chars

  ».\                 # on each of them call the following

  &{
    ~                  # Stringify the following

    S/                 # substituted
      .                # any char
        **             # repeated
           { 10.rand } # a random number of times

      <(               # ignore all of that

      .                # the char to be removed/replaced
    /{

      ( ' ' .. '~' ).pick  # choose a character
      x                    # string repeated
      2.rand               # zero or one times

    }/

  }
}
\$\endgroup\$
0
2
\$\begingroup\$

Pyth - 21 bytes

smO,XdOTOr;\~.DdOTcQT

Try it online here.

\$\endgroup\$
2
\$\begingroup\$

Python 3, 75 bytes

The 75-byte applies the transformation to the first character of each group, and only picks from 2 random characters, such as in the Jelly answer (which OP allowed):

from random import*
f=lambda s:s and choice(['','a','b'])+s[1:10]+f(s[10:])

Try it online!

This is a recursive function which, every iteration, prepends either nothing, 'a', or 'b', and then calls itself with the first 10 characters sliced off. The final iteration short circuits at s and (an empty string is falsy), avoiding infinite recursion.

The result of all the separate calls are then concatenated, and returned to the context which called the function.

120 bytes

Of course, that feels a bit like cheating, so here's one which is completely random:

from random import*;r=randint
def f(S):a=S[:10];R=r(0,len(a)-1);print(end=a[:R]+chr(r(32,126))*r(0,1)+a[R+1:]);f(S[10:])

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Where did OP mention that the index of the replaced character can be 0? \$\endgroup\$
    – Yytsi
    Dec 31, 2016 at 20:12
  • \$\begingroup\$ @TuukkaX the comments have been removed to avoid clutter, but that's what the jelly answer does, and OP said it was fine. \$\endgroup\$
    – FlipTack
    Dec 31, 2016 at 20:14
2
\$\begingroup\$

Vyxal, 13 bytes

₀ẇ⟑ẏ℅kP¤"℅℅Ȧ₴

Try it Online!

₀ẇ            # Chunks of length 10...
  ⟑           # Over each...
           Ȧ  # Replace at ...
   ẏ℅         # random index in the input
          ℅   # A random item of...
         ℅    # A random item of...
     kP       # Printable ascii
       ¤"     # Or the empty string
            ₴ #
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Looks like this gets applied once to the input as a whole when it should be applied to each grouping of 10 characters in the input. \$\endgroup\$
    – Shaggy
    Oct 31, 2022 at 22:44
  • \$\begingroup\$ @Shaggy Fixed.. \$\endgroup\$
    – emanresu A
    Nov 1, 2022 at 5:56
  • \$\begingroup\$ It also looks like it only has a 1/96 chance to remove a character when it should be a 1/2 chance. \$\endgroup\$
    – Shaggy
    Nov 1, 2022 at 16:19
  • \$\begingroup\$ @Shaggy Fixed.. \$\endgroup\$
    – emanresu A
    Nov 1, 2022 at 18:47
1
\$\begingroup\$

Clojure, 135 139 bytes

Edit: Forgot to use partition-all instead of partition.

(fn[i](apply str(flatten(map #(let[r rand-int [b e](split-at(r 9)%)][b(if(<(rand)0.5)""(char(+(r 25)97)))(rest e)])(partition-all 10 i)))))

Ungolfed:

(def f (fn[i]
         (->> i
              (partition-all 10)
              (map #(let [[begin end] (split-at (rand-int 9) %)]
                      [begin (if (< 0.5 (rand)) "" (char (+(rand-int 25)97))) (rest end)]))
              flatten
              (apply str))))

Man those function names are long... Anyway, it splits input into partitions of 10 characters, splits them at random point into two halves, randomly injects an empty string or a random character between them and discards the first character of the 2nd half.

\$\endgroup\$
1
\$\begingroup\$

Mathematica 133 Bytes (129 characters)

StringReplacePart[#,Table[If[(r=RandomInteger)[]<1,"",FromCharacterCode@r@128],c=⌊StringLength@#/10⌋],Array[{g=10#-9+r@9,g}&,c]]&

76 characters to write the names of 8 functions :/

Using the ⌊..⌋ instead of Floor[] saves 5 characters, 1 byte.

\$\endgroup\$
1
\$\begingroup\$

Python 3, 129 bytes

def f(s):f=id(s)%9+1;print(''.join(j[0:f-1]+chr(33+id(s)%94)*(id(s)//10%2)+j[f:]for j in [s[i:i+10]for i in range(0,len(s),10)]))

In order to save some bytes, instead of importing Python's random module, I just did some modulo operations on the id of the string, which should be different every time. For example the program will decide whether or not to remove a char or replace one based on whether or not id(string)//10 is even (I integer divide by 10 as the last digit will always be even).

\$\endgroup\$
1
\$\begingroup\$

Jelly, 15 14  13 bytes 13 characters

2X
s⁵µ¢1¦ṫ¢µ€

TryItOnline!

Replaces or removes the first of every ten characters including that of the last 1-9 if there is such a chunk. Chooses from the, admittedly small, subset of characters: 1; 2.

How?

2X         - Link 1, flip a coin: no arguments
 X         - random choice from
2          - 2 (treated as the integers [1,2])

s⁵µ¢1¦ṫ¢µ€ - Main link: string of printable ASCII
s⁵         - split (s) into chunks of size ten (⁵)
  µ     µ  - monadic chain separation 
         € - for each chunk
   ¢       -     last link as a nilad
    1¦     -     apply to index 1 (replace 1st of the 10 char chunk with the chosen integer)
       ¢   -     last link as a nilad
      ṫ    - tail - if it was 1 this has no effect (50%)
                  - if it was 2 this discards the replaced character (50%)
           - implicit print

To choose from all of printable ASCII rather than just 1 and 2 (still replacing or removing the 1st character of each chunk) in 21 bytes:

s⁵µ32r126¤ỌX¤1¦ṫ2X¤µ€

For a fully random version (50/50 remove/replace, uniform random printable ASCII, and a uniformly random character location within each chunk) I have 30 bytes (probably non-optimal):

95R+31ỌX;

s⁵µṙ⁵X¤©Ṗ2X¤Ŀṙ®C¤µ€

This rotates each chunk left by a random amount, pops the last character off and then calls a random one of the first two links, one of which is empty and the other which concatenates with a random printable ASCII character; it then rotates the chunk right again.

\$\endgroup\$
10
  • \$\begingroup\$ This really goes against the spirit of the post \$\endgroup\$
    – Maltysen
    Dec 17, 2016 at 0:43
  • \$\begingroup\$ @Maltysen - OP said all these things are fine. \$\endgroup\$ Dec 17, 2016 at 0:54
  • \$\begingroup\$ Where? I don't see anything in the comments or OP. \$\endgroup\$
    – Maltysen
    Dec 17, 2016 at 0:56
  • \$\begingroup\$ @Maltysen they have been cleared. \$\endgroup\$ Dec 17, 2016 at 0:57
  • 1
    \$\begingroup\$ There was a comment explicitly saying we could choose from 2 characters AND that they did not want to put it into the Question body. \$\endgroup\$ Dec 17, 2016 at 0:59
1
\$\begingroup\$

Python3, 188 186 184 114 characters

from random import*
s=input()
for c in[s[i:i+10]for i in range(0,len(s),10)]:print(end=choice(["","x","y"])+c[1:])

Seems too long. Could probably be shortened a lot with a lambda.

Apparently the OP has allowed choosing the random character from a list of two characters and the index of the character to be replaced can be a constant. After the modifications, my answer would've looked exactly the same as @FlipTacks Python submission, so this is the form I'm staying with.

@FlipTack saved 5 bytes!

\$\endgroup\$
0
1
\$\begingroup\$

Ruby -pl, 52 bytes

gsub(/.{10}/){_1[rand 0..9]=[*" "..?~,""].sample
_1}

Attempt This Online!

\$\endgroup\$
1
\$\begingroup\$

Pip, 46 bytes

Fi(a<>t){IRR2YiRARR#iRCPAEL{YiRARR#ix}b:bAEy}b

Try It Online!

How?

Fi(a<>t){IRR2YiRARR#iRCPAEL{YiRARR#ix}b:bAEy}b
Fi      {                                   }  - For each in
      t                                        - Var for 10
   a<>t                                        - Split input into 10 pieces
         I                                     - If truthy
          RR2                                  - 0 or 1
             Y                                   - Yank (equivalent to (y:a)):
               RA                                 - Replace item in at index
              i                                   - First input
                 RR#i                             - Random int from 0 to length of input
                     RCPA                         - Random Printable ASCII character
                         EL{         }         - Else
                            Y                    - Yank:
                              RA                  - Replace item in at index
                             i                    - First input
                                RR#i              - Random int from 0 to length of input
                                    x             - Empty string
                                      b:       - Assign b to
                                        bAEy   - Append y to b
                                             b - Output b
\$\endgroup\$
1
\$\begingroup\$

05AB1E, 13 bytes

TôεžQΩæΩ9ÝΩǝ?

Also (potentially) modifies the last part if it's less than 10 characters.

Try it online or see 10 example outputs at once.

Explanation:

Tô             # Split the (implicit) input-string into parts of size 10
               # (where the trailing part is potentially smaller)
  ε            # Foreach over the parts:
   žQ          #  Push the printable ASCII string constant
     Ω         #  Pop and push a random character from it
      æ        #  Get the powerset of this character, resulting in a pair of an empty
               #  string "" and the character
       Ω       #  Pop and push a random item from this pair
        9Ý     #  Push list [0,1,2,3,4,5,6,7,8,9]
          Ω    #  Pop and push a random digit from this list
           ǝ   #  Replace the character at that (0-based) index with the chosen ASCII
               #  character or the empty string
               #  (if the index is ≥ the length, it does nothing)
            ?  #  Pop and print the modified part
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.