Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

#!/usr/bin/env python3 

 

import string 

 

def well_from_row_col(row, col): 

return f'{row}{int(col)}' 

 

def well_from_irow_col(irow, col): 

ii, j = ij_from_row_col(irow, col) 

i = interleave(ii, j) 

return well_from_ij(i, j) 

 

def well_from_row_icol(row, icol): 

i, jj = ij_from_row_col(row, icol) 

j = interleave(jj, i) 

return well_from_ij(i, j) 

 

def well_from_ij(i, j): 

return well_from_row_col( 

row_from_i(i), 

col_from_j(j), 

) 

 

 

def i_from_row(row): 

return ord(row.upper()) - ord('A') 

 

def j_from_col(col): 

return int(col) - 1 

 

def ij_from_well(well): 

return ij_from_row_col(*row_col_from_well(well)) 

 

def ij_from_row_col(row, col): 

return i_from_row(row), j_from_col(col) 

 

 

def row_from_i(i): 

return string.ascii_uppercase[i] 

 

def col_from_j(j): 

return str(j + 1) 

 

def row_col_from_ij(i, j): 

return row_from_i(i), col_from_j(j) 

 

def row_col_from_well(well): 

return well[:1], str(int(well[1:])) 

 

def irow_icol_from_well(well): 

row, col = row_col_from_well(well) 

i, j = ij_from_row_col(row, col) 

ii = interleave(i, j) 

jj = interleave(j, i) 

return row_col_from_ij(ii, jj) 

 

 

def interleave(a, b): 

""" 

Convert the given coordinates between "real" and "interleaved" space. 

 

Only the first coordinate differs between the two spaces. A straight row  

in "interleaved" space will alternative between two adjacent rows in "real"  

space, and vice versa. This function can also used to interleave  

columns (by passing the column as the first argument and the row as the  

second). 

 

Note that this function is its own inverse. 

""" 

if a % 2 == 0: 

return a + b % 2 

else: 

return a - b % 2 

 

def iter_wells_in_block(top_left, width, height): 

top, left = ij_from_well(top_left) 

for dx in range(width): 

for dy in range(height): 

yield well_from_ij(top + dy, left + dx)