Skip to content

Instantly share code, notes, and snippets.

@calexandrepcjr
Created December 16, 2022 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calexandrepcjr/dc18c74702d8bf0e3f30da152aee6664 to your computer and use it in GitHub Desktop.
Save calexandrepcjr/dc18c74702d8bf0e3f30da152aee6664 to your computer and use it in GitHub Desktop.
Code Challenge
/*
Java Age counting
In the Java file, write a program to perform a GET request on the route:
https://coderbyte.com/api/challenges/json/age-counting
Which contains a data key and the value is a string which contains items in the format:
key=STRING, age=INTEGER
Your goal is to count how many items exist that have an age equal to or greater than 50, and print this final value.
Example Input
{"data": "key=IAfpK, age=58, key=WNVdi, age=64, key=jp9zt, age=47"}
Example output
2
/*
import java.util.*;
import java.io.*;
import java.net.*;
import java.util.stream.Collectors;
class Main {
private final static int MAX_AGE = 50;
public static void main (String[] args) {
System.setProperty("http.agent", "Chrome");
try {
AgeRepository ageRepository = new AgeRepository(null);
var ageByKey = ageRepository.countAgeByKey();
var ages = new ArrayList<>(ageByKey.aValue.values());
var amountOfAgesHigherThanMaxAge = ages
.stream()
.filter(age -> age >= Main.MAX_AGE)
.collect(Collectors.toList())
.size();
System.out.println(amountOfAgesHigherThanMaxAge);
} catch (IOException malEx) {
System.out.println(malEx);
}
}
}
class AgeRepository {
private URL url;
public AgeRepository(URL url) throws MalformedURLException {
this.url = url == null ? new URL("https://coderbyte.com/api/challenges/json/age-counting") : url;
}
public AgeByKey countAgeByKey() throws IOException {
String response = new String();
URLConnection connection = this.url.openConnection();
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
for (String line; (line = reader.readLine()) != null;) {
response += line;
}
String[] keyValueJsonTupleResponse = response.split(":");
if (keyValueJsonTupleResponse.length < 2) {
return new AgeByKey("");
}
return new AgeByKey(keyValueJsonTupleResponse[1]);
}
}
final class AgeByKey {
public final Map<String, Integer> aValue = new HashMap<String, Integer>();
public AgeByKey(String value) {
aValue.putAll(parseRawValue(value));
}
private Map<String, Integer> parseRawValue(String value) {
String[] valueByAttributes = value.split(",");
if (valueByAttributes.length == 0) {
return new HashMap<String, Integer>();
}
var attributesTuple = new ArrayList<List<String>>();
for (int i = 0; i < valueByAttributes.length - 2; i+=2) {
attributesTuple.add(
Arrays.asList(
this.getValueFromKeyValueAttribution(valueByAttributes[i]),
this.getValueFromKeyValueAttribution(valueByAttributes[i+1])
)
);
}
return attributesTuple.stream().collect(
Collectors.toConcurrentMap(
v -> v.get(0),
v -> Integer.parseInt(v.get(1)),
(v, v2) -> v2
)
);
}
// Key=Value format
private String getValueFromKeyValueAttribution(String keyValueAttribution) {
return keyValueAttribution.substring(keyValueAttribution.lastIndexOf("=") + 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment