Replacing a character in a string with a set of two possible characters
a = ["0$%","0%%%","0$%$%","0$$"]
The above is a corrupted communication code where the first element of each sequence has been disguised as 0. I want to recover the original and correct code by computing a list of all possible sequences by replacing 0 with either $ or % and then checking which of the sequences is valid. Think of each sequence as corresponding to an alphabet if correct. For instance, "$$$" could correspond to the alphabet "B".
This is what I've done so far
raw_decoded =
word =
for i in a:
for j in i:
if j == "0":
x = list(itertools.product(["$", "%"], *i[1:]))
y = ("".join(i) for i in x)
for i in y:
raw_decoded.append(i)
for i in raw_decoded:
letter = code_dict[i] #access dictionary for converting to alphabet
word.append(letter)
return word
python string python-3.x list permutation
add a comment |
a = ["0$%","0%%%","0$%$%","0$$"]
The above is a corrupted communication code where the first element of each sequence has been disguised as 0. I want to recover the original and correct code by computing a list of all possible sequences by replacing 0 with either $ or % and then checking which of the sequences is valid. Think of each sequence as corresponding to an alphabet if correct. For instance, "$$$" could correspond to the alphabet "B".
This is what I've done so far
raw_decoded =
word =
for i in a:
for j in i:
if j == "0":
x = list(itertools.product(["$", "%"], *i[1:]))
y = ("".join(i) for i in x)
for i in y:
raw_decoded.append(i)
for i in raw_decoded:
letter = code_dict[i] #access dictionary for converting to alphabet
word.append(letter)
return word
python string python-3.x list permutation
2
What have you tried so far? It is not clear if you are asking about how to produce the replacements or about checking the validity of the sequence. For the late, you should provide more info about how that can be computed.
– eguaio
Nov 26 '18 at 12:50
itertools.product
– Fade In
Nov 26 '18 at 12:51
1
Hi @FadeIn the point is, please show what you've tried, then we can try to adjust what you have. If there's no evidence that you've even made an attempt, then it looks like you're just trying to get people to do it for you, which won't often draw a positive response.
– Andrew
Nov 26 '18 at 12:54
Alright, I've added what I've done so far.
– Fade In
Nov 26 '18 at 12:56
Is the 0 present only in the first character of each string?
– eguaio
Nov 30 '18 at 2:44
add a comment |
a = ["0$%","0%%%","0$%$%","0$$"]
The above is a corrupted communication code where the first element of each sequence has been disguised as 0. I want to recover the original and correct code by computing a list of all possible sequences by replacing 0 with either $ or % and then checking which of the sequences is valid. Think of each sequence as corresponding to an alphabet if correct. For instance, "$$$" could correspond to the alphabet "B".
This is what I've done so far
raw_decoded =
word =
for i in a:
for j in i:
if j == "0":
x = list(itertools.product(["$", "%"], *i[1:]))
y = ("".join(i) for i in x)
for i in y:
raw_decoded.append(i)
for i in raw_decoded:
letter = code_dict[i] #access dictionary for converting to alphabet
word.append(letter)
return word
python string python-3.x list permutation
a = ["0$%","0%%%","0$%$%","0$$"]
The above is a corrupted communication code where the first element of each sequence has been disguised as 0. I want to recover the original and correct code by computing a list of all possible sequences by replacing 0 with either $ or % and then checking which of the sequences is valid. Think of each sequence as corresponding to an alphabet if correct. For instance, "$$$" could correspond to the alphabet "B".
This is what I've done so far
raw_decoded =
word =
for i in a:
for j in i:
if j == "0":
x = list(itertools.product(["$", "%"], *i[1:]))
y = ("".join(i) for i in x)
for i in y:
raw_decoded.append(i)
for i in raw_decoded:
letter = code_dict[i] #access dictionary for converting to alphabet
word.append(letter)
return word
python string python-3.x list permutation
python string python-3.x list permutation
edited Nov 26 '18 at 12:56
Fade In
asked Nov 26 '18 at 12:47
Fade InFade In
61
61
2
What have you tried so far? It is not clear if you are asking about how to produce the replacements or about checking the validity of the sequence. For the late, you should provide more info about how that can be computed.
– eguaio
Nov 26 '18 at 12:50
itertools.product
– Fade In
Nov 26 '18 at 12:51
1
Hi @FadeIn the point is, please show what you've tried, then we can try to adjust what you have. If there's no evidence that you've even made an attempt, then it looks like you're just trying to get people to do it for you, which won't often draw a positive response.
– Andrew
Nov 26 '18 at 12:54
Alright, I've added what I've done so far.
– Fade In
Nov 26 '18 at 12:56
Is the 0 present only in the first character of each string?
– eguaio
Nov 30 '18 at 2:44
add a comment |
2
What have you tried so far? It is not clear if you are asking about how to produce the replacements or about checking the validity of the sequence. For the late, you should provide more info about how that can be computed.
– eguaio
Nov 26 '18 at 12:50
itertools.product
– Fade In
Nov 26 '18 at 12:51
1
Hi @FadeIn the point is, please show what you've tried, then we can try to adjust what you have. If there's no evidence that you've even made an attempt, then it looks like you're just trying to get people to do it for you, which won't often draw a positive response.
– Andrew
Nov 26 '18 at 12:54
Alright, I've added what I've done so far.
– Fade In
Nov 26 '18 at 12:56
Is the 0 present only in the first character of each string?
– eguaio
Nov 30 '18 at 2:44
2
2
What have you tried so far? It is not clear if you are asking about how to produce the replacements or about checking the validity of the sequence. For the late, you should provide more info about how that can be computed.
– eguaio
Nov 26 '18 at 12:50
What have you tried so far? It is not clear if you are asking about how to produce the replacements or about checking the validity of the sequence. For the late, you should provide more info about how that can be computed.
– eguaio
Nov 26 '18 at 12:50
itertools.product
– Fade In
Nov 26 '18 at 12:51
itertools.product
– Fade In
Nov 26 '18 at 12:51
1
1
Hi @FadeIn the point is, please show what you've tried, then we can try to adjust what you have. If there's no evidence that you've even made an attempt, then it looks like you're just trying to get people to do it for you, which won't often draw a positive response.
– Andrew
Nov 26 '18 at 12:54
Hi @FadeIn the point is, please show what you've tried, then we can try to adjust what you have. If there's no evidence that you've even made an attempt, then it looks like you're just trying to get people to do it for you, which won't often draw a positive response.
– Andrew
Nov 26 '18 at 12:54
Alright, I've added what I've done so far.
– Fade In
Nov 26 '18 at 12:56
Alright, I've added what I've done so far.
– Fade In
Nov 26 '18 at 12:56
Is the 0 present only in the first character of each string?
– eguaio
Nov 30 '18 at 2:44
Is the 0 present only in the first character of each string?
– eguaio
Nov 30 '18 at 2:44
add a comment |
2 Answers
2
active
oldest
votes
Not sure what you mean, perhaps you could add a desired output. What I got from your question could be solved in the following way:
b =
for el in a:
if el[0] == '0':
b.push(el.replace('0', '%', 1))
b.push(el.replace('0', '$', 1))
else:
b.push(el)
add a comment |
Try that:
output =
for elem in a:
replaced_dollar = elem.replace('0', '$', 1)
replaced_percent = elem.replace('0', '%', 1)
# check replaced_dollar and replaced_percent
# and then write to output
output.append(replaced_...)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2fstackoverflow.com%2fquestions%2f53481456%2freplacing-a-character-in-a-string-with-a-set-of-two-possible-characters%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Not sure what you mean, perhaps you could add a desired output. What I got from your question could be solved in the following way:
b =
for el in a:
if el[0] == '0':
b.push(el.replace('0', '%', 1))
b.push(el.replace('0', '$', 1))
else:
b.push(el)
add a comment |
Not sure what you mean, perhaps you could add a desired output. What I got from your question could be solved in the following way:
b =
for el in a:
if el[0] == '0':
b.push(el.replace('0', '%', 1))
b.push(el.replace('0', '$', 1))
else:
b.push(el)
add a comment |
Not sure what you mean, perhaps you could add a desired output. What I got from your question could be solved in the following way:
b =
for el in a:
if el[0] == '0':
b.push(el.replace('0', '%', 1))
b.push(el.replace('0', '$', 1))
else:
b.push(el)
Not sure what you mean, perhaps you could add a desired output. What I got from your question could be solved in the following way:
b =
for el in a:
if el[0] == '0':
b.push(el.replace('0', '%', 1))
b.push(el.replace('0', '$', 1))
else:
b.push(el)
answered Nov 26 '18 at 12:53
alexisdevarennesalexisdevarennes
1,98721229
1,98721229
add a comment |
add a comment |
Try that:
output =
for elem in a:
replaced_dollar = elem.replace('0', '$', 1)
replaced_percent = elem.replace('0', '%', 1)
# check replaced_dollar and replaced_percent
# and then write to output
output.append(replaced_...)
add a comment |
Try that:
output =
for elem in a:
replaced_dollar = elem.replace('0', '$', 1)
replaced_percent = elem.replace('0', '%', 1)
# check replaced_dollar and replaced_percent
# and then write to output
output.append(replaced_...)
add a comment |
Try that:
output =
for elem in a:
replaced_dollar = elem.replace('0', '$', 1)
replaced_percent = elem.replace('0', '%', 1)
# check replaced_dollar and replaced_percent
# and then write to output
output.append(replaced_...)
Try that:
output =
for elem in a:
replaced_dollar = elem.replace('0', '$', 1)
replaced_percent = elem.replace('0', '%', 1)
# check replaced_dollar and replaced_percent
# and then write to output
output.append(replaced_...)
answered Nov 26 '18 at 12:53
NickNick
2998
2998
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53481456%2freplacing-a-character-in-a-string-with-a-set-of-two-possible-characters%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
2
What have you tried so far? It is not clear if you are asking about how to produce the replacements or about checking the validity of the sequence. For the late, you should provide more info about how that can be computed.
– eguaio
Nov 26 '18 at 12:50
itertools.product
– Fade In
Nov 26 '18 at 12:51
1
Hi @FadeIn the point is, please show what you've tried, then we can try to adjust what you have. If there's no evidence that you've even made an attempt, then it looks like you're just trying to get people to do it for you, which won't often draw a positive response.
– Andrew
Nov 26 '18 at 12:54
Alright, I've added what I've done so far.
– Fade In
Nov 26 '18 at 12:56
Is the 0 present only in the first character of each string?
– eguaio
Nov 30 '18 at 2:44