Last active
May 15, 2018 09:13
-
-
Save jesty/1eafe092bcbaced6a114e7353e3bff1d 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
package com.agiledeveloper; | |
import java.util.*; | |
class Fibonacci { | |
public static int numberAtPosition(int position) { | |
int current = 1; | |
int next = 1; | |
if(position < 2) return 1; | |
for(int i = 2; i <= position; i++) { | |
int temp = current + next; | |
current = next; | |
next = temp; | |
} | |
return next; | |
} | |
public static List<Integer> seriesUntilPosition(int position) { | |
List<Integer> result = new ArrayList<>(); | |
if(position >= 0) | |
result.add(1); | |
if(position >= 1) | |
result.add(1); | |
for(int i = 2; i <= position; i++) { | |
result.add(result.get(result.size() - 1) + result.get(result.size() - 2)); | |
} | |
return result; | |
} | |
} |
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
package com.agiledeveloper; | |
import java.util.*; | |
public class Maps { | |
public static Map<Integer, List<String>> listByScore(Map<String, Integer> scores) { | |
Map<Integer, List<String>> byScores = new HashMap<>(); | |
for(String name : scores.keySet()) { | |
int score = scores.get(name); | |
List<String> names = new ArrayList<>(); | |
if(byScores.containsKey(score)) | |
names = byScores.get(score); | |
names.add(name); | |
byScores.put(score, names); | |
} | |
return byScores; | |
} | |
public static Map<Integer, Set<String>> setByScore(Map<String, Integer> scores) { | |
Map<Integer, Set<String>> byScores = new HashMap<>(); | |
for(String name : scores.keySet()) { | |
int score = scores.get(name); | |
Set<String> names = new HashSet<>(); | |
if(byScores.containsKey(score)) | |
names = byScores.get(score); | |
names.add(name.toUpperCase()); | |
byScores.put(score, names); | |
} | |
return byScores; | |
} | |
public static Map<Integer, Integer> countScores(Map<String, Integer> scores) { | |
Map<Integer, Integer> byScores = new HashMap<>(); | |
for(String name : scores.keySet()) { | |
int score = scores.get(name); | |
int count = 0; | |
if(byScores.containsKey(score)) | |
count = byScores.get(score); | |
byScores.put(score, count + 1); | |
} | |
return byScores; | |
} | |
public static Map<Integer, Set<Integer>> numberOfLetters(Map<String, Integer> scores) { | |
Map<Integer, Set<Integer>> byScores = new HashMap<>(); | |
for(String name : scores.keySet()) { | |
int score = scores.get(name); | |
Set<Integer> letters = new HashSet<>(); | |
if(byScores.containsKey(score)) | |
letters = byScores.get(score); | |
letters.add(name.length()); | |
byScores.put(score, letters); | |
} | |
return byScores; | |
} | |
public static Map<Integer, Integer> maxNumberOfLetters(Map<String, Integer> scores) { | |
Map<Integer, Integer> byScores = new HashMap<>(); | |
for(String name : scores.keySet()) { | |
int score = scores.get(name); | |
int maxLength = 0; | |
if(byScores.containsKey(score)) | |
maxLength = byScores.get(score); | |
maxLength = maxLength < name.length() ? name.length() : maxLength; | |
byScores.put(score, maxLength); | |
} | |
return byScores; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment