title: Code Golf Golf Score

text:
# Question
Title: Code Golf Golf Score
It's Friday... so let's go golfing! Write code that determines the player's scoring on a hole in a game of golf. The code can be either a function or entire program. As the genre suggests, shortest code wins.

Input (parameters or stdin, your choice):

* An integer representing the hole's par, guaranteed to be between 3 and 6
* An integer representing the golfer's score, guaranteed to be between 1 and 64

Output (print to stdout or return, trailing newline allowed but not required, your choice):

* if score is 1, output "Hole in one"
* if score == par - 4 and par \> 5, output "Condor"
* if score == par - 3 and par \> 4, output "Albatross"
* if score == par - 2 and par \> 3, output "Eagle"
* if score == par - 1, output "Birdie"
* if score == par, output "Par"
* if score == par + 1, output "Bogey"
* if score == par + 2, output "Double Bogey"
* if score == par + 3, output "Triple Bogey"
* if score \> par + 3, output "Haha you loser"

EDIT Congrats to Dennis on having the shortest answer!

# Answer
# Jelly, 61 bytes

```
_«4ị“Ƙḷ“&SẆ@ẓ“&T¡UQ“½⁽Ð'÷ṿɼ“½Œż“¡œM“v⁵“¥⁻c“£Ḋ⁶»
ç“µḣ⁻×⁵ñBƑ»’?

```

Try it online!

### Background

This uses Jelly's static dictionary compression. You can find a compressor here. This way,

```
“Ƙḷ“&SẆ@ẓ“&T¡UQ“½⁽Ð'÷ṿɼ“½Œż“¡œM“v⁵“¥⁻c“£Ḋ⁶»

```

and

```
“Bogey“Double Bogey“Triple Bogey“Haha you loser“Condor“Albatross“Eagle“Birdie“Par”

```

as well as

```
“µḣ⁻×⁵ñBƑ»

```

and

```
“Hole in one”

```

are equivalent.

### How it works

```
_«4ị“Ƙḷ“&SẆ@ẓ“&T¡UQ“½⁽Ð'÷ṿɼ“½Œż“¡œM“v⁵“¥⁻c“£Ḋ⁶»  Helper link. Arguments: score, par

_                                                Subtract the par from the score.
 «4                                              Cap the difference at 4.
   ị                                             Index into the list at the right.
    “Ƙḷ“&SẆ@ẓ“&T¡UQ“½⁽Ð'÷ṿɼ“½Œż“¡œM“v⁵“¥⁻c“£Ḋ⁶»  Yield a list of strings.

ç“µḣ⁻×⁵ñBƑ»’?  Main link. Arguments: score, pair

            ?  If...
           ’   the decremented score if non-zero:
ç                Call the helper link on both input arguments.
 “µḣ⁻×⁵ñBƑ»      Else, return “Hole in one”.

```
> 6 votes

# Answer
## PHP 5.3+, ~~173~~ ~~167~~ ~~166~~ ~~159~~ ~~156~~ ~~151~~ ~~127~~ 121 bytes

```
echo[Condor,Albatross,Eagle,Birdie,Par,$b=Bogey,"Double $b","Triple $b","Haha you loser"][min(4+$s-$p,8)]?:"Hole in one";

```
**Notice-free version, ~~139~~ 137 bytes**
```
echo$s-1?["Condor","Albatross","Eagle","Birdie","Par",$b="Bogey","Double $b","Triple $b","Haha you loser"][min(4+$s-$p,8)]:"Hole in one";

```

Set `$s`core and `$p`ar variables before the `echo` and you're off.

**exploded view**
```
echo [Condor,
      Albatross,
      Eagle,
      Birdie,
      Par,
      $b = Bogey,
      "Double $b",
      "Triple $b",
      "Haha you loser"][ min( 4+$s-$p,8 ) ]
  ?: "Hole in one";

```

**Edits**
**-6:** Not storing the array, just calling it if need be.
**-1:** Flipping the ternary around.
**-7:** The lowest `$s-$p` with `$s>1` is `-4`, so the `max()` isn't necessary, since `4+$s-$p >= 0`.
**-3:** `Array -> explode()`, thanks CoolestVeto!
**-5:** Cheaty ~~string literal~~ undefined constant plus `$r[-1] -> false`, thanks Ismael Miguel!
**-24:** Moving from an `explode()` function to a `printf`/`%s` setup, with some tweaks, more thanks to Ismael Miguel for the change of direction.
**-6:** *Swerve*, we're back to `echo` again!
> 12 votes

# Answer
# 05AB1E, ~~91~~ 90 bytes

Code:

```
-5+U“¥Ê€†€µ“ª"0Bogey"Ð”Condor Albatross²è Birdie Par ÿ‹¶ÿ½¿ÿ”ð¡“Haha€î loser“X0¹1Qm*@0ð:ðÛ

```

Explanation:

Part 1:

```
-5+                          # Computes Score - Par + 5
   U                         # Store in X
    “¥Ê€†€µ“ª                # Short for "Hole in one"
             "0Bogey"        # Push this string
                     Ð       # Triplicate

```

Part 2:

```
”Condor Albatross²è Birdie Par ÿ‹¶ÿ½¿ÿ”ð¡

```

This is the same as `"Condor Albatross Eagle Birdie Par 0Bogey Double0Bogey Triple0Bogey"` using string compression and string interpolation. We then split on spaces, using `ð¡`.

Part 3:

```
“Haha€î loser“                # Push "Haha you loser"
              X               # Push X
               0¹1Qm          # Compute 0 ^ (score == 1), this translates 1 to 0 and
                                everything else to 1.
                    *         # Multiply the top two items
                     @        # Get the string from that position
                      0ð:     # Replace zeros with spaces
                         ðÛ   # Trim off leading spaces

```

Discovered a *lot* of bugs, uses **CP-1252** encoding.

Try it online!
> 10 votes

# Answer
# Vitsy, 131 bytes

```
D1-)["eno ni eloH"rZ;]r-5+D9/([X9]mZ
"rodnoC"
"ssortablA"
"elgaE"
"eidriB"
"raP"
"yegoB"
4m" elbuoD"
4m" elpirT"
"resol uoy ahaH"
```

Explanation:

```
D1-)["eno ni eloH"rZ;]r-5+D9/([X9]mZ
D1-)[                ]      If the second input is 1, do the bracketed stuff.
     "eno ni eloH"rZ;       Output "Hole in one" and quit.
r                           Reverse the stack.
 -                          Subtract the top two items.
  5+                        Add 5 to fix for negative values of score.
    D9/([  ]                If the result of that is greater than 8, do the stuff in brackets.
         X                  Remove the top item.
          9                 Push 9. This forces any items greater than 8 to be 9.
            m               Execute this number line.
             Z              Output everything in the stack.

```

This works by figuring out what the score is relative to the par, then executing different lines (and gaining different strings) thereof.

Try It Online!

Verbose Mode (for poops and giggles):

```
duplicate top item;
push 1;
subtract top two;
if (int) top is not 0;
begin recursive area;
toggle double quote;
push 14;
eval(stack);
capture stack as object with next;
 ;
eval(stack);
push input item;
 ;
push 14;
push length of stack;
capture stack as object with next;
push all ints between second to top and top;
toggle double quote;
reverse stack;
output stack as chars;
generic exit;
end recursive area;
reverse stack;
subtract top two;
push 5;
add top two;
duplicate top item;
push 9;
divide top two;
if (int) top is 0;
begin recursive area;
remove top;
push 9;
end recursive area;
goto top method;
output stack as chars;
:toggle double quote;
reverse stack;
capture stack as object with next;
push 13;
eval(stack);
capture stack as object with next;
push cosine of top;
toggle double quote;
:toggle double quote;
push inverse sine of top;
push inverse sine of top;
capture stack as object with next;
reverse stack;
push inverse tangent of top;
push 10;
push 11;
push length of stack;
push inverse cosine of top;
toggle double quote;
:toggle double quote;
push 14;
push length of stack;
g;
push 10;
push e;
toggle double quote;
:toggle double quote;
push 14;
push input item;
push 13;
reverse stack;
push input item;
B;
toggle double quote;
:toggle double quote;
reverse stack;
push 10;
push pi;
toggle double quote;
:toggle double quote;
push number of stacks;
push 14;
g;
capture stack as object with next;
B;
toggle double quote;
:push 4;
goto top method;
toggle double quote;
 ;
push 14;
push length of stack;
push 11;
flatten top two stacks;
capture stack as object with next;
duplicate top item;
toggle double quote;
:push 4;
goto top method;
toggle double quote;
 ;
push 14;
push length of stack;
push whether (int) top item is prime;
push input item;
reverse stack;
push tangent of top;
toggle double quote;
;
;
:toggle double quote;
reverse stack;
push 14;
push inverse sine of top;
capture stack as object with next;
push length of stack;
 ;
flatten top two stacks;
capture stack as object with next;
push number of stacks;
 ;
push 10;
factorize top item;
push 10;
push all ints between second to top and top;
toggle double quote;
```
> 6 votes

# Answer
## LittleLua - 160 Bytes (non-competitive)

```
r()P=I B="Bogey"r()Z={"Hole in one","Condor","Albatross","Eagle","Birdie","Par",B,"Double "..B,"Triple "..B,"Haha, you loser"}p(I=='1'a Z[1]or Z[I-P+6]or Z[10])

```

I'm relatively certain I did this right.

Accepts two integers, par and player's score.

The version of Little Lua that I used to make this was uploaded after this challenge was posted, but was not edited afterwards. It's relatively obvious from the code that nothing has been added to simplify this challenge

**LittleLua Info:**

Once I am satisfied with the built ins and the functionality of Little Lua, source will be available along with an infopage.

LittleLua V0.02
> 4 votes

# Answer
# JavaScript (ES6), ~~125~~ 124 bytes

```
p=>s=>"Hole in one,Condor,Albatross,Eagle,Birdie,Par,Bogey,Double Bogey,Triple Bogey".split`,`[s-1&&s-p+5]||"Haha you loser"

```

Assign to a variable e.g. `f=p=>s=>`, then call it like so: `f(6)(2)` Par first, then score.

May be able to be shortened by combining the `"Bogey"`s.
> 4 votes

# Answer
# Mouse-2002, ~~223~~ 207 bytes

Removing comments would likely help...

```
??s:p:s.1=["Hole in one"]s.p.4-=p.5>["Condor"]s.p.3-=p.4>["Albatross"]s.p.2-=p.3>["Eagle"]s.p.1-=["Birdie"]s.p.=["Par"]s.p.1+=["Bogey"]s.p.2+=["Double Bogey"]s.p.2+=["Double Bogey"]s.p.3+>["Haha you loser"]$

```

Ungolfed:

