1
\$\begingroup\$

SOMe PEOPle lIkE tO TYpE iN A LoGiCAL MAnNER, WITh CapITals aT tHe bEGiNnIng Of ThE SENtENCE and smaLl LETTErS EVeRYWHErE eLsE. NOT Us. NOt Us. Your joB iS to tAkE A SenTEncE Of A LL SMAlL LeTTeRS, And rANDOmly ChAnGe sOMe to caPiTAl. ThiS iS a Code gOLF so tHE SmaLleST coDE WIll win.

the input will always be small letters and spaces with punctuation only.

random meaning running the code multiple times will always yield different results.

test cases for this will be hard since it involves random elements.

here is a shitty example with python

https://onlinegdb.com/Hy0_gEEKH

\$\endgroup\$
  • 2
    \$\begingroup\$ Welcome to CG&CC! This could do with some test cases, and some formal guarantees about the nature of the input. It can't hurt to define random, either. I think I've seen similar challenges, but can't find anything this is a duplicate of. \$\endgroup\$ – Unrelated String Oct 16 '19 at 6:11
  • 4
    \$\begingroup\$ The input will be lower case letters only. Does that mean only a-z, or can there be spaces and other characters? Is it enough that the first letter is randomly capitalized, or is it required that all characters can be capitalized? Do you require uniform randomness? \$\endgroup\$ – Stewie Griffin Oct 16 '19 at 6:40
  • 3
    \$\begingroup\$ Related: Random Capitalization That challenge differs in the input having already-capital letters which should stay capital. Maybe still a duplicate. \$\endgroup\$ – xnor Oct 16 '19 at 6:58
  • 2
    \$\begingroup\$ The "random" specification as it stands is incomplete \$\endgroup\$ – Luis Mendo Oct 16 '19 at 7:21
  • 2
    \$\begingroup\$ (changed my mind) I believe this is far too similar to not be considered an exact duplicate of the other challenge, since the dominant strategy (basically the only one used) is to choose between each character and the character uppercased, which, being valid for both challenges, makes answer tactics fundamentally identical. \$\endgroup\$ – HyperNeutrino Oct 17 '19 at 19:28

15 Answers 15

4
\$\begingroup\$

APL (Dyalog Extended), 6 bytesSBCS

Anonymous tacit prefix function. Assumes ⎕IO←0 (0-based indexing)

⊢×∘?2¨

Try it online!

 number two for each character

? random index among the first that many indices (i.e. 0 or 1 for each character)

 then

× use that to change case (0:lower; 1:upper) on the following:

 the unmodified argument

\$\endgroup\$
4
\$\begingroup\$

Python 3, 59 bytes

i=input()
while i:print(end={i,i.upper()}.pop()[0]);i=i[1:]

Try it online!

-4 bytes thanks to Chas Brown

Uses set.pop() to randomize.

\$\endgroup\$
  • \$\begingroup\$ 63 bytes. Never knew pop could be used for random choice... nice! \$\endgroup\$ – Chas Brown Oct 16 '19 at 22:02
  • \$\begingroup\$ @ChasBrown good find, thanks! pop picks an item based on hash value. From Python 3.something onward, this is completely randomized for a.o. strings. The catch is that it is only assigned once per string per session, so it can only be used in a full program. \$\endgroup\$ – Jitse Oct 17 '19 at 5:46
2
\$\begingroup\$

K (oK), 32 bytes

{,/{`c$x-32*1?2*(x<123)*x>96}'x}

Try it online!

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

Red, 80 78 58 bytes

func[s][foreach c s[prin do pick[c[uppercase c]]random 2]]

Try it online!

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

Japt -m, 5 bytes

c^HÑö

Try it

\$\endgroup\$
  • 1
    \$\begingroup\$ the challenge states input will have"small letters and spaces with punctuation", this fails when there are non-letters \$\endgroup\$ – Embodiment of Ignorance Oct 17 '19 at 3:14
  • 1
    \$\begingroup\$ @EmbodimentofIgnorance, it was lowercase letters only at time of posting. Would you believe, though, I also had your solution written up in another tab but copied the Markdown from the wrong one?! \$\endgroup\$ – Shaggy Oct 17 '19 at 22:09
1
\$\begingroup\$

Japt -m, 4 bytes

+u)ö

Try it

-m    //Map all characters in input
+     //Input prepended to
  u)  //Input to uppercase
    ö //Random character
\$\endgroup\$
1
\$\begingroup\$

Python 2, 72 bytes

lambda i:"".join(choice(x)for x in zip(i,i.upper()))
from random import*

Try it online!

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

PowerShell, 60 46 43 bytes

-3 bytes thanks to mazzy

-join($args|%{"$_"|%('t*g','*per'|random)})

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ perhaps toUpper will be better? \$\endgroup\$ – mazzy Oct 17 '19 at 18:59
  • 1
    \$\begingroup\$ @mazzy Yep, it was. Thanks \$\endgroup\$ – Veskah Oct 17 '19 at 19:08
  • 1
    \$\begingroup\$ nice. -3 bytes :) \$\endgroup\$ – mazzy Oct 18 '19 at 4:28
0
\$\begingroup\$

Jelly, 5 bytes

żŒuX€

Try it online!

Stolen from totallyhuman's solution to Random Capitalization.

My original solution, since I forgot Jelly has ż:

Jelly, 6 bytes

Œu,X¥"

Try it online!

,ŒuXƊ€ works for the same byte count.

     "    For every element of the input and every corresponding element of
Œu        the input capitalized,
  ,X¥     choose one or the other at random.
\$\endgroup\$
0
\$\begingroup\$

Perl 5, 22 bytes

s/./.5<rand?uc$&:$&/ge

Try it online!

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

Charcoal, 8 bytes

⭆S‽⁺↥ι↧ι

Try it online! Link is to verbose version of code. Trivial modification of my answer to Random Capitalization.

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

Python 3, 94 bytes

s=input();from random import*;print("".join([i.upper() if randint(0,1)==1 else i for i in s]))

Try it online!

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

C# (Visual C# Interactive Compiler), 59 bytes

x=>x.Select(l=>z.Next()<1?l.ToUpper():l);var z=new Random()

Try it online!

\$\endgroup\$
  • \$\begingroup\$ 51 bytes - x=>x.Select(l=>new Random().Next()<1?l.ToUpper():l) \$\endgroup\$ – Innat3 Oct 17 '19 at 10:12
0
\$\begingroup\$

C (gcc), 109 46 bytes

Thanks to Johan du Toit for -63 bytes

f(char*s){for(;*s++-=(*s-97u<26&rand())*32;);}

Try it online!

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

PHP, 51 50 bytes

for(;$l=$argn[$i++];)echo$l>_&&rand()%2?$l^' ':$l;

Try it online!


PHP, 51 50 bytes

for(;$l=$argn[$i++];)echo rand()%2?ucfirst($l):$l;

Try it online!

\$\endgroup\$

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