Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

deleted 338 characters in body
Source Link
Martin Ender
  • 198.2k
  • 67
  • 455
  • 998

Retina , (削除) 317 (削除ここまで)(削除) 139 (削除ここまで)(削除) 134 (削除ここまで)(削除) 132 (削除ここまで)(削除) 70 (削除ここまで)(削除) 63 (削除ここまで) 60 bytes

(削除) It's really about time I added some decimal-unary conversion feature to Retina... (削除ここまで) I did! :)

Retina , (削除) 317 (削除ここまで)(削除) 139 (削除ここまで)(削除) 134 (削除ここまで)(削除) 132 (削除ここまで)(削除) 70 (削除ここまで)(削除) 63 (削除ここまで)(削除) 60 (削除ここまで) 55 bytes


100$*
\B.100{`^
¶$`_
\b*\(111a`(___)+\b+
Fi;0ドルFi;$&
\b(1_{5})+\b+$
Bu;
;1*;_*
zz
1+
$'_&`.&

Notice the leading empty line. The byte count assumes that the file is encoded as ISO 8859-1.

Try it online! Try it online!


100$*.100{`^
\B_
¶$`

We start by writing a list of unary numbers from 1 to 100 into the empty input string. The first stage makes use of Retina's new repetition feature. It replaces the empty input with 100 1s (the specific character 1. is implicit).

The second stage inserts a linefeed (using the pilcrow, ) and prefix up to that point into every position the first and last. For example, if we only wanted number 1 to 3, thenglobal silent flag which turns off implicit output at the resultend of the first stage would be 111program. The second stage matches the positions marked with |: 1|1|1100{. The first match inserts a linefeed and wraps the prefixrest of the program in a loop which is 1executed for 100 iterations. This combines with theFinally, the stage itself just inserts a 1_ afterat the match to give 11. The second match inserts another linefeed andbeginning of the prefixstring, which is now 11. This gives 111 together with the last 1effectively increments a unary loop counter. So the result would be:

1
11*\(a`(___)+
111Fi;$&

In realityMore configuration. *\( wraps the remainder of the program in a group, we getprints its result with a trailing linefeed, but also puts the same thing up to 100entire group in a dry run, which means that its result will be discarded after printing, so that our loop counter isn't actually modified. If anyone findsa is a shorter waycustom regex modifier which anchors the regex to generatethe entire string (which saves a unary range from 1 to N I'd be very interested to see it.byte on using :^ and $ explicitly)

Now the actual FizzBuzz:.

\b(111)+\b
Fi;0ドル

The atomic stage itself takes care of Fizz. Divisibility by 3 can easily be checked in unary: just test if the number can be written as a repetition of 111. The word boundaries \b___ make sure we're not matching only part of a number. If this is the case, we prepend Fi; to the linestring. The semicolon is so that there is still a word boundary in front of the number for the next stage. If we turned the line into Fizz111Fizz___... the position between z and 1_ would not be considered a boundary, because regex treats both letters and digitsunderscores as word characters. However, the semicolon also allows us to remove the zz duplication from Fizz and Buzz.

\b(1_{5})+\b+$
Bu;

We do the exact same for divisibility by 5 and Bu;, although we don't need to keep the 1_s around this time. So we would get a resultresults like

1_
11__
Fi;111Fi;___
1111____
Bu;
Fi;111111Fi;______
...
Fi;Bu;
...

This makes it very easy to get rid of numbersthe underscores only in those lines which contain Fizz, while also filling in the zzs:

;1*;_*
zz

That is, we turn each semicolon into zz but we also consume all the 1_s right after it. At this point we're done with FizzBuzz in unary. But the challenge wants decimal output.

1+
$'_&`.&

We convert the numbers to decimal using a new feature in Retina's substitution syntax. By inserting a .& into a group reference we get the length ofindicates a substitution element instead ofconditional: this stage is only executed if the actual string contains an underscore. We match each number withTherefore, 1+Fizz, Buzz and replace it with $.&FizzBuzz, i iterations are left untouched.e In all other iterations (i. the length of the entire matche. That isthose which are neither divisible by 3 nor 5), we simplyjust count the 1snumber of characters, converting the result to decimal.

Retina , (削除) 317 (削除ここまで)(削除) 139 (削除ここまで)(削除) 134 (削除ここまで)(削除) 132 (削除ここまで)(削除) 70 (削除ここまで)(削除) 63 (削除ここまで) 60 bytes

(削除) It's really about time I added some decimal-unary conversion feature to Retina... (削除ここまで) I did! :)


100$*
\B
¶$`
\b(111)+\b
Fi;0ドル
\b(1{5})+\b
Bu;
;1*
zz
1+
$.&

Notice the leading empty line. The byte count assumes that the file is encoded as ISO 8859-1.

Try it online!


100$*
\B
¶$`

We start by writing a list of unary numbers from 1 to 100 into the empty input string. The first stage makes use of Retina's new repetition feature. It replaces the empty input with 100 1s (the specific character 1 is implicit).

The second stage inserts a linefeed (using the pilcrow, ) and prefix up to that point into every position the first and last. For example, if we only wanted number 1 to 3, then the result of the first stage would be 111. The second stage matches the positions marked with |: 1|1|1. The first match inserts a linefeed and the prefix which is 1. This combines with the the 1 after the match to give 11. The second match inserts another linefeed and the prefix which is now 11. This gives 111 together with the last 1. So the result would be:

1
11
111

In reality, we get the same thing up to 100. If anyone finds a shorter way to generate a unary range from 1 to N I'd be very interested to see it. :)

Now the actual FizzBuzz:

\b(111)+\b
Fi;0ドル

Divisibility by 3 can easily be checked in unary: just test if the number can be written as a repetition of 111. The word boundaries \b make sure we're not matching only part of a number. If this is the case, we prepend Fi; to the line. The semicolon is so that there is still a word boundary in front of the number for the next stage. If we turned the line into Fizz111... the position between z and 1 would not be considered a boundary, because regex treats both letters and digits as word characters. However, the semicolon also allows us to remove the zz duplication from Fizz and Buzz.

\b(1{5})+\b
Bu;

We do the exact same for divisibility by 5 and Bu;, although we don't need to keep the 1s around this time. So we get a result like

1
11
Fi;111
1111
Bu;
Fi;111111
...
Fi;Bu;
...

This makes it very easy to get rid of numbers only in those lines which contain Fizz, while also filling in the zzs:

;1*
zz

That is, we turn each semicolon into zz but we also consume all the 1s right after it. At this point we're done with FizzBuzz in unary. But the challenge wants decimal output.

1+
$.&

We convert the numbers to decimal using a new feature in Retina's substitution syntax. By inserting a . into a group reference we get the length of a substitution element instead of the actual string. We match each number with 1+ and replace it with $.&, i.e. the length of the entire match. That is, we simply count the 1s.

Retina , (削除) 317 (削除ここまで)(削除) 139 (削除ここまで)(削除) 134 (削除ここまで)(削除) 132 (削除ここまで)(削除) 70 (削除ここまで)(削除) 63 (削除ここまで)(削除) 60 (削除ここまで) 55 bytes

.100{`^
_
*\(a`(___)+
Fi;$&
\b(_{5})+$
Bu;
;_*
zz
'_&`.

Try it online!

.100{`^
_

The . is the global silent flag which turns off implicit output at the end of the program. 100{ wraps the rest of the program in a loop which is executed for 100 iterations. Finally, the stage itself just inserts a _ at the beginning of the string, which effectively increments a unary loop counter.

*\(a`(___)+
Fi;$&

More configuration. *\( wraps the remainder of the program in a group, prints its result with a trailing linefeed, but also puts the entire group in a dry run, which means that its result will be discarded after printing, so that our loop counter isn't actually modified. a is a custom regex modifier which anchors the regex to the entire string (which saves a byte on using ^ and $ explicitly).

The atomic stage itself takes care of Fizz. Divisibility by 3 can easily be checked in unary: just test if the number can be written as a repetition of ___. If this is the case, we prepend Fi; to the string. The semicolon is so that there is still a word boundary in front of the number for the next stage. If we turned the line into Fizz___... the position between z and _ would not be considered a boundary, because regex treats both letters and underscores as word characters. However, the semicolon also allows us to remove the zz duplication from Fizz and Buzz.

\b(_{5})+$
Bu;

We do the exact same for divisibility by 5 and Bu;, although we don't need to keep the _s around this time. So we would get a results like

_
__
Fi;___
____
Bu;
Fi;______
...
Fi;Bu;
...

This makes it very easy to get rid of the underscores only in those lines which contain Fizz, while also filling in the zzs:

;_*
zz

That is, we turn each semicolon into zz but we also consume all the _s right after it. At this point we're done with FizzBuzz in unary. But the challenge wants decimal output.

'_&`.

& indicates a conditional: this stage is only executed if the string contains an underscore. Therefore, Fizz, Buzz and FizzBuzz iterations are left untouched. In all other iterations (i.e. those which are neither divisible by 3 nor 5), we just count the number of characters, converting the result to decimal.

deleted 69 characters in body
Source Link
Martin Ender
  • 198.2k
  • 67
  • 455
  • 998

Retina, (削除) 317 (削除ここまで) (削除) 139 (削除ここまで) (削除) 134 (削除ここまで) (削除) 132 (削除ここまで) (削除) 70 (削除ここまで) 63(削除) 63 (削除ここまで) 60 bytes


100$*1100$*
\B
¶$`
\b(111)+\b
Fi;0ドル
\b(1{5})+\b
Bu;
;1*
zz
(1)+1+
$#1$.&

Try it online! Try it online!


100$*1100$*
\B
¶$`

We start by writing a list of unary numbers from 1 to 100 into the empty input string. The first stage makes use of Retina's new repetition feature. It replaces the empty input with 100 1s (the specific character 1 is implicit).

(1)+1+
$#1$.&

We convert the numbers to decimal using a new feature in Retina's substitution syntax. By inserting a #. into a group reference we get the numberlength of captures that group made,a substitution element instead of the last capture's valueactual string. We match each number with(1)+. Because the +1+ is outside of the group,and replace it will generate a separate capture for eachwith 1$.&, thereby countingi.e. the numberlength of1s in the numberentire match. $#1 then insertsThat is, we simply count the number of captures of group 1s.

Retina, (削除) 317 (削除ここまで) (削除) 139 (削除ここまで) (削除) 134 (削除ここまで) (削除) 132 (削除ここまで) (削除) 70 (削除ここまで) 63 bytes


100$*1
\B
¶$`
\b(111)+\b
Fi;0ドル
\b(1{5})+\b
Bu;
;1*
zz
(1)+
$#1

Try it online!


100$*1
\B
¶$`

We start by writing a list of unary numbers from 1 to 100 into the empty input string. The first stage makes use of Retina's new repetition feature. It replaces the empty input with 100 1s.

(1)+
$#1

We convert the numbers to decimal using a new feature in Retina's substitution syntax. By inserting a # into a group reference we get the number of captures that group made, instead of the last capture's value. We match each number with(1)+. Because the + is outside of the group, it will generate a separate capture for each 1, thereby counting the number of1s in the number. $#1 then inserts the number of captures of group 1.

Retina, (削除) 317 (削除ここまで) (削除) 139 (削除ここまで) (削除) 134 (削除ここまで) (削除) 132 (削除ここまで) (削除) 70 (削除ここまで) (削除) 63 (削除ここまで) 60 bytes


100$*
\B
¶$`
\b(111)+\b
Fi;0ドル
\b(1{5})+\b
Bu;
;1*
zz
1+
$.&

Try it online!


100$*
\B
¶$`

We start by writing a list of unary numbers from 1 to 100 into the empty input string. The first stage makes use of Retina's new repetition feature. It replaces the empty input with 100 1s (the specific character 1 is implicit).

1+
$.&

We convert the numbers to decimal using a new feature in Retina's substitution syntax. By inserting a . into a group reference we get the length of a substitution element instead of the actual string. We match each number with 1+ and replace it with $.&, i.e. the length of the entire match. That is, we simply count the 1s.

added 64 characters in body
Source Link
Martin Ender
  • 198.2k
  • 67
  • 455
  • 998

Try it online! Try it online!

(I'll update this later.)

^
1
+`\b1{1,99}$100$*1
0ドル¶10ドル\B
¶$`

We start by writing a list of unary numbers from 1 to 100 into the empty input string. This is done by writing a single one with theThe first stage makes use of Retina's new repetition feature. It replaces the empty input with 100 1s.

The second stage inserts a linefeed (using the pilcrow,) and then addingprefix up to that point into every position the nextfirst and last. For example, if we only wanted number 1 to the end3, whilethen the largest number is not more than 99result of the first stage would be 111. The second stage matches the positions marked with | is placeholder for: 1|1|1. The first match inserts a linefeed and the prefix which is 1. When Retina readsThis combines with the source code it preprocesses the file1 after the match to turn all pilcrows into linefeedsgive 11. The second match inserts another linefeed and the prefix which is now 11. This gives 111 together with the last 1. So the result would be:

1
11
111
... up to 99

In reality, we get the same thing up to 100. If anyone finds a shorter way to generate a unary range from 1 to N I'd be very interested to see it. :)

I've used this general technique for unary-to-decimal conversion before, but I managed to golf it significantly using balancing groups .

m`$(1)+
;:0123456789$#1

We start by addingconvert the second linenumbers to the end of each line in the string. We need this asdecimal using a lookup table for the digits, because regexnew feature in Retina's substitution can't conditional conjure up characterssyntax. TheBy inserting a :# isinto a marker sogroup reference we can distinguish the lookup table from other numbers in the result. The ; is another marker which indicates which part ofget the unary number has already been turned into decimal (everything to the right of it), and which parts still needs to be processed (everything to the left of it).

+`\b(1*)1円{9}(1)+;(?=.*:(?<-2>.)*(.))
1ドル;3ドル

This is the actual conversioncaptures that group made, which builds up the decimal number from least to most significant digit. The first bit (1*)1円{9} matches the largest multipleinstead of 10 it can find - i.e. everything not represented by the least significant digitlast capture's value. ThenWe match each number with (1)+ matches. Because the remainder R+ is outside of the number, while pushing R captures into group2. Notice that we ignore the case where R = 0 - this saves a few bytes, and we can do this, because in the range [1,100] all numbers with 0-digits have been turned into Buzz or FizzBuzz.

Now that we have determined the next digit (as the size of stackit will generate a separate capture for each 21), we look upthereby counting the correct character. To do so, we search fornumber of :1s in a lookahead. Then we match individual characters while popping from stack 2 with (?<-2>.)*. That is if the remainder was 4, say, this matches 0123number. Once this stack is empty, (?<-2>.)$#1 fails and the regex engine moves on tothen inserts the next part which is (.) and whichnumber of captures the correct digit (4 in this example) intoof group 31.

The replacement string simply writes group 1 which happens to be our multiple of 10, but divided by 10, then the semicolon to indicate we've processed the new digit, and then the new digit.

;|:.*

Finally, we clean up the string by removing all markers and lookup strings.

Try it online!

(I'll update this later.)

^
1
+`\b1{1,99}$
0ドル¶10ドル

We start by writing a list of unary numbers from 1 to 100 into the empty input string. This is done by writing a single one with the first stage, and then adding the next number to the end, while the largest number is not more than 99. is placeholder for a linefeed. When Retina reads the source code it preprocesses the file to turn all pilcrows into linefeeds.

1
11
111
... up to 99

I've used this general technique for unary-to-decimal conversion before, but I managed to golf it significantly using balancing groups .

m`$
;:0123456789

We start by adding the second line to the end of each line in the string. We need this as a lookup table for the digits, because regex substitution can't conditional conjure up characters. The : is a marker so we can distinguish the lookup table from other numbers in the result. The ; is another marker which indicates which part of the unary number has already been turned into decimal (everything to the right of it), and which parts still needs to be processed (everything to the left of it).

+`\b(1*)1円{9}(1)+;(?=.*:(?<-2>.)*(.))
1ドル;3ドル

This is the actual conversion, which builds up the decimal number from least to most significant digit. The first bit (1*)1円{9} matches the largest multiple of 10 it can find - i.e. everything not represented by the least significant digit. Then (1)+ matches the remainder R of the number, while pushing R captures into group2. Notice that we ignore the case where R = 0 - this saves a few bytes, and we can do this, because in the range [1,100] all numbers with 0-digits have been turned into Buzz or FizzBuzz.

Now that we have determined the next digit (as the size of stack 2), we look up the correct character. To do so, we search for : in a lookahead. Then we match individual characters while popping from stack 2 with (?<-2>.)*. That is if the remainder was 4, say, this matches 0123. Once this stack is empty, (?<-2>.) fails and the regex engine moves on to the next part which is (.) and which captures the correct digit (4 in this example) into group 3.

The replacement string simply writes group 1 which happens to be our multiple of 10, but divided by 10, then the semicolon to indicate we've processed the new digit, and then the new digit.

;|:.*

Finally, we clean up the string by removing all markers and lookup strings.

Try it online!


100$*1
\B
¶$`

We start by writing a list of unary numbers from 1 to 100 into the empty input string. The first stage makes use of Retina's new repetition feature. It replaces the empty input with 100 1s.

The second stage inserts a linefeed (using the pilcrow,) and prefix up to that point into every position the first and last. For example, if we only wanted number 1 to 3, then the result of the first stage would be 111. The second stage matches the positions marked with |: 1|1|1. The first match inserts a linefeed and the prefix which is 1. This combines with the the 1 after the match to give 11. The second match inserts another linefeed and the prefix which is now 11. This gives 111 together with the last 1. So the result would be:

1
11
111

In reality, we get the same thing up to 100. If anyone finds a shorter way to generate a unary range from 1 to N I'd be very interested to see it. :)

(1)+
$#1

We convert the numbers to decimal using a new feature in Retina's substitution syntax. By inserting a # into a group reference we get the number of captures that group made, instead of the last capture's value. We match each number with (1)+. Because the + is outside of the group, it will generate a separate capture for each 1, thereby counting the number of 1s in the number. $#1 then inserts the number of captures of group 1.

added 64 characters in body
Source Link
Martin Ender
  • 198.2k
  • 67
  • 455
  • 998
Loading
deleted 204 characters in body
Source Link
Martin Ender
  • 198.2k
  • 67
  • 455
  • 998
Loading
deleted 50 characters in body
Source Link
Martin Ender
  • 198.2k
  • 67
  • 455
  • 998
Loading
deleted 44 characters in body
Source Link
Martin Ender
  • 198.2k
  • 67
  • 455
  • 998
Loading
added 31 characters in body
Source Link
Martin Ender
  • 198.2k
  • 67
  • 455
  • 998
Loading
added 3674 characters in body
Source Link
Martin Ender
  • 198.2k
  • 67
  • 455
  • 998
Loading
added 12 characters in body
Source Link
Martin Ender
  • 198.2k
  • 67
  • 455
  • 998
Loading
deleted 185 characters in body
Source Link
Martin Ender
  • 198.2k
  • 67
  • 455
  • 998
Loading
Source Link
Martin Ender
  • 198.2k
  • 67
  • 455
  • 998
Loading

AltStyle によって変換されたページ (->オリジナル) /