how can i get the grid representation of the integer - python

I need to create a method set_from_integer(self, integer_encoding) that sets the colours of all points of the board according to the given integer_encoding which does not change the size of the board. To convert an integer n to ternary representation, as required by set_from_integer(), get the last digit by computing n mod 3. You get the next digit by computing [n/3]mod 3 and so on. For example, to get the last two digits of the ternary representation of 7473:
#This is the problematic part
def set_from_integer(self, integer_encoding):
n = int(integer_encoding)
list1 = []
while n != 0:
rem = n % 3
list1.append(rem)
n = n // 3
list1.reverse()
for i in list1:
print(i, end="")
So for now the above is what i came up with to get the ternary base of the decimal number but i need to convert this into a grid of symbols according to the given :
(empty = 0, white = 1, black = 2)
My entire code is as follows:
def load_board(filename):
result = " "
with open(filename) as f:
print(f)
for index, line in enumerate(f):
if index == 0:
result += ' '+' '.join([chr(alphabets + 65) for alphabets in range(len(line) - 1)]) + '\n' # the alphabetical column heading
result += f"{19 - index:2d}"
result += ' ' + ' '.join(line.strip()) + '\n'
return result
def save_board(filename, board):
with open(filename, "wt") as f:
f.write(board)
from string import ascii_uppercase as letters
class Board:
#Dictionary created for the colours and the respected symbols
points = {'E': '.', 'B': '#', 'W': 'O'}
#Constructor
def __init__(self,size=19,from_strings=None):
assert 2 <= size <= 26, "Illegal board size: must be between 2 and 26."
if from_strings != None:
if type(from_strings) != list:
raise AssertionError("input is not a list")
if len(from_strings) != size:
raise AssertionError("length of input list does not match size")
for i in range(size):
if type(from_strings[i]) != str:
raise AssertionError("row " + str(i) + " is not a string")
if len(from_strings[i]) != size:
raise AssertionError("length of row " + str(i) + " does not match size")
for j in range(size):
if from_strings[i][j] not in ".#O":
raise AssertionError("invalid character in row " + str(i))
self.size = size
self.grid = [['E'] * size for _ in range(size)]
self.from_strings = [] if from_strings is None else from_strings
def get_size(self): #Returns the size of the grid created by the constructor
return self.size
def __str__(self): # creating the grid
padding = ' ' # Creating a variable with a space assigned so that it acts as a padding to the rows that have a single digit
heading = ' ' + ' '.join(letters[:self.size]) # Alphabetical heading is created
lines = [heading] # adding the alphabetical heading into a list named lines to which the rows will be added later
for r, row in enumerate(self.grid):
if len(self.grid) < 10: # for the grid with a size less than 10 to add the space to the start of the row for the single digits to be aligned
if (self.from_strings):
line = " " + f'{self.size - r} ' + ' '.join(self.from_strings[r])
else:
line = " " + f'{self.size - r} ' + ' '.join(self.points[x] for x in row)
lines.append(line)
else: # for the grids that are larger than 9
if r > 9: # for rows 1 to 9 the single digits are aligned according to the first digit from the right of the two digit rows
if (self.from_strings):
line = f'{self.size - r} ' + ' '.join(self.from_strings[r])
else:
line = f'{self.size - r} ' + ' '.join(self.points[x] for x in row)
line = padding + line # adding the space using the variable padding to the row created
lines.append(line) # adding the row to the list of rows
else: # for the rows 10 onwards - as there is no requirement to add a padding it is not added here
if (self.from_strings):
line = f'{self.size - r} ' + ' '.join(self.from_strings[r])
else:
line = f'{self.size - r} ' + ' '.join(self.points[x] for x in row) # creation of the row
lines.append(line) # adding the newly created row to the list of rows
return '\n'.join(lines)
def _to_row_and_column(self, coords):
# destructure coordinates like "B2" to "B" and 2
alpha, num = coords
colnum = ord(alpha) - ord('A') + 1
rownum = self.size - int(num) + 1
assert 1 <= rownum <= self.size,"row out of range"
assert 1 <= colnum <= self.size,'column out of range'
return rownum, colnum
def set_colour(self, coords, colour_name):
rownum, colnum = self._to_row_and_column(coords)
assert len(coords)==2 or len(coords)==3, "invalid coordinates"
assert colour_name in self.points,"invalid colour name"
self.grid[rownum - 1][colnum - 1] = colour_name
def get_colour(self, coords):
rownum, colnum = self._to_row_and_column(coords)
return self.grid[rownum - 1][colnum - 1]
def to_strings(self):
padding = ' '
lines = []
for r, row in enumerate(self.grid):
if self.from_strings:
lines.append(''.join(self.from_strings[r]))
else:
lines.append(''.join(self.points[x] for x in row))
return lines
def to_integer(self):
digit_colour=""
for line in self.to_strings():
for character in line:
if character=='.':
character=0
elif character=='O':
character=1
elif character=='#':
character=2
character=str(character)
digit_colour+=character
return int(digit_colour,3)
# return ''.join(self.to_int[x] for line in self.grid for x in line)
def set_from_integer(self, integer_encoding):
n = int(integer_encoding)
list1 = []
while n != 0:
rem = n % 3
list1.append(rem)
n = n // 3
list1.reverse()
for i in list1:
print(i, end="")
print(type(list1))
print(list1)
for i in range(self.size):
for j in range(self.size):
if list1[i * self.size + j] == "0":
self.grid[i][j] = "."
elif list1[i * self.size + j] == "1":
self.grid[i][j] = "#"
elif list1[i * self.size + j] == "2":
self.grid[i][j] = "O"
c = Board(3)
c.set_from_integer(14676)
print(c)
d = Board(5)
d.set_from_integer(1)
print(d)
and the expected output is shown below:
A B C
3 # . #
2 . O .
1 O # .
A B C D E
5 . . . . .
4 . . . . .
3 . . . . .
2 . . . . .
1 . . . . O

