Skip to main content

strassens algo

Oct 13, 2022daybs
Loading...

More TypeScript Posts

React usePrevious Hook

Oct 15, 2022CodeCatch

0 likes • 105 views

import { useState, useEffect, useRef } from "react";
// Usage
function App() {
// State value and setter for our example
const [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 value
return (
<div>
<h1>
Now: {count}, before: {prevCount}
</h1>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
// Hook
function 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 class
const ref = useRef();
// Store current value in ref
useEffect(() => {
ref.current = value;
}, [value]); // Only re-run if value changes
// Return previous value (happens before update in useEffect above)
return ref.current;
}

langToConfig map

Mar 31, 2023Helper

0 likes • 3 views

const langToConfig = {
'javascript': {
image: '',
cmd: ['']
},
'php': {
image: '',
cmd: ['']
},
}

Coderator Follow System CRUD

Nov 18, 2022AustinLeath

1 like • 4 views

const getFollowers = async (req: Request, res: Response) => {
try {
const username = req.params.username || '';
const user = await User.findUnique({
where: { username },
include: { followedBy: true },
});
if (!user) {
res.status(404).json({
error: 'User could not be found',
});
return;
}
const followers = user.followedBy.map(followedBy => {
return {
followerId: followedBy.followerId,
};
});
const followersCount = followers.length;
res.json({ followersCount: followersCount, followers });
} catch (error) {
console.error('getFollowers() error: ', error);
res.status(500).json({
error: `There was an error fetching followers for user with username "${req.params.username}", please try again later`,
});
}
};
const getFollowing = async (req: Request, res: Response) => {
try {
const username = req.params.username || '';
const user = await User.findUnique({
where: { username },
include: { following: true },
});
if (!user) {
res.status(404).json({
error: 'User could not be found',
});
return;
}
const following = user.following.map(following => {
return {
followingId: following.followingId,
};
});
const followingCount = following.length;
res.json({ followingCount: followingCount, following });
} catch (error) {
console.error('getFollowing() error: ', error);
res.status(500).json({
error: `There was an error fetching who is following "${req.params.username}", please try again later`,
});
}
};
const followUser = async (req: Request, res: Response) => {
try {
const username = req.params.username || '';
const user = await User.findUnique({
where: { username },
});
if (!user) {
res.status(404).json({
error: 'User could not be found',
});
return;
}
// dont allow a user to follow themselves
if (req.user.id === user.id) {
res.status(400).json({ error: 'You cannot follow yourself' });
return;
}
//check if a follow relationship already exists
const followrelationship = await prisma.follows.findUnique({
where: {
followerId_followingId: {
followerId: req.user.id,
followingId: user.id,
},
},
});
// check if the follow relationship already exists
if (followrelationship) {
res.status(400).json({ error: 'You are already following this user' });
return;
}
// create the follow relationship
const follow = await prisma.follows.create({
data: {
followerId: req.user.id,
followingId: user.id,
},
});
res.json({ follow });
} catch (error) {
console.error('followUser() error: ', error);
res.status(500).json({
error: `There was an error following "${req.params.username}", please try again later`,
});
}
};
const unfollowUser = async (req: Request, res: Response) => {
try {
const username = req.params.username || '';
const user = await User.findUnique({
where: { username },
});
if (!user) {
res.status(404).json({
error: 'User could not be found',
});
return;
}
// dont allow a user to unfollow themselves
if (req.user.id === user.id) {
res.status(400).json({ error: 'You cannot unfollow yourself' });
return;
}
//check if a follow relationship already exists
const followrelationship = await prisma.follows.findUnique({
where: {
followerId_followingId: {
followerId: req.user.id,
followingId: user.id,
},
},
});
// check if the follow relationship already exists
if (!followrelationship) {
res.status(400).json({ error: 'You are not following this user' });
return;
}
// delete the follow relationship
const follow = await prisma.follows.delete({
where: {
followerId_followingId: {
followerId: req.user.id,
followingId: user.id,
},
},
});
res.json({ follow });
} catch (error) {
console.error('unfollowUser() error: ', error);
res.status(500).json({
error: `There was an error unfollowing "${req.params.username}", please try again later`,
});
}
};

LeetCode - Search Insert Position

Oct 22, 2024C S

0 likes • 2 views

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);
};

Depth-First Search in TypeScript

Sep 19, 2024Sam-C137

0 likes • 2 views

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

Depth-First Search in TypeScript

Oct 15, 2022CodeCatch

0 likes • 59 views

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