```
? ? s: p:

s. 1 = [
  "Hole in one"
]

~ 1
s. p. 4 - = p. 5 > [
  "Condor"
]

~ 2
s. p. 3 - = p. 4 > [
  "Albatross"
]

~ 3
s. p. 2 - = p. 3 > [
  "Eagle"
]

~ 4
s. p. 1 - = [
  "Birdie"
]

~ 5
s. p. = [
  "Par"
]

~ 6
s. p. 1 + = [
  "Bogey"
]

~ 7
s. p. 2 + = [
  "Double Bogey"
]

~ 8
s. p. 2 + = [
  "Double Bogey"
]

s. p. 3 + > [
  "Haha you loser"
]

$

```
> 3 votes

# Answer
## bash, ~~150~~ 136 bytes

```
b=Bogey
(($2<2))&&echo Hole in one||tail -$[$2-$1+5]<<<"Haha you loser
Triple $b
Double $b
$b
Par
Birdie
Eagle
Albatross
Condor"|head -1

```

Test run:

```
llama@llama:...code/shell/ppcg74767golfgolf$ for x in {1..11}; do bash golfgolf.sh 6 $x; done
Hole in one
Condor
Albatross
Eagle
Birdie
Par
Bogey
Double Bogey
Triple Bogey
Haha you loser
Haha you loser

```

Thanks to Dennis for 14 bytes!
> 2 votes

# Answer
# Python 2, ~~186~~ ~~179~~ 158 bytes

```
def c(s,p):a="Bogey";print["Condor","Albatross","Eagle","Birdie","Par",a,"Double "+a,"Triple "+a,"Haha you loser","Hole in one"][([[s-p+4,8][s-p>3],9][s==1])]

```

EDIT 1: added the missing "hole in one" case...

EDIT 2: golfed out some bytes (thanks to tac)
> 1 votes

# Answer
# Python 2.7, 226 bytes

```
p,s=input()
b="Bogey"
l={s==1:"Hole in one",5<p==s+4:"Condor",4<p==s+3:"Albatross",3<p==s+2:"Eagle",s==p-1:"Birdie",s==p:"Par",s==p+1:b,s==p+2:"Double "+b,s==p+3:"Triple "+b,s>p+3:"Haha you loser"}
for r in l:
 if r:print l[r]

```

Hard to come up with a short python code when you're late to the party, best I could think of.
> 1 votes

# Answer
# Haskell - 131 bytes (counting newline)

```
1%p="Hole in one"
s%p=lines"Condore\nAlbatros\nEagle\nBirdie\nPar\nBogey\nDouble Bogey\nTriple Bogey\nHaha you loser"!!min(4-p+s)8

```

`lines` is the only way I can think of to golf in a list of strings that have to contain spaces with access only to `Prelude` so stuck with two character delimiters.

Still, Haskell isn't usually this competitive (against general languages at least).
> 1 votes

# Answer
# C, 198 Bytes

```
main(){char s=8,p=4,m[]="Hole in one.Condor.Albatross.Eagle.Birdie.Par.Bogey.Double Bogey.Triple Bogey.Haha you loser",*t,*x,i=0;for(x=m;t=strtok(x,".");x=0,i++)if((s-1?s-p>3?9:s-p+5:0)==i)puts(t);}

```
> 1 votes

# Answer
# Japt, 97 bytes

```
`Ho¤  e
CÆBr
AlßNoss
Eag¤
Bir¹e
P
zD½e zTp¤ zHa y lo r`rz"Bogey
" ·g9m´V©V-U+6

```

Contains a bunch of unprintables. Test it online!

### How it works

```
`Ho¤  e\nCÆBr\nAlßNoss\nEag¤\nBir¹e\nP\nzD½e zTp¤ zHa y lo r`                        rz"Bogey\n" ·  g9m´ V© V-U+6
"Hole in one\nCondor\nAlbatross\nEagle\nBirdie\nPar\nzDouble zTriple zHaha you loser"rz"Bogey\n" qR g9m--V&&V-U+6

              // Implicit: U = par, V = score
"..."         // Take this long, compressed string.
rz"Bogey\n"   // Replace each instance of "z" with "Bogey\n".
qR            // Split at newlines.

--V&&V-U+6    // If V is 1, take 0; otherwise, take V-U+5.
9m            // Take the smaller of this and 9.
g             // Get the item at this index in the previous list of words.
              // Implicit output

```
> 0 votes

# Answer
## Python 2.7.2, 275 bytes

```
s=int(input())
p=int(input())
a="Bogey"
if s==1:b="Hole in one"
elif p-4==s:b="Condor"
elif p-3==s:b="Albatross"
elif p-2==s:b="Eagle"
elif p-1==s:b="Birdie"
elif p==s:b="Par"
elif p+1==s:b=a
elif p+2==s:b="Double "+a
elif p+3==s:b="Triple "+a
else:b="Haha you loser"
print b

```

Ungolfed/explained:

```
score = int(input())
par = int(input)
foo = "Bogey" # a shortcut for 3 of the outputs
if score == 1:
    output = "Hole in one"
elif par - 4 == score:
    output = "Condor"
...
elif par == score:
    output = "Par"
elif par + 1 == score:
    output = foo # See what I mean?
elif par + 2 == score:
    output = "Double " + foo # Huh? Huh?
elif par + 3 == score:
    output = "Triple " + foo # That makes 3.
else:
    output = "Haha you loser"
print output # Make sense?

```

My second answer, ironically both are in Python. Golfing tips appreciated!
> 0 votes

# Answer
# Python 2, ~~302~~ 284 bytes

```
i=input();j=input()
if j==1:print"Hole in one"
if(j==i-4)&(i>5):print"Condor"
if(j==i-3)&(i>4):print"Albatross"
if(j==i-2)&(i>3):print"Eagle"
if j==i-1:print"Birdie"
if j==i:print"Par"
if j>i:
 k=j-i
 if k<4:
  print["","Double ","Triple "][k-1]+"Bogey"
 else:
  print"Haha you loser"

```

If leading white space was allowed, that'd be 282 bytes:

```
i=input();j=input()
if j==1:print"Hole in one"
if(j==i-4)&(i>5):print"Condor"
if(j==i-3)&(i>4):print"Albatross"
if(j==i-2)&(i>3):print"Eagle"
if j==i-1:print"Birdie"
if j==i:print"Par"
if j>i:
 k=j-i
 if k<4:
  print["","Double","Triple"][k-1],"Bogey"
 else:
  print"Haha you loser"

```
> -1 votes

---
Tags: code-golf, kolmogorov-complexity
---


title: Circles of text

text:
# Question
Title: Circles of text
Find a way to make circles of a given radius using characters in a console. Please specify the font name and size. Also, please provide at least one example of the output.

For example:

**Input:**

```
3

```

**Output:**

```
   ******
 **      **
**        **
*          *
**        **
 **      **
   ******

```

...Well, something better looking than that "hand drawn" "circle" with a radius of 3.

---

**Bonus question:** Ellipses. :)

# Answer
> 5 votes
## Javascript (360)

```
function c(r){var f=1.83;var e=2*Math.PI/(r*r*r*r*r);var s=r*2+1;var g=Array(s);for(var i=0;i<s;i++){g[i]=Array(Math.round(s*f))};for(var i=0;i<=2*Math.PI;i+=e) {var x=Math.round(f*r*Math.cos(i)+f*r);var y=Math.round(r*Math.sin(i))+r;g[y][x]=1;}for(var j=0;j<g.length;j++){for(var i=0;i<g[j].length;i++)document.write((g[j][i]==1)?'*':' ');document.writeln()}}

```

http://jsfiddle.net/YssSb/3/ (`f` is a correction factor for the line-height / font-width ratio. If you use a square font setting, i.e., set line-height = font-size, you can set f=1 and get "square" circles. Or set `f` arbitrarily for ellipses.)

Output for 3 (interestingly enough, accidentally exactly the same shape as OP), 5, 15:

```
   ******
 **      **
**        **
*          *
**        **
 **      **
   ******

     *********
   ***       ****
 ***            **
**               **
*                 *
*                 *
*                 *
**               **
 ***            **
   ***       ****
     *********

                    ***************
               ******             ******
            ****                       *****
          ***                              ***
        ***                                  ***
      ***                                      ***
     **                                          **
    **                                            **
   **                                              **
  **                                                **
 **                                                  **
 *                                                    *
**                                                    **
*                                                      *
*                                                      *
*                                                      *
*                                                      *
*                                                      *
**                                                    **
 *                                                    *
 **                                                  **
  **                                                **
   **                                              **
    **                                            **
     **                                          **
      ***                                      ***
        ***                                  ***
          ***                              ***
            ****                       *****
               ******             ******
                    ***************

```

# Answer
> 6 votes
### a Bresenham circle in Scala (35)

The Bresenham - algorithm has 2 major points:

* works without sin/ cosin.
* you only calculate ¼\*½ circle, the other points are found by mirroring.

How to do it:

```
       2 1
     DCBABCD
   GFE  |  EFG
  IJ   y|----JI
 GJ     |    /JG
 F      |   / |F
DE      | r/  |ED
C       | /   | C
B 4     |/    | B 3
A       +-------A
B 4'          x B 3'
C               C
DE             ED
 F             F
 GJ           JG
  IJ         JI
   GFE     EFG
     DCBABCD
       2'1'

```
* We only calculate the numbers from A in the zenit to I.
    + Point I is at 45°, defined by x == y.
    + Ground zero is where the + is.
    + The A in the zenit is the point (x=0, y=r), r = radius.
    + To draw a closed circle we move clockwise (++x), which is to the right (x+=1) or down to the next point, (y-=1).
    + every point(x,y) on the circle is r away from the center. Pythagoras says, r²=x²+y².
    + This smells like square-root and equitations with 2 solutions, but beware!
    + we start at A and want to know, whether we paint next the point below or the point beneath to the right.
* we calculate for both points (x²+y²) and build for both the difference to r² (which stays of course constant).
    + since the difference can be negative, we take the abs from it.
    + then we look which point is closer to the result (r²), eo ipso smaller.
    + depending on that we draw the right or bottom neighbor.
* the so found point
    + 1 x, y gets mirrored
    + 2 -x, y to the left
    + 3 y, x at the diagonal
    + 4 -y, x from there to the left
* all those points get mirrored again to the south
    + 1' x, -y
    + 2' -x, -y
    + 3' y, -x
    + 4' -y, -x done.

This isn't code golf, but all those numbers at the top of the existing solutions made me think it is, so I spent useless time in golfing my solution. Therefore I added a useless number at the top too. It's 11 times Pi rounded.

```
object BresenhamCircle extends App {
    var count = 0
    val r = args(0).toInt
    // ratio > 1 means expansion in horizontal direction
    val ratio = args(1).toInt
    val field = ((0 to 2 * r).map (i=> (0 to 2 * r * ratio).map (j=> ' ').toArray)).toArray
    def square (x: Int, y: Int): Int = x * x + y * y
    def setPoint (x: Int, y: Int) {
        field (x)(y*ratio) = "Bresenham"(count)
        field (y)(x*ratio) = "Bresenham"(count)
    }
    def points (x: Int, y: Int)
    {
        setPoint (r + x, r + y)
        setPoint (r - x, r + y)
        setPoint (r + x, r - y)
        setPoint (r - x, r - y)
    }
    def bresenwalk () {
        var x = 0;
        var y = r;
        val rxr = r * r
        points (x, y);
        do
        {
            val (dx, dy) = { if (math.abs (rxr - square ((x+1), y)) < math.abs (rxr - square (x, (y-1))))
                (1, 0)
            else
                (0, -1)
            }
            count = (count + 1) % "Bresenham".length
            x += dx
            y += dy
            points (x, y)
        }while ((x <= y))
    }
    bresenwalk ()
    println (field.map (_.mkString ("")).mkString ("\n"))
}

```

