Am I doing this right? Do the ends justify the means?
Heyy! I'm doing the MOOC programming course so I'm just a beginner but I have a question.
I just want to know how "simple" I should make my code. Because often I make the hardest most complicated way to the solution of a problem, my outcomes are correct but I keep thinking if the ends justify the means... Maybe in the long run this will make it harder for me if I don't fix this now, and if so how do I fix this?
For example:
I had this exercise;
Please write a program which asks the user to type in a number. The program then prints out the positive integers between 1 and the number itself, alternating between the two ends of the range as in the examples below.
Sample output
Please type in a number: 5
1
5
2
4
3
My answer was this
number = int(input("Please type in a number:"))
x = 1
y = number
while x <= ((number+1)//2):
print (x)
if x != y:
print (y)
x += 1
y -= 1
The model answer this
number = int(input("Please type in a number: "))
left = 1
right = number
while left < right:
print(left)
print(right)
left += 1
right -= 1
if left == right:
print(left)
I think the model answer is a lot easier to understand and is more bug-proof than mine.
Should I focus more on finding easier solutions for in the long run. Or am I overthinking this and should I just do what works for me and after some practice I'll make simpler solutions anyway.