Skip to content

Instantly share code, notes, and snippets.

@rr-codes
Last active August 27, 2018 19:57
Show Gist options
  • Save rr-codes/fedbca96ce325fd30b163c69ea1c2f29 to your computer and use it in GitHub Desktop.
Save rr-codes/fedbca96ce325fd30b163c69ea1c2f29 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.util.*;
public class SafeOrbits {
public static void main(String[] args) throws Exception {
Scanner fileName = new Scanner(System.in);
System.out.println("Please enter filename: ");
Scanner inputFile = new Scanner(new File(fileName.nextLine()));
fileName.close();
List<Double> list = new ArrayList<>();
while (inputFile.hasNextLine()) {
String[] line = inputFile.nextLine.split(",");
list.add(Double.parseDouble(line[1]))
}
inputFile.close();
Collections.sort(list);
Double minDiff = Double.MAX_VALUE;
Double SAFE_DIST = 5;
for (int i = 1; i < list.size(); i++) {
minDiff = Math.min(list.get(i) - list.get(i-1), minDiff);
}
System.out.println("They are " + (minDiff < SAFE_DIST ? "not " : "") + "in safe orbits");
System.out.println("The minimum distance between orbits (km): " + minDiff);
}
}
public class MaxFrequency {
public static void main(String[] args) throws Exception {
Scanner fileName = new Scanner(System.in);
System.out.println("Please enter filename: ");
Scanner inputFile = new Scanner(new File(fileName.nextLine()));
fileName.close();
String maxKey = null;
int maxFreq = 0;
Map<String, Integer> map = new TreeMap<>();
while (inputFile.hasNextLine()) {
String word = inputFile.nextLine();
Integer freq = map.get(word);
map.put(word, (freq == null) ? 1 : freq + 1);
}
inputFile.close()
for (String key : map.keySet()) {
int freq = map.get(key);
System.out.println("Word: " + key + ", Frequency: " + freq);
if (freq > maxFreq) {
maxFreq = freq;
maxKey = key;
}
}
System.out.println("\nMaximum: " + maxKey + ", " + maxFreq);
}
}
public class DuplicateCheck {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num;
System.out.println("Enter sequence of numbers: ");
List<Integer> list = new ArrayList<>();
do {
num = input.nextInt();
list.add(num);
} while (num >= 0);
input.close();
Set<Integer> set = new TreeSet<>(list);
System.out.println("They are " + ((set.size() < list.size()) ? "not " : "") + "distinct" );
System.out.println("Sorted elements: " + set);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment