python - generate a loop to arrive at certain result -
i have kind of brain fog trying solve following structure , maybe can assist in solving issue.
10 11 12 00 11 12 13 01 12 13 14 02 13 14 15 03 14 15 16 04 15 16 17 05 10 11 12 20 11 12 13 21 12 13 14 22 13 14 15 23 14 15 16 24 15 16 17 25 10 11 12 02 11 12 13 03 12 13 14 04 13 14 15 05 14 15 16 06 15 16 17 07 10 11 12 22 11 12 13 23 12 13 14 24 13 14 15 25 14 15 16 26 15 16 17 27
how algorithm/set of loops generates table? order of appearance not important. bundles of 4 pairs should pop up. pairs need individual digits, i.e. 10
1
, 0
, not ten
!
edit: there pattern in numbers. did not manage create appropriate loop 'catch' pattern.
one pattern in first row (if can solved already):
x = 1 = 0 xi x(i+1) x(i+2) (x-1)i x(i+1) x(i+2) x(i+3) (x-1)(i+1) ...
this code generates desired data 3d list of strings.
a = (0, 0), (2, 0), (0, 2), (2, 2) b = 10, 11, 12 result = [ [ [str(i + j) j in b] + [str(u) + str(v+i)] in range(6) ] u, v in ] # display resulting list in relatively compact way row in result: print([' '.join(u) u in row])
output
['10 11 12 00', '11 12 13 01', '12 13 14 02', '13 14 15 03', '14 15 16 04', '15 16 17 05'] ['10 11 12 20', '11 12 13 21', '12 13 14 22', '13 14 15 23', '14 15 16 24', '15 16 17 25'] ['10 11 12 02', '11 12 13 03', '12 13 14 04', '13 14 15 05', '14 15 16 06', '15 16 17 07'] ['10 11 12 22', '11 12 13 23', '12 13 14 24', '13 14 15 25', '14 15 16 26', '15 16 17 27']
if these pairs supposed pairs of integers need different strategy:
from pprint import pprint = (0, 0), (2, 0), (0, 2), (2, 2) b = 10, 11, 12 result = [ [ [divmod(i + j, 10) j in b] + [(u, v+i)] in range(6) ] u, v in ] pprint(result)
output
[[[(1, 0), (1, 1), (1, 2), (0, 0)], [(1, 1), (1, 2), (1, 3), (0, 1)], [(1, 2), (1, 3), (1, 4), (0, 2)], [(1, 3), (1, 4), (1, 5), (0, 3)], [(1, 4), (1, 5), (1, 6), (0, 4)], [(1, 5), (1, 6), (1, 7), (0, 5)]], [[(1, 0), (1, 1), (1, 2), (2, 0)], [(1, 1), (1, 2), (1, 3), (2, 1)], [(1, 2), (1, 3), (1, 4), (2, 2)], [(1, 3), (1, 4), (1, 5), (2, 3)], [(1, 4), (1, 5), (1, 6), (2, 4)], [(1, 5), (1, 6), (1, 7), (2, 5)]], [[(1, 0), (1, 1), (1, 2), (0, 2)], [(1, 1), (1, 2), (1, 3), (0, 3)], [(1, 2), (1, 3), (1, 4), (0, 4)], [(1, 3), (1, 4), (1, 5), (0, 5)], [(1, 4), (1, 5), (1, 6), (0, 6)], [(1, 5), (1, 6), (1, 7), (0, 7)]], [[(1, 0), (1, 1), (1, 2), (2, 2)], [(1, 1), (1, 2), (1, 3), (2, 3)], [(1, 2), (1, 3), (1, 4), (2, 4)], [(1, 3), (1, 4), (1, 5), (2, 5)], [(1, 4), (1, 5), (1, 6), (2, 6)], [(1, 5), (1, 6), (1, 7), (2, 7)]]]
here's variation of 2nd solution uses "traditional" loops instead of nested list comprehensions. hopefully, it's little easier read. :)
a = (0, 0), (2, 0), (0, 2), (2, 2) b = 10, 11, 12 result = [] u, v in a: row = [] in range(6): row.append([divmod(i + j, 10) j in b] + [(u, v+i)]) result.append(row)
the built-in divmod
function performs division , modulus on arguments, when a
, b
integers divmod(a, b)
equivalent a // b, % b
. if x
2 digit integer `divmod(x, 10) returns tuple containing 2 digits.
Comments
Post a Comment