Attachment 'FarGalaxy_gen.py'

Download

   1 #!/usr/bin/env python3
   2 '''
   3 '''
   4 
   5 import os, sys
   6 import random
   7 
   8 def far(a, b):
   9     return (a[0]-b[0])**2+(a[1]-b[1])**2+(a[2]-b[2])**2
  10 
  11 with open(os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])),"google-10000-english.txt")) as f:
  12    words = [w for w in f.read().split() if 2<len(w)<8]
  13 if len(sys.argv)>2:
  14     words = random.sample(words, int(sys.argv[2]))
  15 
  16 N = 10 if len(sys.argv)<=1 else int(sys.argv[1])
  17 m,M = -1000,1000
  18 
  19 Dists = set()
  20 Galaxies = set()
  21 while len(Galaxies)<N:
  22     x0,y0,z0 = (random.random()*(M-m)+m for i in range(3))
  23     NDists=set()
  24     for x1, y1, z1 in Galaxies:
  25         dist =  far((x0,y0,z0),(x1, y1, z1))
  26         if dist in Dists:
  27             print("OOPS",x0,y0,z0)
  28             break
  29         NDists.add(dist)
  30     else:
  31         Dists |= NDists
  32         Galaxies.add((x0, y0, z0))
  33         print("{:5.3f} {:5.3f} {:5.3f} {}".format(x0, y0, z0, random.choice(words)))
  34 print(".")

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.

You are not allowed to attach a file to this page.