S

@supriyo_biswas

String functions in Java

Java
3 years ago
import java.util.*; import java.lang.*; import java.io.*; class Main { public static void main(String[] args) { String s = "hello world"; char[] c = new char[3]; s.getChars(2, 5, c, 0); System.out.println(c); // llo

Run-time type comparison within a Generic hierarchy

Java
3 years ago
import java.util.*; import java.lang.*; import java.io.*; class Main { public static void main(String[] args) { List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5); List<String> list2 = Arrays.asList("foo", "bar", "baz"); System.out.println(list1 instanceof List<Integer>);

Implementing a stack using generics

Java
3 years ago
interface MinMax<T extends Comparable<T>> { T min(); T max(); } class MyClass<T extends Comparable<T>> implements MinMax<T> { T[] vals; MyClass(T[] o) { vals = o;

Implementing a stack using generics

Java
3 years ago
class MyStack<T> { protected Object[] stack; protected int size; protected int tos = -1; MyStack(T ...objs) { this.size = 16; this.stack = new Object[size]; for (T obj: objs) { this.push(obj);

Working with files with Scanner/FileInputStream (feat. try-with-resources)

Java
3 years ago
import java.util.*; import java.lang.*; import java.io.*; class Main { public static void main(String[] args) { try (Scanner sc = new Scanner(new FileInputStream("/etc/passwd"))) { for (int i = 0; i < 10; i++) { System.out.println(sc.nextLine()); }

Using the Scanner class in Java

Java
3 years ago
import java.util.*; import java.lang.*; import java.io.*; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] a = new int[10]; for (int i = 0; i < 10; i++) { a[i] = sc.nextInt();

Annotations and accessing them at runtime

Java
3 years ago
import java.lang.annotation.*; import java.lang.reflect.*; @Retention(RetentionPolicy.RUNTIME) @interface MyAnno { String str(); int val(); } @Retention(RetentionPolicy.RUNTIME)

Annotations and accessing them at runtime

Java
3 years ago
import java.lang.annotation.*; import java.lang.reflect.*; // from Java, The Complete Reference, 9th Edition @Retention(RetentionPolicy.RUNTIME) @interface MyAnno { String str(); int val(); }

Enumerations - values() and valueOf()

Java
3 years ago
import java.util.*; import java.lang.*; import java.io.*; enum Fruit { Apple, Orange, Pear, Banana, Kiwi, Watermelon }; class Main { public static void main(String[] args) {

Using wait() and notify() methods in threads

Java
3 years ago
class Demo { volatile boolean part1done = false; synchronized void part1() { System.out.println("From thread1"); part1done = true; System.out.println("Thread t1 surrendering"); notify(); }

Collecting running times of threads into a ConcurrentLinkedQueue

Java
3 years ago
import java.util.concurrent.*; import java.util.*; class MyRunnable implements Runnable { Queue<Long> queue; MyRunnable(Queue<Long> queue) { this.queue = queue; }

Collecting results from threads into a ConcurrentLinkedQueue

Java
3 years ago
import java.util.concurrent.*; import java.util.*; class MyRunnable implements Runnable { Queue<Integer> queue; MyRunnable(Queue<Integer> queue) { this.queue = queue; }

Implementing Runnable class

Java
3 years ago
import java.util.concurrent.*; class MyRunnable implements Runnable { public void run() { try { int randomNum = ThreadLocalRandom.current().nextInt(0, 200); Thread.sleep(randomNum); System.out.printf("Slept for %d seconds\n", randomNum); } catch (InterruptedException e) { System.out.println("Interrupted!");

Working with the main thread

Java
3 years ago
import java.util.*; import java.lang.*; import java.io.*; class Main { public static void main(String[] args) { Thread t = Thread.currentThread(); System.out.println("current thread: " + t); t.setName("my thread"); System.out.println("after name change: " + t);

Demonstrating the use of static keyword

Java
3 years ago
import java.util.*; import java.lang.*; import java.io.*; // from Java, The Complete Reference, 9th Edition. class UseStatic { static int a = 3; static int b;

Shift right vs shift right zero fill

Java
3 years ago
import java.util.*; import java.lang.*; import java.io.*; class Main { public static void main(String[] args) { // shift right (<<) preserves the sign int a = 10; int a1 = a >> 2; System.out.printf("%d >> 2 = %d\n", a, a1);

Palindrome partitioning

Python
3 years ago
# Given a string s, partition substrings in the string s # such that every substring in it is a palindrome. Return # all possible palindrome partitions of s. def is_palindrome(s, start, end): while start < end: if s[start] != s[end]: return False start += 1 end -= 1

Splitting strings using Regexp

Go
3 years ago
package main import ( "fmt" "regexp" ) func main() { re := regexp.MustCompile("\\s+") s := "MAIL FROM:<a@b.com> TEST"

Parsing RFC3339 formatted date and time

Go
3 years ago
package main import ( "fmt" "time" ) func main() { ts := "2022-12-07T00:00:01.532Z" layout := time.RFC3339

Check varying bits for Random.nextFloat()

Java
3 years ago
import java.util.*; import java.lang.*; import java.io.*; class Main { public static String padLeftZeros(String inputString, int length) { if (inputString.length() >= length) { return inputString; } StringBuilder sb = new StringBuilder();