Convert to Suzhou numerals











up vote
6
down vote

favorite












Suzhou numerals (蘇州碼子; also 花碼) are Chinese decimal numerals:



0 〇
1 〡 一
2 〢 二
3 〣 三
4 〤
5 〥
6 〦
7 〧
8 〨
9 〩


They pretty much work like Arabic numerals, except that when there are consecutive digits belonging to the set {1, 2, 3}, the digits alternate between vertical stroke notation {〡,〢,〣} and horizontal stroke notation {一,二,三} to avoid ambiguity. The first digit of such a consecutive group is always written with vertical stroke notation.



The task is to convert a positive integer into Suzhou numerals.



Test cases



1          〡
11 〡一
25 〢〥
50 〥〇
99 〩〩
111 〡一〡
511 〥〡一
2018 〢〇〡〨
123321 〡二〣三〢一
1234321 〡二〣〤〣二〡
9876543210 〩〨〧〦〥〤〣二〡〇


Shortest code in bytes wins.










share|improve this question




























    up vote
    6
    down vote

    favorite












    Suzhou numerals (蘇州碼子; also 花碼) are Chinese decimal numerals:



    0 〇
    1 〡 一
    2 〢 二
    3 〣 三
    4 〤
    5 〥
    6 〦
    7 〧
    8 〨
    9 〩


    They pretty much work like Arabic numerals, except that when there are consecutive digits belonging to the set {1, 2, 3}, the digits alternate between vertical stroke notation {〡,〢,〣} and horizontal stroke notation {一,二,三} to avoid ambiguity. The first digit of such a consecutive group is always written with vertical stroke notation.



    The task is to convert a positive integer into Suzhou numerals.



    Test cases



    1          〡
    11 〡一
    25 〢〥
    50 〥〇
    99 〩〩
    111 〡一〡
    511 〥〡一
    2018 〢〇〡〨
    123321 〡二〣三〢一
    1234321 〡二〣〤〣二〡
    9876543210 〩〨〧〦〥〤〣二〡〇


    Shortest code in bytes wins.










    share|improve this question


























      up vote
      6
      down vote

      favorite









      up vote
      6
      down vote

      favorite











      Suzhou numerals (蘇州碼子; also 花碼) are Chinese decimal numerals:



      0 〇
      1 〡 一
      2 〢 二
      3 〣 三
      4 〤
      5 〥
      6 〦
      7 〧
      8 〨
      9 〩


      They pretty much work like Arabic numerals, except that when there are consecutive digits belonging to the set {1, 2, 3}, the digits alternate between vertical stroke notation {〡,〢,〣} and horizontal stroke notation {一,二,三} to avoid ambiguity. The first digit of such a consecutive group is always written with vertical stroke notation.



      The task is to convert a positive integer into Suzhou numerals.



      Test cases



      1          〡
      11 〡一
      25 〢〥
      50 〥〇
      99 〩〩
      111 〡一〡
      511 〥〡一
      2018 〢〇〡〨
      123321 〡二〣三〢一
      1234321 〡二〣〤〣二〡
      9876543210 〩〨〧〦〥〤〣二〡〇


      Shortest code in bytes wins.










      share|improve this question















      Suzhou numerals (蘇州碼子; also 花碼) are Chinese decimal numerals:



      0 〇
      1 〡 一
      2 〢 二
      3 〣 三
      4 〤
      5 〥
      6 〦
      7 〧
      8 〨
      9 〩


      They pretty much work like Arabic numerals, except that when there are consecutive digits belonging to the set {1, 2, 3}, the digits alternate between vertical stroke notation {〡,〢,〣} and horizontal stroke notation {一,二,三} to avoid ambiguity. The first digit of such a consecutive group is always written with vertical stroke notation.



      The task is to convert a positive integer into Suzhou numerals.



      Test cases



      1          〡
      11 〡一
      25 〢〥
      50 〥〇
      99 〩〩
      111 〡一〡
      511 〥〡一
      2018 〢〇〡〨
      123321 〡二〣三〢一
      1234321 〡二〣〤〣二〡
      9876543210 〩〨〧〦〥〤〣二〡〇


      Shortest code in bytes wins.







      code-golf number unicode






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 26 mins ago









      qwr

      5,59952556




      5,59952556










      asked 4 hours ago









      lastresort

      805311




      805311






















          5 Answers
          5






          active

          oldest

          votes

















          up vote
          3
          down vote














          Retina, 46 bytes



          /[1-3]{2}|./_T`d`〇〡-〩`^.
          T`123`一二三


          Try it online! Link includes test cases. Explanation:



          /[1-3]{2}|./


          Match either two digits 1-3 or any other digit.



          _T`d`〇〡-〩`^.


          Replace the first character of each match with its Suzhou.



          T`123`一二三


          Replace any remaining digits with horizontal Suzhou.



          51 bytes in Retina 0.8.2:



          M!`[1-3]{2}|.
          mT`d`〇〡-〩`^.
          T`¶123`_一二三


          Try it online! Link includes test cases. Explanation:



          M!`[1-3]{2}|.


          Split the input into individual digits or pairs of digits if they are both 1-3.



          mT`d`〇〡-〩`^.


          Replace the first character of each line with its Suzhou.



          T`¶123`_一二三


          Join the lines back together and replace any remaining digits with horizontal Suzhou.






          share|improve this answer




























            up vote
            2
            down vote














            Perl 5 -p -Mutf8, 53 bytes





            s/[123]K[123]/chr$&+57/ge;y/0-</〇〡-〩一二三/


            Try it online!






            share|improve this answer




























              up vote
              2
              down vote














              Perl 6 -p, 85 61 bytes



              -13 bytes thanks to Jo King





              s:g[(1|2|3)<((1|2|3)]=chr $/+57;tr/0..</〇〡..〩一二三/


              Try it online!






              share|improve this answer






























                up vote
                2
                down vote













                JavaScript (ES6),  95  89 bytes



                Saved 6 bytes thanks to @ShieruAsakoto



                Takes input as a string.





                s=>s.replace(i=/./g,c=>'三二一〇〡〢〣〤〥〦〧〨〩'[i=c<4&i>3&i<7?3-c:+c+3])


                Try it online!






                share|improve this answer























                • 89 bytes
                  – Shieru Asakoto
                  2 hours ago












                • @ShieruAsakoto That's much better! Thanks a lot!
                  – Arnauld
                  2 hours ago


















                up vote
                0
                down vote














                Clean, 181 165 bytes



                All octal escapes can be replaced by the equivalent single-byte characters (and are counted as one byte each), but used for readability and because otherwise it breaks TIO and SE with invalid UTF-8.



                import StdEnv
                u=mapc={'343','200',c}
                ?s=((!!)["〇":s++u['244245246247250']])o digitToInt
                $=
                $[h:t]=[?(u['241242243'])h:if(h-'1'<'03')f$t]
                f=
                f[h:t]=[?["一","二","三"]h: $t]


                Try it online!



                An encoding-unaware compiler is both a blessing and a curse.






                share|improve this answer























                  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: "200"
                  };
                  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',
                  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%2fcodegolf.stackexchange.com%2fquestions%2f177517%2fconvert-to-suzhou-numerals%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  5 Answers
                  5






                  active

                  oldest

                  votes








                  5 Answers
                  5






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes








                  up vote
                  3
                  down vote














                  Retina, 46 bytes



                  /[1-3]{2}|./_T`d`〇〡-〩`^.
                  T`123`一二三


                  Try it online! Link includes test cases. Explanation:



                  /[1-3]{2}|./


                  Match either two digits 1-3 or any other digit.



                  _T`d`〇〡-〩`^.


                  Replace the first character of each match with its Suzhou.



                  T`123`一二三


                  Replace any remaining digits with horizontal Suzhou.



                  51 bytes in Retina 0.8.2:



                  M!`[1-3]{2}|.
                  mT`d`〇〡-〩`^.
                  T`¶123`_一二三


                  Try it online! Link includes test cases. Explanation:



                  M!`[1-3]{2}|.


                  Split the input into individual digits or pairs of digits if they are both 1-3.



                  mT`d`〇〡-〩`^.


                  Replace the first character of each line with its Suzhou.



                  T`¶123`_一二三


                  Join the lines back together and replace any remaining digits with horizontal Suzhou.






                  share|improve this answer

























                    up vote
                    3
                    down vote














                    Retina, 46 bytes



                    /[1-3]{2}|./_T`d`〇〡-〩`^.
                    T`123`一二三


                    Try it online! Link includes test cases. Explanation:



                    /[1-3]{2}|./


                    Match either two digits 1-3 or any other digit.



                    _T`d`〇〡-〩`^.


                    Replace the first character of each match with its Suzhou.



                    T`123`一二三


                    Replace any remaining digits with horizontal Suzhou.



                    51 bytes in Retina 0.8.2:



                    M!`[1-3]{2}|.
                    mT`d`〇〡-〩`^.
                    T`¶123`_一二三


                    Try it online! Link includes test cases. Explanation:



                    M!`[1-3]{2}|.


                    Split the input into individual digits or pairs of digits if they are both 1-3.



                    mT`d`〇〡-〩`^.


                    Replace the first character of each line with its Suzhou.



                    T`¶123`_一二三


                    Join the lines back together and replace any remaining digits with horizontal Suzhou.






                    share|improve this answer























                      up vote
                      3
                      down vote










                      up vote
                      3
                      down vote










                      Retina, 46 bytes



                      /[1-3]{2}|./_T`d`〇〡-〩`^.
                      T`123`一二三


                      Try it online! Link includes test cases. Explanation:



                      /[1-3]{2}|./


                      Match either two digits 1-3 or any other digit.



                      _T`d`〇〡-〩`^.


                      Replace the first character of each match with its Suzhou.



                      T`123`一二三


                      Replace any remaining digits with horizontal Suzhou.



                      51 bytes in Retina 0.8.2:



                      M!`[1-3]{2}|.
                      mT`d`〇〡-〩`^.
                      T`¶123`_一二三


                      Try it online! Link includes test cases. Explanation:



                      M!`[1-3]{2}|.


                      Split the input into individual digits or pairs of digits if they are both 1-3.



                      mT`d`〇〡-〩`^.


                      Replace the first character of each line with its Suzhou.



                      T`¶123`_一二三


                      Join the lines back together and replace any remaining digits with horizontal Suzhou.






                      share|improve this answer













                      Retina, 46 bytes



                      /[1-3]{2}|./_T`d`〇〡-〩`^.
                      T`123`一二三


                      Try it online! Link includes test cases. Explanation:



                      /[1-3]{2}|./


                      Match either two digits 1-3 or any other digit.



                      _T`d`〇〡-〩`^.


                      Replace the first character of each match with its Suzhou.



                      T`123`一二三


                      Replace any remaining digits with horizontal Suzhou.



                      51 bytes in Retina 0.8.2:



                      M!`[1-3]{2}|.
                      mT`d`〇〡-〩`^.
                      T`¶123`_一二三


                      Try it online! Link includes test cases. Explanation:



                      M!`[1-3]{2}|.


                      Split the input into individual digits or pairs of digits if they are both 1-3.



                      mT`d`〇〡-〩`^.


                      Replace the first character of each line with its Suzhou.



                      T`¶123`_一二三


                      Join the lines back together and replace any remaining digits with horizontal Suzhou.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 3 hours ago









                      Neil

                      78.8k744175




                      78.8k744175






















                          up vote
                          2
                          down vote














                          Perl 5 -p -Mutf8, 53 bytes





                          s/[123]K[123]/chr$&+57/ge;y/0-</〇〡-〩一二三/


                          Try it online!






                          share|improve this answer

























                            up vote
                            2
                            down vote














                            Perl 5 -p -Mutf8, 53 bytes





                            s/[123]K[123]/chr$&+57/ge;y/0-</〇〡-〩一二三/


                            Try it online!






                            share|improve this answer























                              up vote
                              2
                              down vote










                              up vote
                              2
                              down vote










                              Perl 5 -p -Mutf8, 53 bytes





                              s/[123]K[123]/chr$&+57/ge;y/0-</〇〡-〩一二三/


                              Try it online!






                              share|improve this answer













                              Perl 5 -p -Mutf8, 53 bytes





                              s/[123]K[123]/chr$&+57/ge;y/0-</〇〡-〩一二三/


                              Try it online!







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered 3 hours ago









                              nwellnhof

                              6,4331125




                              6,4331125






















                                  up vote
                                  2
                                  down vote














                                  Perl 6 -p, 85 61 bytes



                                  -13 bytes thanks to Jo King





                                  s:g[(1|2|3)<((1|2|3)]=chr $/+57;tr/0..</〇〡..〩一二三/


                                  Try it online!






                                  share|improve this answer



























                                    up vote
                                    2
                                    down vote














                                    Perl 6 -p, 85 61 bytes



                                    -13 bytes thanks to Jo King





                                    s:g[(1|2|3)<((1|2|3)]=chr $/+57;tr/0..</〇〡..〩一二三/


                                    Try it online!






                                    share|improve this answer

























                                      up vote
                                      2
                                      down vote










                                      up vote
                                      2
                                      down vote










                                      Perl 6 -p, 85 61 bytes



                                      -13 bytes thanks to Jo King





                                      s:g[(1|2|3)<((1|2|3)]=chr $/+57;tr/0..</〇〡..〩一二三/


                                      Try it online!






                                      share|improve this answer















                                      Perl 6 -p, 85 61 bytes



                                      -13 bytes thanks to Jo King





                                      s:g[(1|2|3)<((1|2|3)]=chr $/+57;tr/0..</〇〡..〩一二三/


                                      Try it online!







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited 3 hours ago

























                                      answered 4 hours ago









                                      nwellnhof

                                      6,4331125




                                      6,4331125






















                                          up vote
                                          2
                                          down vote













                                          JavaScript (ES6),  95  89 bytes



                                          Saved 6 bytes thanks to @ShieruAsakoto



                                          Takes input as a string.





                                          s=>s.replace(i=/./g,c=>'三二一〇〡〢〣〤〥〦〧〨〩'[i=c<4&i>3&i<7?3-c:+c+3])


                                          Try it online!






                                          share|improve this answer























                                          • 89 bytes
                                            – Shieru Asakoto
                                            2 hours ago












                                          • @ShieruAsakoto That's much better! Thanks a lot!
                                            – Arnauld
                                            2 hours ago















                                          up vote
                                          2
                                          down vote













                                          JavaScript (ES6),  95  89 bytes



                                          Saved 6 bytes thanks to @ShieruAsakoto



                                          Takes input as a string.





                                          s=>s.replace(i=/./g,c=>'三二一〇〡〢〣〤〥〦〧〨〩'[i=c<4&i>3&i<7?3-c:+c+3])


                                          Try it online!






                                          share|improve this answer























                                          • 89 bytes
                                            – Shieru Asakoto
                                            2 hours ago












                                          • @ShieruAsakoto That's much better! Thanks a lot!
                                            – Arnauld
                                            2 hours ago













                                          up vote
                                          2
                                          down vote










                                          up vote
                                          2
                                          down vote









                                          JavaScript (ES6),  95  89 bytes



                                          Saved 6 bytes thanks to @ShieruAsakoto



                                          Takes input as a string.





                                          s=>s.replace(i=/./g,c=>'三二一〇〡〢〣〤〥〦〧〨〩'[i=c<4&i>3&i<7?3-c:+c+3])


                                          Try it online!






                                          share|improve this answer














                                          JavaScript (ES6),  95  89 bytes



                                          Saved 6 bytes thanks to @ShieruAsakoto



                                          Takes input as a string.





                                          s=>s.replace(i=/./g,c=>'三二一〇〡〢〣〤〥〦〧〨〩'[i=c<4&i>3&i<7?3-c:+c+3])


                                          Try it online!







                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited 2 hours ago

























                                          answered 3 hours ago









                                          Arnauld

                                          71.3k688298




                                          71.3k688298












                                          • 89 bytes
                                            – Shieru Asakoto
                                            2 hours ago












                                          • @ShieruAsakoto That's much better! Thanks a lot!
                                            – Arnauld
                                            2 hours ago


















                                          • 89 bytes
                                            – Shieru Asakoto
                                            2 hours ago












                                          • @ShieruAsakoto That's much better! Thanks a lot!
                                            – Arnauld
                                            2 hours ago
















                                          89 bytes
                                          – Shieru Asakoto
                                          2 hours ago






                                          89 bytes
                                          – Shieru Asakoto
                                          2 hours ago














                                          @ShieruAsakoto That's much better! Thanks a lot!
                                          – Arnauld
                                          2 hours ago




                                          @ShieruAsakoto That's much better! Thanks a lot!
                                          – Arnauld
                                          2 hours ago










                                          up vote
                                          0
                                          down vote














                                          Clean, 181 165 bytes



                                          All octal escapes can be replaced by the equivalent single-byte characters (and are counted as one byte each), but used for readability and because otherwise it breaks TIO and SE with invalid UTF-8.



                                          import StdEnv
                                          u=mapc={'343','200',c}
                                          ?s=((!!)["〇":s++u['244245246247250']])o digitToInt
                                          $=
                                          $[h:t]=[?(u['241242243'])h:if(h-'1'<'03')f$t]
                                          f=
                                          f[h:t]=[?["一","二","三"]h: $t]


                                          Try it online!



                                          An encoding-unaware compiler is both a blessing and a curse.






                                          share|improve this answer



























                                            up vote
                                            0
                                            down vote














                                            Clean, 181 165 bytes



                                            All octal escapes can be replaced by the equivalent single-byte characters (and are counted as one byte each), but used for readability and because otherwise it breaks TIO and SE with invalid UTF-8.



                                            import StdEnv
                                            u=mapc={'343','200',c}
                                            ?s=((!!)["〇":s++u['244245246247250']])o digitToInt
                                            $=
                                            $[h:t]=[?(u['241242243'])h:if(h-'1'<'03')f$t]
                                            f=
                                            f[h:t]=[?["一","二","三"]h: $t]


                                            Try it online!



                                            An encoding-unaware compiler is both a blessing and a curse.






                                            share|improve this answer

























                                              up vote
                                              0
                                              down vote










                                              up vote
                                              0
                                              down vote










                                              Clean, 181 165 bytes



                                              All octal escapes can be replaced by the equivalent single-byte characters (and are counted as one byte each), but used for readability and because otherwise it breaks TIO and SE with invalid UTF-8.



                                              import StdEnv
                                              u=mapc={'343','200',c}
                                              ?s=((!!)["〇":s++u['244245246247250']])o digitToInt
                                              $=
                                              $[h:t]=[?(u['241242243'])h:if(h-'1'<'03')f$t]
                                              f=
                                              f[h:t]=[?["一","二","三"]h: $t]


                                              Try it online!



                                              An encoding-unaware compiler is both a blessing and a curse.






                                              share|improve this answer















                                              Clean, 181 165 bytes



                                              All octal escapes can be replaced by the equivalent single-byte characters (and are counted as one byte each), but used for readability and because otherwise it breaks TIO and SE with invalid UTF-8.



                                              import StdEnv
                                              u=mapc={'343','200',c}
                                              ?s=((!!)["〇":s++u['244245246247250']])o digitToInt
                                              $=
                                              $[h:t]=[?(u['241242243'])h:if(h-'1'<'03')f$t]
                                              f=
                                              f[h:t]=[?["一","二","三"]h: $t]


                                              Try it online!



                                              An encoding-unaware compiler is both a blessing and a curse.







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited 1 hour ago

























                                              answered 1 hour ago









                                              Οurous

                                              6,18311032




                                              6,18311032






























                                                  draft saved

                                                  draft discarded




















































                                                  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.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f177517%2fconvert-to-suzhou-numerals%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

                                                  數位音樂下載

                                                  When can things happen in Etherscan, such as the picture below?

                                                  格利澤436b