The font-question is decided by sites webserver and your browser settings. Now, that I'm looking it is

```
'Droid Sans Mono',Consolas,Menlo,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New,monospace,serif

```

Font-size is 12px. Pretty useless information, if you ask me, but who does?

Bonus: ellipses and sample output:

The invocation is

```
    scala BresenhamCircle SIZE RATIO

```

for example

```
    scala BresenhamCircle 10 2
              s e r B r e s
          h n e           e n h
      e m a                   a m e
    e r                           r e
    m                               m
  h a                               a h
  n                                   n
s e                                   e s
e                                       e
r                                       r
B                                       B
r                                       r
e                                       e
s e                                   e s
  n                                   n
  h a                               a h
    m                               m
    e r                           r e
      e m a                   a m e
          h n e           e n h
              s e r B r e s

A ratio of 2 will print a circular shape for most fonts which happen to be about twice as tall than wide. To compensate for that, we widen by 2.
# As smaller value than 2 only 1 is available:

scala BresenhamCircle 6 1
    erBre
  aes   sea
 ah       ha
 e         e
es         se
r           r
B           B
r           r
es         se
 e         e
 ah       ha
  aes   sea
    erBre

# widening it has more freedom:

scala BresenhamCircle 12 5
                                             s    e    r    B    r    e    s
                              a    h    n    e                             e    n    h    a
                         B    m                                                           m    B
                    e    r                                                                     r    e
               e    s                                                                               s    e
          B    r                                                                                r    B
     a    m                                                                                m    a
     h                                                                                h
     n                                                                                n
s    e                                                                                e    s
e                                                                                e
r                                                                                r
B                                                                                B
r                                                                                r
e                                                                                e
s    e                                                                                e    s
     n                                                                                n
     h                                                                                h
     a    m                                                                                m    a
          B    r                                                                                r    B
               e    s                                                                               s    e
                    e    r                                                                     r    e
                         B    m                                                           m    B
                              a    h    n    e                             e    n    h    a
                                             s    e    r    B    r    e    s

```

I restricted the ratio parameter for Int to keep it simple, but it can easily be widened to allow floats.

# Answer
> 4 votes
## Python (172)

172 chars including the two mandatory newlines. Uses the Bresenham algorithm for conic curves (no divisions or multiplications); it only outputs circles for square fonts, but should be exempt from staircase effects (*ie.* always has the same width).

```
y=input();t=[y*[' ']for x in range(y)];x=0;y-=1;p=3-2*y
while x<=y:t[x][y]=t[y][x]='*';n,y=((x-y+1,y-1),(x,y))[p<0];p+=4*n+6;x+=1
for s in t[::-1]+t:print"".join(s[::-1]+s)

```

Not very pretty, but well, I thought I'd give it a shot.

```
  ****
 *    *
*      *
*      *
*      *
*      *
 *    *
  ****

          ********
       ***        ***
      *              *
     *                *
    *                  *
   *                    *
  *                      *
 *                        *
 *                        *
 *                        *
*                          *
*                          *
*                          *
*                          *
*                          *
*                          *
*                          *
*                          *
 *                        *
 *                        *
 *                        *
  *                      *
   *                    *
    *                  *
     *                *
      *              *
       ***        ***
          ********

```

**Edit**: typo, replaced *addition* with *division*.

# Answer
> 3 votes
## Python, 180 characters

This code makes circles if the font is square. It's pretty easy to modify to generate nominal ellipses if you know your font height/width ratio.

```
import math
r=input()
d=2*r+1
c=[' '*d]*d
for a in xrange(9*d):f=math.pi*a/r/9; x=int(r+r*math.sin(f)+.5);y=int(r+r*math.cos(f)+.5);c[y]=c[y][:x]+'*'+c[y][x+1:]
for s in c:print s

```

Examples:

```
4:
  *****
 **   **
**     **
*       *
*       *
*       *
**     **
 **   **
  *****

7:
     *****
   **     **
  **       **
 **         **
**           **
*             *
*             *
*             *
*             *
*             *
**           **
 **         **
  **       **
   **     **
     *****

```

# Answer
> 3 votes
## Perl (92)

I went for the “bonus question” and made it exploit the character aspect ratio to draw ellipses :)

```
($w)=@ARGV;for$x(-$w..$w){$p.=abs($x*$x+$_*$_-$w*$w)<$w?'*':$"for(-$w..$w);$p.=$/;}print $p;

```

Example outputs:

```
>perl circle.pl 3
  ***
 *   *
*     *
*     *
*     *
 *   *
  ***

>perl circle.pl 5
   *****
  *     *
 *       *
*         *
*         *
*         *
*         *
*         *
 *       *
  *     *
   *****

>perl circle.pl 8
      *****
    **     **
   *         *
  *           *
 *             *
 *             *
*               *
*               *
*               *
*               *
*               *
 *             *
 *             *
  *           *
   *         *
    **     **
      *****

```

# Answer
> 3 votes
## Haskell (~~112~~ 109)

```
g n=map(zipWith(?)f.repeat)f where x?y|abs(x^2+y^2-n^2)<n='*'|0<1=' ';f=[-n..n]
main=interact$unlines.g.read

```

This works by checking, whether x² + y² - r² \< n for all points. All points for which this is true are stars, all others are blanks.

### Examples:

```
$ echo 3 | runhaskell circ.hs
  ***
 *   *
*     *
*     *
*     *
 *   *
  ***
$ echo 10 | runhaskell circ.hs
       *******
     **       **
    *           *
   *             *
  *               *
 *                 *
 *                 *
*                   *
*                   *
*                   *
*                   *
*                   *
*                   *
*                   *
 *                 *
 *                 *
  *               *
   *             *
    *           *
     **       **
       *******

```

See here for big example: http://www.ideone.com/t042u

# Answer
> 0 votes
# C, 127 Bytes, Font name: Arial Super Bold

```
#include<math.h>
main(){int r=10,c=r*2+1,q=c*c,d;for(;q--;)d=hypot(r-q%c,r-q/c),printf("%c%s",d>r-4&&d<=r?42:32,q%c?"":"\n");}

```

Result:

```
      *********
    *************
   ***************
  *****************
 ******       ******
 *****         *****
*****           *****
****             ****
****             ****
****             ****
****             ****
****             ****
****             ****
****             ****
*****           *****
 *****         *****
 ******       ******
  *****************
   ***************
    *************
      *********

```

---
Tags: code-challenge, ascii-art
---


# Question
Title: Hard code golf: Regex for divisibility by 7
Matthias Goergens has a 25,604-character (down from the original 63,993-character) regex to match numbers divisible by 7, but that includes a lot of fluff: redundant parentheses, distribution (`xx|xy|yx|yy` rather than `[xy]{2}`) and other issues, though I'm sure a fresh start would be helpful in saving space. How small can this be made?

Any reasonable variety of regular expressions are allowed, but no executable code in the regex.

The regular expression should match all strings containing the decimal representation of a number divisible by 7 and no others. Extra credit for a regex that does not allow initial 0s.

# Answer
> 28 votes
# 10791 characters, leading zeros allowed

