Write the following. Include a type declaration for each value or function. You
ID: 3815363 • Letter: W
Question
Write the following. Include a type declaration for each value or function. You should not define a function with one word (because there happens to be an identical function built in) or define a value directly (instead of constructing it with, e.g., a list comprehension) A function "duplicates that takes a list and returns true if any two elements in the list are the same. A list of pairs 'rolls' showing all possible rolls of two six-sided dice. A function 'counts' that takes a list and returns a list of pairs, with each pair containing an element and the number of times it appears. A list of pairs 'sums', showing how often each sum appears when rolling two six-sided dice.Explanation / Answer
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Test {
public static void main(String[] args) {
Test test = new Test();
//create list having duplicate elements
List<String> list = Arrays.asList(new String[] {"A","B","C","A"});
System.out.println("Check duplicacy in list: ");
System.out.println(test.checkDuplicates(list));
System.out.println("All possible dice rolls are: ");
test.allPossibleDiceRolls("", 2);
System.out.println("Count digit frequency: ");
test.countDigitFrequency(new int[] {1,2,3,1,1,1,2,2,3,4,6,5,5,5});
System.out.println("Number appears in dice");
Map<Integer, Integer> map = test.getDiceMapOfNumberList(new int[] {4,7,8,5});
Set<Integer> keys = map.keySet();
for(int key : keys) {
System.out.println("number " +key+" comes => " + map.get(key) + " times" );
}
}
public <T> boolean checkDuplicates(List<T> list) {
Set<T> set = new HashSet<T>();
for(T data : list)
if(!set.add(data))
return true;
return false;
}
public void allPossibleDiceRolls(String s, int depth) {
if(depth == 0)
System.out.println(s.substring(1));
else
for(int i = 1; i <= 6; i++)
allPossibleDiceRolls(s + "," + i, depth - 1);
}
public void countDigitFrequency(int x[]) {
HashMap<Integer,Integer>digits=new HashMap<Integer,Integer>();
for (int i : x){
if (digits.containsKey(i)){
digits.put(i, digits.get(i)+1);
} else {
digits.put(i, 1);
}
}
for (int key:digits.keySet()){
System.out.println(key+" comes "+digits.get(key) + " times");
}
}
private int numberPossibilitiesInRollingDices(int numberOfDice, int sum) {
if (numberOfDice == sum)
return 1;
else if (numberOfDice == 0 || sum < numberOfDice)
return 0;
else
return numberPossibilitiesInRollingDices(numberOfDice, sum - 1) +
numberPossibilitiesInRollingDices(numberOfDice - 1, sum - 1) -
numberPossibilitiesInRollingDices(numberOfDice - 1, sum - 7);
}
public Map<Integer, Integer> getDiceMapOfNumberList(int[] numbers) {
Map<Integer, Integer> diceMap = new HashMap<Integer, Integer>();
for(int num : numbers) {
diceMap.put(num,numberPossibilitiesInRollingDices(2, num));
}
return diceMap;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.