Related

Usage: align <input file> Error in python

Code implements the dynamic programming solution for global pairwise alignment of two sequences. Trying to perform a semi-global alignment between the SARS-CoV-2 reference genome and the first read in the Nanopore sample. The length of the reference genome is 29903 base pairs and the length of the first Nanopore read is 1246 base pairs. When I run the following code, I get this message in my terminal:
import sys
import numpy as np
GAP = -2
MATCH = 5
MISMATCH = -3
MAXLENGTH_A = 29904
MAXLENGTH_B = 1247
# insert sequence files
A = open("SARS-CoV-2 reference genome.txt", "r")
B = open("Nanopore.txt", "r")
def max(A, B, C):
if (A >= B and A >= C):
return A
elif (B >= A and B >= C):
return B
else:
return C
def Tmax(A, B, C):
if (A > B and A > C):
return 'D'
elif (B > A and B > C):
return 'L'
else:
return 'U'
def m(p, q):
if (p == q):
return MATCH
else:
return MISMATCH
def append(st, c):
return c + "".join(i for i in st)
if __name__ == "__main__":
if (len(sys.argv) != 2):
print("Usage: align <input file>")
sys.exit()
if (not os.path.isfile(sys.argv[1])):
print("input file not found.")
sys.exit()
S = np.empty([MAXLENGTH_A, MAXLENGTH_B], dtype = int)
T = np.empty([MAXLENGTH_A, MAXLENGTH_B], dtype = str)
with open(sys.argv[1], "r") as file:
A = str(A.readline())[:-1]
B = str(B.readline())[:-1]
print("Sequence A:",A)
print("Sequence B:",B)
N = len(A)
M = len(B)
S[0][0] = 0
T[0][0] = 'D'
for i in range(0, N + 1):
S[i][0] = GAP * i
T[i][0] = 'U'
for i in range(0, M + 1):
S[0][i] = GAP * i
T[0][i] = 'L'
for i in range(1, N + 1):
for j in range(1, M + 1):
S[i][j] = max(S[i-1][j-1]+m(A[i-1],B[j-1]),S[i][j-1]+GAP,S[i-1][j]+GAP)
T[i][j] = Tmax(S[i-1][j-1]+m(A[i-1],B[j-1]),S[i][j-1]+GAP,S[i-1][j]+GAP)
print("The score of the alignment is :",S[N][M])
i, j = N, M
RA = RB = RM = ""
while (i != 0 or j != 0):
if (T[i][j]=='D'):
RA = append(RA,A[i-1])
RB = append(RB,B[j-1])
if (A[i-1] == B[j-1]):
RM = append(RM,'|')
else:
RM = append(RM,'*')
i -= 1
j -= 1
elif (T[i][j]=='L'):
RA = append(RA,'-')
RB = append(RB,B[j-1])
RM = append(RM,' ')
j -= 1
elif (T[i][j]=='U'):
RA = append(RA,A[i-1])
RB = append(RB,'-')
RM = append(RM,' ')
i -= 1
print(RA)
print(RM)
print(RB)
This has nothing to do with python. The error message comes this line:
if (len(sys.argv) != 2):
print("Usage: align <input file>")
The code expects to be called with exactly one argument, the input file:
align path/to/input/file
You provided a different number of arguments, probably zero.