```
(0|7|46*[29]|(1|8|46*3|(2|9|46*4)(3|56*4)*(2|9|56*3))(5|[18]6*3|(6|[18]6*4)(3|56*4)*(2|9|56*3))*(4|63*[18]|(1|8|63*5)(6|43*5)*(2|9|43*[18]))|(2|9|46*4)(3|56*4)*(1|8|56*[29])|(3|46*5|(1|8|46*3|(2|9|46*4)(3|56*4)*(2|9|56*3))(5|[18]6*3|(6|[18]6*4)(3|56*4)*(2|9|56*3))*(0|7|63*4|(1|8|63*5)(6|43*5)*(5|43*4))|(2|9|46*4)(3|56*4)*(4|56*5)|(5|46*[07]|(1|8|46*3|(2|9|46*4)(3|56*4)*(2|9|56*3))(5|[18]6*3|(6|[18]6*4)(3|56*4)*(2|9|56*3))*(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))|(2|9|46*4)(3|56*4)*(6|56*[07]))(4|36*[07]|(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3))(5|[18]6*3|(6|[18]6*4)(3|56*4)*(2|9|56*3))*(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))|(1|8|36*4)(3|56*4)*(6|56*[07]))*(2|9|36*5|(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3))(5|[18]6*3|(6|[18]6*4)(3|56*4)*(2|9|56*3))*(0|7|63*4|(1|8|63*5)(6|43*5)*(5|43*4))|(1|8|36*4)(3|56*4)*(4|56*5)))(1|8|(0|7|[29]6*4)(3|56*4)*(4|56*5)|[29]6*5|(3|[07]3*6|(2|9|[07]3*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(2|9|36*5|(1|8|36*4)(3|56*4)*(4|56*5))|(6|(0|7|[29]6*4)(3|56*4)*(2|9|56*3)|[29]6*3|(3|[07]3*6|(2|9|[07]3*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3)))(5|[18]6*3|(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3))|(6|[18]6*4)(3|56*4)*(2|9|56*3))*(0|7|63*4|(1|8|63*5)(6|43*5)*(5|43*4)|(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(2|9|36*5|(1|8|36*4)(3|56*4)*(4|56*5))))*(5|34*6|(0|7|34*[18]|(2|9|34*3)(6|[07]4*3)*(4|[07]4*[18]))(3|56*4|(6|56*[07])(4|36*[07])*(1|8|36*4))*(1|8|64*6|(5|64*3)(6|[07]4*3)*(2|9|[07]4*6))|(2|9|34*3)(6|[07]4*3)*(2|9|[07]4*6)|(6|(0|7|[29]6*4)(3|56*4)*(2|9|56*3)|[29]6*3|(3|[07]3*6|(2|9|[07]3*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3)))(5|[18]6*3|(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3))|(6|[18]6*4)(3|56*4)*(2|9|56*3))*(4|63*[18]|(1|8|63*5)(6|43*5)*(2|9|43*[18])|(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(6|36*[29]|(1|8|36*4)(3|56*4)*(1|8|56*[29]))))|(5|46*[07]|(1|8|46*3|(2|9|46*4)(3|56*4)*(2|9|56*3))(5|[18]6*3|(6|[18]6*4)(3|56*4)*(2|9|56*3))*(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))|(2|9|46*4)(3|56*4)*(6|56*[07]))(4|36*[07]|(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3))(5|[18]6*3|(6|[18]6*4)(3|56*4)*(2|9|56*3))*(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))|(1|8|36*4)(3|56*4)*(6|56*[07]))*(6|36*[29]|(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3))(5|[18]6*3|(6|[18]6*4)(3|56*4)*(2|9|56*3))*(4|63*[18]|(1|8|63*5)(6|43*5)*(2|9|43*[18]))|(1|8|36*4)(3|56*4)*(1|8|56*[29]))|(6|46*[18]|(1|8|46*3|(2|9|46*4)(3|56*4)*(2|9|56*3))(5|[18]6*3|(6|[18]6*4)(3|56*4)*(2|9|56*3))*(3|63*[07]|(1|8|63*5)(6|43*5)*(1|8|43*[07]))|(2|9|46*4)(3|56*4)*(0|7|56*[18])|(3|46*5|(1|8|46*3|(2|9|46*4)(3|56*4)*(2|9|56*3))(5|[18]6*3|(6|[18]6*4)(3|56*4)*(2|9|56*3))*(0|7|63*4|(1|8|63*5)(6|43*5)*(5|43*4))|(2|9|46*4)(3|56*4)*(4|56*5)|(5|46*[07]|(1|8|46*3|(2|9|46*4)(3|56*4)*(2|9|56*3))(5|[18]6*3|(6|[18]6*4)(3|56*4)*(2|9|56*3))*(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))|(2|9|46*4)(3|56*4)*(6|56*[07]))(4|36*[07]|(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3))(5|[18]6*3|(6|[18]6*4)(3|56*4)*(2|9|56*3))*(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))|(1|8|36*4)(3|56*4)*(6|56*[07]))*(2|9|36*5|(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3))(5|[18]6*3|(6|[18]6*4)(3|56*4)*(2|9|56*3))*(0|7|63*4|(1|8|63*5)(6|43*5)*(5|43*4))|(1|8|36*4)(3|56*4)*(4|56*5)))(1|8|(0|7|[29]6*4)(3|56*4)*(4|56*5)|[29]6*5|(3|[07]3*6|(2|9|[07]3*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(2|9|36*5|(1|8|36*4)(3|56*4)*(4|56*5))|(6|(0|7|[29]6*4)(3|56*4)*(2|9|56*3)|[29]6*3|(3|[07]3*6|(2|9|[07]3*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3)))(5|[18]6*3|(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3))|(6|[18]6*4)(3|56*4)*(2|9|56*3))*(0|7|63*4|(1|8|63*5)(6|43*5)*(5|43*4)|(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(2|9|36*5|(1|8|36*4)(3|56*4)*(4|56*5))))*(4|34*5|(0|7|34*[18]|(2|9|34*3)(6|[07]4*3)*(4|[07]4*[18]))(3|56*4|(6|56*[07])(4|36*[07])*(1|8|36*4))*(0|7|64*5|(5|64*3)(6|[07]4*3)*(1|8|[07]4*5))|(2|9|34*3)(6|[07]4*3)*(1|8|[07]4*5)|(6|(0|7|[29]6*4)(3|56*4)*(2|9|56*3)|[29]6*3|(3|[07]3*6|(2|9|[07]3*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3)))(5|[18]6*3|(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3))|(6|[18]6*4)(3|56*4)*(2|9|56*3))*(3|63*[07]|(1|8|63*5)(6|43*5)*(1|8|43*[07])|(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(5|36*[18]|(1|8|36*4)(3|56*4)*(0|7|56*[18]))))|(5|46*[07]|(1|8|46*3|(2|9|46*4)(3|56*4)*(2|9|56*3))(5|[18]6*3|(6|[18]6*4)(3|56*4)*(2|9|56*3))*(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))|(2|9|46*4)(3|56*4)*(6|56*[07]))(4|36*[07]|(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3))(5|[18]6*3|(6|[18]6*4)(3|56*4)*(2|9|56*3))*(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))|(1|8|36*4)(3|56*4)*(6|56*[07]))*(5|36*[18]|(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3))(5|[18]6*3|(6|[18]6*4)(3|56*4)*(2|9|56*3))*(3|63*[07]|(1|8|63*5)(6|43*5)*(1|8|43*[07]))|(1|8|36*4)(3|56*4)*(0|7|56*[18])))(2|9|53*[07]|(0|7|53*5)(6|43*5)*(1|8|43*[07])|(1|8|53*6|(0|7|53*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(5|36*[18]|(1|8|36*4)(3|56*4)*(0|7|56*[18]))|(4|[07]6*3|(1|8|53*6|(0|7|53*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3))|(5|[07]6*4)(3|56*4)*(2|9|56*3))(5|[18]6*3|(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3))|(6|[18]6*4)(3|56*4)*(2|9|56*3))*(3|63*[07]|(1|8|63*5)(6|43*5)*(1|8|43*[07])|(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(5|36*[18]|(1|8|36*4)(3|56*4)*(0|7|56*[18])))|(6|53*4|(0|7|53*5)(6|43*5)*(5|43*4)|(1|8|53*6|(0|7|53*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(2|9|36*5|(1|8|36*4)(3|56*4)*(4|56*5))|(4|[07]6*3|(1|8|53*6|(0|7|53*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3))|(5|[07]6*4)(3|56*4)*(2|9|56*3))(5|[18]6*3|(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3))|(6|[18]6*4)(3|56*4)*(2|9|56*3))*(0|7|63*4|(1|8|63*5)(6|43*5)*(5|43*4)|(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(2|9|36*5|(1|8|36*4)(3|56*4)*(4|56*5))))(1|8|(0|7|[29]6*4)(3|56*4)*(4|56*5)|[29]6*5|(3|[07]3*6|(2|9|[07]3*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(2|9|36*5|(1|8|36*4)(3|56*4)*(4|56*5))|(6|(0|7|[29]6*4)(3|56*4)*(2|9|56*3)|[29]6*3|(3|[07]3*6|(2|9|[07]3*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3)))(5|[18]6*3|(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3))|(6|[18]6*4)(3|56*4)*(2|9|56*3))*(0|7|63*4|(1|8|63*5)(6|43*5)*(5|43*4)|(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(2|9|36*5|(1|8|36*4)(3|56*4)*(4|56*5))))*(4|34*5|(0|7|34*[18]|(2|9|34*3)(6|[07]4*3)*(4|[07]4*[18]))(3|56*4|(6|56*[07])(4|36*[07])*(1|8|36*4))*(0|7|64*5|(5|64*3)(6|[07]4*3)*(1|8|[07]4*5))|(2|9|34*3)(6|[07]4*3)*(1|8|[07]4*5)|(6|(0|7|[29]6*4)(3|56*4)*(2|9|56*3)|[29]6*3|(3|[07]3*6|(2|9|[07]3*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3)))(5|[18]6*3|(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3))|(6|[18]6*4)(3|56*4)*(2|9|56*3))*(3|63*[07]|(1|8|63*5)(6|43*5)*(1|8|43*[07])|(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(5|36*[18]|(1|8|36*4)(3|56*4)*(0|7|56*[18])))))*(3|53*[18]|(0|7|53*5)(6|43*5)*(2|9|43*[18])|(1|8|53*6|(0|7|53*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(6|36*[29]|(1|8|36*4)(3|56*4)*(1|8|56*[29]))|(4|[07]6*3|(1|8|53*6|(0|7|53*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3))|(5|[07]6*4)(3|56*4)*(2|9|56*3))(5|[18]6*3|(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3))|(6|[18]6*4)(3|56*4)*(2|9|56*3))*(4|63*[18]|(1|8|63*5)(6|43*5)*(2|9|43*[18])|(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(6|36*[29]|(1|8|36*4)(3|56*4)*(1|8|56*[29])))|(6|53*4|(0|7|53*5)(6|43*5)*(5|43*4)|(1|8|53*6|(0|7|53*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(2|9|36*5|(1|8|36*4)(3|56*4)*(4|56*5))|(4|[07]6*3|(1|8|53*6|(0|7|53*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3))|(5|[07]6*4)(3|56*4)*(2|9|56*3))(5|[18]6*3|(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3))|(6|[18]6*4)(3|56*4)*(2|9|56*3))*(0|7|63*4|(1|8|63*5)(6|43*5)*(5|43*4)|(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(2|9|36*5|(1|8|36*4)(3|56*4)*(4|56*5))))(1|8|(0|7|[29]6*4)(3|56*4)*(4|56*5)|[29]6*5|(3|[07]3*6|(2|9|[07]3*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(2|9|36*5|(1|8|36*4)(3|56*4)*(4|56*5))|(6|(0|7|[29]6*4)(3|56*4)*(2|9|56*3)|[29]6*3|(3|[07]3*6|(2|9|[07]3*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3)))(5|[18]6*3|(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3))|(6|[18]6*4)(3|56*4)*(2|9|56*3))*(0|7|63*4|(1|8|63*5)(6|43*5)*(5|43*4)|(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(2|9|36*5|(1|8|36*4)(3|56*4)*(4|56*5))))*(5|34*6|(0|7|34*[18]|(2|9|34*3)(6|[07]4*3)*(4|[07]4*[18]))(3|56*4|(6|56*[07])(4|36*[07])*(1|8|36*4))*(1|8|64*6|(5|64*3)(6|[07]4*3)*(2|9|[07]4*6))|(2|9|34*3)(6|[07]4*3)*(2|9|[07]4*6)|(6|(0|7|[29]6*4)(3|56*4)*(2|9|56*3)|[29]6*3|(3|[07]3*6|(2|9|[07]3*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3)))(5|[18]6*3|(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(0|7|36*3|(1|8|36*4)(3|56*4)*(2|9|56*3))|(6|[18]6*4)(3|56*4)*(2|9|56*3))*(4|63*[18]|(1|8|63*5)(6|43*5)*(2|9|43*[18])|(2|9|63*6|(1|8|63*5)(6|43*5)*(0|7|43*6))(4|36*[07]|(1|8|36*4)(3|56*4)*(6|56*[07]))*(6|36*[29]|(1|8|36*4)(3|56*4)*(1|8|56*[29]))))))+

```

# 10795 characters, leading zeros forbidden

`0|((foo)0*)+`, where the above regex is `(0|foo)+`.

# Explanation

Numbers divisible by 7 are matched by the obvious finite automaton with 7 states Q = {0, …, 6}, initial and final state 0, and transitions d: i ↦ (10i + d) mod 7. I converted this finite automaton into a regular expression, using recursion on the set of allowed intermediate states:

Given i, j ∈ Q and S ⊆ Q, let f(i, S, j) be a regular expression that matches all automaton paths from i to j using only intermediate states within S. Then,

f(i, ∅, j) = (j − 10i) mod 7,

f(i, S ∪ {k}, j) = f(i, S, j) ∣ f(i, S, k) f(k, S, k)\* f(k, S, j).

I used dynamic programming to choose k so as to minimize the length of the resulting expression.

# Answer
> 104 votes
# 13,755 12,699 12,731 Characters

This regex does not reject leading zero.

