contains common code snippets.
coroutines:
implementation 'androidx.fragment:fragment:1.4.1'
implementation 'androidx.fragment:fragment-ktx:1.4.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.0-rc01'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.1'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.okhttp3:logging-interceptor:4.9.3'
implementation 'com.squareup.retrofit2:converter-scalars:2.7.1'
reading response from http connection
fun getData(url: String) : Result<String> {
val urlObj = URL(url)
(urlObj.openConnection() as? HttpURLConnection)?.run {
requestMethod = "GET"
val reader = BufferedReader(InputStreamReader(inputStream))
val result = StringBuffer()
var line: String?
while (reader.readLine().also { line = it } != null) {
result.append(line)
}
return Result.Success(result.toString())
}
return Result.Error(Exception("Cannot open HttpURLConnection"))
}
}
sealed class Result<out R> {
data class Success<out T>(val data: T) : Result<T>()
data class Error(val exception: Exception) : Result<Nothing>()
}
int[][] dirs = {{0,1},{1,0},{0,-1},{-1,0}};
int[][] dirs = {{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};
void reverse(int[] nums){
int i = 0;
int j = nums.length - 1;
while(i < j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
// usage - PriorityQueue<MapTypePQ<Integer, int[]>> maxQue = new PriorityQueue<>((a,b) -> b.key - a.key);
// PriorityQueue<MapTypePQ<Integer, int[]>> minQue = new PriorityQueue<>((a,b) -> a.key - b.key);
class MapTypePQ<T,K> {
public T key;
public K value;
public MapTypePQ(T key, K value){
this.key = key;
this.value = value;
}
}