deRpiFy tHe sTriNg!

8

1

yOu wiLl bE giVen A sTriNg wHich cOnsiSts oF pRintAble asCii cHarActErs.

yOu iTeraTe tHrougH thE sTrIng tHen chAnge rAndOM(uNifoRm, 50% cHanCe uPPercAse) lEtteRs to uPPercAse aNd eVerytHing elSe tO lOwercAse.

tHat'S iT.

(sorry for the punctuation, it was for the concept of the question)

Readable version:

You will be given a string which consists of printable ASCII characters.

You iterate through the string and change random(uniform, 50% chance uppercase) letters to uppercase, and everything else to lowercase.

that's it.

exaMplEs

iNpuT => pOssiBle oUtPUt
Programming puzzles and Code Golf => pRogRaMMiNg pUzzlEs aNd coDe goLf
dErpity deRp derP => deRpiTy dErp DerP
CAAAPSLOOOCK => cAAapslOoocK
_#$^&^&* => _#$^&^&*

Matthew Roh

Posted 2017-04-07T15:58:52.987

Reputation: 5 043

Question was closed 2017-04-08T02:05:11.417

2What does "randomly" mean exactly? Can there be two consecutive capital letters (your test cases don't have any such configurations)? I'd say this question is underspecified in its current state, but I'm not going to vote on it yet. Please specify these two things. – HyperNeutrino – 2017-04-07T16:02:20.170

Is time%2 allowed for pseudorandomness? – fəˈnɛtɪk – 2017-04-07T16:06:51.787

@fəˈnɛtɪk Sure (if it is uniform) – Matthew Roh – 2017-04-07T16:08:21.383

Does the code have to lowercase it first, if doing so has zero effect because of how the following code works? – Brad Gilbert b2gills – 2017-04-07T16:12:46.483

@BradGilbertb2gills it has to. – Matthew Roh – 2017-04-07T16:15:21.273

@SIGSEGV Don't contradict yourself. If you pass an all-uppercase string as input, you should still come out with a string that's derpified, right? Make that an example. – mbomb007 – 2017-04-07T16:18:22.627

@mbomb007 added. – Matthew Roh – 2017-04-07T16:20:46.753

2you say printable ASCII, but your test cases only include alphabetic characters. Should the program be able to deal with non-alphabetic characters, or can we expect the input to be purely alphabetic? – MildlyMilquetoast – 2017-04-07T16:59:09.547

5I think most of the existing answers also assume that the decision is independent for each letter, but that's nowhere in the question. At present I think it would technically be compatible with the spec to write something along the lines of (pseudocode) s=>rand()%2?s.upper():s.lower() – Peter Taylor – 2017-04-07T21:02:10.200

@PeterTaylor I disagree with that. The challenge explicitly states to iterate through the string and change random letters, uniformly, to either uppercase or lowercase. Your pseudocode doesn't do that. – AdmBorkBork – 2017-04-10T13:01:54.070

@AdmBorkBork, I don't think the sentence which mentions randomness actually makes sense, so while I agree that your interpretation is the most likely to be the intended one I think that mine is also possible. – Peter Taylor – 2017-04-10T13:24:40.920

Answers

10

C – 65 bytes

Pretty good for a mainstream language!

main(c){while((c=getchar())>0)putchar(isalpha(c)?c^rand()&32:c);}

Uses XOR to randomly flip the bit at 0x20 for each alphabetic character. Program assumes ASCII character set and that EOF < 0.

Sample run on its own source!

$ main < main.c
MaIN(c){WhILe((C=GETChAr())>0)pUtCHaR(iSALpha(C)?C^rANd()&32:C);}

Very derpy.

user15259

Posted 2017-04-07T15:58:52.987

Reputation:

1"Pretty good for a mainstream language!" - C is often surprisingly good for golfing. – Steadybox – 2017-04-08T22:03:11.510

7

JavaScript, 87 bytes

function(s){return s.replace(/./g,c=>Math.random()<.5?c.toLowerCase():c.toUpperCase())}

68 bytes in ES6:

f=
s=>s.replace(/./g,c=>c[`to${Math.random()<.5?`Low`:`Upp`}erCase`]())
<input oninput=o.textContent=f(this.value)><pre id=o>

Neil

Posted 2017-04-07T15:58:52.987

Reputation: 95 035

the second code does work but the first one: Uncaught SyntaxError: Unexpected token ( – Felo Vilches – 2017-04-08T02:20:51.167

@FeloVilches You have to use it in an expression context for it to work, otherwise the engine tries to parse it as a function statement. – Neil – 2017-04-08T09:43:55.097

Can be shortened using ES6. – CalculatorFeline – 2017-07-10T18:00:25.003

@CalculatorFeline To 68 bytes, no doubt? – Neil – 2017-07-10T18:34:11.217

5

Jelly, 6 bytes

,ŒsXµ€

Try it online!

How?

Lower casing all chars of the input and then uppercasing each with 50% probability is the same as choosing one of the original char and the swapped-case char with equal probability...

,ŒsXµ€ - Main link: string s
    µ  - monadic chain separation
     € - for each char c in s
,      -     pair c with
 Œs    -     swapped case of c
   X   -     choose a random item from the list (of the two chars)
       - implicit print

Jonathan Allan

Posted 2017-04-07T15:58:52.987

Reputation: 67 804

Ohh. Clever interpretation of the challenge. – Matthew Roh – 2017-04-07T16:22:10.537

By the way: is this uniform? – Matthew Roh – 2017-04-07T16:25:09.047

@SIGSEGV If by "uniform" you mean each char has a 50% chance of being each case then yes - X is implemented using Python's random.choice, so when presented with a list of 2 chars will pick each with 50% probability - every char in the input produces such a list (non-alphabetic chars will be a list of 2 equal chars, but that does not matter if the interpretation of "uniform" above is correct). – Jonathan Allan – 2017-04-07T16:34:01.833

Here is an example of line feed separated lists from which one char of each would be chosen. – Jonathan Allan – 2017-04-07T16:38:01.733

5

PowerShell, 64 60 bytes

-join([char[]]"$args".ToLower()|%{"$_".ToUpper(),$_|random})

Try it online! (make sure that "disable output cache" is checked if you want random results)

Exact translation of the challenge. Takes the input string, ToLower()s it, converts it to a char array, loops through each character |%{...}, and randomly selects between either the existing character or the uppercase variant. Then -joins them all back together into a single string. This works because ToUpper and ToLower only affect alphabetical characters, leaving punctuation and the like unchanged.

(Dennis fixed the alias list on TIO so that Random isn't trying Linux random but correctly aliases to Get-Random as a PowerShell command, as it should. Thanks, Dennis!)

AdmBorkBork

Posted 2017-04-07T15:58:52.987

Reputation: 41 581

4

MATL, 13 12 bytes

"@ktXkhlZr&h

Try it at MATL Online

Explanation

        % Implicitly grab input as a string
"       % For each character...
  k     % Convert to lowercase
  tXk   % Make a copy and convert to uppercase
  h     % Horizontally concatenate these two characters
  lZr   % Randomly select one of them
  &h    % Horizontal concatenate the entire stack
        % Implicit end of for loop and implicit display

Suever

Posted 2017-04-07T15:58:52.987

Reputation: 10 257

4

PHP, 53 Bytes

for(;a&$c=$argn[$i++];)echo(lu[rand()&1].cfirst)($c);

Jörg Hülsermann

Posted 2017-04-07T15:58:52.987

Reputation: 13 026

1Nice trick w/ the strto.*.er. :) – Alex Howansky – 2017-04-07T18:46:23.800

1Save a byte w/ rand()&1 – Alex Howansky – 2017-04-07T18:46:42.343

1Save a bunch of bytes by using foreach(str_split($argv[1]as$c)... instead of array indexing. – Alex Howansky – 2017-04-07T18:48:45.563

4

Japt, 10 bytes

®m"uv"gMq2

Try it online!

Explanation

So this is kind of a cheesy hack, but it works. In JavaScript you can do something like

x[`to${Math.random()<.5?"Upp":"Low"}erCase`]()

to randomly convert x to upper- or lowercase. In Japt, the equivalent functions are u for toUpperCase and v for toLowerCase. But in Japt there is no direct way to get a calculated property value (x[expression] in JavaScript).

One of my favorite features of Japt is that if you have a function which is composed of a single method call (e.g. mX{Xq}, or .m(X=>X.q()) in JS), you can leave out everything except the name of the method, e.g. mq. The compiler then turns this into a string which gets passed to the original method call (.m("q")), and the method turns this back into a function. So there's no difference between mq and m"q"; both produce the same output.

Now, where I was going with this: while we can't directly call a random method on a string, we can call m on that string with a random method name. So, for the explanation:

®m"uv"gMq2
®           // Replace each char in the input by this function:
 m          //   Replace each char in this char by this function:
      g     //     the char at index
       Mq2  //       random integer in [0,2)
  "uv"      //     in "uv".
            //   This randomly calls either .u() or .v() on the char.
            // Implicit: output result of last expression

ETHproductions

Posted 2017-04-07T15:58:52.987

Reputation: 47 880

Doesn't Mq default to 2? Or was that recently added? – Oliver – 2017-07-10T18:00:57.673

@obarakon I think that was more recent. A quick search on GitHub says it was May 5 – ETHproductions – 2017-07-10T18:49:55.707

2

Perl 5, 26 bytes

25 bytes + -p flag.

s/./rand 2&1?uc$&:lc$&/ge

Try it online!

Dada

Posted 2017-04-07T15:58:52.987

Reputation: 8 279

2

Perl 6,  32  29 bytes

{[~] .comb.map:{(.lc,.uc).pick}}

Try it

{S:g/./{($/.uc,$/.lc).pick}/}

Try it

Brad Gilbert b2gills

Posted 2017-04-07T15:58:52.987

Reputation: 12 713

2

Python 2, 77 74 bytes

lambda s:"".join(map(choice,zip(s.upper(),s.lower())))
from random import*

Try it online!

ovs

Posted 2017-04-07T15:58:52.987

Reputation: 21 408

2

Japt, 12 10 bytes

£M¬?Xv :Xu

Explanation:

£M¬?Xv :Xu
£             // Iterate through the input. X becomes the iterative item
 M¬           // Return a random number, 0 or 1
    ?         // If 1:
     Xv       //   X becomes lowercase
        :     // Else:
         Xu   //   X becomes uppercase 

Try it online!

Oliver

Posted 2017-04-07T15:58:52.987

Reputation: 7 160

1

05AB1E, 8 bytes

vyDš‚.RJ

Try it online!

Explanation

v          # for each char y in input
 yDš‚      # pair lower-case y with upper-case y
     .R    # pick one at random
       J   # join to string

Emigna

Posted 2017-04-07T15:58:52.987

Reputation: 50 798

1

JavaScript, 77 bytes

x=>x.toLowerCase().split``.map(y=>Math.random()*2|0?y:y.toUpperCase()).join``

Try it online!

fəˈnɛtɪk

Posted 2017-04-07T15:58:52.987

Reputation: 4 166

1

CJam, 14 bytes

qel{2mr{eu}&}%

Try it online!

Explanation

q               e# Read the input
 el             e# Make it lowercase
   {            e# For each character in it
    2mr         e#  Randomly choose 0 or 1
       {eu}&    e#  If 1, make the character uppercase
            }%  e# (end of block)

Business Cat

Posted 2017-04-07T15:58:52.987

Reputation: 8 927

1

MATL, 12 11 bytes

1 byte removed using Jonathan Allan's idea of directly changing case.

"@rEk?Yo]&h

Try at MATL online!

Explanation

"         % Implicit input. For each
  @       %   Push current char
  r       %   Random number uniformly distributed on (0,1)
  Ek      %   Duplicate, floor: gives 0 or 1 with the same probability
  ?       %   If nonzero
    Yo    %     Change case. Leaves non-letters unaffected
  ]       %   End
  &h      %   Horizontally concatenate evverything so far
          % Implicit end and display

Luis Mendo

Posted 2017-04-07T15:58:52.987

Reputation: 87 464

1

Pyth, 5 bytes

srLO2

Test suite

srLO2
srLO2Q    Variable introduction
  L  Q    Map over the input
 r        Set the case of each character to 
   O2     Random number from [0, 1]. 0 means lowercase, 1 means uppercase.
s         Concatenate

isaacg

Posted 2017-04-07T15:58:52.987

Reputation: 39 268

1

Befunge, 136 bytes

>~:0`!#@_:"@"`!#v_:"Z"`!#v_:"`"`!#v_:"z"`!#v_,
^,<    <        <                 <
 >?<                     <             -*84<
  >84*+^

Try it online!

There is a lot of whitespace that I think is possible to get rid of. Befunge doesn't have a way of figuring out what's a letter and what isn't, so this is what I'm doing on the first row.

user55852

Posted 2017-04-07T15:58:52.987

Reputation:

This snippet is a faster way of checking if the top value of the stack is a letter - pushes a 0 if it is, and a 1 if it isn't. It's a lot shorter to "and" conditions together by multiplying them instead of using a bunch of if statements routing to the same place – MildlyMilquetoast – 2017-04-08T02:26:38.457

I don't understand how that works. When I run it, it always gives me a 0, and it doesn't accept any input. – None – 2017-04-09T04:04:12.267

Sorry, in that program you put the value in the first quotation marks. In retrospect, this is a really dumb way to do that. It should be pretty easy to change the first three characters to a ~ – MildlyMilquetoast – 2017-04-10T21:58:17.287

0

Swift - way too many bytes (176 167)

uppercased(),lowercased(),arc4random_uniform() really kill the score, besides for me having to add a function since Swift has no standard input method!

import Foundation
func g(x:String){var result="";for i in x.characters{let e=String(i);result+=(arc4random_uniform(2)>0 ?e.uppercased():e.lowercased())};print(result)}

Function with usage: print(g(x: "Your String Here"))

Mr. Xcoder

Posted 2017-04-07T15:58:52.987

Reputation: 39 774

0

Bash, 64 bytes

first try

a=$(shuf -e -n 13 {A..Z}|tr -d "\n");b=${a,,};tr $b $a <<<${1,,}

Try it online!

shuffles capital letters, takes first 13, removes newlines and saved to $a. $b holds $a to lowercase. input is set to lowercase by ${1,,} and passed as a heredoc to tr which replaces each ocurrence of $b by $a

This is somewhat non compeating because the same letter is always capitalized.

marcosm

Posted 2017-04-07T15:58:52.987

Reputation: 986

0

JavaScript + HTML, 115 bytes

<input oninput="this.value=this.value.toLowerCase().split('').map(v=>Math.random()<.5?v.toUpperCase():v).join('')">

cnorthfield

Posted 2017-04-07T15:58:52.987

Reputation: 181

0

Bash, 162 Bytes

a=$1
b=1
c="echo $a|head -c$b|tail -c1"
while(($b<=${#a}))
do
r=$[RANDOM%2]
if [ $r = 1 ]
then d=$(eval $c);echo -n ${d^}
else echo -n $(eval $c)
fi
b=$[b+1]
done

Pretty self explanatory. Takes input from command line arg, writes to stdout.

Run like derpifier.sh "Derp this"

Man, once I start applying the tips the code shrinks fast.

Feldspar15523

Posted 2017-04-07T15:58:52.987

Reputation: 211