For any and all questions relating to Challenge 6 post away!
this was not too bad. For those a bit more experienced with python, I used
List.index() functions. Loops take too long to type plus you’ll need to loop breaks and a few more conditional statements.
index() returns the index of the first instance of the element, however if there’s none, it’ll throw a
As python docs are super good to have, I have attached the link to them. Ctrl (or CMD) + F to search for
list.index and you’ll find it
The recipe to complete this challenge is:
- Look through the list.
a. If there’s an UPPERCASE letter (thank you @emergencyward for catching my mistake), you can park there, but only if you follow specific rules.
b. If the plane you need to park is
wide,you can only park in the
c. If the plane is narrow, you can park in ANY UPPERCASE spots (
d. If you find a spot, return the index of the FIRST spot found
- If there aren’t any spots available, you should
This problem builds off of previous issues, except for maybe the idea of returning early.
An example of such is:
def find_me_3_5_7_or_8(): a_list = [1,2,3,4,5] for i in a_list: if i==3 or i==5 or i==7 or i ==8: return "WE FOUND IT" return "no 3 or 5 or 7 or 8 found" print(find_me_3_5_7_or_8()) >WE FOUND IT
The above code will run through the list and try to find specific values (3,5,7,8). If it finds the value, it will return “WE FOUND IT” and exit the loop. If it doesn’t see those numbers while looping, it will finally reach the last line and return “no 3 or 5 or 7 or 8 found”.
What about returning the index of an element in a list?
Looping over a list and returning an index is similar to previous questions, and a similar technique would probably work here.
If you’re stuck, I recommend googling “return index and value of a list in python” or something similar.
I would also recommend looking at @asotocinal 's post about a_list.index(), which may be helpful in this situation, assuming you can ‘catch the error.’
Just to clarify, the challenge says that lower-case spots are unavailable. It’s the upper-case spots (W or N) that you want to find for the plane to park in.
My first thought was to test for both the plane type and the gate size within one “if” statement using the “and” conditional, but I realized that wouldn’t work for the narrow planes since I can’t seem to use an “or” conditional as well as an “and” conditional in the same line. It worked fine to get the answer for this particular case they want the answer for, but I’m glad I saw the LHL solution to get my “oh, of course” moment.
Having aircrafts circling in the air irresponsibly like this may have just been why unfortunate mysteries like the missing MH370 take place
@Here2Win nice explanation.
Guys, feel free to ask questions.
Enjoy your weekend.
return False didn’t return False as expected. It returned
None in the output. Does anyone happen to have a good explanation for this behavior?
Maybe it’s just the Jupyter notebook handling of False, but for me this is worry as that can impact scripts that are anticipating a specific exit code.
Please remove the asterisk marks in the question. It’s confusing for no reason.
Wide-Body planes can only use gates in W spots.
Narrow-Body planes can use gates in either N or W spots. <<
The person writing the question probably wanted to bold the text, but added an extra * mark.
Enjoyed this problem! I didn’t build into my function the condition that narrow planes can fit into wide gates but looks like it wasn’t particularly necessary to get to the final solution anyways
We can create one or more functions for a higher understanding language. In bigger codes, it helps a lot. You’ll read the code almost reading a normal sentence.
find_the_gate = lambda s, v: s.index(v.upper())
doesn’t this fail when there’s no spot? (list.index returns value error)
Hi all, I will be moderating the forum for the next 2 hours. Please let me know if you face any barriers in attempting to complete todays challenge. Also, please remember to not share any answers on this forum.
Todays problem is an interesting one. Tip: Define the function and then use the example cases as test cases to check if your solution works as desired. Should be easy, good luck!
beaut. You’ll need to catch the exception with a
try-except block though. or not. and treat the exception as ‘unavailable’ lol
You can solve without try-except as well. As long as you got it right its all good.
correct. you’ll need exception handling with a
try-catch block. or treat the thrown exception as ‘unavailable’ (if you’re too lazy) lol
or just let the code run and search for a gate,
no try except