Skip to content

Instantly share code, notes, and snippets.

@darddan
Last active December 15, 2017 14:08
Show Gist options
  • Save darddan/429c108ec2db594ae05cf89f7b4c456d to your computer and use it in GitHub Desktop.
Save darddan/429c108ec2db594ae05cf89f7b4c456d to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.lang.Math;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
if (args.length == 2) {
printRandom(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
System.out.println("");
return;
} if (args.length == 0) {
Scanner scanner = new Scanner(System.in);
System.out.println("Text array:");
ArrayList<Integer> text = getAsList(scanner.nextLine());
System.out.println("Pattern array");
ArrayList<Integer> pattern = getAsList(scanner.nextLine());
print(text, pattern);
System.out.println("");
return;
} else {
System.out.println("Wrong number of arguments");
}
}
public static void printRandom(int tsize, int psize) {
ArrayList<Integer> text = getRandomList(tsize);
ArrayList<Integer> pattern = getRandomList(psize);
print(text, pattern);
}
public static ArrayList<Integer> getRandomList(int size) {
ArrayList<Integer> numbers = new ArrayList<>();
ArrayList<Integer> ret = new ArrayList<>();
for (int i = 0; i < size; i++) {
numbers.add(i+1);
}
while (numbers.size() > 0) {
int rand = (int) (Math.random() * numbers.size());
ret.add(numbers.remove(rand));
}
return ret;
}
public static ArrayList<Integer> getAsList(String in) {
ArrayList<Integer> ret = new ArrayList<>();
ret.add(0);
for (char c : in.trim().toCharArray()) {
if (c == ' ') ret.add(0);
else {
int pos = ret.size() - 1;
int newVal = ret.get(pos) * 10;
newVal += Character.getNumericValue(c);
ret.set(pos, newVal);
}
}
return ret;
}
public static void print(ArrayList<Integer> text, ArrayList<Integer> pattern) {
print(pattern.size(), "size");
print(text, "text");
print(pattern, "pattern");
}
public static void print(ArrayList<Integer> values, String name) {
for (int i = 0; i < values.size(); i++) {
print((i+1) + "," + values.get(i), name);
}
}
public static void print(int value, String name) {
print(((Integer)value).toString(), name);
}
public static void print(String value, String name) {
System.out.print(name);
System.out.print("(");
System.out.print(value);
System.out.print("). ");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment