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
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 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);
}
@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
}
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();
- kind: ReplicationController
apiVersion: v1
metadata:
name: bad-frontend
labels:
name: bad-frontend
spec:
replicas: 1
selector:
name: bad-frontend
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_weather_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="20sp" />
/*
* Iterate through the array and append the Strings to the TextView. The reason why we add
* the "\n\n\n" after the String is to give visual separation between each String in the
* TextView. Later, we'll learn about a better way to display lists of data.
*/
for (String toyName : toyNames) {
mToysListTextView.append(toyName + "\n\n\n");
}
public class MainActivity extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.activity_main); 
       // other code to setup the activity 
    } 
    // other code