Welcome To CodeCatch
Store, compile, and edit code snippets with other developers on CodeCatch
154
Users
443
Snippets
86
Languages
Unlimited
Uploads
Store
CodeCatch allows you to easily upload and keep track of code snippets that are important to you. Create a post, upload the post, and never waste your time finding code snippets again.
Compile
CodeCatch supports code compiling for 15+ languages such as JavaScript, Python, C, Go, and more. Simply click the "Run" button when viewing a post to see the compiled output.
1# Python binary search function2def binary_search(arr, target):3left = 04right = len(arr) - 15while left <= right:6mid = (left + right) // 27if arr[mid] == target:8return mid9elif arr[mid] < target:10left = mid + 111else:12right = mid - 113return -11415# Usage16arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]17target = 718result = binary_search(arr, target)19if result != -1:20print(f"Element is present at index {result}")21else:22print("Element is not present in array")
12345678910111213141516171819
Edit
Want to extend or simplify a code snippet? Click the Edit option while viewing a post to easily modify a block of code to your liking.
Lets See Some Posts!
Below are examples of featured posts to get you started. Click on a post to view and share it or to get the full CodeCatch experience.
const express = require('express')const app = express()const port = 3000app.get('/', (req, res) => {res.send('Hello World!')})app.listen(port, () => {console.log(`Example app listening on port ${port}`)})
awk '\{ for (i=1; i<=NF; i++) { ++D[$i]; } }\END { for (i in D) { print i, D[i] } }\' words.txt | sort -nr -k 2
.loader {font-size: 10px;margin: 50px auto;text-indent: -9999em;width: 11em;height: 11em;border-radius: 50%;background: #ffffff;background: -moz-linear-gradient(left, #ffffff 10%, rgba(255, 255, 255, 0) 42%);background: -webkit-linear-gradient(left, #ffffff 10%, rgba(255, 255, 255, 0) 42%);background: -o-linear-gradient(left, #ffffff 10%, rgba(255, 255, 255, 0) 42%);background: -ms-linear-gradient(left, #ffffff 10%, rgba(255, 255, 255, 0) 42%);background: linear-gradient(to right, #ffffff 10%, rgba(255, 255, 255, 0) 42%);position: relative;-webkit-animation: load3 1.4s infinite linear;animation: load3 1.4s infinite linear;-webkit-transform: translateZ(0);-ms-transform: translateZ(0);transform: translateZ(0);}.loader:before {width: 50%;height: 50%;background: #ffffff;border-radius: 100% 0 0 0;position: absolute;top: 0;left: 0;content: '';}.loader:after {background: #0dc5c1;width: 75%;height: 75%;border-radius: 50%;content: '';margin: auto;position: absolute;top: 0;left: 0;bottom: 0;right: 0;}@-webkit-keyframes load3 {0% {-webkit-transform: rotate(0deg);transform: rotate(0deg);}100% {-webkit-transform: rotate(360deg);transform: rotate(360deg);}}@keyframes load3 {0% {-webkit-transform: rotate(0deg);transform: rotate(0deg);}100% {-webkit-transform: rotate(360deg);transform: rotate(360deg);}}
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.}}}}
// C# program to print BFS traversal// from a given source vertex.// BFS(int s) traverses vertices// reachable from s.using System;using System.Collections.Generic;using System.Linq;using System.Text;// This class represents a directed// graph using adjacency list// representationclass Graph{// No. of verticesprivate int _V;//Adjacency ListsLinkedList<int>[] _adj;public Graph(int V){_adj = new LinkedList<int>[V];for(int i = 0; i < _adj.Length; i++){_adj[i] = new LinkedList<int>();}_V = V;}// Function to add an edge into the graphpublic void AddEdge(int v, int w){_adj[v].AddLast(w);}// Prints BFS traversal from a given source spublic void BFS(int s){// Mark all the vertices as not// visited(By default set as false)bool[] visited = new bool[_V];for(int i = 0; i < _V; i++)visited[i] = false;// Create a queue for BFSLinkedList<int> queue = new LinkedList<int>();// Mark the current node as// visited and enqueue itvisited[s] = true;queue.AddLast(s);while(queue.Any()){// Dequeue a vertex from queue// and print its = queue.First();Console.Write(s + " " );queue.RemoveFirst();// Get all adjacent vertices of the// dequeued vertex s. If a adjacent// has not been visited, then mark it// visited and enqueue itLinkedList<int> list = _adj[s];foreach (var val in list){if (!visited[val]){visited[val] = true;queue.AddLast(val);}}}}
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;}