Replacing a character in a string with a set of two possible characters












-1















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









share|improve this question




















  • 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
















-1















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









share|improve this question




















  • 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














-1












-1








-1








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









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












2 Answers
2






active

oldest

votes


















0














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)





share|improve this answer































    0














    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_...)





    share|improve this answer
























      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
      });


      }
      });














      draft saved

      draft discarded


















      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









      0














      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)





      share|improve this answer




























        0














        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)





        share|improve this answer


























          0












          0








          0







          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)





          share|improve this answer













          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)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 26 '18 at 12:53









          alexisdevarennesalexisdevarennes

          1,98721229




          1,98721229

























              0














              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_...)





              share|improve this answer




























                0














                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_...)





                share|improve this answer


























                  0












                  0








                  0







                  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_...)





                  share|improve this answer













                  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_...)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 26 '18 at 12:53









                  NickNick

                  2998




                  2998






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Tonle Sap (See)

                      I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

                      Guatemaltekische Davis-Cup-Mannschaft