Guest User

Untitled

a guest
Apr 21st, 2018
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.57 KB | None | 0 0
  1. ## ORMs
  2.  
  3. > Hey mentor,
  4. >
  5. > I finished Rails and I feel like I understand it pretty well,
  6. > but I don't understand ActiveRecord or ORMs in general.
  7. > How does calling save on my model get turned into data in the database?
  8. >
  9. > -Student
  10.  
  11. Hi Student,
  12.  
  13. ActiveRecord is a module that Rails uses to communicate with database. When you call methods that need to get / save the data, from the database,
  14. you write ActiveRecord queries. These queries are translated into SQL queries (language that is used to manipulate data in databases).
  15.  
  16. Let's suppose that you have a _User_ model. Database table that matches this model is _users_ (that is one of the Rails conventions).
  17.  
  18. If you want to get all users you will write ```User.all```. In the background this SQL query is executed: ```SELECT * FROM users```
  19.  
  20. If you want to insert data into database, first you need to create an object:
  21.  
  22. ```ruby
  23. user = User.new(email: 'test@example.com', password: 'password'
  24. ```
  25.  
  26. This object is still not in database. If you want to save it you need to execute ```user.save```.
  27. In the background this SQL query is executed ```INSERT INTO users (email, password) VALUES ('test@example.com', 'password')``` and the data is saved in database.
  28. There are other ways you can save data using ActiveRecord (_create_ in example), but all of them generate the same SQL query as above.
  29.  
  30. You can monitor SQL queries that are executed if you open log file (file you need to open is _logs/development.log_).
  31.  
  32.  
  33. I hope that this answers your question.
  34.  
  35. Regards,
  36.  
  37. Dalibor
  38.  
  39. -----
  40.  
  41. ## Algorithms
  42.  
  43. >Hey mentor,
  44. >
  45. >I hope you're doing well. I'm really enjoying learning algorithms, but I'm pretty confused by this Big O Notation thing. Can you explain how I'm supposed to figure out which notation my algorithm uses?
  46. >
  47. >-Student
  48.  
  49. Hi Student,
  50.  
  51. in order to determine
  52. of your method, you need to determine number of operations that are performed (in a worst case scenarion) based on an input parameter(s). In ex., if your input parameter is _n_, you can create table like:
  53.  
  54. n | Number of operations (worst case scenario)
  55. -----|---------------
  56. 1 | x
  57. 2 | y
  58. 4 | z
  59. 8 | m
  60. 16 | ...
  61. --------------
  62.  
  63. Now you need to determine the way that number of operation changes as _n_ grows. Each growth type has corresponding time complexity. Ordered from the slowest to the fastest growth (from the smallest to biggest time complexity):
  64.  
  65. * constant- O(1)
  66. * logarithmic - O(log n)
  67. * linear - O(n)
  68. * loglinear - O(n log n)
  69. * quadratic - O(n^2)
  70. * exponential - O(2^n)
  71. * factorial - O(n!)
  72.  
  73. **Constant** means that number of operations does not change when n changes (x = y = z = m = ..). The example constant time complexity would be:
  74.  
  75. ```ruby
  76. def m1(n)
  77. return 'even' if n.even?
  78. return false
  79. end
  80. ```
  81. Number of operations is always 1, no matter of the value of _n_ in the _m1_ method.
  82.  
  83. **Logarithmic** means that number of operations increases quickly in the beginning, and then the slows down as _n_ grows. The example of logarithmic time complexity is _binary search algorithm_.
  84.  
  85.  
  86. **Linear** means that number of operations always grows with the same speed as n grows. The example of method with linear time complexity is:
  87.  
  88. ```ruby
  89. def m2(n)
  90. n.times do |i|
  91. puts i
  92. end
  93. end
  94. ```
  95. Number of operations is always equal to _n_ in the _m2_ method.
  96.  
  97. In other time complexity types number of operations grows faster, and the time complexity is higher. Genarally you should avoid algorithms with high complexity, and try to implement solutions with lower complexity types.
  98.  
  99. Please let me know if we need to go in more details.
  100.  
  101. Regards,
  102.  
  103. Dalibor
  104.  
  105. -----
  106.  
  107. ## SQL
  108.  
  109. >Hey mentor,
  110. >
  111. >I am really stuck on joins in SQL. It seems like it should be easy but I'm having a hard time figuring it out. I'm used to Rails which does all of this for me. What does the JOIN SQL clause really do?
  112. >
  113. >Thanks!
  114. >
  115. >-Student
  116.  
  117. Hi Student,
  118.  
  119. JOIN does exactly what it's names says. It joins data from two (or more) tables.
  120.  
  121. Let's suppose that you have two tables with following fields:
  122.  
  123. **posts**
  124. * title
  125. * body
  126.  
  127. **comments**
  128. * post_id
  129. * comment
  130.  
  131. As you can see from the table structure, comment belongs to post, and one post can have many comments. If you would want to get all posts and their comments, you will need to check both tables at the same time. To accomplish that, you have to use JOIN statement. The query would look like:
  132.  
  133. ```sql
  134. SELECT posts.*, comments.* -- you choose what you want to select from tables. Here we select everything from both tables
  135. FROM posts -- you specify that you query posts table
  136. JOIN comments ON posts.id = comments.post_id -- you specify that you want to query comments table as well, and you also specify how are these 2 tables connected: posts.id field matches comments.post_id field.
  137. ```
  138.  
  139. I hope that my comments in query explain well what happens and how JOIN statement works.
  140.  
  141. Regards,
  142.  
  143. Dalibor
  144.  
  145. -----
  146.  
  147. ## Data Structures
  148.  
  149. > Hey mentor! I'm confused about stacks and queues, they seem like the same thing. What's different about them and when are they used? How are they related to a linked list?
  150. >
  151. > -Student
  152.  
  153. Hi Student,
  154.  
  155. Slacks, Queues and Linked Lists are different data structures.
  156.  
  157. **Stack** is a data structure that implements Last In First Out (LIFO) principle.
  158. That means that the last element that was added to stack will be the first one that is taken out.
  159. One stack example is the implementation of the _undo_ method. When you call this method, the last action added to stack will be undone.
  160.  
  161. **Queue** is data structure that implements First In First Out (FIFO) principle.
  162. That means that the first element that was added to queue will be the first one that is taken out.
  163. Queue example can be any kind of queue. The first person that comes to the line should be the first one that leaves the queue.
  164.  
  165. **Linked list** is another data structure where each node has value and point to the next node in the list.
  166. That meand that if you want to access n<sup>th</sup> node, you need to access all _n-1_ nodes before it.
  167. One linked listed example is a train. If you are in the first wagon, and want to go to the last, you need to go trough second, third,... until you come to the last.
  168.  
  169. Please let me know if we need to go in more details.
  170.  
  171. Regards,
  172.  
  173. Dalibor
  174.  
  175. -----
  176.  
  177. ## Sorting
  178.  
  179. >Mentor,
  180. >
  181. >I'm learning about searching and optimization, but it seems like there are so many different kinds of searches. What are some common collection sorting algorithms, and when would you use them?
  182.  
  183. >-Student
  184.  
  185. Hi Student,
  186.  
  187. sorting is very important because it is prerequisite for efficient search. As you learned from the checkpoint there are many search algorithms, and all of them implement different strategies. I am not an expert in this area, so this is result of my search:
  188.  
  189. * Insertion Sort has O(n^2) time complexity and poor performance compared to other sorting algorithms.
  190. * Selection Sort has O(n^2) time complexity and poor performance compared to other sorting algorithms.
  191. * Bubble Sort has O(n^2) time complexity and poor performance compared to other sorting algorithms.
  192. * Merge Sort has O(nlogn) time complexity and good performance compared to other sorting algorithms. Databases use it to sort big sets of data.
  193. * Quick Sort has O(n^2) time complexity and good performance compared to other sorting algorithms. Ruby's _sort_ method uses this method in the background.
  194. * Heap Sort has O(nlogn) time complexity and good performance compared to other sorting algorithms. Linux Kernel uses this method to sort data.
  195.  
  196. Quick Sort is usually the fastest, but it depends on the data structures it needs to sort. Alternative method that is used is Heap Sort.
  197.  
  198. I hope that I answered your questions. Please let me know if you want we to study this in more details.
  199.  
  200. Regards,
  201.  
  202. Dalibor
Add Comment
Please, Sign In to add comment