Skip to content

Instantly share code, notes, and snippets.

View ahmedash95's full-sized avatar

Ahmed Ammar ahmedash95

View GitHub Profile
local ts_utils = require('nvim-treesitter.ts_utils')
local get_master_node = function()
local node = ts_utils.get_node_at_cursor()
if not node then
error('Cannot get node at cursor')
end
-- When in normal mode, find the smallest node starting at the cursor position
local root = ts_utils.get_root_for_node(node)
@ahmedash95
ahmedash95 / 159.java
Created April 5, 2024 14:36
159. Longest Substring with At Most Two Distinct Characters
class Solution {
public int lengthOfLongestSubstringTwoDistinct(String s) {
int ans = 0;
int left = 0;
Map<Character, Integer> freq = new HashMap<>();
for(int right = 0; right < s.length(); right++) {
char c = s.charAt(right);
freq.put(c, freq.getOrDefault(c, 0) + 1);
while(freq.size() > 2) {
@ahmedash95
ahmedash95 / EasyRelations.php
Last active June 21, 2023 07:46
Laravel easy relations
<?php
namespace App\Utils;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Str;
trait EasyRelations
{
protected function getDefinedRelations()
@ahmedash95
ahmedash95 / tableplus-open-db.bash
Created March 15, 2021 15:22
tableplus-open-db
opendb () {
[ ! -f .env ] && { echo "No .env file found."; exit 1; }
DB_CONNECTION=$(grep DB_CONNECTION .env | grep -v -e '^\s*#' | cut -d '=' -f 2-)
DB_HOST=$(grep DB_HOST .env | grep -v -e '^\s*#' | cut -d '=' -f 2-)
DB_PORT=$(grep DB_PORT .env | grep -v -e '^\s*#' | cut -d '=' -f 2-)
DB_DATABASE=$(grep DB_DATABASE .env | grep -v -e '^\s*#' | cut -d '=' -f 2-)
DB_USERNAME=$(grep DB_USERNAME .env | grep -v -e '^\s*#' | cut -d '=' -f 2-)
DB_PASSWORD=$(grep DB_PASSWORD .env | grep -v -e '^\s*#' | cut -d '=' -f 2-)
@ahmedash95
ahmedash95 / script.php
Last active November 11, 2020 21:26
Codeforces problems scrapper
<?php
$data = file_get_contents('https://codeforces.com/api/problemset.problems?tags=greedy');
$data = json_decode($data, true)['result']['problems'];
$out = collect($data)
->filter(function($row){
return isset($row['rating']) && $row['rating'] >= 500;
})
->sortBy('rating')
@ahmedash95
ahmedash95 / index.php
Last active July 12, 2020 09:35
Github visitors counter
<?php
function createImage($content)
{
$font1 = 5;
$width1 = imagefontwidth($font1) * strlen($content);
$img_w = 240;
$img_h = 64;
$image = imagecreatetruecolor($img_w, $img_h);
$white = imagecolorallocate($image, 255, 255, 255);
module FunctionsHelper
def render_code(ruby)
CodeRender.new.render(ruby)
end
class CustomRender < Redcarpet::Render::HTML
def codespan(code)
CodeRender.new.render(code)
end
end
@ahmedash95
ahmedash95 / README.md
Created March 4, 2020 23:42
Laravel Eloquent Collection set relations

Usage

// Instead Of

$categories = Category::all();

$products = Products::all();

$products-&gt;each(function($product) use ($categories) {
@ahmedash95
ahmedash95 / README.md
Created February 26, 2020 15:18 — forked from nikcub/README.md
Facebook PHP Source Code from August 2007