Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Josh Keenan
20,315 Pointssingle line if statement
I'm doing a coding challenge to find 1, 3, 4 in an array and am trying to shorten it as much as possible. I'm getting an indentation error on this, can someone explain why and help me fix it?
Thanks!
def sequence(arr):
catch_first = 0
catch_second = 0
catch_third = 0
win = False
for i in arr:
catch_first=1 if i==1
catch_second=3 if i==3 and catch_first==1 else catch_first=0
print("win") if i==4 and catch_second==3 else catch_first, catch_second=0, 0
sequence([1,2,3,4])
print("end1".upper())
sequence([1,2,4])
print("end2".upper())
sequence([1,3,4])
print("end3".upper())
sequence([2,1,2,3,4,6,1,3,4])
print("end4".upper())
3 Answers
Chris Freeman
Treehouse Moderator 68,468 PointsHappy New Year, Josh!
My approach would be to look ahead in the array instead of tracking state. Remember to stop short of the end to avoid index errors.
def sequence(arr):
for i in range(len(arr)-2):
if arr[i] == 1 and arr[i+1] == 3 and arr[i+2] == 4:
print("True")
Josh Keenan
20,315 Pointsyou are a God, for reference I came up with the solution myself in the end as you can see. I got the job! First development job! I would say I credit you a bit as you always helped when it came to it and explained it better than anyone, thanks a lot Chris, and Happy New Year!
Chris Freeman
Treehouse Moderator 68,468 PointsCongratulations on the new job!! Glad I was able to help!
Chris Freeman
Treehouse Moderator 68,468 PointsCame across another approach on StackOverflow using zip():
def sequence(arr):
for first, second, third in zip(arr, arr[1:], arr[2:]):
if first == 1 and second == 3 and third == 4:
print("True")
No need to worry about invalid indices as zip will stop at the end of the shorted list!
Adding this to my bag-o-tricks!
Josh Keenan
20,315 PointsLove that solution! Will definitely save that for later!
Ryan S
27,276 PointsHi Josh,
I'm not that well versed with single-line if statements like you are using them, but as far as I understand the conditionals need to return something when used in this way. So you might be getting an error due to the lack of an else condition on your first statement. It doesn't know what to do if "i" is not equal to 1.
Hope this helps.
Josh Keenan
20,315 PointsYeah I have read through lots of documentation, I wanted to add an elif thing too but it isn't possible sadly. Was hoping to do this really well, oh well
Josh Keenan
20,315 PointsEnded up with this for my solution:
def sequence(arr):
catch_first, catch_second, catch_third = 0, 0, 0
for i in arr:
if i == 1: catch_first = 1
elif i == 3 and catch_first == 1: catch_second = 3
elif i == 4 and catch_second == 3: print("True")
else: catch_first, catch_second = 0, 0
Under 10 lines still, but it's cheating imo..
Josh Keenan
20,315 PointsJosh Keenan
20,315 PointsConditional expressions, that's the proper name for this. I have also now answered my own question, what I am trying to do is not possible with conditional expressions sadly.