MCPcopy Create free account
hub / github.com/careercup/ctci / AssortedMethods

Class AssortedMethods

java/CtCILibrary/CtCILibrary/AssortedMethods.java:6–570  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4import java.awt.*;
5
6public class AssortedMethods {
7 public static int randomInt(int n) {
8 return (int) (Math.random() * n);
9 }
10
11 public static int randomIntInRange(int min, int max) {
12 return randomInt(max + 1 - min) + min;
13 }
14
15 public static boolean randomBoolean() {
16 return randomIntInRange(0, 1) == 0;
17 }
18
19 public static boolean randomBoolean(int percentTrue) {
20 return randomIntInRange(1, 100) <= percentTrue;
21 }
22
23 public static int[][] randomMatrix(int M, int N, int min, int max) {
24 int[][] matrix = new int[M][N];
25 for (int i = 0; i < M; i++) {
26 for (int j = 0; j < N; j++) {
27 matrix[i][j] = randomIntInRange(min, max);
28 }
29 }
30 return matrix;
31 }
32
33 public static int[] randomArray(int N, int min, int max) {
34 int[] array = new int[N];
35 for (int j = 0; j < N; j++) {
36 array[j] = randomIntInRange(min, max);
37 }
38 return array;
39 }
40
41 public static LinkedListNode randomLinkedList(int N, int min, int max) {
42 LinkedListNode root = new LinkedListNode(randomIntInRange(min, max),
43 null, null);
44 LinkedListNode prev = root;
45 for (int i = 1; i < N; i++) {
46 int data = randomIntInRange(min, max);
47 LinkedListNode next = new LinkedListNode(data, null, null);
48 prev.setNext(next);
49 prev = next;
50 }
51 return root;
52 }
53
54 public static LinkedListNode linkedListWithValue(int N, int value) {
55 LinkedListNode root = new LinkedListNode(value, null, null);
56 LinkedListNode prev = root;
57 for (int i = 1; i < N; i++) {
58 LinkedListNode next = new LinkedListNode(value, null, null);
59 prev.setNext(next);
60 prev = next;
61 }
62 return root;
63 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected