Convert to Suzhou numerals
up vote
3
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.
code-golf number
add a comment |
up vote
3
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.
code-golf number
add a comment |
up vote
3
down vote
favorite
up vote
3
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.
code-golf number
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
code-golf number
asked 1 hour ago
lastresort
790311
790311
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
up vote
2
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.
add a comment |
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!
add a comment |
up vote
1
down vote
Perl 5 -p -Mutf8
, 53 bytes
s/[123]K[123]/chr$&+57/ge;y/0-</〇〡-〩一二三/
Try it online!
add a comment |
up vote
0
down vote
JavaScript (ES6), 95 bytes
Takes input as a string.
s=>s.replace(i=/./g,c=>'〡〢〣〇一二三〤〥〦〧〨〩'[c--?c<3?c|(i=!i*4):i=c+4:i=3])
Try it online!
Note
Within the callback of replace()
, the variable $c$ is a 1-character string, which is always truthy (even for "0"
) and cannot be used as-is for arithmetic addition.
We could use +c
each time we need its decimal value, but that would be a bit long.
Instead of that, we use the fact that c--
yields the value of $c$ before it's decremented but already coerced to a number.
With this single expression, we can simultaneously:
- coerce $c$ to a number
- test if it was originally set to
"0"
- store $c-1$ in $c$
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
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.
add a comment |
up vote
2
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.
add a comment |
up vote
2
down vote
up vote
2
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.
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.
answered 1 hour ago
Neil
78.8k744175
78.8k744175
add a comment |
add a comment |
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!
add a comment |
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!
add a comment |
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!
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!
edited 31 mins ago
answered 1 hour ago
nwellnhof
6,4231125
6,4231125
add a comment |
add a comment |
up vote
1
down vote
Perl 5 -p -Mutf8
, 53 bytes
s/[123]K[123]/chr$&+57/ge;y/0-</〇〡-〩一二三/
Try it online!
add a comment |
up vote
1
down vote
Perl 5 -p -Mutf8
, 53 bytes
s/[123]K[123]/chr$&+57/ge;y/0-</〇〡-〩一二三/
Try it online!
add a comment |
up vote
1
down vote
up vote
1
down vote
Perl 5 -p -Mutf8
, 53 bytes
s/[123]K[123]/chr$&+57/ge;y/0-</〇〡-〩一二三/
Try it online!
Perl 5 -p -Mutf8
, 53 bytes
s/[123]K[123]/chr$&+57/ge;y/0-</〇〡-〩一二三/
Try it online!
answered 41 mins ago
nwellnhof
6,4231125
6,4231125
add a comment |
add a comment |
up vote
0
down vote
JavaScript (ES6), 95 bytes
Takes input as a string.
s=>s.replace(i=/./g,c=>'〡〢〣〇一二三〤〥〦〧〨〩'[c--?c<3?c|(i=!i*4):i=c+4:i=3])
Try it online!
Note
Within the callback of replace()
, the variable $c$ is a 1-character string, which is always truthy (even for "0"
) and cannot be used as-is for arithmetic addition.
We could use +c
each time we need its decimal value, but that would be a bit long.
Instead of that, we use the fact that c--
yields the value of $c$ before it's decremented but already coerced to a number.
With this single expression, we can simultaneously:
- coerce $c$ to a number
- test if it was originally set to
"0"
- store $c-1$ in $c$
add a comment |
up vote
0
down vote
JavaScript (ES6), 95 bytes
Takes input as a string.
s=>s.replace(i=/./g,c=>'〡〢〣〇一二三〤〥〦〧〨〩'[c--?c<3?c|(i=!i*4):i=c+4:i=3])
Try it online!
Note
Within the callback of replace()
, the variable $c$ is a 1-character string, which is always truthy (even for "0"
) and cannot be used as-is for arithmetic addition.
We could use +c
each time we need its decimal value, but that would be a bit long.
Instead of that, we use the fact that c--
yields the value of $c$ before it's decremented but already coerced to a number.
With this single expression, we can simultaneously:
- coerce $c$ to a number
- test if it was originally set to
"0"
- store $c-1$ in $c$
add a comment |
up vote
0
down vote
up vote
0
down vote
JavaScript (ES6), 95 bytes
Takes input as a string.
s=>s.replace(i=/./g,c=>'〡〢〣〇一二三〤〥〦〧〨〩'[c--?c<3?c|(i=!i*4):i=c+4:i=3])
Try it online!
Note
Within the callback of replace()
, the variable $c$ is a 1-character string, which is always truthy (even for "0"
) and cannot be used as-is for arithmetic addition.
We could use +c
each time we need its decimal value, but that would be a bit long.
Instead of that, we use the fact that c--
yields the value of $c$ before it's decremented but already coerced to a number.
With this single expression, we can simultaneously:
- coerce $c$ to a number
- test if it was originally set to
"0"
- store $c-1$ in $c$
JavaScript (ES6), 95 bytes
Takes input as a string.
s=>s.replace(i=/./g,c=>'〡〢〣〇一二三〤〥〦〧〨〩'[c--?c<3?c|(i=!i*4):i=c+4:i=3])
Try it online!
Note
Within the callback of replace()
, the variable $c$ is a 1-character string, which is always truthy (even for "0"
) and cannot be used as-is for arithmetic addition.
We could use +c
each time we need its decimal value, but that would be a bit long.
Instead of that, we use the fact that c--
yields the value of $c$ before it's decremented but already coerced to a number.
With this single expression, we can simultaneously:
- coerce $c$ to a number
- test if it was originally set to
"0"
- store $c-1$ in $c$
edited 14 mins ago
answered 54 mins ago
Arnauld
71.3k688298
71.3k688298
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%2f177517%2fconvert-to-suzhou-numerals%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