Skip to content

Instantly share code, notes, and snippets.

View Rosuav's full-sized avatar

Chris Angelico Rosuav

View GitHub Profile
Computer Information:
Manufacturer: Unknown
Model: Unknown
Form Factor: Desktop
No Touch Input Detected
Processor Information:
CPU Vendor: GenuineIntel
CPU Brand: Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz
CPU Family: 0x6
//Given a sorted array of numbers, find how many elements are less than a given number
//Do not include any NaN in the array.
function how_many_less(arr, n) {
if (!arr.length || arr[0] >= n) return 0;
let low = 0, high = arr.length;
while (high > low) {
let mid = Math.floor((high + low) / 2);
if (arr[mid] >= n) high = mid;
else if (arr[mid + 1] < n) low = mid;
else return mid + 1;
//Basic string padding functions
function lpad(str, wid) {
return (" ".repeat(wid) + str).slice(-wid);
}
function rpad(str, wid) {
return (str + " ".repeat(wid)).slice(0, wid);
}
function cpad(str, wid) {

Palindromes

A palindrome is a word, phrase, or number that is spelled the same forward and backward. For example, “dad” is a palindrome; “A man, a plan, a canal: Panama” is a palindrome if you take out the spaces and ignore the punctuation; and 1,001 is a numeric palindrome. We can use a stack to determine whether or not a given string is a palindrome.

Write a function that takes a string of letters and returns true or false to determine whether it is palindromic. For example:

function is_palindrome(s) {
    s = s.toLowerCase().replace(/[^a-z]/g, "");
    // your code goes here
}

Two of the most commonly used data structures in web development are stacks and queues. The history of pages visited in a web browser and the undo operation in a text editor are examples of operations made possible using stacks. The handling of events in web browsers often uses a queue data structure.

Stack

A stack is a data structure that stores elements in a LIFO (Last In First Out) order. It's like a stack of plates in your kitchen. When a plate is added, it is pushed towards the bottom of a stack. The last plate that you stack becomes the one on the top of the stack and it is the first one that you get to use.

A stack has two basic functions:

  • push(): places data onto the top of a stack
  • pop(): removes data from the top of the stack

Solutions to these problems

Note that there are other ways to solve these problems, different algorithms to achieve the same goals, which may have different algorithmic complexities.

Exercise 1:

Write a program that determines if an input is even or odd. Explain its average and worst run time

function is_odd(n) {
    return (n % 2) != 0;
}
"use strict";
function g(n) {
if (n) {
const me_cry = "stand here";
var break_of_dawn = "perfect";
} else {
//Some variables rise to the top of the function.
//Do their values?
console.log(break_of_dawn);
function triangle(x)
{
if (x <= 1)
{
if (x == 1) return 1;
else throw "Can't get triangle number of " + x;
}
return triangle(x - 1) + x;
}
//Write an algorithm which will sum two numbers stored in linked lists, where
//each node contains a single digit of the number.
//Assuming that the head of the list is the least significant digit, and that
//each list contains a minimum of one node (which may be zero).
var BASE = 10; //Each digit in the linked list is smaller than this.
function add_numbers(list1, list2) {
var carry = 0;
var ret, tail = null;
while (list1 || list2 || carry) {
@Rosuav
Rosuav / W10D4.js
Last active December 15, 2020 23:50
//Write an O(n) algorithm to sort an array of integers, where you know in
//advance what the lowest and highest values are.
function bucket_sort(arr, min, max) {
var buckets = Array(max - min + 1);
for (var i = 0; i < arr.length; ++i)
buckets[arr[i] - min] = (buckets[arr[i] - min]|0) + 1;
var ret = [];
for (var i = min; i <= max; ++i)
for (var j = 0; j < buckets[i - min]; ++j)
ret.push(i);