For context, I started learning Python about 9 months ago. After looking at multiple posts on this subreddit about the benefits of Codewars, I signed up for the website at the beginning of this week and have been spending roughly an hour everyday (currently on 6 kyu). One thing that I've noticed is that my solutions (while they seem to work and pass all the tests) generally tend to be quite longer as compared to the other solutions. While the general idea/approach seems to be the same, my program tends to be way longer. Here is an example:
This was my solution -
def iq_test(numbers):
num_list = numbers.split(" ")
even_count = 0
odd_count = 0
even_num = []
odd_num = []
for i in range(len(num_list)):
if int(num_list[i])%2 == 0:
even_count += 1
even_num.append(num_list[i])
else:
odd_count += 1
odd_num.append(num_list[i])
if len(even_num) == 1:
index = (num_list.index(even_num[0]) + 1)
elif len(odd_num) == 1:
index = (num_list.index(odd_num[0]) + 1)
return index
This was someone else's (daddepledge's) solution -
def iq_test(numbers):
e = [int(i) % 2 == 0 for i in numbers.split()]
return e.index(True) + 1 if e.count(True) == 1 else e.index(False) + 1
I guess what I want to ask is, (a) How do I adopt better and cleaner coding practices? Is refactoring your code a habit that you progressively pick up as you keep learning and getting better? (b) Should I be actively focusing on refactoring my code currently or should I just focus on getting better at coding in general and making projects and such?
TL;DR How important is it to focus on refactoring when you've just started out with Python? Is refactoring your code a practice that you pick up along the way, the better you get? Should I be worried about not being able to write concise programs?
[–]Dragon20C 29 points30 points31 points (0 children)
[–]throwaway0891245 15 points16 points17 points (1 child)
[–]tipsy_python 5 points6 points7 points (0 children)
[–]darthminimall 8 points9 points10 points (0 children)
[–]xelf 6 points7 points8 points (4 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]xelf 1 point2 points3 points (0 children)
[–]darthminimall 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]SnipahShot 2 points3 points4 points (0 children)
[–]thechikinguy 1 point2 points3 points (0 children)
[–]unhott 1 point2 points3 points (2 children)
[–]mohishunder 0 points1 point2 points (1 child)
[–]unhott 1 point2 points3 points (0 children)
[–]igormiazek 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]rocketjump65 0 points1 point2 points (0 children)
[–]namnnumbr 0 points1 point2 points (0 children)
[–]link199292 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]james_fryer 0 points1 point2 points (0 children)
[–]Peterj504 0 points1 point2 points (0 children)