Skip to content

Add itinerary tickets code #4340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.thealgorithms.datastructures.hashmap.hashing;

import java.util.HashMap;

/**
* This class represents an itinerary of tickets. Given a list of tickets, it finds the
* complete itinerary.
*/
public class ItineraryTickets {

/**
* This method finds the starting point of the itinerary.
*
* @param tickets A map of tickets where the key is the departure point, and the value is the destination.
* @return The starting point of the itinerary.
*/
public static String getStart(HashMap<String, String> tickets) {
HashMap<String, String> revTickets = new HashMap<>();
for (String key : tickets.keySet()) {
revTickets.put(tickets.get(key), key);
}
for (String key : tickets.keySet()) {
if (!revTickets.containsKey(key)) {
return key; // Starting point
}
Comment on lines +21 to +27
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about such renaming?

Suggested change
for (String key : tickets.keySet()) {
revTickets.put(tickets.get(key), key);
}
for (String key : tickets.keySet()) {
if (!revTickets.containsKey(key)) {
return key; // Starting point
}
for (final var startStation : tickets.keySet()) {
revTickets.put(tickets.get(startStation), startStation);
}
for (final var startStation : tickets.keySet()) {
if (!revTickets.containsKey(startStation)) {
return startStation; // Starting point
}

}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.thealgorithms.datastructures.hashmap.hashing;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.HashMap;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

public class ItineraryTicketsTest {

private HashMap<String, String> tickets;

@BeforeEach
public void setUp() {
tickets = new HashMap<>();
tickets.put("Chennai", "Bengaluru");
tickets.put("Mumbai", "Delhi");
tickets.put("Goa", "Chennai");
tickets.put("Delhi", "Goa");
}

@Test
public void testGetStart() {
String start = ItineraryTickets.getStart(tickets);
assertEquals("Mumbai", start);
}

@Test
public void testGetStartEmptyInput() {
HashMap<String, String> emptyTickets = new HashMap<>();
String start = ItineraryTickets.getStart(emptyTickets);
assertNull(start);
}

@Test
public void testGetStartCircularItinerary() {

tickets.put("Bengaluru", "Mumbai");
String start = ItineraryTickets.getStart(tickets);
assertNull(start);
}
}