Computer Cipher
up vote
10
down vote
favorite
Introduction:
I have loads of different ciphers stored in a document I once compiled as a kid, I picked a few of the ones I thought were best suitable for challenges (not too trivial, and not too hard) and transformed them into challenges. Most of them are still in the sandbox, and I'm not sure yet whether I'll post all of them, or only a few. But here is the first of them to start things of.
A Computer Cipher will encipher the given text into 'random' character groups of a given length
. If such a group contains a digit, it will use that digit to index into its own group for the enciphered character. If no digit is present in the group, it means the first character is used.
For example, let's say we want to encipher the text this is a computer cipher
with a given length of 5
. This is a potential output (note: numbers are 1-indexed in the example below):
t h i s i s a c o m p u t e r c i p h e r (without spaces of course, but added as clarification)
qu5dt hprit k3iqb osyw2 jii2o m5uzs akiwb hwpc4 eoo3j muxer z4lpc 4lsuw 2tsmp eirkr r3rsi b5nvc vid2o dmh5p hrptj oeh2l 4ngrv (without spaces of course, but added as clarification)
Let's take a few groups as examples to explain how to decipher the group:
qu5dt
: This group contains a digit5
, so the (1-indexed) 5th character of this group is the character used for the deciphered text:t
.
hprit
: This group contains no digits, so the first character of this group is used implicitly for the deciphered text:h
.
osyw2
: This groups contains a digit2
, so the (1-indexed) 2nd character of this group is the character used for the deciphered text:s
.
Challenge:
Given an integer length
and string word_to_encipher
, output a random enciphered string as described above.
You only have to encipher given the length
and word_to_encipher
, so no need to create a deciphering program/function as well. I might make a part 2 challenge for the deciphering in the future however.
Challenge rules:
- You can assume the
length
will be in the range[3,9]
. - You can assume the
word_to_encipher
will only contain letters. - You can use either full lowercase or full uppercase (please state which one you've used in your answer).
- Your outputs, every group, and the positions of the digits in a group (if present) should be uniformly random. So all random letters of the alphabet have the same chance of occurring; the position of the enciphered letter in each group has the same chance of occurring; and the position of the digit has the same chance of occurring (except when it's the first character and no digit is present; and it obviously cannot be on the same position as the enciphered character).
- You are also allowed to use 0-indexed digits instead of 1-indexed. Please state which of the two you've used in your answer.
- The digit
1
(or0
when 0-indexed) will never be present in the output. Sob1ndh
is not a valid group to encipher the character 'b'. However,b4tbw
is valid, where the4
enciphers theb
at the 4th (1-indexed) position, and the other charactersb
,t
,w
are random (which coincidentally also contains ab
). Other possible valid groups oflength
5 to encipher the character 'b' are:abcd2
,ab2de
,babbk
,hue5b
, etc.
General rules:
- This is code-golf, so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
Default Loopholes are forbidden.- If possible, please add a link with a test for your code (i.e. TIO).
- Also, adding an explanation for your answer is highly recommended.
Test cases:
Input:
Length: 5
Word to encipher: thisisacomputercipher
Possible output:
qu5dthpritk3iqbosyw2jii2om5uzsakiwbhwpc4eoo3jmuxerz4lpc4lsuw2tsmpeirkrr3rsib5nvcvid2odmh5phrptjoeh2l4ngrv
Input:
Length: 8
Word to encipher: test
Possible output:
ewetng4o6smptebyo6ontsrbtxten3qk
Input:
Length: 3
Word to encipher: three
Possible output:
tomv3h2rvege3le
code-golf string random cipher encoding
|
show 6 more comments
up vote
10
down vote
favorite
Introduction:
I have loads of different ciphers stored in a document I once compiled as a kid, I picked a few of the ones I thought were best suitable for challenges (not too trivial, and not too hard) and transformed them into challenges. Most of them are still in the sandbox, and I'm not sure yet whether I'll post all of them, or only a few. But here is the first of them to start things of.
A Computer Cipher will encipher the given text into 'random' character groups of a given length
. If such a group contains a digit, it will use that digit to index into its own group for the enciphered character. If no digit is present in the group, it means the first character is used.
For example, let's say we want to encipher the text this is a computer cipher
with a given length of 5
. This is a potential output (note: numbers are 1-indexed in the example below):
t h i s i s a c o m p u t e r c i p h e r (without spaces of course, but added as clarification)
qu5dt hprit k3iqb osyw2 jii2o m5uzs akiwb hwpc4 eoo3j muxer z4lpc 4lsuw 2tsmp eirkr r3rsi b5nvc vid2o dmh5p hrptj oeh2l 4ngrv (without spaces of course, but added as clarification)
Let's take a few groups as examples to explain how to decipher the group:
qu5dt
: This group contains a digit5
, so the (1-indexed) 5th character of this group is the character used for the deciphered text:t
.
hprit
: This group contains no digits, so the first character of this group is used implicitly for the deciphered text:h
.
osyw2
: This groups contains a digit2
, so the (1-indexed) 2nd character of this group is the character used for the deciphered text:s
.
Challenge:
Given an integer length
and string word_to_encipher
, output a random enciphered string as described above.
You only have to encipher given the length
and word_to_encipher
, so no need to create a deciphering program/function as well. I might make a part 2 challenge for the deciphering in the future however.
Challenge rules:
- You can assume the
length
will be in the range[3,9]
. - You can assume the
word_to_encipher
will only contain letters. - You can use either full lowercase or full uppercase (please state which one you've used in your answer).
- Your outputs, every group, and the positions of the digits in a group (if present) should be uniformly random. So all random letters of the alphabet have the same chance of occurring; the position of the enciphered letter in each group has the same chance of occurring; and the position of the digit has the same chance of occurring (except when it's the first character and no digit is present; and it obviously cannot be on the same position as the enciphered character).
- You are also allowed to use 0-indexed digits instead of 1-indexed. Please state which of the two you've used in your answer.
- The digit
1
(or0
when 0-indexed) will never be present in the output. Sob1ndh
is not a valid group to encipher the character 'b'. However,b4tbw
is valid, where the4
enciphers theb
at the 4th (1-indexed) position, and the other charactersb
,t
,w
are random (which coincidentally also contains ab
). Other possible valid groups oflength
5 to encipher the character 'b' are:abcd2
,ab2de
,babbk
,hue5b
, etc.
General rules:
- This is code-golf, so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
Default Loopholes are forbidden.- If possible, please add a link with a test for your code (i.e. TIO).
- Also, adding an explanation for your answer is highly recommended.
Test cases:
Input:
Length: 5
Word to encipher: thisisacomputercipher
Possible output:
qu5dthpritk3iqbosyw2jii2om5uzsakiwbhwpc4eoo3jmuxerz4lpc4lsuw2tsmpeirkrr3rsib5nvcvid2odmh5phrptjoeh2l4ngrv
Input:
Length: 8
Word to encipher: test
Possible output:
ewetng4o6smptebyo6ontsrbtxten3qk
Input:
Length: 3
Word to encipher: three
Possible output:
tomv3h2rvege3le
code-golf string random cipher encoding
1
How does "uniform" mean
– l4m2
yesterday
@l4m2 That there is an equal chance for any output. So all random letters of the alphabet have the same chance of occurring; the position of the enciphered letter in each group has the same chance of occurring; and the position of the digit has the same chance of occurring (except when it's the first character and no digit is present, and also not on the same position as the enciphered character).
– Kevin Cruijssen
yesterday
Soabcd2
,ab2de
,babbk
all same? Also isb1akk
valid?
– l4m2
yesterday
@l4m2 Yep, all three are possible outputs enciphering the character 'b'. As forb1akk
I'd say no. Will edit it in the challenge description to clarify. If the first character is the enciphered one, no digit should be present.
– Kevin Cruijssen
yesterday
1
For example, when length = 3, char = "a"; The form"a??"
has 676 possible results, but"1a?"
,"?a1"
,"2?a"
,"?2a"
, has only104 results. So, if I'm trying to chose one result from all these 780 results, the distribution of "position of the enciphered letter" is 13:1:1, not 1:1:1. And I would consider this as how "uniformly random" work.
– tsh
17 hours ago
|
show 6 more comments
up vote
10
down vote
favorite
up vote
10
down vote
favorite
Introduction:
I have loads of different ciphers stored in a document I once compiled as a kid, I picked a few of the ones I thought were best suitable for challenges (not too trivial, and not too hard) and transformed them into challenges. Most of them are still in the sandbox, and I'm not sure yet whether I'll post all of them, or only a few. But here is the first of them to start things of.
A Computer Cipher will encipher the given text into 'random' character groups of a given length
. If such a group contains a digit, it will use that digit to index into its own group for the enciphered character. If no digit is present in the group, it means the first character is used.
For example, let's say we want to encipher the text this is a computer cipher
with a given length of 5
. This is a potential output (note: numbers are 1-indexed in the example below):
t h i s i s a c o m p u t e r c i p h e r (without spaces of course, but added as clarification)
qu5dt hprit k3iqb osyw2 jii2o m5uzs akiwb hwpc4 eoo3j muxer z4lpc 4lsuw 2tsmp eirkr r3rsi b5nvc vid2o dmh5p hrptj oeh2l 4ngrv (without spaces of course, but added as clarification)
Let's take a few groups as examples to explain how to decipher the group:
qu5dt
: This group contains a digit5
, so the (1-indexed) 5th character of this group is the character used for the deciphered text:t
.
hprit
: This group contains no digits, so the first character of this group is used implicitly for the deciphered text:h
.
osyw2
: This groups contains a digit2
, so the (1-indexed) 2nd character of this group is the character used for the deciphered text:s
.
Challenge:
Given an integer length
and string word_to_encipher
, output a random enciphered string as described above.
You only have to encipher given the length
and word_to_encipher
, so no need to create a deciphering program/function as well. I might make a part 2 challenge for the deciphering in the future however.
Challenge rules:
- You can assume the
length
will be in the range[3,9]
. - You can assume the
word_to_encipher
will only contain letters. - You can use either full lowercase or full uppercase (please state which one you've used in your answer).
- Your outputs, every group, and the positions of the digits in a group (if present) should be uniformly random. So all random letters of the alphabet have the same chance of occurring; the position of the enciphered letter in each group has the same chance of occurring; and the position of the digit has the same chance of occurring (except when it's the first character and no digit is present; and it obviously cannot be on the same position as the enciphered character).
- You are also allowed to use 0-indexed digits instead of 1-indexed. Please state which of the two you've used in your answer.
- The digit
1
(or0
when 0-indexed) will never be present in the output. Sob1ndh
is not a valid group to encipher the character 'b'. However,b4tbw
is valid, where the4
enciphers theb
at the 4th (1-indexed) position, and the other charactersb
,t
,w
are random (which coincidentally also contains ab
). Other possible valid groups oflength
5 to encipher the character 'b' are:abcd2
,ab2de
,babbk
,hue5b
, etc.
General rules:
- This is code-golf, so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
Default Loopholes are forbidden.- If possible, please add a link with a test for your code (i.e. TIO).
- Also, adding an explanation for your answer is highly recommended.
Test cases:
Input:
Length: 5
Word to encipher: thisisacomputercipher
Possible output:
qu5dthpritk3iqbosyw2jii2om5uzsakiwbhwpc4eoo3jmuxerz4lpc4lsuw2tsmpeirkrr3rsib5nvcvid2odmh5phrptjoeh2l4ngrv
Input:
Length: 8
Word to encipher: test
Possible output:
ewetng4o6smptebyo6ontsrbtxten3qk
Input:
Length: 3
Word to encipher: three
Possible output:
tomv3h2rvege3le
code-golf string random cipher encoding
Introduction:
I have loads of different ciphers stored in a document I once compiled as a kid, I picked a few of the ones I thought were best suitable for challenges (not too trivial, and not too hard) and transformed them into challenges. Most of them are still in the sandbox, and I'm not sure yet whether I'll post all of them, or only a few. But here is the first of them to start things of.
A Computer Cipher will encipher the given text into 'random' character groups of a given length
. If such a group contains a digit, it will use that digit to index into its own group for the enciphered character. If no digit is present in the group, it means the first character is used.
For example, let's say we want to encipher the text this is a computer cipher
with a given length of 5
. This is a potential output (note: numbers are 1-indexed in the example below):
t h i s i s a c o m p u t e r c i p h e r (without spaces of course, but added as clarification)
qu5dt hprit k3iqb osyw2 jii2o m5uzs akiwb hwpc4 eoo3j muxer z4lpc 4lsuw 2tsmp eirkr r3rsi b5nvc vid2o dmh5p hrptj oeh2l 4ngrv (without spaces of course, but added as clarification)
Let's take a few groups as examples to explain how to decipher the group:
qu5dt
: This group contains a digit5
, so the (1-indexed) 5th character of this group is the character used for the deciphered text:t
.
hprit
: This group contains no digits, so the first character of this group is used implicitly for the deciphered text:h
.
osyw2
: This groups contains a digit2
, so the (1-indexed) 2nd character of this group is the character used for the deciphered text:s
.
Challenge:
Given an integer length
and string word_to_encipher
, output a random enciphered string as described above.
You only have to encipher given the length
and word_to_encipher
, so no need to create a deciphering program/function as well. I might make a part 2 challenge for the deciphering in the future however.
Challenge rules:
- You can assume the
length
will be in the range[3,9]
. - You can assume the
word_to_encipher
will only contain letters. - You can use either full lowercase or full uppercase (please state which one you've used in your answer).
- Your outputs, every group, and the positions of the digits in a group (if present) should be uniformly random. So all random letters of the alphabet have the same chance of occurring; the position of the enciphered letter in each group has the same chance of occurring; and the position of the digit has the same chance of occurring (except when it's the first character and no digit is present; and it obviously cannot be on the same position as the enciphered character).
- You are also allowed to use 0-indexed digits instead of 1-indexed. Please state which of the two you've used in your answer.
- The digit
1
(or0
when 0-indexed) will never be present in the output. Sob1ndh
is not a valid group to encipher the character 'b'. However,b4tbw
is valid, where the4
enciphers theb
at the 4th (1-indexed) position, and the other charactersb
,t
,w
are random (which coincidentally also contains ab
). Other possible valid groups oflength
5 to encipher the character 'b' are:abcd2
,ab2de
,babbk
,hue5b
, etc.
General rules:
- This is code-golf, so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
Default Loopholes are forbidden.- If possible, please add a link with a test for your code (i.e. TIO).
- Also, adding an explanation for your answer is highly recommended.
Test cases:
Input:
Length: 5
Word to encipher: thisisacomputercipher
Possible output:
qu5dthpritk3iqbosyw2jii2om5uzsakiwbhwpc4eoo3jmuxerz4lpc4lsuw2tsmpeirkrr3rsib5nvcvid2odmh5phrptjoeh2l4ngrv
Input:
Length: 8
Word to encipher: test
Possible output:
ewetng4o6smptebyo6ontsrbtxten3qk
Input:
Length: 3
Word to encipher: three
Possible output:
tomv3h2rvege3le
code-golf string random cipher encoding
code-golf string random cipher encoding
edited yesterday
asked yesterday
Kevin Cruijssen
34.8k554184
34.8k554184
1
How does "uniform" mean
– l4m2
yesterday
@l4m2 That there is an equal chance for any output. So all random letters of the alphabet have the same chance of occurring; the position of the enciphered letter in each group has the same chance of occurring; and the position of the digit has the same chance of occurring (except when it's the first character and no digit is present, and also not on the same position as the enciphered character).
– Kevin Cruijssen
yesterday
Soabcd2
,ab2de
,babbk
all same? Also isb1akk
valid?
– l4m2
yesterday
@l4m2 Yep, all three are possible outputs enciphering the character 'b'. As forb1akk
I'd say no. Will edit it in the challenge description to clarify. If the first character is the enciphered one, no digit should be present.
– Kevin Cruijssen
yesterday
1
For example, when length = 3, char = "a"; The form"a??"
has 676 possible results, but"1a?"
,"?a1"
,"2?a"
,"?2a"
, has only104 results. So, if I'm trying to chose one result from all these 780 results, the distribution of "position of the enciphered letter" is 13:1:1, not 1:1:1. And I would consider this as how "uniformly random" work.
– tsh
17 hours ago
|
show 6 more comments
1
How does "uniform" mean
– l4m2
yesterday
@l4m2 That there is an equal chance for any output. So all random letters of the alphabet have the same chance of occurring; the position of the enciphered letter in each group has the same chance of occurring; and the position of the digit has the same chance of occurring (except when it's the first character and no digit is present, and also not on the same position as the enciphered character).
– Kevin Cruijssen
yesterday
Soabcd2
,ab2de
,babbk
all same? Also isb1akk
valid?
– l4m2
yesterday
@l4m2 Yep, all three are possible outputs enciphering the character 'b'. As forb1akk
I'd say no. Will edit it in the challenge description to clarify. If the first character is the enciphered one, no digit should be present.
– Kevin Cruijssen
yesterday
1
For example, when length = 3, char = "a"; The form"a??"
has 676 possible results, but"1a?"
,"?a1"
,"2?a"
,"?2a"
, has only104 results. So, if I'm trying to chose one result from all these 780 results, the distribution of "position of the enciphered letter" is 13:1:1, not 1:1:1. And I would consider this as how "uniformly random" work.
– tsh
17 hours ago
1
1
How does "uniform" mean
– l4m2
yesterday
How does "uniform" mean
– l4m2
yesterday
@l4m2 That there is an equal chance for any output. So all random letters of the alphabet have the same chance of occurring; the position of the enciphered letter in each group has the same chance of occurring; and the position of the digit has the same chance of occurring (except when it's the first character and no digit is present, and also not on the same position as the enciphered character).
– Kevin Cruijssen
yesterday
@l4m2 That there is an equal chance for any output. So all random letters of the alphabet have the same chance of occurring; the position of the enciphered letter in each group has the same chance of occurring; and the position of the digit has the same chance of occurring (except when it's the first character and no digit is present, and also not on the same position as the enciphered character).
– Kevin Cruijssen
yesterday
So
abcd2
, ab2de
, babbk
all same? Also is b1akk
valid?– l4m2
yesterday
So
abcd2
, ab2de
, babbk
all same? Also is b1akk
valid?– l4m2
yesterday
@l4m2 Yep, all three are possible outputs enciphering the character 'b'. As for
b1akk
I'd say no. Will edit it in the challenge description to clarify. If the first character is the enciphered one, no digit should be present.– Kevin Cruijssen
yesterday
@l4m2 Yep, all three are possible outputs enciphering the character 'b'. As for
b1akk
I'd say no. Will edit it in the challenge description to clarify. If the first character is the enciphered one, no digit should be present.– Kevin Cruijssen
yesterday
1
1
For example, when length = 3, char = "a"; The form
"a??"
has 676 possible results, but "1a?"
, "?a1"
, "2?a"
, "?2a"
, has only104 results. So, if I'm trying to chose one result from all these 780 results, the distribution of "position of the enciphered letter" is 13:1:1, not 1:1:1. And I would consider this as how "uniformly random" work.– tsh
17 hours ago
For example, when length = 3, char = "a"; The form
"a??"
has 676 possible results, but "1a?"
, "?a1"
, "2?a"
, "?2a"
, has only104 results. So, if I'm trying to chose one result from all these 780 results, the distribution of "position of the enciphered letter" is 13:1:1, not 1:1:1. And I would consider this as how "uniformly random" work.– tsh
17 hours ago
|
show 6 more comments
9 Answers
9
active
oldest
votes
up vote
4
down vote
Perl 6, 125 bytes
->n{*.&{S:g{.}=(65..90)>>.chr.roll(n).join.subst(/./,$/,:th($!=roll 1..n:)).subst(/./,$!,:th($!-1??(^n+1∖$!).roll!!n+1))}}
Try it online!
Takes input and output in uppercase. Takes input curried, like f(n)(string)
. Uses 1 indexing.
Explanation:
->n{*.&{ ... }} # Anonymous code block that takes a number n and returns a function
S:g{.}= # That turns each character of the given string into
.roll(n) # Randomly pick n times with replacement
(65..90)>>.chr # From the uppercase alphabet
.join # And join
.subst( ) # Then replace
/./, ,:th($!=roll 1..n:) # A random index (saving the number in $!)
$/ # With the original character
.subst( ) # Replace again
/./,$!,:th( ... ) # The xth character with $!, where x is:
$!-1?? # If $! is not 1
(^n+1∖$!).roll # A random index that isn't $!
!!n+1 # Else an index out of range
add a comment |
up vote
3
down vote
Python 2, 187 177 176 156 154 148 bytes
lambda l,s:''.join([chr(choice(R(65,91))),c,`n`][(j==n)-(j==i)*(n>0)]for c in s for n,i in[sample(R(l),2)]for j in R(l))
from random import*
R=range
Try it online!
Uses uppercase letters, and 0-indexed numbers.
-3 bytes, thanks to Kevin Cruijssen
@KevinCruijssen Thanks :)
– TFeld
yesterday
What doessample(R(l),2)[::1|-(random()<.5)]
mean?
– l4m2
yesterday
@l4m2 It takes 2 numbers fromrange(l)
, and shuffles them. But apparently sample does not guarantee order, so it's not needed :)
– TFeld
yesterday
Can't you remove the parenthesis around(j==i)*(n>0)
? The multiply has operator precedence over the subtract doesn't it?
– Kevin Cruijssen
yesterday
1
@KevinCruijssen Yeah, I forgot to remove them, when i had some problems
– TFeld
yesterday
add a comment |
up vote
3
down vote
JavaScript (Node.js), 135 bytes
n=>f=([c,...s])=>c?(g=n=>n?g(--n)+(n-p?n-q|!p?(r(26)+10).toString(36):p:c):'')(n,r=n=>Math.random()*n|0,p=r(n),q=r(n-1),q+=q>=p)+f(s):s
Try it online!
Thank Arnauld for 1B
add a comment |
up vote
3
down vote
R, 134 132 123 bytes
function(S,n,s=sample)for(k in utf8ToInt(S)){o=k+!1:n
P=s(n,1)
o[-P]=s(c(P[i<-P>1],s(17:42,n-1-i,T)))+48
cat(intToUtf8(o))}
Try it online!
Takes uppercase letters.
Explanation of old code (mostly the same approach):
function(S,n){s=sample # alias
K=el(strsplit(S,"")) # split to characters
o=1:n # output array
for(k in K){ # for each character in the string
P=s(n,1) # pick a Position for that character
o[-P]= # assign to everywhere besides P:
s( # a permutation of:
c(P[i<-P>1], # P if it's greater than 1
s(letters,n-1-i,T))) # and a random sample, with replacement, of lowercase letters
o[P]=k # set k to position P
cat(o,sep="")}} # and print
add a comment |
up vote
2
down vote
Pyth, 22 bytes
smsXWJOQXmOGQJdO-UQJJz
Try it online.
Uses lowercase and zero-indexing.
Explanation
Very straightforward algorithm.
Implicit: read word in z
Implicit: read number in Q
m z For each char d in z:
OQ Choose a number 0..Q-1
J and call it J.
m Q Make an array of Q
OG random letters.
X d Place d in this string
J at position J.
W If J is not 0,
X J place J in this string
O at a random position from
UQ 0..Q-1
- J except for J.
s Concatenate the letters.
s Concatenate the results.
add a comment |
up vote
1
down vote
Java (JDK), 193 bytes
s->n->s.flatMap(c->{int a=new int[n],i=n,x=0;for(;i-->0;)a[i]+=Math.random()*26+97;a[i+=Math.random()*n+1]=c;x+=Math.random()*~-n;if(i>0)a[x<i?x:x+1]=48+i;return java.util.Arrays.stream(a);})
Try it online!
The index are 0-based.
This entry uses an IntStream
(gotten through String::chars
) as input, as well as a number and returns another IntStream
.
add a comment |
up vote
1
down vote
Charcoal, 35 bytes
NθFS«≔‽θη≔‽Φθ⁻κηζFθ¿⁼κηι¿∧η⁼κζIη‽β
Try it online! Link is to verbose version of code. 0-indexed. Explanation:
Nθ
Input the length.
FS«
Input the word and loop over the characters.
≔‽θη
Choose a random position for the deciphered letter.
≔‽Φθ⁻κηζ
Choose a different random position for the digit.
Fθ
Loop once for each output character.
¿⁼κηι
If this is the position of the deciphered letter then output it.
¿∧η⁼κζIη
Otherwise if the deciphered letter is not at the beginning and this is the position of the digit then output the position of the deciphered letter.
‽β
Otherwise output a random letter.
add a comment |
up vote
1
down vote
Clean, 256 bytes
import StdEnv
s::!Int->Int
s _=code {
ccall time "I:I"
ccall srand "I:I"
}
r::!Int->Int
r _=code {
ccall rand "I:I"
}
$n|s 0<1#k=mape.r e rem n
=flatten o mapc.hd[map(i|i==x=c=toChar if(i==y&&x>0)(x+48)(r i rem 26+97))[0..n-1]\x<-k[0..]&y<-k[0..]|x<>y]
Try it online!
Chooses:
- a random
x
(position of the character in the segment) - a random
y
that isn't equal tox
(position of the digit in the segment) - a random lowercase letter for each position not equal to
x
and not equal toy
unlessx
is zero
add a comment |
up vote
1
down vote
Japt, 30 bytes
;£W=VöJ;CöV hWÎX hWÅÎWÎ?WÎs:Cö
Try it online!
Zero-indexed.
Explanation:
; :Set C = [a...z]
£ :For each character of the input:
W=VöJ; : Get two different random indexes from [0,length)
CöV : Get 5 random letters
hWÎX : Replace one at random with the character from the input
hWÅÎ : Replace a different random character with:
WÎ? : If the input character was not placed at 0:
WÎs : The index of the input character
: : Otherwise:
Cö : A random letter
:Implicitly join back to a string
add a comment |
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
Perl 6, 125 bytes
->n{*.&{S:g{.}=(65..90)>>.chr.roll(n).join.subst(/./,$/,:th($!=roll 1..n:)).subst(/./,$!,:th($!-1??(^n+1∖$!).roll!!n+1))}}
Try it online!
Takes input and output in uppercase. Takes input curried, like f(n)(string)
. Uses 1 indexing.
Explanation:
->n{*.&{ ... }} # Anonymous code block that takes a number n and returns a function
S:g{.}= # That turns each character of the given string into
.roll(n) # Randomly pick n times with replacement
(65..90)>>.chr # From the uppercase alphabet
.join # And join
.subst( ) # Then replace
/./, ,:th($!=roll 1..n:) # A random index (saving the number in $!)
$/ # With the original character
.subst( ) # Replace again
/./,$!,:th( ... ) # The xth character with $!, where x is:
$!-1?? # If $! is not 1
(^n+1∖$!).roll # A random index that isn't $!
!!n+1 # Else an index out of range
add a comment |
up vote
4
down vote
Perl 6, 125 bytes
->n{*.&{S:g{.}=(65..90)>>.chr.roll(n).join.subst(/./,$/,:th($!=roll 1..n:)).subst(/./,$!,:th($!-1??(^n+1∖$!).roll!!n+1))}}
Try it online!
Takes input and output in uppercase. Takes input curried, like f(n)(string)
. Uses 1 indexing.
Explanation:
->n{*.&{ ... }} # Anonymous code block that takes a number n and returns a function
S:g{.}= # That turns each character of the given string into
.roll(n) # Randomly pick n times with replacement
(65..90)>>.chr # From the uppercase alphabet
.join # And join
.subst( ) # Then replace
/./, ,:th($!=roll 1..n:) # A random index (saving the number in $!)
$/ # With the original character
.subst( ) # Replace again
/./,$!,:th( ... ) # The xth character with $!, where x is:
$!-1?? # If $! is not 1
(^n+1∖$!).roll # A random index that isn't $!
!!n+1 # Else an index out of range
add a comment |
up vote
4
down vote
up vote
4
down vote
Perl 6, 125 bytes
->n{*.&{S:g{.}=(65..90)>>.chr.roll(n).join.subst(/./,$/,:th($!=roll 1..n:)).subst(/./,$!,:th($!-1??(^n+1∖$!).roll!!n+1))}}
Try it online!
Takes input and output in uppercase. Takes input curried, like f(n)(string)
. Uses 1 indexing.
Explanation:
->n{*.&{ ... }} # Anonymous code block that takes a number n and returns a function
S:g{.}= # That turns each character of the given string into
.roll(n) # Randomly pick n times with replacement
(65..90)>>.chr # From the uppercase alphabet
.join # And join
.subst( ) # Then replace
/./, ,:th($!=roll 1..n:) # A random index (saving the number in $!)
$/ # With the original character
.subst( ) # Replace again
/./,$!,:th( ... ) # The xth character with $!, where x is:
$!-1?? # If $! is not 1
(^n+1∖$!).roll # A random index that isn't $!
!!n+1 # Else an index out of range
Perl 6, 125 bytes
->n{*.&{S:g{.}=(65..90)>>.chr.roll(n).join.subst(/./,$/,:th($!=roll 1..n:)).subst(/./,$!,:th($!-1??(^n+1∖$!).roll!!n+1))}}
Try it online!
Takes input and output in uppercase. Takes input curried, like f(n)(string)
. Uses 1 indexing.
Explanation:
->n{*.&{ ... }} # Anonymous code block that takes a number n and returns a function
S:g{.}= # That turns each character of the given string into
.roll(n) # Randomly pick n times with replacement
(65..90)>>.chr # From the uppercase alphabet
.join # And join
.subst( ) # Then replace
/./, ,:th($!=roll 1..n:) # A random index (saving the number in $!)
$/ # With the original character
.subst( ) # Replace again
/./,$!,:th( ... ) # The xth character with $!, where x is:
$!-1?? # If $! is not 1
(^n+1∖$!).roll # A random index that isn't $!
!!n+1 # Else an index out of range
edited yesterday
answered yesterday
Jo King
19.9k245105
19.9k245105
add a comment |
add a comment |
up vote
3
down vote
Python 2, 187 177 176 156 154 148 bytes
lambda l,s:''.join([chr(choice(R(65,91))),c,`n`][(j==n)-(j==i)*(n>0)]for c in s for n,i in[sample(R(l),2)]for j in R(l))
from random import*
R=range
Try it online!
Uses uppercase letters, and 0-indexed numbers.
-3 bytes, thanks to Kevin Cruijssen
@KevinCruijssen Thanks :)
– TFeld
yesterday
What doessample(R(l),2)[::1|-(random()<.5)]
mean?
– l4m2
yesterday
@l4m2 It takes 2 numbers fromrange(l)
, and shuffles them. But apparently sample does not guarantee order, so it's not needed :)
– TFeld
yesterday
Can't you remove the parenthesis around(j==i)*(n>0)
? The multiply has operator precedence over the subtract doesn't it?
– Kevin Cruijssen
yesterday
1
@KevinCruijssen Yeah, I forgot to remove them, when i had some problems
– TFeld
yesterday
add a comment |
up vote
3
down vote
Python 2, 187 177 176 156 154 148 bytes
lambda l,s:''.join([chr(choice(R(65,91))),c,`n`][(j==n)-(j==i)*(n>0)]for c in s for n,i in[sample(R(l),2)]for j in R(l))
from random import*
R=range
Try it online!
Uses uppercase letters, and 0-indexed numbers.
-3 bytes, thanks to Kevin Cruijssen
@KevinCruijssen Thanks :)
– TFeld
yesterday
What doessample(R(l),2)[::1|-(random()<.5)]
mean?
– l4m2
yesterday
@l4m2 It takes 2 numbers fromrange(l)
, and shuffles them. But apparently sample does not guarantee order, so it's not needed :)
– TFeld
yesterday
Can't you remove the parenthesis around(j==i)*(n>0)
? The multiply has operator precedence over the subtract doesn't it?
– Kevin Cruijssen
yesterday
1
@KevinCruijssen Yeah, I forgot to remove them, when i had some problems
– TFeld
yesterday
add a comment |
up vote
3
down vote
up vote
3
down vote
Python 2, 187 177 176 156 154 148 bytes
lambda l,s:''.join([chr(choice(R(65,91))),c,`n`][(j==n)-(j==i)*(n>0)]for c in s for n,i in[sample(R(l),2)]for j in R(l))
from random import*
R=range
Try it online!
Uses uppercase letters, and 0-indexed numbers.
-3 bytes, thanks to Kevin Cruijssen
Python 2, 187 177 176 156 154 148 bytes
lambda l,s:''.join([chr(choice(R(65,91))),c,`n`][(j==n)-(j==i)*(n>0)]for c in s for n,i in[sample(R(l),2)]for j in R(l))
from random import*
R=range
Try it online!
Uses uppercase letters, and 0-indexed numbers.
-3 bytes, thanks to Kevin Cruijssen
edited yesterday
answered yesterday
TFeld
13.7k21239
13.7k21239
@KevinCruijssen Thanks :)
– TFeld
yesterday
What doessample(R(l),2)[::1|-(random()<.5)]
mean?
– l4m2
yesterday
@l4m2 It takes 2 numbers fromrange(l)
, and shuffles them. But apparently sample does not guarantee order, so it's not needed :)
– TFeld
yesterday
Can't you remove the parenthesis around(j==i)*(n>0)
? The multiply has operator precedence over the subtract doesn't it?
– Kevin Cruijssen
yesterday
1
@KevinCruijssen Yeah, I forgot to remove them, when i had some problems
– TFeld
yesterday
add a comment |
@KevinCruijssen Thanks :)
– TFeld
yesterday
What doessample(R(l),2)[::1|-(random()<.5)]
mean?
– l4m2
yesterday
@l4m2 It takes 2 numbers fromrange(l)
, and shuffles them. But apparently sample does not guarantee order, so it's not needed :)
– TFeld
yesterday
Can't you remove the parenthesis around(j==i)*(n>0)
? The multiply has operator precedence over the subtract doesn't it?
– Kevin Cruijssen
yesterday
1
@KevinCruijssen Yeah, I forgot to remove them, when i had some problems
– TFeld
yesterday
@KevinCruijssen Thanks :)
– TFeld
yesterday
@KevinCruijssen Thanks :)
– TFeld
yesterday
What does
sample(R(l),2)[::1|-(random()<.5)]
mean?– l4m2
yesterday
What does
sample(R(l),2)[::1|-(random()<.5)]
mean?– l4m2
yesterday
@l4m2 It takes 2 numbers from
range(l)
, and shuffles them. But apparently sample does not guarantee order, so it's not needed :)– TFeld
yesterday
@l4m2 It takes 2 numbers from
range(l)
, and shuffles them. But apparently sample does not guarantee order, so it's not needed :)– TFeld
yesterday
Can't you remove the parenthesis around
(j==i)*(n>0)
? The multiply has operator precedence over the subtract doesn't it?– Kevin Cruijssen
yesterday
Can't you remove the parenthesis around
(j==i)*(n>0)
? The multiply has operator precedence over the subtract doesn't it?– Kevin Cruijssen
yesterday
1
1
@KevinCruijssen Yeah, I forgot to remove them, when i had some problems
– TFeld
yesterday
@KevinCruijssen Yeah, I forgot to remove them, when i had some problems
– TFeld
yesterday
add a comment |
up vote
3
down vote
JavaScript (Node.js), 135 bytes
n=>f=([c,...s])=>c?(g=n=>n?g(--n)+(n-p?n-q|!p?(r(26)+10).toString(36):p:c):'')(n,r=n=>Math.random()*n|0,p=r(n),q=r(n-1),q+=q>=p)+f(s):s
Try it online!
Thank Arnauld for 1B
add a comment |
up vote
3
down vote
JavaScript (Node.js), 135 bytes
n=>f=([c,...s])=>c?(g=n=>n?g(--n)+(n-p?n-q|!p?(r(26)+10).toString(36):p:c):'')(n,r=n=>Math.random()*n|0,p=r(n),q=r(n-1),q+=q>=p)+f(s):s
Try it online!
Thank Arnauld for 1B
add a comment |
up vote
3
down vote
up vote
3
down vote
JavaScript (Node.js), 135 bytes
n=>f=([c,...s])=>c?(g=n=>n?g(--n)+(n-p?n-q|!p?(r(26)+10).toString(36):p:c):'')(n,r=n=>Math.random()*n|0,p=r(n),q=r(n-1),q+=q>=p)+f(s):s
Try it online!
Thank Arnauld for 1B
JavaScript (Node.js), 135 bytes
n=>f=([c,...s])=>c?(g=n=>n?g(--n)+(n-p?n-q|!p?(r(26)+10).toString(36):p:c):'')(n,r=n=>Math.random()*n|0,p=r(n),q=r(n-1),q+=q>=p)+f(s):s
Try it online!
Thank Arnauld for 1B
edited yesterday
answered yesterday
l4m2
4,5061634
4,5061634
add a comment |
add a comment |
up vote
3
down vote
R, 134 132 123 bytes
function(S,n,s=sample)for(k in utf8ToInt(S)){o=k+!1:n
P=s(n,1)
o[-P]=s(c(P[i<-P>1],s(17:42,n-1-i,T)))+48
cat(intToUtf8(o))}
Try it online!
Takes uppercase letters.
Explanation of old code (mostly the same approach):
function(S,n){s=sample # alias
K=el(strsplit(S,"")) # split to characters
o=1:n # output array
for(k in K){ # for each character in the string
P=s(n,1) # pick a Position for that character
o[-P]= # assign to everywhere besides P:
s( # a permutation of:
c(P[i<-P>1], # P if it's greater than 1
s(letters,n-1-i,T))) # and a random sample, with replacement, of lowercase letters
o[P]=k # set k to position P
cat(o,sep="")}} # and print
add a comment |
up vote
3
down vote
R, 134 132 123 bytes
function(S,n,s=sample)for(k in utf8ToInt(S)){o=k+!1:n
P=s(n,1)
o[-P]=s(c(P[i<-P>1],s(17:42,n-1-i,T)))+48
cat(intToUtf8(o))}
Try it online!
Takes uppercase letters.
Explanation of old code (mostly the same approach):
function(S,n){s=sample # alias
K=el(strsplit(S,"")) # split to characters
o=1:n # output array
for(k in K){ # for each character in the string
P=s(n,1) # pick a Position for that character
o[-P]= # assign to everywhere besides P:
s( # a permutation of:
c(P[i<-P>1], # P if it's greater than 1
s(letters,n-1-i,T))) # and a random sample, with replacement, of lowercase letters
o[P]=k # set k to position P
cat(o,sep="")}} # and print
add a comment |
up vote
3
down vote
up vote
3
down vote
R, 134 132 123 bytes
function(S,n,s=sample)for(k in utf8ToInt(S)){o=k+!1:n
P=s(n,1)
o[-P]=s(c(P[i<-P>1],s(17:42,n-1-i,T)))+48
cat(intToUtf8(o))}
Try it online!
Takes uppercase letters.
Explanation of old code (mostly the same approach):
function(S,n){s=sample # alias
K=el(strsplit(S,"")) # split to characters
o=1:n # output array
for(k in K){ # for each character in the string
P=s(n,1) # pick a Position for that character
o[-P]= # assign to everywhere besides P:
s( # a permutation of:
c(P[i<-P>1], # P if it's greater than 1
s(letters,n-1-i,T))) # and a random sample, with replacement, of lowercase letters
o[P]=k # set k to position P
cat(o,sep="")}} # and print
R, 134 132 123 bytes
function(S,n,s=sample)for(k in utf8ToInt(S)){o=k+!1:n
P=s(n,1)
o[-P]=s(c(P[i<-P>1],s(17:42,n-1-i,T)))+48
cat(intToUtf8(o))}
Try it online!
Takes uppercase letters.
Explanation of old code (mostly the same approach):
function(S,n){s=sample # alias
K=el(strsplit(S,"")) # split to characters
o=1:n # output array
for(k in K){ # for each character in the string
P=s(n,1) # pick a Position for that character
o[-P]= # assign to everywhere besides P:
s( # a permutation of:
c(P[i<-P>1], # P if it's greater than 1
s(letters,n-1-i,T))) # and a random sample, with replacement, of lowercase letters
o[P]=k # set k to position P
cat(o,sep="")}} # and print
edited 6 hours ago
answered 10 hours ago
Giuseppe
16.2k31052
16.2k31052
add a comment |
add a comment |
up vote
2
down vote
Pyth, 22 bytes
smsXWJOQXmOGQJdO-UQJJz
Try it online.
Uses lowercase and zero-indexing.
Explanation
Very straightforward algorithm.
Implicit: read word in z
Implicit: read number in Q
m z For each char d in z:
OQ Choose a number 0..Q-1
J and call it J.
m Q Make an array of Q
OG random letters.
X d Place d in this string
J at position J.
W If J is not 0,
X J place J in this string
O at a random position from
UQ 0..Q-1
- J except for J.
s Concatenate the letters.
s Concatenate the results.
add a comment |
up vote
2
down vote
Pyth, 22 bytes
smsXWJOQXmOGQJdO-UQJJz
Try it online.
Uses lowercase and zero-indexing.
Explanation
Very straightforward algorithm.
Implicit: read word in z
Implicit: read number in Q
m z For each char d in z:
OQ Choose a number 0..Q-1
J and call it J.
m Q Make an array of Q
OG random letters.
X d Place d in this string
J at position J.
W If J is not 0,
X J place J in this string
O at a random position from
UQ 0..Q-1
- J except for J.
s Concatenate the letters.
s Concatenate the results.
add a comment |
up vote
2
down vote
up vote
2
down vote
Pyth, 22 bytes
smsXWJOQXmOGQJdO-UQJJz
Try it online.
Uses lowercase and zero-indexing.
Explanation
Very straightforward algorithm.
Implicit: read word in z
Implicit: read number in Q
m z For each char d in z:
OQ Choose a number 0..Q-1
J and call it J.
m Q Make an array of Q
OG random letters.
X d Place d in this string
J at position J.
W If J is not 0,
X J place J in this string
O at a random position from
UQ 0..Q-1
- J except for J.
s Concatenate the letters.
s Concatenate the results.
Pyth, 22 bytes
smsXWJOQXmOGQJdO-UQJJz
Try it online.
Uses lowercase and zero-indexing.
Explanation
Very straightforward algorithm.
Implicit: read word in z
Implicit: read number in Q
m z For each char d in z:
OQ Choose a number 0..Q-1
J and call it J.
m Q Make an array of Q
OG random letters.
X d Place d in this string
J at position J.
W If J is not 0,
X J place J in this string
O at a random position from
UQ 0..Q-1
- J except for J.
s Concatenate the letters.
s Concatenate the results.
edited yesterday
answered yesterday
Pietu1998
15.5k22780
15.5k22780
add a comment |
add a comment |
up vote
1
down vote
Java (JDK), 193 bytes
s->n->s.flatMap(c->{int a=new int[n],i=n,x=0;for(;i-->0;)a[i]+=Math.random()*26+97;a[i+=Math.random()*n+1]=c;x+=Math.random()*~-n;if(i>0)a[x<i?x:x+1]=48+i;return java.util.Arrays.stream(a);})
Try it online!
The index are 0-based.
This entry uses an IntStream
(gotten through String::chars
) as input, as well as a number and returns another IntStream
.
add a comment |
up vote
1
down vote
Java (JDK), 193 bytes
s->n->s.flatMap(c->{int a=new int[n],i=n,x=0;for(;i-->0;)a[i]+=Math.random()*26+97;a[i+=Math.random()*n+1]=c;x+=Math.random()*~-n;if(i>0)a[x<i?x:x+1]=48+i;return java.util.Arrays.stream(a);})
Try it online!
The index are 0-based.
This entry uses an IntStream
(gotten through String::chars
) as input, as well as a number and returns another IntStream
.
add a comment |
up vote
1
down vote
up vote
1
down vote
Java (JDK), 193 bytes
s->n->s.flatMap(c->{int a=new int[n],i=n,x=0;for(;i-->0;)a[i]+=Math.random()*26+97;a[i+=Math.random()*n+1]=c;x+=Math.random()*~-n;if(i>0)a[x<i?x:x+1]=48+i;return java.util.Arrays.stream(a);})
Try it online!
The index are 0-based.
This entry uses an IntStream
(gotten through String::chars
) as input, as well as a number and returns another IntStream
.
Java (JDK), 193 bytes
s->n->s.flatMap(c->{int a=new int[n],i=n,x=0;for(;i-->0;)a[i]+=Math.random()*26+97;a[i+=Math.random()*n+1]=c;x+=Math.random()*~-n;if(i>0)a[x<i?x:x+1]=48+i;return java.util.Arrays.stream(a);})
Try it online!
The index are 0-based.
This entry uses an IntStream
(gotten through String::chars
) as input, as well as a number and returns another IntStream
.
edited yesterday
answered yesterday
Olivier Grégoire
8,30711842
8,30711842
add a comment |
add a comment |
up vote
1
down vote
Charcoal, 35 bytes
NθFS«≔‽θη≔‽Φθ⁻κηζFθ¿⁼κηι¿∧η⁼κζIη‽β
Try it online! Link is to verbose version of code. 0-indexed. Explanation:
Nθ
Input the length.
FS«
Input the word and loop over the characters.
≔‽θη
Choose a random position for the deciphered letter.
≔‽Φθ⁻κηζ
Choose a different random position for the digit.
Fθ
Loop once for each output character.
¿⁼κηι
If this is the position of the deciphered letter then output it.
¿∧η⁼κζIη
Otherwise if the deciphered letter is not at the beginning and this is the position of the digit then output the position of the deciphered letter.
‽β
Otherwise output a random letter.
add a comment |
up vote
1
down vote
Charcoal, 35 bytes
NθFS«≔‽θη≔‽Φθ⁻κηζFθ¿⁼κηι¿∧η⁼κζIη‽β
Try it online! Link is to verbose version of code. 0-indexed. Explanation:
Nθ
Input the length.
FS«
Input the word and loop over the characters.
≔‽θη
Choose a random position for the deciphered letter.
≔‽Φθ⁻κηζ
Choose a different random position for the digit.
Fθ
Loop once for each output character.
¿⁼κηι
If this is the position of the deciphered letter then output it.
¿∧η⁼κζIη
Otherwise if the deciphered letter is not at the beginning and this is the position of the digit then output the position of the deciphered letter.
‽β
Otherwise output a random letter.
add a comment |
up vote
1
down vote
up vote
1
down vote
Charcoal, 35 bytes
NθFS«≔‽θη≔‽Φθ⁻κηζFθ¿⁼κηι¿∧η⁼κζIη‽β
Try it online! Link is to verbose version of code. 0-indexed. Explanation:
Nθ
Input the length.
FS«
Input the word and loop over the characters.
≔‽θη
Choose a random position for the deciphered letter.
≔‽Φθ⁻κηζ
Choose a different random position for the digit.
Fθ
Loop once for each output character.
¿⁼κηι
If this is the position of the deciphered letter then output it.
¿∧η⁼κζIη
Otherwise if the deciphered letter is not at the beginning and this is the position of the digit then output the position of the deciphered letter.
‽β
Otherwise output a random letter.
Charcoal, 35 bytes
NθFS«≔‽θη≔‽Φθ⁻κηζFθ¿⁼κηι¿∧η⁼κζIη‽β
Try it online! Link is to verbose version of code. 0-indexed. Explanation:
Nθ
Input the length.
FS«
Input the word and loop over the characters.
≔‽θη
Choose a random position for the deciphered letter.
≔‽Φθ⁻κηζ
Choose a different random position for the digit.
Fθ
Loop once for each output character.
¿⁼κηι
If this is the position of the deciphered letter then output it.
¿∧η⁼κζIη
Otherwise if the deciphered letter is not at the beginning and this is the position of the digit then output the position of the deciphered letter.
‽β
Otherwise output a random letter.
answered yesterday
Neil
78.5k744175
78.5k744175
add a comment |
add a comment |
up vote
1
down vote
Clean, 256 bytes
import StdEnv
s::!Int->Int
s _=code {
ccall time "I:I"
ccall srand "I:I"
}
r::!Int->Int
r _=code {
ccall rand "I:I"
}
$n|s 0<1#k=mape.r e rem n
=flatten o mapc.hd[map(i|i==x=c=toChar if(i==y&&x>0)(x+48)(r i rem 26+97))[0..n-1]\x<-k[0..]&y<-k[0..]|x<>y]
Try it online!
Chooses:
- a random
x
(position of the character in the segment) - a random
y
that isn't equal tox
(position of the digit in the segment) - a random lowercase letter for each position not equal to
x
and not equal toy
unlessx
is zero
add a comment |
up vote
1
down vote
Clean, 256 bytes
import StdEnv
s::!Int->Int
s _=code {
ccall time "I:I"
ccall srand "I:I"
}
r::!Int->Int
r _=code {
ccall rand "I:I"
}
$n|s 0<1#k=mape.r e rem n
=flatten o mapc.hd[map(i|i==x=c=toChar if(i==y&&x>0)(x+48)(r i rem 26+97))[0..n-1]\x<-k[0..]&y<-k[0..]|x<>y]
Try it online!
Chooses:
- a random
x
(position of the character in the segment) - a random
y
that isn't equal tox
(position of the digit in the segment) - a random lowercase letter for each position not equal to
x
and not equal toy
unlessx
is zero
add a comment |
up vote
1
down vote
up vote
1
down vote
Clean, 256 bytes
import StdEnv
s::!Int->Int
s _=code {
ccall time "I:I"
ccall srand "I:I"
}
r::!Int->Int
r _=code {
ccall rand "I:I"
}
$n|s 0<1#k=mape.r e rem n
=flatten o mapc.hd[map(i|i==x=c=toChar if(i==y&&x>0)(x+48)(r i rem 26+97))[0..n-1]\x<-k[0..]&y<-k[0..]|x<>y]
Try it online!
Chooses:
- a random
x
(position of the character in the segment) - a random
y
that isn't equal tox
(position of the digit in the segment) - a random lowercase letter for each position not equal to
x
and not equal toy
unlessx
is zero
Clean, 256 bytes
import StdEnv
s::!Int->Int
s _=code {
ccall time "I:I"
ccall srand "I:I"
}
r::!Int->Int
r _=code {
ccall rand "I:I"
}
$n|s 0<1#k=mape.r e rem n
=flatten o mapc.hd[map(i|i==x=c=toChar if(i==y&&x>0)(x+48)(r i rem 26+97))[0..n-1]\x<-k[0..]&y<-k[0..]|x<>y]
Try it online!
Chooses:
- a random
x
(position of the character in the segment) - a random
y
that isn't equal tox
(position of the digit in the segment) - a random lowercase letter for each position not equal to
x
and not equal toy
unlessx
is zero
answered yesterday
Οurous
6,08311032
6,08311032
add a comment |
add a comment |
up vote
1
down vote
Japt, 30 bytes
;£W=VöJ;CöV hWÎX hWÅÎWÎ?WÎs:Cö
Try it online!
Zero-indexed.
Explanation:
; :Set C = [a...z]
£ :For each character of the input:
W=VöJ; : Get two different random indexes from [0,length)
CöV : Get 5 random letters
hWÎX : Replace one at random with the character from the input
hWÅÎ : Replace a different random character with:
WÎ? : If the input character was not placed at 0:
WÎs : The index of the input character
: : Otherwise:
Cö : A random letter
:Implicitly join back to a string
add a comment |
up vote
1
down vote
Japt, 30 bytes
;£W=VöJ;CöV hWÎX hWÅÎWÎ?WÎs:Cö
Try it online!
Zero-indexed.
Explanation:
; :Set C = [a...z]
£ :For each character of the input:
W=VöJ; : Get two different random indexes from [0,length)
CöV : Get 5 random letters
hWÎX : Replace one at random with the character from the input
hWÅÎ : Replace a different random character with:
WÎ? : If the input character was not placed at 0:
WÎs : The index of the input character
: : Otherwise:
Cö : A random letter
:Implicitly join back to a string
add a comment |
up vote
1
down vote
up vote
1
down vote
Japt, 30 bytes
;£W=VöJ;CöV hWÎX hWÅÎWÎ?WÎs:Cö
Try it online!
Zero-indexed.
Explanation:
; :Set C = [a...z]
£ :For each character of the input:
W=VöJ; : Get two different random indexes from [0,length)
CöV : Get 5 random letters
hWÎX : Replace one at random with the character from the input
hWÅÎ : Replace a different random character with:
WÎ? : If the input character was not placed at 0:
WÎs : The index of the input character
: : Otherwise:
Cö : A random letter
:Implicitly join back to a string
Japt, 30 bytes
;£W=VöJ;CöV hWÎX hWÅÎWÎ?WÎs:Cö
Try it online!
Zero-indexed.
Explanation:
; :Set C = [a...z]
£ :For each character of the input:
W=VöJ; : Get two different random indexes from [0,length)
CöV : Get 5 random letters
hWÎX : Replace one at random with the character from the input
hWÅÎ : Replace a different random character with:
WÎ? : If the input character was not placed at 0:
WÎs : The index of the input character
: : Otherwise:
Cö : A random letter
:Implicitly join back to a string
edited 10 hours ago
answered yesterday
Kamil Drakari
2,646416
2,646416
add a comment |
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f176931%2fcomputer-cipher%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
How does "uniform" mean
– l4m2
yesterday
@l4m2 That there is an equal chance for any output. So all random letters of the alphabet have the same chance of occurring; the position of the enciphered letter in each group has the same chance of occurring; and the position of the digit has the same chance of occurring (except when it's the first character and no digit is present, and also not on the same position as the enciphered character).
– Kevin Cruijssen
yesterday
So
abcd2
,ab2de
,babbk
all same? Also isb1akk
valid?– l4m2
yesterday
@l4m2 Yep, all three are possible outputs enciphering the character 'b'. As for
b1akk
I'd say no. Will edit it in the challenge description to clarify. If the first character is the enciphered one, no digit should be present.– Kevin Cruijssen
yesterday
1
For example, when length = 3, char = "a"; The form
"a??"
has 676 possible results, but"1a?"
,"?a1"
,"2?a"
,"?2a"
, has only104 results. So, if I'm trying to chose one result from all these 780 results, the distribution of "position of the enciphered letter" is 13:1:1, not 1:1:1. And I would consider this as how "uniformly random" work.– tsh
17 hours ago