Problem-Solving

Dijkstra's Algorithm on JavaScript

Koowoy
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.

Sorting Algorithms

Koowoy
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(!

JavaScript Recursion Exercise

Koowoy
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.

Problem Solving Patterns

Koowoy
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.

[JS/Problem Solving] Getting the highest possible number

Koowoy
Task Requirements A number between 0 and 1,000,000 and k that is smaller than the first number is given. When k amount of digit is removed, return the highest possible number. My Attemps for Solution No.1) Using Combination On first thought, I wasn’t aware of how big the number could be, and came up with the idea to find all possiblities and to compare these to find the maximum value.

[Sidenote/JS] Using Recursion to Get All Possible Combinations

Koowoy
The definition of combination in mathematics is as follows: In mathematics, a combination is a selection of items from a collection, such that the order of selection does not matter. (from Wikipedia) In order to get all the possible selections, the following formula is used: image-source The first part refers to all the combination that includes a certain value, and the latter part refers to all the combinations that does not include that value.

Enlightenment of the Day 21.02.13

Koowoy
Handling Edge Cases in Image Filtering / Understanding Rounding of Floats in C 1. Handling Edge Cases in Image Filtering Problem Using C, create a program that apllies different filters on images. Each pixel is stored in a two dimensional array. Obsctacle When filtering the image with ‘blur’ and ‘border’ filter, for each pixel in the image, certain values of all surrounding pixels needs to be reached. However, the number of surrounding pixels vary depending on the location of the pixel, more precisely, depending on whether the pixel is in the corner, edge or the middle.

[Javascript Regex] Recommending Alternative ID

Koowoy
Task Requirements The Rules for the ID Length of the ID must be 3 to 15 characters. Only alphanumeric(lowercase) characters and [ -] , [ _ ], [ . ] are allowed. [ . ] can not be used at the beginning and the end, and cannot be used consecutively. Seven Steps in Creating Recommended ID Step 1. Change all the uppercase letters to lowercase.

Identifying the Loser from a Game of Word Chain

Koowoy
Rule of the Game Certain number of players present a word in turns, that begins with the letter of the last word’s ending. For instance, if the last word was apple, the current player needs to present a word that starts with e. No word can be presented twice in the same game. Therefore the loser is a. whose word presented didn't begin with the last word's ending(let's call this following the previous word) or b.

Enlightenment of the Day 21.01.21

Koowoy
Getting Student with Highest Score Problem 3 Students are taking a multiple choice test with 5 answer options. Without actually solving the problem, they repeat the same pattern of numbers. When fed with the answer array, return the student who scored the highest. Requirements 1. The pattern of each student is as follows: Student 1: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2 .... Student 2: 2, 1, 2, 3, 2, 4, 2, 5, 2, 1, 2, 3 .