Tuesday, March 29, 2016

Passwordify the string – codegolf.stackexchange.com #JHedzWorlD




Your challenge is to passwordify the string! What is passwordifying, you ask?


Take a string as input. This string will only contain uppercase letters, lowercase letters, positive integers, and spaces.


You must replace all spaces with underscores and move all numbers to the end of the string in the order that they appear from left to right. Then, for every letter in the string, randomly change it to uppercase or lowercase.


Examples (case of characters should vary every time):


Input Hello world Output HElLo_wORld Input pa55 w0rd Output pA_Wrd550 Input 14 35 Output _1435 Input 0971 Output 0971 Input [space] Output _ 

Shortest code in bytes wins!


Whoever asks on Information Security SE if this is a good hashing algorithm wins!–Don’t worry SE overlords, I’m just kidding.




s}D`MTrRO2Xzd_ 

Demonstration


s}D`MTrRO2Xzd_ Implicit: z = input(), d = ' ' Xzd_ In z, replace ' ' with '_'. rR To each character, apply r-function O2 Randomly 0 or 1. r 0 is lowercase, r 1 is uppercase. }D Order the characters based on presence in `MT The strings of the numbers 0 to 9. s Concatenate into a string. 



Code:


þ¹žh-s«ð'_:vyždÈiš}? 

Uses CP-1252 encoding.


Try it online!




k32'_'XEt58<2$S"@rEk?Xk]]v! 

Try it online!


k % implicit input. Make lowercase. Non-lettters are not affected 32'_'XE % replace spaces by underscores t58< % duplicate, Create logical index: true for digits, false otherwise 2$S % sort first string according to second. Sort is stable " % for each character in that string @ % push that character rEk % generate 0 or 1 with 1/2 probability each ? % if it's a 1 Xk % make uppercase. Non-letters are not affected ] % end if ] % end for each v % concatenate vertically all stack contents ! % transpose into a row char array, i.e. string. Implicit display 



lelS'_er58<$2mreu&% 

Try it online!


Explanation


Translation of my MATL answer.


l e# read line as a string el e# make lowercase S'_er e# replace spaces by underscores 58<$ e# (stable) sort with key "is digit" % e# map block over the string 2mr e# generate 0 or 1 equiprobably eu& e# if it's 1, make uppercase 



smrdO2o}N`UT:zd_ 

Try it here!


Explanation


 smrdO2o}N`UT:zd_ # z = input :zd_ # replace spaces with underscores o # Sort ^ with key function(N) }N`UT # N in "0123456789", gives 1 for numbers so they get sorted to the right m # map every character d of ^ rdO2 # Convert d randoms to upper- or lowercase s # join list back into one string 



CJam, 23 bytes


lS'_er60<$eu_el+mR% 

Test it here.


Explanation


l e# Read input. S'_er e# Turn spaces into underscores. 60<$ e# Sort (stably) by whether the character is a digit or not. This moves digits e# to the end, without changing the relative order within digits or non-digits. e# Map this block over the characters... eu e# Convert to upper case. _el e# Make a copy and convert to lower case. + e# Join them into one string. mR e# Randomly choose one of the characters. Of course, this entire block is a e# no-op for non-letter characters. % 



-join([char[]]$args[0]-replace" ","_"|%if($_-match"d")$d+=$_else"'$_'.To$("Low","Upp")+$d 


PowerShell stands for horrible golfing language. Split into char array and replace spaces with underscores. Take each character and process. Collect numbers into variable $d for later output. Each other character is randomly make into uppercase or lowercase by invoking an expression using 'char'.ToLower() or 'char'.ToUpper(). If any digits were collected append them on the end.




join '',(.&((&uc,&lc).pick)for .trans(" "=>"_").comb(/D/)),.comb(/d/) 

I’m tired but I think this is pretty good for now




Mathematica, 77 bytes


RandomChoice[ToLowerCase,ToUpperCase]@#&/@Characters@#~SortBy~DigitQ<>""& 

Ahhh, string processing Mathematica… isn’t it lovely. This is an unnamed function that takes and returns a string.


Due to all the syntactic sugar, the reading order is a bit funny:


Characters@# 

Split the string into characters, otherwise we can’t really do anything at all with it.


...~SortBy~DigitQ 

Sorts the digits to the end. By wrapping the test function in a list, we make the sorting stable.


...&/@... 

Maps the left-hand function over each character in the list.


RandomChoice[ToLowerCase,ToUpperCase] 

Chooses a random case-changing function for the current character.


...@#... 

Applies it to the current character.


...<>"" 

Finally joins all the characters back together into a string.




Python 2, 179 bytes


from random import* f=lambda s,a=[str.upper,str.lower],n='0123456789':''.join(map(lambda x:choice(a)(x),filter(lambda x:x not in n,s).replace(' ','_')))+filter(lambda x:x in n,s) 

There’s probably a lot of room for improvement here that I’ll work out later.




from random import* l=['']*2 for i in input().replace(' ','_'):l[i.isnumeric()]+=choice([i.upper(),i.lower()]) print("".join(l)) 



AWK, 128 Bytes


srand();for(;++i<=split($0,a,"");)s=a[i];if(s!=s+0)o=s==" "?"_":rand()>0.5?tolower(s):toupper(s);A=A oelseN=N sprint A N 

The srand() is needed to give us different random numbers each time it runs.
For this to work properly with multi-line input, we’d need to put something like A=N="" before the for loop.




JavaScript (ES6), 114 bytes


s=>s[r="replace"](/ /g,"_")[r](/d/g,"")[r](/./g,c=>c[`to$Math.random()<.5?"Low":"Upp"erCase`]())+s[r](/D/g,"") 

47 bytes just to randomise the case of a character…




or 222 with importing kernel splitting sequences ascii combinators.random regexp


: f ( x -- y ) R/ d/ R/ D/ [ all-matching-subseqs ] bi-curry@ bi [ [ >upper ] [ >lower ] call-random ] map [ "" join ] bi@ " " "_" replace prepend ; 

I’m not too good at golfing in factor, and I’m not sure if I took the best approach here, but thought I’d give it a go



JHedzWorlD






Source link




Passwordify the string – codegolf.stackexchange.com #JHedzWorlD

No comments:

Post a Comment