How to replace all "&int-int" with the respective string slices in an input string?

I have a school project question (for Python) that goes like this:
Given a string_input such as "abcd&1-4efg", the function must remove the "&1-4" and insert the string slice from 1 to 4 where the "&1-4" was.
eg. if string_input = "abcd&1-4efg",
"&1-4" is removed.
The remaining characters are indexed as follows: a=0, b=1, c=2, d=3, e=4, f=5, g=6
The new string becomes:
"abcdbcdeefg"
I've managed to write a long chunk of code to do this, but I'm wondering if anyone has any more efficient solutions?
Things to note:
The instructions can include double digits (eg. &10-15)
If the index isn't found, the returned string should print "?" for every missing index
(eg. "abcd&5-10efgh" would return "abcdfgh???efgh")
Intructions can be back-to-back (eg. "&10-15abcdef&1-5&4-5pqrs")
The code I've written is:
def expand(text):
text += "|"
import string
digits_dash = string.digits + "-"
idx_ref_str = ""
replace_list = []
record_val = False
output_to_list = []
instruct = ""
and_idx_mark = 0
#builds replace_list & idx_ref_list
for idx in range(len(text)):
if text[idx] == "&" and record_val==True:
output_to_list.append(instruct)
output_to_list.append(and_idx_mark)
replace_list.append(output_to_list)
output_to_list, instruct, inst_idx, and_idx_mark = [],"",0,0
and_idx_mark = idx
continue
elif text[idx] == "&":
record_val = True
and_idx_mark = idx
continue
#executes if currently in instruction part
if record_val == True:
#adds to instruct
if text[idx] in digits_dash:
instruct += text[idx]
#take info, add to replace list
else:
output_to_list.append(instruct)
output_to_list.append(and_idx_mark)
replace_list.append(output_to_list)
output_to_list, instruct, inst_idx, and_idx_mark, record_val = [],"",0,0,False
#executes otherwise
if record_val == False:
idx_ref_str += text[idx]
idx_ref_str = idx_ref_str[:-1]
text = text[:-1]
#converts str to int indexes in replace list[x][2]
for item in replace_list:
start_idx = ""
end_idx = ""
#find start idx
for char in item[0]:
if char in string.digits:
start_idx += char
elif char == "-":
start_idx = int(start_idx)
break
#find end idx
for char in item[0][::-1]:
if char in string.digits:
end_idx = char + end_idx
elif char == "-":
end_idx = int(end_idx)
break
start_end_list = [start_idx,end_idx]
item+=start_end_list
#split text into parts in list
count = 0
text_block = ""
text_block_list = []
idx_replace = 0
for char in text:
if char == "&":
text_block_list.append(text_block)
text_block = ""
count += len(replace_list[idx_replace][0])
idx_replace +=1
elif count > 0:
count -= 1
else:
text_block += char
text_block_list.append(text_block)
#creates output str
output_str = ""
for idx in range(len(text_block_list)-1):
output_str += text_block_list[idx]
#creates to_add var to add to output_str
start_repl = replace_list[idx][1]
end_repl = replace_list[idx][1] + len(replace_list[idx][0])
find_start = replace_list[idx][2]
find_end = replace_list[idx][3]
if end_idx >= len(idx_ref_str):
gap = end_idx + 1 - len(idx_ref_str)
to_add = idx_ref_str[find_start:] + "?" * gap
else:
to_add = idx_ref_str[find_start:find_end+1]
output_str += to_add
output_str += text_block_list[-1]
return output_str
Here's how I would do it. Always open to criticism.
import re
s = 'abcd&1-4efg'
c = re.compile('&[0-9]+-[0-9]+')
if (m := c.search(s)):
a, b = m.span()
left = s[:a]
right = s[b:]
o = [int(x) for x in m.group(0)[1:].split('-')]
mid = (left+right)[o[0]:o[1]+1]
print(left + mid + right)

