patternjavaMinor
Find the Biggest Traveler
Viewed 0 times
biggestthetravelerfind
Problem
This is from one of those online test sites, so the result had to be contained in a single class.
The problem was stated as follows:
Your java method will be given a 2-D array
representing a travel log for a group of people. For each 'n',
the city visited by the person in the respective trip. More than one
person can visit the same city, and an individual can visit the same
city more than once.
Your java method will output a string, containing the name of the
biggest traveler. The biggest traveler will be determined like this:
-
Who made the most trips?
-
Tie breaker: who visited the most cities?
-
Tie breaker of tie breaker: who' s travel is most distributed?
(fewest trips to their most visited city)
So you can test your algorithm, you can try it with data formatted
like this:
Note: the 12 at the top indicates how many trip there are in the data
set
Note: Order of the lines may be jumbled in an actual example
In the above example:
-
Person B and person C each made 5 trips...Tie
-
Person B and person C each visited 3 cities...Tie
-
Person C
visited the same city 3 times; Person B's most visited is 2.
Correct return value: "Person B"
And my solution was as included below. I added a main method to test the example problem, but in practice the input would be handled by the default code provided in the web site:
```
import java.util.*;
public class BiggestTraveler {
public static class TravelerInfo {
//map that contains the cities a person has visited and how times they have visited it
String name; //not including setters just because
The problem was stated as follows:
Your java method will be given a 2-D array
trip[n][2] of strings,representing a travel log for a group of people. For each 'n',
trip[n][0] is the name of the person and travel [n][1] is the name ofthe city visited by the person in the respective trip. More than one
person can visit the same city, and an individual can visit the same
city more than once.
Your java method will output a string, containing the name of the
biggest traveler. The biggest traveler will be determined like this:
-
Who made the most trips?
-
Tie breaker: who visited the most cities?
-
Tie breaker of tie breaker: who' s travel is most distributed?
(fewest trips to their most visited city)
So you can test your algorithm, you can try it with data formatted
like this:
12
Person A, City 1
Person A, City 2
Person B, City 1
Person B, City 3
Person B, City 3
Person B, City 4
Person B, City 4
Person C, City 1
Person C, City 2
Person C, City 2
Person C, City 2
Person C, City 4Note: the 12 at the top indicates how many trip there are in the data
set
Note: Order of the lines may be jumbled in an actual example
In the above example:
-
Person B and person C each made 5 trips...Tie
-
Person B and person C each visited 3 cities...Tie
-
Person C
visited the same city 3 times; Person B's most visited is 2.
Correct return value: "Person B"
And my solution was as included below. I added a main method to test the example problem, but in practice the input would be handled by the default code provided in the web site:
```
import java.util.*;
public class BiggestTraveler {
public static class TravelerInfo {
//map that contains the cities a person has visited and how times they have visited it
String name; //not including setters just because
Solution
In
main you can use the more concise syntaxString[][] trips = {
{ "Person A", "City 1" },
{ "Person A", "City 2" },
{ "Person B", "City 1" },
{ "Person B", "City 3" },
{ "Person B", "City 3" },
{ "Person B", "City 4" },
{ "Person B", "City 4" },
{ "Person C", "City 1" },
{ "Person C", "City 2" },
{ "Person C", "City 2" },
{ "Person C", "City 2" },
{ "Person C", "City 4" }
};Code Snippets
String[][] trips = {
{ "Person A", "City 1" },
{ "Person A", "City 2" },
{ "Person B", "City 1" },
{ "Person B", "City 3" },
{ "Person B", "City 3" },
{ "Person B", "City 4" },
{ "Person B", "City 4" },
{ "Person C", "City 1" },
{ "Person C", "City 2" },
{ "Person C", "City 2" },
{ "Person C", "City 2" },
{ "Person C", "City 4" }
};Context
StackExchange Code Review Q#82313, answer score: 3
Revisions (0)
No revisions yet.