Skip to content

Instantly share code, notes, and snippets.

@9999years
Created January 31, 2018 07:06
Show Gist options
  • Select an option

  • Save 9999years/f46cfd47fa8c5a9ecc1ead8e7c6002fc to your computer and use it in GitHub Desktop.

Select an option

Save 9999years/f46cfd47fa8c5a9ecc1ead8e7c6002fc to your computer and use it in GitHub Desktop.
# matricies that increase from left to right and top to bottom
import random
import math
def matrix(width, max_increase=3, max_seed=10):
"""
width: matrix width > 0
max_increase: the maximum increase between a cell and its lower / right
neighbors
max_seed: maximum value for the top left element
"""
if width <= 0:
raise ValueError('width must be > 0')
increase = lambda: random.randint(1, max_increase)
mat = [[0 for x in range(width)] for x in range(width)]
mat[0][0] = random.randint(0, max_seed)
for x in range(1, width):
mat[x][0] = mat[x - 1][0] + increase()
mat[0][x] = mat[0][x - 1] + increase()
for x in range(1, width):
for y in range(1, width):
mat[x][y] = max(mat[x - 1][y], mat[x][y - 1]) + increase()
return mat
def smatrix(mat):
largest = max(max(mat))
width = math.ceil(math.log10(largest))
fmt = str(width)
ret = []
for x in range(len(mat)):
ret.append('[')
row = []
for y in range(len(mat[x])):
row.append(format(mat[x][y], fmt))
ret.append(', '.join(row))
ret.append(']\n')
return ''.join(ret);
def smatrix_java(mat):
largest = max(max(mat))
width = math.ceil(math.log10(largest))
fmt = str(width)
ret = []
for x in range(len(mat)):
row = []
for y in range(len(mat[x])):
row.append(format(mat[x][y], fmt))
ret.append('{' + ', '.join(row) + '}')
return 'int[][] matrix = new int[][] {\n\t' + ',\n\t'.join(ret) + '};'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment