Created
December 8, 2021 18:28
-
-
Save ravichandrae/6aeddb5512c419c7db6e5dff3420797e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.util.*; | |
public class SecretSanta { | |
public static void main(String[] args) { | |
File inputFile = new File("names.txt"); | |
List<String> names = readNamesFromFile(inputFile); | |
Map<String, String> secretSanta = generateSecretSanta(names); | |
printSecretSanta(secretSanta); | |
} | |
/** | |
* Utility method to read from a file | |
* @param inputFile | |
* @return | |
*/ | |
private static List<String> readNamesFromFile(File inputFile) { | |
List<String> names = new ArrayList<>(); | |
try { | |
Scanner fileReader = new Scanner(inputFile); | |
while (fileReader.hasNext()) { | |
//Assuming one line per name | |
names.add(fileReader.nextLine()); | |
} | |
} catch (FileNotFoundException fileNotFoundException) { | |
System.out.println("Input file not found"); | |
} | |
return names; | |
} | |
/** | |
* Generate a shuffled list from the original list and generate a random secret santa for each name | |
* @param names | |
* @return | |
*/ | |
private static Map<String, String> generateSecretSanta(List<String> names) { | |
return zipListsToMap(names, getShuffledList(names)); | |
} | |
/** | |
* This method shuffles the given list of strings so that no string is in the original place | |
* @param list input list of strings | |
* @return shuffled list of strings | |
*/ | |
private static List<String> getShuffledList(List<String> list) { | |
List<String> shuffled = new ArrayList<>(list); | |
for (int i = 0; i < list.size() - 1; i++) { | |
int randomIndex = getRandomNumber(i + 1, shuffled.size()); | |
swapNames(shuffled, i, randomIndex); | |
} | |
return shuffled; | |
} | |
/** | |
* Combines two lists into a map, assuming that two lists are of same length | |
* @param list1 list of keys | |
* @param list2 list of corresponding values for keys in list1 | |
* @return Map | |
*/ | |
private static Map<String, String> zipListsToMap(List<String> list1, List<String> list2) { | |
Map<String, String> map = new HashMap<>(); | |
for(int i = 0; i < list1.size(); i++) { | |
map.put(list1.get(i), list2.get(i)); | |
} | |
return map; | |
} | |
/** | |
* Utility method to print a map | |
* @param secretSanta a Map | |
*/ | |
private static void printSecretSanta(Map<String, String> secretSanta) { | |
for (String name : secretSanta.keySet()) { | |
System.out.println(name + " -> " + secretSanta.get(name)); | |
} | |
} | |
/** | |
* Generate a random number between [min, max) - min inclusive, max exclusive | |
* @param min : int | |
* @param max : int | |
* @return a random integer in the given range | |
*/ | |
public static int getRandomNumber(int min, int max) { | |
return (int) ((Math.random() * (max - min)) + min); | |
} | |
/** | |
* Swap two elements in a list at index1, and index2 | |
* @param names : List of strings | |
* @param index1 : first index | |
* @param index2 : second index | |
*/ | |
private static void swapNames(List<String> names, int index1, int index2) { | |
String temp = names.get(index1); | |
names.set(index1, names.get(index2)); | |
names.set(index2, temp); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment