• API
• FAQ
• Tools
• Archive
daily pastebin goal
45%
SHARE
TWEET

# Untitled

a guest Feb 13th, 2018 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. # matricies that increase from left to right and top to bottom
2.
3. import random
4. import math
5.
6. def matrix(width, max_increase=3, max_seed=10):
7.     """
8.     width: matrix width > 0
9.     max_increase: the maximum increase between a cell and its lower / right
10.     neighbors
11.     max_seed: maximum value for the top left element
12.     """
13.     if width <= 0:
14.         raise ValueError('width must be > 0')
15.     increase = lambda: random.randint(1, max_increase)
16.     mat = [[0 for x in range(width)] for x in range(width)]
17.     mat[0][0] = random.randint(0, max_seed)
18.     for x in range(1, width):
19.         mat[x][0] = mat[x - 1][0] + increase()
20.         mat[0][x] = mat[0][x - 1] + increase()
21.
22.     for x in range(1, width):
23.         for y in range(1, width):
24.             mat[x][y] = max(mat[x - 1][y], mat[x][y - 1]) + increase()
25.     return mat
26.
27. def smatrix(mat):
28.     largest = max(max(mat))
29.     width = math.ceil(math.log10(largest))
30.     fmt = str(width)
31.     ret = []
32.     for x in range(len(mat)):
33.         ret.append('[')
34.         row = []
35.         for y in range(len(mat[x])):
36.             row.append(format(mat[x][y], fmt))
37.         ret.append(', '.join(row))
38.         ret.append(']\n')
39.     return ''.join(ret);
40.
41. def smatrix_java(mat):
42.     largest = max(max(mat))
43.     width = math.ceil(math.log10(largest))
44.     fmt = str(width)
45.     ret = []
46.     for x in range(len(mat)):
47.         row = []
48.         for y in range(len(mat[x])):
49.             row.append(format(mat[x][y], fmt))
50.         ret.append('{' + ', '.join(row) + '}')
51.     return 'int[][] matrix = new int[][] {\n\t' + ',\n\t'.join(ret) + '};'
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top