```
(0|7|[18]5*4|(2|9|[18]5*6)(3|[29]5*6)*(1|8|[29]5*4)|(3|[18]5*[07]|(2|9|[18]5*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(5|65*4|(0|7|65*6)(3|[29]5*6)*(1|8|[29]5*4))|(4|[18]5*[18]|(2|9|[18]5*6)(3|[29]5*6)*(5|[29]5*[18])|(3|[18]5*[07]|(2|9|[18]5*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))(6|35*[18]|(4|35*6)(3|[29]5*6)*(5|[29]5*[18])|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))*(2|9|35*4|(4|35*6)(3|[29]5*6)*(1|8|[29]5*4)|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(5|65*4|(0|7|65*6)(3|[29]5*6)*(1|8|[29]5*4)))|(5|[18]5*[29]|(2|9|[18]5*6)(3|[29]5*6)*(6|[29]5*[29])|(3|[18]5*[07]|(2|9|[18]5*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(3|65*[29]|(0|7|65*6)(3|[29]5*6)*(6|[29]5*[29]))|(4|[18]5*[18]|(2|9|[18]5*6)(3|[29]5*6)*(5|[29]5*[18])|(3|[18]5*[07]|(2|9|[18]5*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))(6|35*[18]|(4|35*6)(3|[29]5*6)*(5|[29]5*[18])|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))*(0|7|35*[29]|(4|35*6)(3|[29]5*6)*(6|[29]5*[29])|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(3|65*[29]|(0|7|65*6)(3|[29]5*6)*(6|[29]5*[29]))))(4|[07]5*[29]|(1|8|[07]5*6)(3|[29]5*6)*(6|[29]5*[29])|(2|9|[07]5*[07]|(1|8|[07]5*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(3|65*[29]|(0|7|65*6)(3|[29]5*6)*(6|[29]5*[29]))|(3|[07]5*[18]|(1|8|[07]5*6)(3|[29]5*6)*(5|[29]5*[18])|(2|9|[07]5*[07]|(1|8|[07]5*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))(6|35*[18]|(4|35*6)(3|[29]5*6)*(5|[29]5*[18])|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))*(0|7|35*[29]|(4|35*6)(3|[29]5*6)*(6|[29]5*[29])|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(3|65*[29]|(0|7|65*6)(3|[29]5*6)*(6|[29]5*[29]))))*(6|[07]5*4|(1|8|[07]5*6)(3|[29]5*6)*(1|8|[29]5*4)|(2|9|[07]5*[07]|(1|8|[07]5*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(5|65*4|(0|7|65*6)(3|[29]5*6)*(1|8|[29]5*4))|(3|[07]5*[18]|(1|8|[07]5*6)(3|[29]5*6)*(5|[29]5*[18])|(2|9|[07]5*[07]|(1|8|[07]5*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))(6|35*[18]|(4|35*6)(3|[29]5*6)*(5|[29]5*[18])|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))*(2|9|35*4|(4|35*6)(3|[29]5*6)*(1|8|[29]5*4)|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(5|65*4|(0|7|65*6)(3|[29]5*6)*(1|8|[29]5*4))))|(6|[18]5*3|(2|9|[18]5*6)(3|[29]5*6)*(0|7|[29]5*3)|(3|[18]5*[07]|(2|9|[18]5*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(4|65*3|(0|7|65*6)(3|[29]5*6)*(0|7|[29]5*3))|(4|[18]5*[18]|(2|9|[18]5*6)(3|[29]5*6)*(5|[29]5*[18])|(3|[18]5*[07]|(2|9|[18]5*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))(6|35*[18]|(4|35*6)(3|[29]5*6)*(5|[29]5*[18])|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))*(1|8|35*3|(4|35*6)(3|[29]5*6)*(0|7|[29]5*3)|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(4|65*3|(0|7|65*6)(3|[29]5*6)*(0|7|[29]5*3)))|(5|[18]5*[29]|(2|9|[18]5*6)(3|[29]5*6)*(6|[29]5*[29])|(3|[18]5*[07]|(2|9|[18]5*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(3|65*[29]|(0|7|65*6)(3|[29]5*6)*(6|[29]5*[29]))|(4|[18]5*[18]|(2|9|[18]5*6)(3|[29]5*6)*(5|[29]5*[18])|(3|[18]5*[07]|(2|9|[18]5*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))(6|35*[18]|(4|35*6)(3|[29]5*6)*(5|[29]5*[18])|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))*(0|7|35*[29]|(4|35*6)(3|[29]5*6)*(6|[29]5*[29])|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(3|65*[29]|(0|7|65*6)(3|[29]5*6)*(6|[29]5*[29]))))(4|[07]5*[29]|(1|8|[07]5*6)(3|[29]5*6)*(6|[29]5*[29])|(2|9|[07]5*[07]|(1|8|[07]5*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(3|65*[29]|(0|7|65*6)(3|[29]5*6)*(6|[29]5*[29]))|(3|[07]5*[18]|(1|8|[07]5*6)(3|[29]5*6)*(5|[29]5*[18])|(2|9|[07]5*[07]|(1|8|[07]5*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))(6|35*[18]|(4|35*6)(3|[29]5*6)*(5|[29]5*[18])|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))*(0|7|35*[29]|(4|35*6)(3|[29]5*6)*(6|[29]5*[29])|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(3|65*[29]|(0|7|65*6)(3|[29]5*6)*(6|[29]5*[29]))))*(5|[07]5*3|(1|8|[07]5*6)(3|[29]5*6)*(0|7|[29]5*3)|(2|9|[07]5*[07]|(1|8|[07]5*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(4|65*3|(0|7|65*6)(3|[29]5*6)*(0|7|[29]5*3))|(3|[07]5*[18]|(1|8|[07]5*6)(3|[29]5*6)*(5|[29]5*[18])|(2|9|[07]5*[07]|(1|8|[07]5*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))(6|35*[18]|(4|35*6)(3|[29]5*6)*(5|[29]5*[18])|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))*(1|8|35*3|(4|35*6)(3|[29]5*6)*(0|7|[29]5*3)|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(4|65*3|(0|7|65*6)(3|[29]5*6)*(0|7|[29]5*3)))))(2|9|45*3|(5|45*6)(3|[29]5*6)*(0|7|[29]5*3)|(6|45*[07]|(5|45*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(4|65*3|(0|7|65*6)(3|[29]5*6)*(0|7|[29]5*3))|(0|7|45*[18]|(5|45*6)(3|[29]5*6)*(5|[29]5*[18])|(6|45*[07]|(5|45*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))(6|35*[18]|(4|35*6)(3|[29]5*6)*(5|[29]5*[18])|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))*(1|8|35*3|(4|35*6)(3|[29]5*6)*(0|7|[29]5*3)|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(4|65*3|(0|7|65*6)(3|[29]5*6)*(0|7|[29]5*3)))|(1|8|45*[29]|(5|45*6)(3|[29]5*6)*(6|[29]5*[29])|(6|45*[07]|(5|45*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(3|65*[29]|(0|7|65*6)(3|[29]5*6)*(6|[29]5*[29]))|(0|7|45*[18]|(5|45*6)(3|[29]5*6)*(5|[29]5*[18])|(6|45*[07]|(5|45*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))(6|35*[18]|(4|35*6)(3|[29]5*6)*(5|[29]5*[18])|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))*(0|7|35*[29]|(4|35*6)(3|[29]5*6)*(6|[29]5*[29])|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(3|65*[29]|(0|7|65*6)(3|[29]5*6)*(6|[29]5*[29]))))(4|[07]5*[29]|(1|8|[07]5*6)(3|[29]5*6)*(6|[29]5*[29])|(2|9|[07]5*[07]|(1|8|[07]5*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(3|65*[29]|(0|7|65*6)(3|[29]5*6)*(6|[29]5*[29]))|(3|[07]5*[18]|(1|8|[07]5*6)(3|[29]5*6)*(5|[29]5*[18])|(2|9|[07]5*[07]|(1|8|[07]5*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))(6|35*[18]|(4|35*6)(3|[29]5*6)*(5|[29]5*[18])|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))*(0|7|35*[29]|(4|35*6)(3|[29]5*6)*(6|[29]5*[29])|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(3|65*[29]|(0|7|65*6)(3|[29]5*6)*(6|[29]5*[29]))))*(5|[07]5*3|(1|8|[07]5*6)(3|[29]5*6)*(0|7|[29]5*3)|(2|9|[07]5*[07]|(1|8|[07]5*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(4|65*3|(0|7|65*6)(3|[29]5*6)*(0|7|[29]5*3))|(3|[07]5*[18]|(1|8|[07]5*6)(3|[29]5*6)*(5|[29]5*[18])|(2|9|[07]5*[07]|(1|8|[07]5*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))(6|35*[18]|(4|35*6)(3|[29]5*6)*(5|[29]5*[18])|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))*(1|8|35*3|(4|35*6)(3|[29]5*6)*(0|7|[29]5*3)|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(4|65*3|(0|7|65*6)(3|[29]5*6)*(0|7|[29]5*3)))))*(3|45*4|(5|45*6)(3|[29]5*6)*(1|8|[29]5*4)|(6|45*[07]|(5|45*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(5|65*4|(0|7|65*6)(3|[29]5*6)*(1|8|[29]5*4))|(0|7|45*[18]|(5|45*6)(3|[29]5*6)*(5|[29]5*[18])|(6|45*[07]|(5|45*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))(6|35*[18]|(4|35*6)(3|[29]5*6)*(5|[29]5*[18])|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))*(2|9|35*4|(4|35*6)(3|[29]5*6)*(1|8|[29]5*4)|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(5|65*4|(0|7|65*6)(3|[29]5*6)*(1|8|[29]5*4)))|(1|8|45*[29]|(5|45*6)(3|[29]5*6)*(6|[29]5*[29])|(6|45*[07]|(5|45*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(3|65*[29]|(0|7|65*6)(3|[29]5*6)*(6|[29]5*[29]))|(0|7|45*[18]|(5|45*6)(3|[29]5*6)*(5|[29]5*[18])|(6|45*[07]|(5|45*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))(6|35*[18]|(4|35*6)(3|[29]5*6)*(5|[29]5*[18])|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))*(0|7|35*[29]|(4|35*6)(3|[29]5*6)*(6|[29]5*[29])|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(3|65*[29]|(0|7|65*6)(3|[29]5*6)*(6|[29]5*[29]))))(4|[07]5*[29]|(1|8|[07]5*6)(3|[29]5*6)*(6|[29]5*[29])|(2|9|[07]5*[07]|(1|8|[07]5*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(3|65*[29]|(0|7|65*6)(3|[29]5*6)*(6|[29]5*[29]))|(3|[07]5*[18]|(1|8|[07]5*6)(3|[29]5*6)*(5|[29]5*[18])|(2|9|[07]5*[07]|(1|8|[07]5*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))(6|35*[18]|(4|35*6)(3|[29]5*6)*(5|[29]5*[18])|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))*(0|7|35*[29]|(4|35*6)(3|[29]5*6)*(6|[29]5*[29])|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(3|65*[29]|(0|7|65*6)(3|[29]5*6)*(6|[29]5*[29]))))*(6|[07]5*4|(1|8|[07]5*6)(3|[29]5*6)*(1|8|[29]5*4)|(2|9|[07]5*[07]|(1|8|[07]5*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(5|65*4|(0|7|65*6)(3|[29]5*6)*(1|8|[29]5*4))|(3|[07]5*[18]|(1|8|[07]5*6)(3|[29]5*6)*(5|[29]5*[18])|(2|9|[07]5*[07]|(1|8|[07]5*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))(6|35*[18]|(4|35*6)(3|[29]5*6)*(5|[29]5*[18])|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(2|9|65*[18]|(0|7|65*6)(3|[29]5*6)*(5|[29]5*[18])))*(2|9|35*4|(4|35*6)(3|[29]5*6)*(1|8|[29]5*4)|(5|35*[07]|(4|35*6)(3|[29]5*6)*(4|[29]5*[07]))(1|8|65*[07]|(0|7|65*6)(3|[29]5*6)*(4|[29]5*[07]))*(5|65*4|(0|7|65*6)(3|[29]5*6)*(1|8|[29]5*4))))))*

```

This is tested with The Regex Coach.

# How We Get There

The Regex above produced by first constructing a DFA which would accept the input we want (decimals divisible by 7) and then converting to a Regular Expression and fixing the notation

To understand this, it helps to first make a DFA which accepts the following language:

```
L = {w | w is a binary representation of an integer divisible by 7 }

```

That is, it will 'match' binary numbers that are divisible by 7.

The DFA looks like this:

## How it works

You keep a current value `A` that represents the value of the bits the DFA has read. When you read a `0` then `A = 2*A` and when you read a `1` `A = 2*A + 1`. At each step you calculate `A mod 7` then you go to the state that represents the answer.

### So a test run

We're reading in `10101` which is the binary representation for 21 in decimal.

1. We start at state `q0`, currently `A=0`
2. We read a `1`, from the 'rule' above `A = 2*A + 1` so `A = 1`. `A mod 7 = 1` so we move to state `q1`
3. We read a `0`, `A = 2*A = 2`, `A mod 7 = 2` so we move to `q2`
4. Read a `1`, `A = 2*A + 1 = 5`, `A mod 7 = 5`, move to `q5`
5. Read a `0`, `A = 2*A = 10`, `A mod 7 = 3`, move to `q3`
6. Read a `1`, `A = 2*A + 1 = 21`, `A mod 7 = 0`, move to `q0`
7. The input is accepted so the number `10101` is divisible by 7!

Converting the DFA to a Regular Expression is a tricky task so I got JFLAP to do it for me, producing the following:

```
(0|111|100((1|00)0)*011|(101|100((1|00)0)*(1|00)1)(1((1|00)0)*(1|00)1)*(01|1((1|00)0)*011)|(110|100((1|00)0)*010|(101|100((1|00)0)*(1|00)1)(1((1|00)0)*(1|00)1)*(00|1((1|00)0)*010))(1|0(1((1|00)0)*(1|00)1)*(00|1((1|00)0)*010))*0(1((1|00)0)*(1|00)1)*(01|1((1|00)0)*011))*

```

# For Decimal Numbers

The process is much the same:

I constructed a DFA which accepts the language:

```
L = {w | w is a decimal number that is divisible by 7}

```

Here's the DFA:

The logic is similar, same number of states just many more transitions to handle all the extra digits decimal numbers bring.

Now the rule to change `A` at each step is: when you read a decimal digit `n`: `A = 10*A + n`. Then again you just `mod` `A` by 7 and go to the next state.

## Revisions

### Revision 5

The above regular expression now rejects numbers leading zeros - apart from zero itself of course.

This makes the DFA slightly different, basically you branch off from the initial node when you read the first zero. Reading another zero puts you into an infinite loop on the branched state. I **haven't** fixed the diagram to show this.

### Revision 7

Did some "metaregex" and shortened my regex by replacing some of the unions with character classes.

### Revision 10 and 11 (by nhahtdh)

The author's modification to reject leading zero is incorrect. It makes the regexes fail to match valid numbers, such as 1110 (decimal = 14) in the case of binary regex, and 70 in the case of decimal regex. These revision reverts the modification, and consequently, allows arbitrary leading zeros and empty string to match.

This revision increases the size of the decimal regex, since it corrects a bug in the original regex, caused by a missing an edge (9) from state 5 to state 3 in the original DFA.

# Answer
> 42 votes
# .NET regex, ~~119~~ ~~118~~ 105 bytes

```
^(?>(?=[1468](?<4>)|)(?=[2569](?<4>){2}|)([3-6]()|\d)((?<-2>)(){3}|){7}((?<-4>){7}|(?<2-4>)|){9})+$(?!\2)

```

111 characters disallowing initial 0s:

```
^(?!0.)(?>(?=[1468](?<4>)|)(?=[2569](?<4>){2}|)([3-6]()|\d)((?<-2>)(){3}|){7}((?<-4>){7}|(?<2-4>)|){9})+$(?!\2)

```

113 characters disallowing initial 0s and supporting negative numbers:

```
^-?(?>(?=[1468](?<4>)|)(?=[2569](?<4>){2}|)([3-6]()|\d)((?<-2>)(){3}|){7}((?<-4>){7}|(?<2-4>)|){9})+$(?!\2)

```

Try it here.

### Explanation (of the previous version)

It uses the techniques used by various answers in this question: Cops and Robbers: Reverse Regex Golf. The .NET regex has a feature called balancing group, which can be used for doing arithmetic. `(?<a>)` pushes a group `a`. `(?<-a>)` pops that and doesn't match if there isn't a group `a` matched before.

* `(?>...)` Match and don't backtrack later. So it will always match only the first matched alternative.
* `((?<-t>)(){3}|){6}` Multiply the number of group t by 3. Save the result in the number of group 2.
* `(?=[1468](?<2>)|)(?=[2569](?<2>){2}|)([3-6](?<2>){3}|\d)` Match a number, and that number of group 2.
* `((?<-2>){7}|){3}` Remove group 2 a multiple of 7 times.
* `((?<t-2>)|){6}` Remove group 2 and match the same number of group t.
* `$(?(t)a)` If there is still a group t matched, match `a` after the end of string, which is impossible.

I thought this 103 byte version should also work, but didn't find a workaround of the bug in the compiler.

```
^(?(?(?((?<3>){2}[2569]|)([3-6])?((?<-1>)(){3}|){7})(?<3>[1468])?((?<-3>){7}|(?<1-3>)|){9})\d)+$(?(1)a)

```

# Answer
> 32 votes
## 468 characters

Ruby's regex flavor allows recursion (although it's sort of cheating), so it is straightforward to implement a DFA that recognizes numbers divisible by 7 using that. Each named group corresponds to a state, and each branch in the alternations consumes one digit and then jumps to the appropriate state. If the end of the number is reached, the regex matches only if the engine is in the "A" group, otherwise it fails.

It recognizes leading zeros.

```
(?!$)(?>(|(?<B>4\g<A>|5\g<B>|6\g<C>|[07]\g<D>|[18]\g<E>|[29]\g<F>|3\g<G>))(|(?<C>[18]\g<A>|[29]\g<B>|3\g<C>|4\g<D>|5\g<E>|6\g<F>|[07]\g<G>))(|(?<D>5\g<A>|6\g<B>|[07]\g<C>|[18]\g<D>|[29]\g<E>|3\g<F>|4\g<G>))(|(?<E>[29]\g<A>|3\g<B>|4\g<C>|5\g<D>|6\g<E>|[07]\g<F>|[18]\g<G>))(|(?<F>6\g<A>|[07]\g<B>|[18]\g<C>|[29]\g<D>|3\g<E>|4\g<F>|5\g<G>))(|(?<G>3\g<A>|4\g<B>|5\g<C>|6\g<D>|[07]\g<E>|[18]\g<F>|[29]\g<G>)))(?<A>$|[07]\g<A>|[18]\g<B>|[29]\g<C>|3\g<D>|4\g<E>|5\g<F>|6\g<G>)

```

# Answer
> 26 votes
I was really impressed by Griffin's answer and needed to figure out how it worked! The result is the following JavaScript. (It's 3.5k characters, which is shorter in a way!) The `gen` function takes a divisor and base and generates a regular expression that matches numbers in the specified base that are divisible by that divisor.

I've generalized Griffin's NFA for any base: the `nfa` function takes a divisor and base and returns a two dimensional array of transitions. The input required to go from state 0 to state 2, for example, is `states[0][2] == "1"`.

The `reduce` function takes in the `states` array and runs it through this algorithm to translate the NFA to regex. The regexes that are generated are huge and look like they have a lot of redundant clauses, despite my attempts at optimization. The regex for 7 base 10 is about ~67k characters long; Firefox throws an "InternalError" for n \> 5 trying to parse the regex; running the regex on Chrome starts getting slow for n \> 6.

There's also the `test` function that takes a regex and base and runs it against the numbers 0 to 100, so `test(gen(5)) == [0, 5, 10, 15, ...]`.

Despite the suboptimal result, this was a fantastic learning opportunity, and I hope some of this code will be useful in the future!

