Regex to find variants of “Google”












4












$begingroup$


From the HackerRank question definition:




The word google can be spelled in many different ways.



E.g. google, g00gle, g0oGle, g<>0gl3, googl3, GooGIe etc...



Because



g = G



o = O = 0 = () = = <>



l = L = I



e = E = 3



That's the problem here to solve.



And the match has to be only this single word, nothing more, nothing
less.



E.g.



"g00gle" = True, "g00gle " = False, "g google" = False, "GGOOGLE" =
False, "hey google" = False




What I'm looking in review: How can I make my code more pythonic?



#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import re

g = ["g", "G"]
o = ["o", "O", "0", "()", "", "<>"]
l = ["l", "L", "I"]
e = ["e", "E", "3"]

regex = (g, o, o, g, l, e)
regex = ((re.escape(y) for y in x) for x in regex)
regex = ("(?:{})".format("|".join(x)) for x in regex)
regex = "^{}$".format("".join(regex))

print(bool(re.match(regex, input())))









share|improve this question











$endgroup$

















    4












    $begingroup$


    From the HackerRank question definition:




    The word google can be spelled in many different ways.



    E.g. google, g00gle, g0oGle, g<>0gl3, googl3, GooGIe etc...



    Because



    g = G



    o = O = 0 = () = = <>



    l = L = I



    e = E = 3



    That's the problem here to solve.



    And the match has to be only this single word, nothing more, nothing
    less.



    E.g.



    "g00gle" = True, "g00gle " = False, "g google" = False, "GGOOGLE" =
    False, "hey google" = False




    What I'm looking in review: How can I make my code more pythonic?



    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-

    import re

    g = ["g", "G"]
    o = ["o", "O", "0", "()", "", "<>"]
    l = ["l", "L", "I"]
    e = ["e", "E", "3"]

    regex = (g, o, o, g, l, e)
    regex = ((re.escape(y) for y in x) for x in regex)
    regex = ("(?:{})".format("|".join(x)) for x in regex)
    regex = "^{}$".format("".join(regex))

    print(bool(re.match(regex, input())))









    share|improve this question











    $endgroup$















      4












      4








      4





      $begingroup$


      From the HackerRank question definition:




      The word google can be spelled in many different ways.



      E.g. google, g00gle, g0oGle, g<>0gl3, googl3, GooGIe etc...



      Because



      g = G



      o = O = 0 = () = = <>



      l = L = I



      e = E = 3



      That's the problem here to solve.



      And the match has to be only this single word, nothing more, nothing
      less.



      E.g.



      "g00gle" = True, "g00gle " = False, "g google" = False, "GGOOGLE" =
      False, "hey google" = False




      What I'm looking in review: How can I make my code more pythonic?



      #!/usr/bin/env python3
      # -*- coding: utf-8 -*-

      import re

      g = ["g", "G"]
      o = ["o", "O", "0", "()", "", "<>"]
      l = ["l", "L", "I"]
      e = ["e", "E", "3"]

      regex = (g, o, o, g, l, e)
      regex = ((re.escape(y) for y in x) for x in regex)
      regex = ("(?:{})".format("|".join(x)) for x in regex)
      regex = "^{}$".format("".join(regex))

      print(bool(re.match(regex, input())))









      share|improve this question











      $endgroup$




      From the HackerRank question definition:




      The word google can be spelled in many different ways.



      E.g. google, g00gle, g0oGle, g<>0gl3, googl3, GooGIe etc...



      Because



      g = G



      o = O = 0 = () = = <>



      l = L = I



      e = E = 3



      That's the problem here to solve.



      And the match has to be only this single word, nothing more, nothing
      less.



      E.g.



      "g00gle" = True, "g00gle " = False, "g google" = False, "GGOOGLE" =
      False, "hey google" = False




      What I'm looking in review: How can I make my code more pythonic?



      #!/usr/bin/env python3
      # -*- coding: utf-8 -*-

      import re

      g = ["g", "G"]
      o = ["o", "O", "0", "()", "", "<>"]
      l = ["l", "L", "I"]
      e = ["e", "E", "3"]

      regex = (g, o, o, g, l, e)
      regex = ((re.escape(y) for y in x) for x in regex)
      regex = ("(?:{})".format("|".join(x)) for x in regex)
      regex = "^{}$".format("".join(regex))

      print(bool(re.match(regex, input())))






      python python-3.x programming-challenge regex






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 8 hours ago









      200_success

      129k15153416




      129k15153416










      asked 8 hours ago









      422_unprocessable_entity422_unprocessable_entity

      1,88231750




      1,88231750






















          1 Answer
          1






          active

          oldest

          votes


















          4












          $begingroup$

          Honestly there's not much code to comment on, it's also a programming challenge, so there's not much incentive to make it any better than it is. I too would have done it your way.



          The only objective criticism I have is I'd just add a function regex_options to build the non-capturing group.



          Other than that I'd apply this to the creation of g, o, l and e. As I think it's a little cleaner. But you may want to perform it before "".join, and after regex = (g, o, o, g, l, e).



          import re


          def regex_options(options):
          options = (re.escape(o) for o in options)
          return "(?:{})".format("|".join(options))


          g = regex_options(["g", "G"])
          o = regex_options(["o", "O", "0", "()", "", "<>"])
          l = regex_options(["l", "L", "I"])
          e = regex_options(["e", "E", "3"])

          regex = "^{}$".format("".join((g, o, o, g, l, e)))

          print(bool(re.match(regex, input())))


          If this were professional code, I'd suggest using an if __name__ == '__main__': block, and possibly a main function.






          share|improve this answer









          $endgroup$









          • 2




            $begingroup$
            I like this solution better, because it doesn't repeatedly redefine regex and change its type.
            $endgroup$
            – 200_success
            7 hours ago











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          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: "196"
          };
          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: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2fcodereview.stackexchange.com%2fquestions%2f213746%2fregex-to-find-variants-of-google%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4












          $begingroup$

          Honestly there's not much code to comment on, it's also a programming challenge, so there's not much incentive to make it any better than it is. I too would have done it your way.



          The only objective criticism I have is I'd just add a function regex_options to build the non-capturing group.



          Other than that I'd apply this to the creation of g, o, l and e. As I think it's a little cleaner. But you may want to perform it before "".join, and after regex = (g, o, o, g, l, e).



          import re


          def regex_options(options):
          options = (re.escape(o) for o in options)
          return "(?:{})".format("|".join(options))


          g = regex_options(["g", "G"])
          o = regex_options(["o", "O", "0", "()", "", "<>"])
          l = regex_options(["l", "L", "I"])
          e = regex_options(["e", "E", "3"])

          regex = "^{}$".format("".join((g, o, o, g, l, e)))

          print(bool(re.match(regex, input())))


          If this were professional code, I'd suggest using an if __name__ == '__main__': block, and possibly a main function.






          share|improve this answer









          $endgroup$









          • 2




            $begingroup$
            I like this solution better, because it doesn't repeatedly redefine regex and change its type.
            $endgroup$
            – 200_success
            7 hours ago
















          4












          $begingroup$

          Honestly there's not much code to comment on, it's also a programming challenge, so there's not much incentive to make it any better than it is. I too would have done it your way.



          The only objective criticism I have is I'd just add a function regex_options to build the non-capturing group.



          Other than that I'd apply this to the creation of g, o, l and e. As I think it's a little cleaner. But you may want to perform it before "".join, and after regex = (g, o, o, g, l, e).



          import re


          def regex_options(options):
          options = (re.escape(o) for o in options)
          return "(?:{})".format("|".join(options))


          g = regex_options(["g", "G"])
          o = regex_options(["o", "O", "0", "()", "", "<>"])
          l = regex_options(["l", "L", "I"])
          e = regex_options(["e", "E", "3"])

          regex = "^{}$".format("".join((g, o, o, g, l, e)))

          print(bool(re.match(regex, input())))


          If this were professional code, I'd suggest using an if __name__ == '__main__': block, and possibly a main function.






          share|improve this answer









          $endgroup$









          • 2




            $begingroup$
            I like this solution better, because it doesn't repeatedly redefine regex and change its type.
            $endgroup$
            – 200_success
            7 hours ago














          4












          4








          4





          $begingroup$

          Honestly there's not much code to comment on, it's also a programming challenge, so there's not much incentive to make it any better than it is. I too would have done it your way.



          The only objective criticism I have is I'd just add a function regex_options to build the non-capturing group.



          Other than that I'd apply this to the creation of g, o, l and e. As I think it's a little cleaner. But you may want to perform it before "".join, and after regex = (g, o, o, g, l, e).



          import re


          def regex_options(options):
          options = (re.escape(o) for o in options)
          return "(?:{})".format("|".join(options))


          g = regex_options(["g", "G"])
          o = regex_options(["o", "O", "0", "()", "", "<>"])
          l = regex_options(["l", "L", "I"])
          e = regex_options(["e", "E", "3"])

          regex = "^{}$".format("".join((g, o, o, g, l, e)))

          print(bool(re.match(regex, input())))


          If this were professional code, I'd suggest using an if __name__ == '__main__': block, and possibly a main function.






          share|improve this answer









          $endgroup$



          Honestly there's not much code to comment on, it's also a programming challenge, so there's not much incentive to make it any better than it is. I too would have done it your way.



          The only objective criticism I have is I'd just add a function regex_options to build the non-capturing group.



          Other than that I'd apply this to the creation of g, o, l and e. As I think it's a little cleaner. But you may want to perform it before "".join, and after regex = (g, o, o, g, l, e).



          import re


          def regex_options(options):
          options = (re.escape(o) for o in options)
          return "(?:{})".format("|".join(options))


          g = regex_options(["g", "G"])
          o = regex_options(["o", "O", "0", "()", "", "<>"])
          l = regex_options(["l", "L", "I"])
          e = regex_options(["e", "E", "3"])

          regex = "^{}$".format("".join((g, o, o, g, l, e)))

          print(bool(re.match(regex, input())))


          If this were professional code, I'd suggest using an if __name__ == '__main__': block, and possibly a main function.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 7 hours ago









          PeilonrayzPeilonrayz

          25.5k337107




          25.5k337107








          • 2




            $begingroup$
            I like this solution better, because it doesn't repeatedly redefine regex and change its type.
            $endgroup$
            – 200_success
            7 hours ago














          • 2




            $begingroup$
            I like this solution better, because it doesn't repeatedly redefine regex and change its type.
            $endgroup$
            – 200_success
            7 hours ago








          2




          2




          $begingroup$
          I like this solution better, because it doesn't repeatedly redefine regex and change its type.
          $endgroup$
          – 200_success
          7 hours ago




          $begingroup$
          I like this solution better, because it doesn't repeatedly redefine regex and change its type.
          $endgroup$
          – 200_success
          7 hours ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • 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.


          Use MathJax to format equations. MathJax reference.


          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%2fcodereview.stackexchange.com%2fquestions%2f213746%2fregex-to-find-variants-of-google%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

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          Origin of the phrase “under your belt”?