find function does not display desired string

Hey so I am doing a project that is supposed to be a word search calculator. The word search is a 10x10 list of characters turned into rows of characters. I did a .find function for my check forward, however it is not working correctly. I will provide my get puzzle function, my check forward and what the output should be (ignore the format of the puzzle for output).
def input_puzzle():
print("Puzzle:\n")
puzzle_input = input()
puzzle_list = []
for i in range(10):
row = ""
for j in range(10):
row += puzzle_input[i*10 + j]
puzzle_list.append(row)
return puzzle_list
def check_forward(puzzle_list, word, direction):
for row in range(len(puzzle_list)):
row_string = puzzle_list[row]
finder = row_string.find(word)
if finder > 0:
print(word + ":" + direction + "row:" + row, "column:", finder) # finder is the indeci in row #+ means right after and comma means space
return True
else:
print(word + ": word not found")
return False
"""
Puzzle:
WAQHGTTWEE
CBMIVQQELS
APXWKWIIIL
LDELFXPIPV
PONDTMVAMN
OEDSOYQGOB
LGQCKGMMCT
YCSLOAPUZM
XVDMGSXCYZ
UUIUNIXFNU
UNIX: (FORWARD) row: 9 column: 3
"""
The following code works
def input_puzzle():
print("Puzzle:\n")
puzzle_input = input()
puzzle_input = puzzle_input.strip()
puzzle_list = []
for i in range(10):
row = ""
for j in range(10):
row += puzzle_input[i*11 + j]
puzzle_list.append(row)
return puzzle_list
def check_forward(puzzle_list, word, direction):
for row in range(len(puzzle_list)):
row_string = puzzle_list[row]
finder = row_string.find(word)
if finder > 0:
print("{}: ({}) row: {} column: {}".format(word, direction, str(row), str(finder)))
return True
else:
continue
print(word + ": word not found")
print(check_forward(input_puzzle(), 'UNIX', 'FORWARD'))
# UNIX: (FORWARD) row: 9 column: 3

Output matches expected output. Yet submission is not being accepted

https://www.hackerrank.com/challenges/designer-door-mat/problem
Below is my submission:
n = input().split(' ')
rows, columns = int(n[0]), int(n[1])
if(rows % 2 == 1 and columns == 3*rows):
printale = ''
j = 1
k = rows - 7
for i in range(int(rows/2), -1, -1):
printale = '---'*i + '.|.'*j + '---'*i
if(i == 0):
print('-'*int(j+k/2) + 'Welcome' + '-'*int(j+k/2))
j -= 2
else:
print(printale)
j += 2
for l in range(1, int(rows/2)+1):
printale = '---'*l + '.|.'*j + '---'*l
print(printale)
j -= 2
Is there anything wrong with the code?
Yes, there is. The "WELCOME" in the problem statement is all-caps.

Removing colors from an image in python

This code removes red but I can’t get it to work for removing other colors.
def removeRed(inLines, outfile):
for i in range(3, len(lines)):
line = lines[i]
nums = line.split()
outLine = ''
for j in range(len(nums)):
if j % 3 == 0:
outLine = outLine + '0 '
else:
outLine = outLine + nums[j] + ' '
outfile.write(outLine + '\n')

Categories