```
function gen(b, base) {
    var states = nfa(b, base)
    for (var i = 0; i < states.length; i++)
        states = reduce(states, i);
    return states[0][0] != 'phi' && new RegExp('^' + wrap(states[0][0]) + '$');
}

function test(reg, base) {
    if (!base)
        base = 10;

    var x = [];
    for (var i = 0; i < 100; i++)
        x.push(i);
    return x.map(function (a) {return a.toString(base)}).filter(reg.test.bind(reg)).map(function (a) {return parseInt(a, base)})
}

function nfa(b, base) {
    if (!base)
        base = 10;

    var states = [];
    for (var i = 0; i < b; i++) {
        states[i] = [];
        for (var j = 0; j < b; j++)
            states[i][j] = [];
    }

    for (var i = 0; i < b; i++)
        for (var n = 0; n < base; n++)
            states[i][(i * base + n) % b].push(n.toString());

    for (var i = 0; i < b; i++)
        for (var j = 0; j < b; j++)
            states[i][j] = states[i][j].length > 1 ? '[' + states[i][j].join('') + ']' : (states[i][j][0] || 'phi');
    return states;
}

// http://www.cs.umbc.edu/~squire/cs451_l7.html
function reduce(states, n) {
    var s = states.length;
    var reduced = [];
    for (var i = 0; i < s; i++) {
        reduced[i] = [];
        for (var j = 0; j < s; j++) {
            // reduced[i][j] = wrap(states[i][n] + wrap(states[n][n]) + '*' + states[n][j] + '|' + states[i][j]);
            reduced[i][j] = '';

            if (states[i][n] == 'phi' || states[n][j] == 'phi') {
                reduced[i][j] = states[i][j];
                continue;
            }

            if (states[i][n] != states[n][n])
                reduced[i][j] += wrap(states[i][n]);

            if (states[n][n] != 'phi') {
                reduced[i][j] += wrap(states[n][n]);

                if (states[i][n] == states[n][n] && states[n][j] == states[n][n])
                    reduced[i][j] += wrap(states[n][n]);

                if (states[i][n] == states[n][n] || states[n][j] == states[n][n])
                    reduced[i][j] += '+';
                else
                    reduced[i][j] += '*';
            }

            if (states[n][j] != states[n][n])
                reduced[i][j] += wrap(states[n][j]);

            reduced[i][j] = states[i][j] == 'phi' ? wrap(reduced[i][j]) : alternate(reduced[i][j], states[i][j]);
        }
    }
    return reduced;
}

function matching(x, open, close) {
    // Test if the parens are actually matching
    if ('(['.indexOf(x.charAt(open)) != -1 && ')]'.indexOf(x.charAt(close)) != -1) {
        var count = 0;
        for (var i = open; i <= close; i++) {
            if ('(['.indexOf(x.charAt(i)) != -1)
                count++;
            else if (')]'.indexOf(x.charAt(i)) != -1) {
                count--;

                if (count == 0)
                    return i == close;
            }
        }
    }

    return false;
}

function wrap(x) {
    if (x.length < 2 || matching(x, 0, x.length - 1))
        return x;
    return '(' + x + ')';
}

function optional(cond) {
    if (matching(cond, 0, cond.length - 2)) {
        var op = cond.charAt(cond.length - 1);
        if (op == '+')
            return cond.slice(0, -1) + '*';
        else if (op == '*' || op == '?')
            return cond;
    } else if (matching(cond, 0, cond.length - 1))
        return optional(cond.slice(1, -1));

    return wrap(cond) + '?';
}

function alternate(cond1, cond2) {
    cond2 = wrap(cond2);
    var index = cond1.indexOf(cond2);
    var len = cond2.length;
    var cond = '';

    if (index == 0) {
        var op = cond1.charAt(len);
        if (op == '*')
            cond = cond2 + '+' + optional(cond1.slice(len));
        else if (op == '+')
            cond = cond1;
        else
            cond = cond2 + optional(cond1.slice(len));
    } else if (index == cond1.length - len)
        cond = optional(cond1.slice(0, index)) + cond2;
    else if (cond1.length == 1 && cond2.length == 1)
        cond = '[' + cond1 + cond2 + ']';
    else
        cond = cond1 + '|' + cond2;

    return wrap(cond);
}

```

# Answer
> 10 votes
# Perl/PCRE, 370 characters

```
^(?!$|0.)([07]*(?:[18](?2)|[29](?3)|3(?4)|4(?5)|5(?7)|6(?9)|$))|(5*(?:[07](?4)|[18](?5)|[29](?7)|4(?1)|6(?3)|3(?9)))(3*(?:[18](?1)|[29](?2)|[07](?9)|4(?4)|5(?5)|6(?7)))([18]*(?:[07](?3)|[29](?5)|5(?1)|6(?2)|3(?7)|4(?9)))(6*([29](?1)|[07](?7)|[18](?9)|3(?2)|4(?3)|5(?4)))(4*([07](?2)|[18](?3)|[29](?4)|6(?1)|3(?5)|5(?9)))([29]*([07](?5)|[18](?7)|3(?1)|4(?2)|5(?3)|6(?4)))

```

Rejects the empty string, as well as strings with leading 0’s (except "0").

---
Tags: code-golf, math, regular-expression
---


title: Randomize the test

text:
# Question
Title: Randomize the test
I'm a teacher, and in a few days I have to give my students a test. Now, I'm always worried about my loveliest students being hurt by the least loveliest ones when they cheat, so I want to randomize the tests so no one can cheat off of anyone.

Right now, I have my tests saved in this format:

```
When was Java invented?
  Why does this matter?
  1941
  War was beginning
  None of the above

What is the increment operator in Java?
  Stack Overflow>how 2 incrememnt
  Google>how 2 incrememnt
  increment
  ++

```

That is, the questions are separated by a single blank line, and the answers are all preceded by two spaces. This is the output I need:

```
What is the increment operator in Java?
  ++
  increment
  Google>how 2 incrememnt
  Stack Overflow>how 2 incrememnt

When was Java invented?
  War was beginning
  1941
  Why does this matter?
  None of the above

```

That is, each answer on a question in a random order, and the question order randomized as well. Keep in mind that if the answer choice is "None of the above", it should stay at the bottom. Every question always has exactly four answers, and "None of the above" only ever appears as the last answer choice -- and never appears as a substring of an answer choice that is not "None of the above". Unfortunately, I can't rewrite all of my tests, so you'll have to take them in that format. Also, my students have to be able to read it, so I can't really take output any other way (except as described below).

I don't need it to be perfectly random. As long as it's close.

Standard loopholes are banned.

---

### Bonus

Now, my students are used to that plain format, but if you can make it look like this...

```
1. What is the increment operator in Java?
  A. ++
  B. increment
  C. Google>how 2 incrememnt
  D. Stack Overflow>how 2 incrememnt

2. When was Java invented?
  A. War was beginning
  B. 1941
  C. Why does this matter?
  D. None of the above

```

I'll take 25% off of your final score. You'll get input the same way, though.

That is, if you number the questions and label the answers. Please keep in mind that questions should start at 1, and the answers are from A to D.

---

NB: The examples are different from the ones in the answers because I changed them after the fact. The specific examples aren't the point anyway; answers had to work with any arbitrary question(s).

# Answer
# Pyth - ~~48~~ ~~42~~ ~~41~~ 39 bytes

Will packed string.

```
js.Sm++hd/D"None of the above".Stdkc.zk

```

Try it online here.
> 7 votes

# Answer
# JavaScript ES6, 170 bytes

Is an anonymous function, name it. Note: this uses the random sort method, which isn't entirely random, but is sufficient, provided you aren't a probability teacher.

```
t=>t.split`

`.map(x=>(x=x.split`
`,R=[],(k=x.pop())=="  None of the above"?(R=[k]):x.push(k),[x.shift(),...x.sort(r=_=>.5-Math.random()),...R].join`
`)).sort(r).join`

`

```

### With the bonus, 180.75 bytes

```
t=>t.split`

`.map(x=>(x=x.split`
`,R=[],(k=x.pop())=="  None of the above"?(R=[k]):x.push(k),[x.shift(),...x.sort(r=_=>.5-Math.random()),...R].map((k,i)=>(i?`  ${" ABCD"[i]}. `:"")+k.trim()).join`
`),a=0).sort(r).map(e=>++a+". "+e).join`

`

```

## Test it out!

```
F=t=>t.split`

`.map(x=>(x=x.split`
`,R=[],(k=x.pop())=="  None of the above"?(R=[k]):x.push(k),[x.shift(),...x.sort(r=_=>.5-Math.random()),...R].join`
`)).sort(r).join`

`;

g.onclick=function(){o.innerHTML = ""; o.appendChild(document.createTextNode(F(i.value)));}
g.click();
```

```
textarea{width:100%;height:14em;}textarea,div,button{font-family:Consolas,monospace;white-space:pre;}
```

```
<textarea id=i>When was the War of 1812?
  1812.5
  1776
  1812
  1821

What is the capital of France?
  46
  F
  Nice
  None of the above

What is the square root of -1 (simplified)?
  i
  i don't know
  square root of -1
  wait this is a math test?</textarea><button id=g>go →</button><div id=o></div>
```
> 4 votes

# Answer
## CJam, ~~54~~ ~~53~~ ~~55~~ 52 bytes

Saved 1 byte from using a later release (avaliable on TIO). Gained 2 bytes because I forgot to randomize order of questions. Saved 2 bytes from yet another bug fixed on TIO.

```
qNN+/mr{N/(\mr_"  None of the above"#3e\N*N\++}%NN+*

```

Try it online!
> 2 votes

---
Tags: code-golf, random
---


title: PPCG Senior Prank

text:
# Question
Title: PPCG Senior Prank
We're graduating to a full site soon, and there's only one thing left to do before graduation: pull a senior prank! I think we should do a variation on the classic "fill a hallway with cups of water" gag.

## Challenge

Your program will read in text and output that text, covered in upside-down cups of water. An upside-down cup of water looks like this: `/~\`
These cups can only be placed in whitespace in the input, and can only be placed so that all three characters of the cup are directly above a non-whitespace character (otherwise the water would spill out!). Cups cannot be stacked on top of other cups. Cups must be placed in every available opening, and it is assumed that every input is surrounded by an infinite field of whitespace.

You may assume that the input is padded to a rectangle with spaces.

We need to pull the prank off quickly and without anyone noticing, so fewest bytes in each language wins.

## Test Cases

Input:

```
     ____________________________________________
    /   ___    /   ___    /   ______/   ________/
   /   /__/   /   /__/   /   /     /   /_______
  /   _______/   _______/   /     /   //__    /
 /   /      /   /      /   /_____/   /___/   /
/___/      /___/      /_________/___________/

```

Output:

```
     /~\/~\/~\/~\/~\/~\/~\/~\/~\/~\/~\/~\/~\/~\
     ____________________________________________
    /   ___    /   ___    /   ______/   ________/
   /   /__//~\/   /__//~\/   /     /   /_______
  /   _______/   _______/   //~\  /   //__    /
 //~\/      //~\/      //~\/_____//~\/___//~\/
/___/      /___/      /_________/___________/

```

Input:

```
 L
LOL  ROFL:ROFL:LOL:ROFL:ROFL
 L\\        ____I____
    ========    |  |[\
            \___O==___)
            ___I_I__/

```

Output:

```
 L   /~\/~\/~\/~\/~\/~\/~\
LOL  ROFL:ROFL:LOL:ROFL:ROFL
 L\\/~\/~\  ____I____
    ========/~\ |  |[\
            \___O==___)
            ___I_I__/

```

# Answer
## Retina, 41 bytes

Byte count assumes ISO 8859-1 encoding.

```
(?<=(.)*)(?=.*¶(?>(?<-1>.)*)\S{3})
/~\

```

Note that the first line has three trailing spaces.

Requires the input to be padded to a rectangle.

Try it online!

### Explanation

This is fairly standard vertical matching:

```
(?<=(.)*)

```

This counts the characters preceding the match by capturing that many characters into group `1`.

```
(?=.*¶(?>(?<-1>.)*)\S{3})

```

This checks that there are three non-space characters on at the same position in the next line. This is done by popping from group `1` until its empty with `(?<-1>.)*` and preventing backtracking with the atomic group `(?>...)`.

Finally we match the actual spaces. Those are simply replaced with the literal string `/~\`. Conveniently, matches are found from left to right and cannot overlap.
> 10 votes

# Answer
# JavaScript (ES6), 163 bytes

```
a=>(z=a.split`
`,z.unshift(z[0].replace(/./g,' ')),z).map((b,i)=>b.replace(/   /g, (c,j)=>(!z[i+1]||!z[i+1][j+2]||/ /.test(z[i+1].slice(j,j+3))?c:'/~\\'))).join`
`

