here2share

# len3_base3_sorted.py

Jun 7th, 2022 (edited)
879
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.67 KB | None | 0 0
  1. # len3_base3_sorted.py
  2.  
  3. base = 3
  4. baselength = 3
  5.  
  6. def nbase(n, b=3,length=3):
  7.     b += 1
  8.     if n == 0:
  9.         return [0]*length
  10.     values = []
  11.     for z in 'Z'*length:
  12.         r = n % b
  13.         n = int(n/b)
  14.         values.append(r)
  15.     return values[::-1]
  16. 0
  17.  
  18. ttt = []
  19. i = 0
  20. while 1:
  21.     t = nbase(i)
  22.     ttt += [t]
  23.     if t == [base]*baselength:
  24.         break
  25.     i += 1
  26. 0
  27. ttt.sort(key=sum)
  28. for t in ttt:
  29.     print ''.join([str(i) for i in t]),
  30.  
  31. '''
  32. 000 001 010 100 002 011 020 101 110 200 003 012 021 030 102 111 120 201 210 300 013 022 031 103 112 121 130 202 211 220 301 310 023 032 113 122 131 203 212 221 230 302 311 320 033 123 132 213 222 231 303 312 321 330 133 223 232 313 322 331 233 323 332 333
  33. '''
Add Comment
Please, Sign In to add comment