I just completed a coding challenge on MOOC.
Below are spoilers for Part 3 #4 Chessboard problem. If you don’t want it spoiled for you then please don’t read on, I only post both my solution and the model solution for feedback on how to improve my boding abilities.
Here is my code:
def chessboard(length): i = 1 current_char = 0 forwards = True while i <= length: j = 1 while j <= length: if current_char == 0: current_char = 1 else: current_char = 0 print(current_char, end="") if j == length: if length % 2 == 0: if current_char == 0: current_char = 1 else: current_char = 0 print() j += 1 i += 1 # You can test your function by calling it within the following block if __name__ == "__main__": chessboard(3)
And here is the model solution:
def chessboard(size): i = 0 while i < size: if i % 2 == 0: row = "10"*size else: row = "01"*size # Remove extra characters at the end of the row print(row[0:size]) i += 1
My code works and passes all the tests but the logic and problem solving that I have obviously used is far more outlandish than the very simple solution provided.
Are there exercises or something I can do to begin thinking about problem solving/coding in ways that will allow me to make my code more streamlined? Or is this just how everyone starts and then you refactor once the code is written and see where you can tighten things up?
submitted by /u/Clearhead09
[link] [comments]
r/learnpython I just completed a coding challenge on MOOC. Below are spoilers for Part 3 #4 Chessboard problem. If you don’t want it spoiled for you then please don’t read on, I only post both my solution and the model solution for feedback on how to improve my boding abilities. Here is my code: def chessboard(length): i = 1 current_char = 0 forwards = True while i <= length: j = 1 while j <= length: if current_char == 0: current_char = 1 else: current_char = 0 print(current_char, end=””) if j == length: if length % 2 == 0: if current_char == 0: current_char = 1 else: current_char = 0 print() j += 1 i += 1 # You can test your function by calling it within the following block if __name__ == “__main__”: chessboard(3) And here is the model solution: def chessboard(size): i = 0 while i < size: if i % 2 == 0: row = “10”*size else: row = “01”*size # Remove extra characters at the end of the row print(row[0:size]) i += 1 My code works and passes all the tests but the logic and problem solving that I have obviously used is far more outlandish than the very simple solution provided. Are there exercises or something I can do to begin thinking about problem solving/coding in ways that will allow me to make my code more streamlined? Or is this just how everyone starts and then you refactor once the code is written and see where you can tighten things up? submitted by /u/Clearhead09 [link] [comments]
I just completed a coding challenge on MOOC.
Below are spoilers for Part 3 #4 Chessboard problem. If you don’t want it spoiled for you then please don’t read on, I only post both my solution and the model solution for feedback on how to improve my boding abilities.
Here is my code:
def chessboard(length): i = 1 current_char = 0 forwards = True while i <= length: j = 1 while j <= length: if current_char == 0: current_char = 1 else: current_char = 0 print(current_char, end="") if j == length: if length % 2 == 0: if current_char == 0: current_char = 1 else: current_char = 0 print() j += 1 i += 1 # You can test your function by calling it within the following block if __name__ == "__main__": chessboard(3)
And here is the model solution:
def chessboard(size): i = 0 while i < size: if i % 2 == 0: row = "10"*size else: row = "01"*size # Remove extra characters at the end of the row print(row[0:size]) i += 1
My code works and passes all the tests but the logic and problem solving that I have obviously used is far more outlandish than the very simple solution provided.
Are there exercises or something I can do to begin thinking about problem solving/coding in ways that will allow me to make my code more streamlined? Or is this just how everyone starts and then you refactor once the code is written and see where you can tighten things up?
submitted by /u/Clearhead09
[link] [comments]