Skip to content

Instantly share code, notes, and snippets.

View riyafa's full-sized avatar

Riyafa Abdul Hameed riyafa

View GitHub Profile
@riyafa
riyafa / MoveZeros.java
Created September 8, 2020 14:01
Solution for LeetCode Move Zeroes
public class MoveZeros {
public void moveZeroes(int[] nums) {
int i = 0;
int j = 0;
while(i < nums.length && j < nums.length) {
if(nums[i] != 0) {
i++;
} else if(j <= i || nums[j] == 0) {
j++;
} else {
@riyafa
riyafa / LongestDuplicateSubstring.java
Last active September 8, 2020 14:00
Solution for LeetCode Longest Duplicate Substring
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
public class LongestDuplicateSubstring {
private static final int PRIME = java.math.BigInteger.probablePrime(15, ThreadLocalRandom.current()).intValue();
public String longestDupSubstring(String S) {
public class HammingDistance {
public int hammingDistance(int x, int y) {
int res = x ^ y;
int count = 0;
while (res > 0) {
res = res & (res - 1);
count++;
}
return count;
}
@riyafa
riyafa / MiddleOfLinkedList.java
Created September 3, 2020 00:28
[Leetcode] 876. Middle of the Linked List
class MiddleOfLinkedList {
public ListNode middleNode(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
@riyafa
riyafa / echo.bal
Created May 19, 2020 07:42
Simple WebSocket echo server written in Ballerina language
import ballerina/http;
service websocket on new http:Listener(15300) {
resource function onText(http:WebSocketCaller caller, string text,
boolean finalFrame) {
checkpanic caller->pushText(text);
}
}
@riyafa
riyafa / bing_wallpaper_downloader.py
Last active June 15, 2020 08:50
[Python2] Download weekly Bing wallpapers to a given folder. Can run it as a cron job everyday
import json, os
import os
import time
import urllib
import urllib2
from os.path import expanduser
from datetime import datetime
home_site = "http://bing.com"
weekly_wallpapers_url = home_site + "/HPImageArchive.aspx?format=js&idx=0&n=8&mkt=en-US"
@riyafa
riyafa / websocket_service_attach.bal
Last active October 14, 2019 14:05
Attach a Ballerina WebSocket service to a http:Listener.
import ballerina/http;
import ballerina/io;
listener http:Listener ep = new (9090);
service wsService = @http:WebSocketServiceConfig {} service {
resource function onText(http:WebSocketCaller wsEp, string text) {
io:println("Sever Received: " + text);
}
resource function onClose(http:WebSocketCaller wsEp, int statusCode, string reason) {
io:println("Connection closed");
@riyafa
riyafa / MatrixMultiplicationStrassen.java
Created October 5, 2019 10:43
Strassen's algorithm for matrix multiplication using index calculations in java: https://riyafa.wordpress.com/?p=965
import java.util.Arrays;
public class MatrixMultiplicationStrassen {
public static void main(String[] args) {
/*int[][] A = new int[][]{{1, 3}, {7, 5}};
int[][] B = new int[][]{{6, 8}, {4, 2}};*/
int[][] A = new int[][]{{13, -3, -25, 20}, {-3, -16, -23, 18}, {20, -7, 12, -5}, {-22, 15, -4, 7}};
int[][] B = new int[][]{{13, 10, 11, 12}, {13, 14, -23, 18}, {20, -7, 12, -11}, {-12, -13, -14, 7}};
System.out.println(Arrays.deepToString(
@riyafa
riyafa / MatrixMultiplicationRecursive.java
Last active February 24, 2021 16:12
Recursive square matrix multiplication in java using index calculations: https://riyafa.wordpress.com/?p=958
import java.util.Arrays;
public class MatrixMultiplicationRecursive {
public static void main(String[] args) {
int[][] A = new int[][]{{13, -3, -25, 20}, {-3, -16, -23, 18}, {20, -7, 12, -5}, {-22, 15, -4, 7}};
int[][] B = new int[][]{{13, 10, 11, 12}, {13, 14, -23, 18}, {20, -7, 12, -11}, {-12, -13, -14, 7}};
System.out.println(Arrays.deepToString(
multiply(A, B, 0, 0, 0, 0, A.length)
));
import ballerina/http;
import ballerina/io;
import ballerina/log;
public function main() {
http:WebSocketClient wsClientEp = new("ws://localhost:9090/ws/xyz/Mawanella?age=26",
config = {callbackService: ClientService, customHeaders:{"X-name":"Riyafa"}, subProtocols:["xml"]});
var err = wsClientEp->pushText("hello");
if (err is error) {
log:printError("Error in sending text", err = err);