This cat has bugs? Really?

11

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:

n4melyh4xor

Posted 2016-12-16T16:37:15.600

Reputation: 507

5

@n4melyh4xor also you could post future challenges on the Sandbox. There you will get some feedback about your challenge.

– Rod – 2016-12-16T16:57:58.273

1What if the replacement char is the same as the char to be replaced? – Brad Gilbert b2gills – 2016-12-16T17:16:30.997

@BradGilbertb2gills, I'm not that picky, it's fine. – n4melyh4xor – 2016-12-16T17:21:14.850

Closely related – AdmBorkBork – 2016-12-16T17:27:26.137

Does the index of the character to be replaced have to be random? – Yytsi – 2016-12-30T22:43:56.507

Does it have to be uniformly random, or just each character just need a non-zero chance of appearing? – FlipTack – 2016-12-31T12:44:19.537

Is the input guaranteed to be at least 10 characters long? – FlipTack – 2016-12-31T13:45:01.993

@FlipTack "If the set is of less than 10 characters, you don't have to modify it, but you can." I would interpret that as a no. – Yytsi – 2016-12-31T20:09:52.147

Answers

4

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.

Mike Bufardeci

Posted 2016-12-16T16:37:15.600

Reputation: 1 680

1Nice use of the delete char! One tip is that range operates on string too – Maltysen – 2016-12-17T00:44:51.797

@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. – Mike Bufardeci – 2016-12-19T14:53:46.487

@Maltysen So it took me a little bit but I realized what you meant and I've shaved off 2 bytes. Thanks again! – Mike Bufardeci – 2016-12-20T15:29:26.447

3

*><>, 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 :).

redstarcoder

Posted 2016-12-16T16:37:15.600

Reputation: 1 771

One more thing, someone suggested a random character so it's random now. – n4melyh4xor – 2016-12-16T17:13:24.713

@n4melyh4xor, that was me! It is random :). – redstarcoder – 2016-12-16T17:16:09.050

2

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

    }/

  }
}

Brad Gilbert b2gills

Posted 2016-12-16T16:37:15.600

Reputation: 12 713

2

Pyth - 21 bytes

smO,XdOTOr;\~.DdOTcQT

Try it online here.

Maltysen

Posted 2016-12-16T16:37:15.600

Reputation: 25 023

2

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!

FlipTack

Posted 2016-12-16T16:37:15.600

Reputation: 13 242

Where did OP mention that the index of the replaced character can be 0? – Yytsi – 2016-12-31T20:12:41.860

@TuukkaX the comments have been removed to avoid clutter, but that's what the jelly answer does, and OP said it was fine.

– FlipTack – 2016-12-31T20:14:14.100

1

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.

Jonathan Allan

Posted 2016-12-16T16:37:15.600

Reputation: 67 804

This really goes against the spirit of the post – Maltysen – 2016-12-17T00:43:10.387

@Maltysen - OP said all these things are fine. – Jonathan Allan – 2016-12-17T00:54:23.087

Where? I don't see anything in the comments or OP. – Maltysen – 2016-12-17T00:56:25.280

@Maltysen they have been cleared. – Jonathan Allan – 2016-12-17T00:57:27.323

ok then, sorry about that. Can you make a trivial edit so I can undownvote you? – Maltysen – 2016-12-17T00:59:09.743

1There was a comment explicitly saying we could choose from 2 characters AND that they did not want to put it into the Question body. – Jonathan Allan – 2016-12-17T00:59:11.550

@Maltysen FWIW I have added a fully random version (although I'm sure it could be shorter!) – Jonathan Allan – 2016-12-17T01:30:27.557

OP has stated that the least amount of characters wins. Jelly doesn't get affected by this though, unless it has multibyte characters. – Yytsi – 2016-12-30T23:03:09.417

@TuukkaX yeah, each byte is mapped to a character (one of the byte instructions has two possible characters, either a line feed or a pilcrow - which also has no effect); many of them would require more than one byte to represent with unicode (but it does not specify a byte count of the characters in a specific encoding). – Jonathan Allan – 2016-12-31T02:16:11.990

...or each character maps to a byte – Jonathan Allan – 2016-12-31T02:16:49.020

1

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!

Yytsi

Posted 2016-12-16T16:37:15.600

Reputation: 3 582

0

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.

NikoNyrh

Posted 2016-12-16T16:37:15.600

Reputation: 2 361

0

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.

Kelly Lowder

Posted 2016-12-16T16:37:15.600

Reputation: 3 225

0

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).

Cormac

Posted 2016-12-16T16:37:15.600

Reputation: 101