```

Quickly hacked together solution, can definitely be golfed down. Adds a blank line above, finds triple spaces, and replaces with a cup if the next line does not contain any spaces. Makes the allowed assumption that input will be rectangular. Any backspaces in the input need escaping (as one would expect with JS).
> 3 votes

# Answer
## JavaScript (ES6), 109 bytes

```
s=>s.replace(/.*\n/,m=>(t=m).replace(/./g,' ')+m).replace(eval(`/   (?=[^]{${t.length-3}}[^ ]{3})/g`),"/~\\")

```

As well as requiring rectangular input, also assumes the first line ends with a newline, even if it is the only line of input. Uses a dynamically generated lookahead to ensure that it finds three spaces "above" three non-spaces, so as not to get confused by holes.
> 1 votes

---
Tags: code-golf, ascii-art
---


title: Hexagon-In or Hexagon-Out?

text:
# Question
Title: Hexagon-In or Hexagon-Out?
There is a great story to tell about regular hexagons found for example in honeycombs. But this busy bee needs your help in telling him which point is inside or outside his honeypot. So, given a regular hexagon as pictured below, centered at the origin and with edge size l, determine if a set of coordinates (x,y) are inside, exactly on the edge or outside of my regular hexagon.

## Input, output and rules

The rules are:

* Input and output methods follow the default rules.
* Input consists of three *integers*: `x,y,l`.
* `x` and `y` are of any convenient signed integer format. `l` is positive (never 0).
* Your program must output/return a `1` if the point `(x,y)` is inside the regular hexagon, `-1` if it's outside or `0` if it's exactly on the edge.
* This is a code-golf, so shortest code wins. In case of a tie, the earliest post wins.
* For output to stdout: leading/trailing spaces or newlines in the output are permitted.
* Standard loopholes apply.

## Test cases

Here are some test cases:

```
0,0,1        --> 1
0,1,1        --> -1
0,-1,1       --> -1
1,0,1        --> 0
-1,0,1       --> 0
-1,-1,1      --> -1
1,1,1        --> -1
-2,-3,4      --> 1
32,45,58     --> 1
99,97,155    --> -1
123,135,201  --> 1

```

# Answer
> 1 votes
# MATL, ~~29~~ 25 bytes

```
3X^/Eh|-IG2G-IX^*1G-hZSX<

```

Inputs are `y`, `x`, `l` in that order.

**Try it online!**

# Answer
> 3 votes
# JavaScript (ES6) 77 ~~83~~

```
(a,b,l,h=Math.sqrt(3)*l,x=a<0?-a:a,y=b<0?-b:b)=>y|x!=l?2*y<h&x/l+y/h<1?1:-1:0

```

**Test**

```
f=(a,b,l,h=Math.sqrt(3)*l,x=a<0?-a:a,y=b<0?-b:b)=>y|x!=l?2*y<h&x/l+y/h<1?1:-1:0

// TEST

function go() {
  C.width=400;C.height=300;
  var l=+I.value, x,y, cols={0:'#ff0',1:'#0f0','-1':'#888'},
  ctx = C.getContext("2d")
  ctx.translate(200,150)
  ctx.strokeStyle='#000'
  ctx.lineWidth=1;
  ctx.beginPath();
  ctx.moveTo(0,-150);ctx.lineTo(0,150);ctx.moveTo(-200,0);ctx.lineTo(200,0);
  ctx.stroke();
  ctx.strokeStyle='#f00'
  ctx.beginPath();
  ctx.moveTo(l*10,0);ctx.lineTo(l*5,l*Math.sqrt(3)*5);ctx.lineTo(-l*5,l*Math.sqrt(3)*5)
  ctx.lineTo(-l*10,0);ctx.lineTo(-l*5,-l*Math.sqrt(3)*5);ctx.lineTo(l*5,-l*Math.sqrt(3)*5)
  ctx.closePath();
  ctx.stroke();

  for(y=-14;y<15;y++)
    for(x=-19;x<20;x++) {
      ctx.beginPath();
      ctx.moveTo(x*10,y*10-3);ctx.lineTo(x*10,y*10+3);
      ctx.moveTo(x*10-3,y*10);ctx.lineTo(x*10+3,y*10);
      ctx.strokeStyle=cols[f(x,y,l)]
      ctx.stroke()
    }
}

go()
```

```
#C {
  border: 1px solid #000
}
```

```
<b>L</b> <input id=I value=15><button onclick="go()">GO</button><br>
<canvas id=C width=400 height=300></canvas>
```

# Answer
> 2 votes
**Ruby, ~~150~~ ~~145~~ ~~137~~ ~~127~~ ~~125~~ ~~106~~ ~~88~~ 76 bytes**

**76 bytes**

```
->(x,y,l){x,y,t=x.abs,y.abs,3**0.5;d=t*l;z=d-t*x-y;2*y>d ?-1:2*x<l ?1:z<=>0}

```

Changed triple comparison to a rocket.

**88 bytes**

```
->(x,y,l){x,y,t=x.abs,y.abs,3**0.5;d=t*l;z=d-t*x-y;2*y>d ?-1:2*x<l ?1:z==0 ?0:0<z ?1:-1}

```

Remove the y equal to apothem test for points on the hexagon, because for integers, that can never be true.

**106 bytes:**

```
->(x,y,l){x,y,t=x.abs,y.abs,3**0.5;d=t*l;z=d-t*x-y;2*y==d&&2*x<=l ?0:2*y>d ?-1:2*x<l ?1:z==0 ?0:0<z ?1:-1}

```

Poster suggested not using epsilon, so replaced epsilon with zero and rearranged, removed an abs, etc.

**125 bytes:**

```
->(x,y,l){x,y,t,e=x.abs,y.abs,3**0.5,1e-9;d=t*l;z=d-t*x-y;(2*y-d).abs<=e&&2*x<=l ?0:2*y>d ?-1:2*x<l ?1:z.abs<=e ?0:0<z ?1:-1}

```

Incorporate y into definition of z and remove some parentheses.

**127 bytes:**

```
->(x,y,l){x,y,t,e=x.abs,y.abs,3**0.5,1e-9;d=t*l;z=d-t*x;(2*y-d).abs<=e&&2*x<=l ?0:2*y>d ?-1:2*x<l ?1:(z-y).abs<=e ?0:y<z ?1:-1}

```

Rearranged terms to avoid necessity of to\_f cast. Use d (double the apothem) instead of a (the apothem). Combine multiple assignments.

**137 bytes:**

```
->(x,y,l){x=x.abs.to_f;y=y.abs.to_f;a=3**0.5*l/2;e=1e-9;z=2*a*(1-x/l);(y-a).abs<=e&&2*x<=l ?0:y>a ?-1:2*x<l ?1:(z-y).abs<=e ?0:y<z ?1:-1}

```

Inlined 'c'.

**150 bytes:**

```
->(x,y,l){c=l/2.0;x=x.abs.to_f;y=y.abs.to_f;a=3**0.5*l/2;e=1e-10;z=2*a*(1-x/l);(y-a).abs<=e&&x<=c ?0:(y>a ?-1:(x<c ?1:((z-y).abs<=e ?0:(y<z ?1:-1))))}

```

This works for integers or floats! The epsilon test is so that points within round off error of being on the edge are correctly identified.

The absolute values move everything into quadrant one.

The value 'a' is the apothem distance (the y-intercept of the hexagon).

The value 'c' is the x-value of the upper right corner of the hexagon.

The value 'z' is to see if the point is above or below the slant line from the corner to the x-intercept.

**Ungolfed:**

```
hex = ->(x,y,l){
    c = l/2.0;
    x = x.abs.to_f;
    y = y.abs.to_f;
    a = 3**0.5 * l / 2;
    e = 1e-10;
    z = 2*a*(1 - x/l);
    if (y-a).abs <= e && x <= c then 0
    elsif (y>a) then -1
    elsif (x<c) then 1
    elsif (z-y).abs <= e then 0
    elsif y < z then 1
    else -1
    end
}

```

**Test**

```
hex = ->(x,y,l){x,y,t=x.abs,y.abs,3**0.5;d=t*l;z=d-t*x-y;2*y>d ?-1:2*x<l ?1:z<=>0}

cases = [
    [0,0,1,1],
    [0,1,1,-1],
    [0,-1,1,-1],
    [1,0,1,0],
    [-1,0,1,0],
    [-1,-1,1,-1],
    [1,1,1,-1],
    [-2,-3,4,1],
    [32,45,58,1],
    [99,97,155,-1],
    [123,135,201,1]
]

cases.each { |test|
  expected = test[3]
  actual = hex.call(test[0],test[1],test[2])
  status = expected == actual ? "PASS" : "FAIL";
  p "#{status}. #(x,y) L = (#{test[0]},#{test[1]}) #{test[2]} Expects #{expected}. Actual #{actual}"
}
"Done!"

```

# Answer
> 0 votes
# Julia, ~~65~~ 58 bytes

```
f(x,l)=(t=maxabs(x/l*[[0 1 1];[2 1 -1]/3^.5]);(t<1)-(t>1))

```

`x` is a row vector `[x y]`. Call like this: `f([0 0],1)`.

# Answer
> 0 votes
## Python 2, 89 bytes

almost the same solution as Julia answer but we can use operation on vector without numpy

```
lambda x,y,L,K=3**0.5/2.:cmp(K*L,max([abs(x*i+y*j)for i,j in[[K,1/2.],[0,1],[-K,1/2.]]]))

```

## Results

```
>>> f(0,0,1)
1
>>> f(32,45,58)
1
>>> f(99,97,155)
-1
>>> f(-1,0,1)
0
>>> [f(0,0,1)== 1,f(0,1,1)== -1,f(0,-1,1)== -1,f(1,0,1)== 0,f(-1,0,1)== 0,f(-1,-1,1)== -1,f(1,1,1)== -1,f(-2,-3,4)== 1,f(32,45,58)== 1,f(99,97,155)== -1,f(123,135,201)== 1,f(0,0,1)== 1,f(0,1,1)== -1,f(0,-1,1)== -1,f(1,0,1)== 0,f(-1,0,1)== 0,f(-1,-1,1)== -1,f(1,1,1)== -1,f(-2,-3,4)== 1,f(32,45,58)== 1,f(99,97,155)== -1,f(123,135,201)== 1]
[True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True]

```

# Answer
> 0 votes
# Pyth, 41 bytes

```
?<=d.5K+?>ZJ-c.ahQeQdZJ.acchtQeQ^3d_1s>dK

```

Test it here

# Answer
> 0 votes
## JavaScript (ES6), 67 bytes

```
with(Math)(a,b,l)=>sign(min(l*l*3-b*b*4,(l-abs(a))*sqrt(3)-abs(b)))

```

Note: To assign this to a variable so that you can call it, put the `f=` after the `with(Math)`.

I used `l*l` and `b*b` in the first parameter to `min` to avoid calls to `abs` and `sqrt` but I couldn't work out whether I could do a similar trick with the second parameter.

---
Tags: code-golf, geometry
---