Loading...
More TypeScript Posts
interface Task {id: number;title: string;completed: boolean;}class TaskManager {private tasks: Task[] = [];constructor() {}addTask(title: string) {const taskId = this.tasks.length + 1;const task: Task = {id: taskId,title,completed: false,};this.tasks.push(task);}markTaskAsComplete(taskId: number) {const task = this.tasks.find((task) => task.id === taskId);if (task) {task.completed = true;}}markTaskAsIncomplete(taskId: number) {const task = this.tasks.find((task) => task.id === taskId);if (task) {task.completed = false;}}listTasks() {console.log('Tasks:');this.tasks.forEach((task) => {console.log(`${task.id}. [${task.completed ? 'X' : ' '}] ${task.title}`);});}}// Example usage:const taskManager = new TaskManager();taskManager.addTask('Buy groceries');taskManager.addTask('Pay bills');taskManager.addTask('Clean the house');taskManager.listTasks();// Output:// Tasks:// 1. [ ] Buy groceries// 2. [ ] Pay bills// 3. [ ] Clean the house
function removeDuplicates(nums: number[]): number {nums.splice(0, nums.length, ...Array.from(new Set(nums)));return nums.length;};
const langToConfig = {'javascript': {image: '',cmd: ['']},'php': {image: '',cmd: ['']},}
// This function takes a matrix and outputs 4 equal size submatrices, starting from top left going to bottom right.const split = (matrix: number[][]): number[][][] => {const n: number = matrix.lengthconst mid: number = n/2const C11: number[][] = new Array<Array<number>> // first n/2 rows intersection first n/2 colummsconst C12: number[][] = new Array<Array<number>> // first n/2 rows intersection last n/2 columnsconst C21: number[][] = new Array<Array<number>> // last n/2 rows intersection first n/2 columnsconst C22: number[][] = new Array<Array<number>> // last n/2 rows intersection last n/2 columnsfor(let i: number = 0; i < mid; i++) {C11.push([])C12.push([])for (let j: number = 0; j < mid; j++) {C11[i].push(matrix[i][j])}for (let j: number = mid; j < n; j++) {C12[i].push(matrix[i][j])}}for(let i: number = 0; i < mid; i++) {C21.push([])C22.push([])for (let j: number = 0; j < mid; j++) {C21[i].push(matrix[i+mid][j])}for (let j: number = mid; j < n; j++) {C22[i].push(matrix[i+mid][j])}}return [C11, C12, C21, C22]}// This function takes 4 matrices, in order from top left to bottom right, and combines them into one matrixconst combine = (A: number[][], B: number[][], C: number[][], D: number[][]): number[][] => {const n: number = A.length*2const combo: number[][] = new Array<Array<number>>// Populate combo with temporary 0sfor(let i: number = 0; i < A.length; i++) {combo.push([])for (let j: number = 0; j < A.length; j++) {combo[i].push(A[i][j])}for (let j: number = A.length; j < n; j++) {combo[i].push(B[i][j-A.length])}}for(let i: number = A.length; i < n; i++) {combo.push([])for (let j: number = 0; j < A.length; j++) {combo[i].push(C[i-A.length][j])}for (let j: number = A.length; j < n; j++) {combo[i].push(D[i-A.length][j-A.length])}}return combo}// Basic matrix addition functionconst matrix_add = (A: number[][], B: number[][]): number[][] => {const matrix_sum: number[][] = [[]]const n: number = A.lengthfor(let i: number = 0; i < n; i++) {matrix_sum[i] = []for (let j: number = 0; j < n; j++) {matrix_sum[i][j] = A[i][j] + B[i][j]}}return matrix_sum}// Basic matrix subtraction functionconst matrix_subtract = (A: number[][], B: number[][]): number[][] => {const matrix_diff: number[][] = [[]]const n: number = A.lengthfor(let i: number = 0; i < n; i++) {matrix_diff[i] = []for (let j: number = 0; j < n; j++) {matrix_diff[i][j] = A[i][j] - B[i][j]}}return matrix_diff}const strassen = (A: number[][], B: number[][]): number[][] => {const n: number = A.lengthvar C: number[][] = [[]]// Populate C with temporary 0sfor(let i: number = 0; i < n; i++) {C[i] = []for (let j: number = 0; j < n; j++) {C[i][j] = 0}}// Once the matrices reach a small enough size, compute using the brute force methodif (n <= 2) {for (let i: number = 0; i < n; i++) {for (let j: number = 0; j < n; j++) {for (let z: number = 0; z < n; z++) {C[i][j] = C[i][j] + (A[i][z]*B[z][j])}}}return C}// Create independent variables from function that returns an array with array destructuring.const [a, b, c, d] = split(A)const [e, f, g, h] = split(B)// Using Strassen's method to calculate 1 less productconst p1: number[][] = strassen(matrix_add(a, d), matrix_add(e,h))const p2: number[][] = strassen(d, matrix_subtract(g, e))const p3: number[][] = strassen(matrix_add(a, b), h)const p4: number[][] = strassen(matrix_subtract(b, d), matrix_add(g, h))const p5: number[][] = strassen(a, matrix_subtract(f, h))const p6: number[][] = strassen(matrix_add(c, d), e)const p7: number[][] = strassen(matrix_subtract(a, c), matrix_add(e, f))const C11: number[][] = matrix_add(matrix_subtract(matrix_add(p1, p2), p3), p4)const C12: number[][] = matrix_add(p5, p3)const C21: number[][] = matrix_add(p6, p2)const C22: number[][] = matrix_subtract(matrix_subtract(matrix_add(p5, p1), p6), p7)C = combine(C11, C12, C21, C22)return C}const M1: number[][] = [[1,3],[7,5]]const M2: number[][] = [[6,8],[4,2]]const M_Product: number[][] = strassen(M1, M2)console.log("Matrix 1:")console.log(M1)console.log("Matrix 2:")console.log(M2)console.log("Product: ")console.log(M_Product)
import { useState, useEffect, useRef } from "react";// Usagefunction App() {// State value and setter for our exampleconst [count, setCount] = useState(0);// Get the previous value (was passed into hook on last render)const prevCount = usePrevious(count);// Display both current and previous count valuereturn (<div><h1>Now: {count}, before: {prevCount}</h1><button onClick={() => setCount(count + 1)}>Increment</button></div>);}// Hookfunction usePrevious(value) {// The ref object is a generic container whose current property is mutable ...// ... and can hold any value, similar to an instance property on a classconst ref = useRef();// Store current value in refuseEffect(() => {ref.current = value;}, [value]); // Only re-run if value changes// Return previous value (happens before update in useEffect above)return ref.current;}
function binarySearch(nums: number[], target: number): number {let left = 0;let right = nums.length - 1;while (left <= right) {const mid = left + Math.floor((right - left) / 2);if (nums[mid] === target) {return mid;} else if (nums[mid] < target) {left = mid + 1;} else {right = mid - 1;}}return left;}function searchInsert(nums: number[], target: number): number {return binarySearch(nums, target);};