Skip to content

Instantly share code, notes, and snippets.

View AhmadElsagheer's full-sized avatar

Ahmad Elsagheer AhmadElsagheer

View GitHub Profile
@AhmadElsagheer
AhmadElsagheer / scenario_1.js
Created February 22, 2019 15:46
Fluent API with promises
const user = new Promise((resolve, reject) => {
console.log('User promise prepared')
resolve({ name: 'sagheer' })
})
user.message = new Promise((resolve, reject) => {
console.log('Message promise prepared')
resolve(['Hey', 'There!'])
})
user.then(r => {
@AhmadElsagheer
AhmadElsagheer / benchmark_redis.go
Last active August 25, 2021 23:40
Redis mget vs hget with keys sharing prefix
package benchmark
// Processor: Intel® Core™ i3-2348M CPU @ 2.30GHz × 4
// Memory: 5.7 GiB
//
// Benchmarking on 100,000 records
// - record key length = 50
// - length of common key (prefix) among all records = 15
// - record value length = 5
//
@AhmadElsagheer
AhmadElsagheer / io.md
Last active August 17, 2018 14:46
Reading from/Writing to File

Input

Standard Input

  Scanner sc = new Scanner(System.in);
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

Input From File

 Scanner sc = new Scanner(new FileReader("file_name"));
@AhmadElsagheer
AhmadElsagheer / offline.java
Created February 18, 2018 14:02
Processing queries on multi-version data structure using an offline algorithm
static void run(InputReader in, OutputWriter out) {
int n = in.readRequestCount();
children = new ArrayList[n]; // maximum number of versions is n
relevantQueries = new ArrayList[n];
for(int i = 0; i < n; ++i) {
children[i] = new ArrayList<>(); // stores a list of versions that are directly produced from version i
relevantQueries[i] = new ArrayList<>(); // stores a list of queries that are asked on version i
}
@AhmadElsagheer
AhmadElsagheer / segtree-combine.md
Last active February 17, 2018 12:56
Holding multiple attributes in a segment tree node

Build and Combine for Segment trees with nodes carrying more than one variable. The example below is on a segment tree that can find maximum and its frequency in a given interval [L, R]. The combine() function will also be used in update() and query().

	static class Node
	{
		int max, freq;
		
		Node(int a, int b)
		{
 max = a;
@AhmadElsagheer
AhmadElsagheer / practice_sheet_guide.md
Last active January 13, 2018 20:29
Practice Sheet Guide

Practice Sheet Guide

The practice sheet is designed to let you:

  1. find all related links for resources, problems and contests.
  2. see progress and codes of all people in the community.
  3. share and be proud of your progress.

How to use the practice sheet?

The spreadsheet is divided into sheets, one for each mentor group (sheets are named by the mentors' names at the bottom of the spreadsheet). Every ACMer will have a column where he/she can update his progress.

@AhmadElsagheer
AhmadElsagheer / problems.md
Created January 12, 2018 20:32
Day 1 Problems

Hey!

Add problem links here. Have a look at markdown cheatsheet

@AhmadElsagheer
AhmadElsagheer / news.pl
Created December 31, 2017 12:19
Daily Galaxy New Coverage (CLPFD)
:-use_module(library(clpfd)).
reporters(["Corey", "Jimmy", "Lois", "Perry"]).
locations(["Bayonne", "New Hope", "Port Charles", "South Amboy"]).
events(["30-pound baby", "blimp launching", "skyscraper dedication", "beached whale"]).
match():-
reporters(R), length(R, N), length(L, N), length(E, N),
L ins 1..4, E ins 1..4, all_distinct(L), all_distinct(E),
% 1. Reporter of event 1 didn't go to location 2 or 4
@AhmadElsagheer
AhmadElsagheer / Shower.java
Created December 31, 2017 11:19
Players Showering
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
// Input: Number of players, showering time per player
public class Shower {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
@AhmadElsagheer
AhmadElsagheer / shower.pl
Last active December 31, 2017 11:21
Players Showers
:-use_module(library(clpfd)).
shower(Times, End):-
maplist(player_task, Times, Tasks, Es),
Es ins 1..100,
cumulative(Tasks, [limit(3)]),
max_list2(Es, End),
once(labeling([min(End)], Es)).
player_task(Time, task(S, Time, E, 1, _), E):- S #>= 0.