Skip to content

Instantly share code, notes, and snippets.

View exhesham's full-sized avatar
💭
the grass is greener over here, you're the fog that makes it so clear

Hesham Yassin exhesham

💭
the grass is greener over here, you're the fog that makes it so clear
View GitHub Profile
@exhesham
exhesham / Network Kit (Ping & Scan)
Last active November 16, 2023 02:56
This is the code for the android app named Network Kit (Ping & Scan)You can find the app in this link:https://play.google.com/apps/publish/?dev_acc=00133172886064494147#AppDashboardPlace:p=network.hesham.pinger
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
public int lengthOfLongestSubstring(String s) {
HashMap<Character,Integer> set = new HashMap<>();
int max = 0;int j=0;
for(int i=0;i<s.length();i++){
char c = s.charAt(i);
if(set.containsKey(c)){
// as we dont delete history, we dont
// want to jump back to index in a substring that is dead
j = Math.max(j, set.get(c)+1);
}
public int findUnsortedSubarray(int[] A) {
int n = A.length, beg = -1, end = -2, min = A[n-1], max = A[0];
for (int i=1;i<n;i++) {
max = Math.max(max, A[i]);
min = Math.min(min, A[n-1-i]);
if (A[i] < max) end = i;
if (A[n-1-i] > min) beg = n-1-i;
}
return end - beg + 1;
}
public boolean isCycleUtil(int vertex, boolean[] visited, boolean[] recursiveArr) {
visited[vertex] = true;
recursiveArr[vertex] = true;
//recursive call to all the adjacent vertices
for (int i = 0; i < adjList[vertex].size(); i++) {
//if not already visited
int adjVertex = adjList[vertex].get(i);
if (!visited[adjVertex] && isCycleUtil(adjVertex, visited, recursiveArr)) {
return true;
@exhesham
exhesham / coinChange.cpp
Created March 23, 2018 18:13
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
/* Dynamic programming solution:
1. Let dp[i] where i is between [0..amount+1] represents the minimum amount of coins needed for i.
2. dynamic programming solution gives:
for each coin in coins and foreach amount i: dp[i] = min(dp[i], dp[i - coins[j]] + 1);
*/
int coinChange(vector<int>& coins, int amount) {
int Max = amount + 1;
vector<int> dp(amount + 1, Max);
dp[0] = 0;
for (int i = 1; i <= amount; i++) {
@exhesham
exhesham / lock.c
Created March 23, 2018 18:36
mutex
function lock(boolean *lock) {
while (test_and_set(lock) == 1);
}
@exhesham
exhesham / sum_without_arithmatic.cpp
Created March 27, 2018 18:06
Write a function that adds two numbers. You should not use + or any arithmetic operators.
int add_no_arithm(int a, int b) {
if (b == 0) return a;
int sum = a ^ b; // add without carrying
int carry = (a & b) << 1; // carry, but don’t add
return add_no_arithm(sum, carry); // recurse
}
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello");
CompletableFuture<String> future2  = CompletableFuture.supplyAsync(() -> "Beautiful");
CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> "World");
CompletableFuture<Void> combinedFuture = CompletableFuture.allOf(future1, future2, future3);
combinedFuture.get();
assertTrue(future1.isDone());
assertTrue(future2.isDone());
assertTrue(future3.isDone());
// the result of each one can be processed as follows:
Predicate<Person> isAnAdult = person -> person.getAge() >= 18;
List<Person> people = getAllPeople();
Integer nrOfAdults = people.stream() .filter(isAnAdult).count();
Function<String, Predicate<Ticket>> ticketFor = event -> ticket -> event.equals(ticket.getName());
List<Ticket> tickets = getAllTickets();
Integer soldTicketsForCoolEvent = tickets.stream()
.filter(ticketFor.apply("CoolEvent")).count();