Definition of Dijkstra’s Algorithm Algorithm to calculate the shortest path from one vertex of a graph to another vertex, using a priority array
Input and Output Input: starting vertex, ending vertex Output: an array of vertices consisting of the shortest path from start to end in order Logic 1. To begin with, 4 variables need to be initialized. distances object: used to store the shortest distance from start to each vertex previous object: used to store the previous vertex of the path shortest to the current vertex priority queue: used to store vertices to pay visits to, in ascending order of distances from the starting vertex to the current vertex, this will be used for looping through the graph path: an array for storing path info at the end for returning the path 2.
1. Bubble Sort In each loop, place the largest number on the top, creating a “bubble”
const bubbleSort = (arr) => { for (let i = arr.length - 1; i > 0; i--) { let swap = false; for (let j = 0; j < i; j++) { if(arr[j] > arr[j + 1]) { let temp = arr[j + 1]; arr[j + 1] = arr[j]; arr[j] = temp; swap = true; } } if(!
Flatten Create a function that flattens all nested arrays in the given array.
My initial instinct was to use the JavaScript function .flat() and recursion. However, a problem occurred with the base case. In order for recursion to function correctly, the base case needs to be set to determine if the recursion should stop. In my above solution, I wanted to compare the array before and after flattening it.
Source: JavaScript Algorithms and Data Structures Masterclass Frequency Counter When counting frequency of iterable objects, avoid nested looping that results in O(n^2), by using objects or sets to collect values.
Example Create a function named “same” that compares 2 arrays, and check if one array contains the squared values of the other, with matching frequencies
The Naive Solution Use nested loop to check if squared value of current looping element exists.
Task Create a search input and add an event listener to it in JavaScript. Whenever the search query changes, send a request to the view in the backend via url.
Code // Function to Get Token function getCookie(name) { let cookieValue = null; if (document.cookie && document.cookie !== "") { const cookies = document.cookie.split(";"); for (let i = 0; i < cookies.length; i++) { const cookie = cookies[i].trim(); // Does this cookie string begin with the name we want?
Background The audio tag from HTML provides a default music player, or more specifically the browser provides a controllable music player for audio tags. However, since this is powered by the browser, customizing the player to fit the style and need of the web page is impossible. Therefore the alternative is to create a custom music player that is connected to the original audio source.
Code HTML <!-- hidden audio tag (with tachyon display-none class) --> <audio class="media-player dn" src="{{ post.
Make the First Element of a Django Template Loop Different from Others The method of adding and removing current class with JavaScript to give different styling and action needs an extra step with django. When elements are created with loops, adding current class to one of the elements will automatically copy them into all others, unless there is a way to set the first item of the loop.
{% for post in posts_list %} {% if forloop.
Problem Since I was using Github in cooperation for the first time, there were a few problems I have encountered with pull and merges.
Before I pushed my working branch to Github, I should have checked whether an existing pull request exists, and if that were the case, the existing pull request should have be reviewed and merged first before I pushed my branch. This would have prevented me from facing merge conflicts after pushing to github.
Reference Heroku Webhosting for Django - Python Django Dentist Website #12
Configuring Django Apps for Heroku
General guincorn: Django uses “runserver” as the default package to run a server during development. However, when deploying with heroku, “gunicorn” should be used to run a server.
whitenoise: In local environment, django is capable of searching for static files by itself. In deployment, static files are to be manually collected using “whitenoise”.
This is a really short note that might save loads of time When working with chrome, some things just might not work because the wrong codes that has been fixed might be saved in the cache and prevents the right code from working. Today this problem occurred when working with Redux. Make sure to get in the habit of using hard reload (ctrl + F5) when reloading in development.