def simple _board(size): Given a size, produce a square \"ASCII art\" grid repre
ID: 3722490 • Letter: D
Question
def simple _board(size): Given a size, produce a square "ASCII art" grid representing a game board (like fou checkers or chess). The game board will use dashes (-) for the horizontal lines, bars () for the vertical lines, ane plus signs (+) for the intersections. size indicates the number of play spaces wide the board is. For example: Size 1 Board Size 2 Board Size 3 Board Size 4 Board . Return value: a string of the game board . Assumptions: o size will be a positive integer Notes: o Hint I: Modulus is your friend for this function! o Hint 2: You must use at least one loop, but you are not limited to one loop. o Make sure to return a string, not print a string. o What is "ASClI art"? It is basically "art made with computer text" and creating it is a (popular?) pastime for some people. Google it for more examples, but here's an example of a mouse: Vi 2 . Example Console Run: >>> simple board (1) >>> print(simple board(1)) 4-4Explanation / Answer
''' To solve this type of problems you only need to find pattern in shapes and we need to think how we can write code for these patterns in programming language. So to solve this problem , 1. I found that every shape has size*2+1 rows and size*2+1 columns. 2. After that just check for even - odd value of loop variable and append corresponding character to the result string. Algorithm: 1. Declare an empty string variable 2. Run one loop for each row count, if row is even append only + and - , if row is odd append | and space(' ') 3. Run one more loop for each column , -if row count is even ---(a)add + to string , if col count is even ---(b)add - to string , if col count is odd -else row count is odd ---(a)add | to string , if col count is even ---(b)add ' '(space) to string , if col count is odd Sample Input: simple_board(2) Sample Output : +-+-+ | | | +-+-+ | | | +-+-+ Sample Input: simple_board(5) Sample Output: +-+-+-+ | | | | +-+-+-+ | | | | +-+-+-+ | | | | +-+-+-+ ''' #Please code from below line to end of answer in your .py file and execute. #Please code from below line to end of answer in your .py file and execute. #Function to store shape in string def simple_board(size): #String to store board board=""; #loop for rows for row in range(0,size*2+1): #Checking for even row if(row%2==0): #loop for col count for col in range(0,size*2+1): #Checking for even col, else for odd if(col%2==0): #Appending + to string board =board + '+' else: board =board + '-' else: # if row is odd for col in range(0,size*2+1): # Checking for even col, else for odd if(col%2==0): #Appending | to string board =board + '|' else: board =board + ' ' #Adding new line to the string board =board + ' ' #Removing last character board=board[:-1] #Returning board string return board #Calling function simple_board print(simple_board(2))
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.