Loading...
More TypeScript Posts
import React, { useEffect, useRef, useState } from 'react';import * as d3 from 'd3';export default function useD3(renderChartFn : (svg : d3.Selection<SVGElement,{},HTMLElement>) => any, dependencies : any[]) {const ref = useRef<SVGSVGElement>();useEffect(() => {let d3El = d3.select(ref.current)renderChartFn(d3.select(ref.current));return () => {};}, dependencies);return ref;}function MyChart(props : {w: number,h: number,data: Array<any>}) {const [currentData,setCurrentData] = useState(props.data)let svgRef = useD3((svg) => {if (props.data === currentData) returnsetCurrentData(props.data)// add d3 codesvg.selectAll("*").remove();const margin = { top: 0, right: 80, bottom: 20, left: 10 };const width = props.w - margin.left - margin.right;const height = props.h - margin.top - margin.bottom;const canvas = svg.attr('width', width + margin.left + margin.right).attr('height', height + margin.top + margin.bottom).append('g').attr('transform', `translate(${margin.left},${margin.top})`);// dataconst data = props.data},[props])return (<svg ref={svgRef} />)}
// Print Hello, World!console.log("Hello, World!");
const langToConfig = {'javascript': {image: '',cmd: ['']},'php': {image: '',cmd: ['']},}
function removeDuplicates(nums: number[]): number {nums.splice(0, nums.length, ...Array.from(new Set(nums)));return nums.length;};
function twoSum(nums: number[], target: number): number[] {const map = new Map();for(let i = 0; i < nums.length; i++) {if(map.has(target - nums[i])) {return [map.get(target - nums[i]), i];} else {map.set(nums[i], i);}}return []}
class DFS{public dfs(G: DFSGraph, startVert: number){let visited: boolean[] = Array<boolean>();// Pre-populate array:for(let i = 0; i < G.size; i++){visited.push(false);}let s: number[] = new Array();visited[startVert] = true;s.push(startVert);while(s.length > 0){const v = s.pop();for(let adjV of G.adj[v]){if(!visited[adjV]){visited[adjV] = true;s.push(adjV);}}}}public dfsRecursive(G: DFSGraph, startVert: number){let visited: boolean[] = Array<boolean>();// Pre-populate array:for(let i = 0; i < G.size; i++){visited.push(false);}this.dfsAux(G, startVert, visited);}private dfsAux(G: DFSGraph, v: number, visited: boolean[]){visited[v] = true;for(let adjV of G.adj[v]){if(!visited[adjV]){// this.foo(); // Something can happen before the visit.this.dfsAux(G, adjV, visited);// this.bar(); // Something can happen after the visit.}}}}