English subtitles

← Order By Solution - Web Development

Get Embed Code
4 Languages

Showing Revision 10 created 05/25/2016 by Udacity Robot.

  1. Okay, so here's my answer.
  2. The SQL is Select * From Links,
  3. where submitter ID equals 62443,
  4. order by submitted time
  5. ASC, for ascending.
  6. And these are the constraints of the question--62443 and submission time.
  7. And then what I'm doing is I'm iterating over the results,
  8. making a link,
  9. appending the link's ID to the list of results,
  10. and then I'm returning results.
  11. Okay, so let's go ahead and give this a run to make sure it works.
  12. We see our answer is 15, 18, 21.
  13. Those are the correct link IDs.
  14. My answer looks the way it does
  15. because I've been following the form we've been kind of working in--
  16. the general structure of this function.
  17. But if I were actually to make a function that does just this--
  18. just returns the IDs of the links--
  19. there's a couple things that are wasteful in here
  20. that I'll go ahead and clean up and show you.
  21. So the first thing is we don't need to select * from links.
  22. All we care about is the IDs.
  23. So we'll just select the IDs.
  24. Here we're creating a link object
  25. for every row we get back.
  26. Well, we don't actually need the link object.
  27. If we're just going to get the IDs from the results,
  28. we can just return those nearly directly.
  29. So let's get rid of this loop
  30. and instead change it to something like this.
  31. Okay, so I simplified this quite a bit.
  32. This syntax here is a list comprehension.
  33. It's a simple way of making a list.
  34. So we still run our SQL.
  35. This time we're selecting ID.
  36. And since SQLite Library in Python returns tuples of the results,
  37. and in this case we're only asking for the ID,
  38. we know the first column is going to be the ID.
  39. So for t in c--so for the tuple in the cursor--
  40. make a list of the first element of each of those tuples,
  41. store that in results, and return it.
  42. Let's go ahead and print this.
  43. And the answer is the same--15, 18, 101.
  44. Okay, so just a little extra Python there for you.
  45. Let's move along.