Wednesday, October 11, 2023

EM-Tirupati Codeathon Series #02

[Question]

Algorithms/Data Structures — [Problem Solving]

An Institutional Broker wants to Review their Book of Customers to see which are Most Active. Given a List of Trades By “Customer Name, Determine which Customers Account for At Least 5% of the Total Number of Trades. Order the List Alphabetically Ascending By Name.”

Example

n = 23

“Customers = {“Big Corp”, “Big Corp”, ”Acme”, “Big Corp”, “Zork” ,”Zork” ,”Abe”, “Big Corp”, “Acme”, “Big Corp” ,”Big Corp”, “Zork”, “Big Corp”, “Zork”, “Zork”, “Big Corp”,” Acme”, ”Big Corp”, “Acme”, “Big Corp”, “Acme”, “Little Corp”, “Nadir Corp”}

 “Big Corp had 10 Trades out of 23, which is 43.48% of the Total Trades.”

 “Both Acme and Zork had 5 trades, which is 21.74% of the Total Trades.”

“The Little Corp, Nadir Corp and Abe had 1 Trade Each, which is 4.35%…”

“So the Answer is [“”Acme””, “” Big Corp ,””Zork””] (In Alphabetical Order) Because only These Three Companies Placed at least 5% of the Trades.

Constraints

• 1 < n < 10⁵
• 1 < Length of customers[] < 20
• The First Character of customers[i] is a Capital English letter.
• All Characters of customers[i] except for the First One are Lowercase.
 Guaranteed that At least One Customer makes at least 5% of Trades.

[Sample Input]

“The First Line contains an integer, n, The Number of Elements in customers.”

“Each Line of the n Subsequent Lines (where 0 s i< n) contains a string, customers[i].”

Sample Case 0 Input For Custom Testing

20

Omega Alpha Omega Alpha Omega Alpha Omega Alpha Omega Alpha Omega Alpha Omega Alpha Omega Alpha Omega Alpha Omega Beta

Fnction Description

Complete the function mostActive in the Editor Below.

  • mostActive has the following parameter:
  • String customers[n]: An Array Customers Names
  • (Actual Questions Says String Array, But Signatures is List of Strings)
  • Returns String[] : An Alphabetically Ascending Array

Constraints

• 1 < n < 10⁵

• 1 < Length of customers[] < 20

• The First Character of customers[i] is a Capital English letter.

• All Characters of customers[i] except for the First One are Lowercase.

• Guaranteed that At least One Customer makes at least 5% of Trades.

[Sample Input]

“The First Line contains an integer, n, The Number of Elements in customers.”

“Each Line of the n Subsequent Lines (where 0 s i< n) contains a string, customers[i].”

Sample Case 0 Input For Custom Testing

20

Omega Alpha Omega Alpha Omega Alpha Omega Alpha Omega Alpha Omega Alpha Omega Alpha Omega Alpha Omega Alpha Omega Beta

Function most ACTIVE

customers[] size n = 20

customers[] = [As Provided Above]

[Sample Output]

Alpha

Beta

Omega

[Explanation of Solution]

In this problem  Alpha made 10 Trades out of 20 (50% of the Total), Omega made 9 Trades (45% of the Total). and Beta made 1 Trade (5% of the Total).All of them have met the 5% Threshold, so all the Strings are Returned in an Alphabetically Ordered Array.

Code Explaination:

  1. This code is written to identify and display the most active traders based on a given list of trades.
  1. The code consists of two classes: MostActiveTraders and Codeathon02_Venkat.
  1. MostActiveTraders contains the core logic for finding and displaying the most active traders, while Codeathon02_Venkat serves as the entry point for user input and result display.
  1. It employs two primary data structures: a Map<String, Integer> named tradeCounts to store trade counts for each trader and a List<String> named activeTraders to store the most active traders.
  1. User input is collected through a Scanner, allowing users to specify the number of trades they want to analyze and provide a list of trades (one trade per line).
  1. The findMostActiveTraders method counts the trades for each trader using a for-each loop and sets a threshold based on the number of trades (in this case, 5% of the total).
  1. Active traders are identified by iterating through the tradeCounts map and adding those whose trade counts exceed the threshold to the activeTraders list.
  1. Active traders are sorted alphabetically using Collections.sort.
  1. The displayActiveTraders method is used to display the active traders to the user.
  1. The code includes exception handling for potential IllegalArgumentException, although there is no specific case in the given code where this exception is thrown.

GitHub Repo link: https://rb.gy/609wc

Source Code(Java):

import java.util.*;

class MostActiveTraders {

    public List<String> findMostActiveTraders(List<String> trades, int numTrades) {

        Map<String, Integer> tradeCounts = new HashMap<>();


        // Count the trades for each trader

        for (String trader : trades) {

            tradeCounts.put(trader, tradeCounts.getOrDefault(trader, 0) + 1);

        }

        int threshold = numTrades * 5 / 100;

        List<String> activeTraders = new ArrayList<>();

        // Find traders with trade counts above the threshold

        for (Map.Entry<String, Integer> entry : tradeCounts.entrySet()) {

            if (entry.getValue() > threshold) {

                activeTraders.add(entry.getKey());

            }

        }

        // Sort the active traders alphabetically

        Collections.sort(activeTraders);

        int totalTrades = trades.size();

        return activeTraders;

    }

    void displayActiveTraders(List<String> activeTraders) {

        System.out.println("Most Active Traders:");

        for (String trader : activeTraders) {

            System.out.println(trader);

        }

    }

}

 

public class Codeathon02_Venkat {

    public static void main(String[] args) {

        MostActiveTraders mat = new MostActiveTraders();

        Scanner scanner = new Scanner(System.in);

        List<String> trades = new ArrayList<>();

        System.out.print("Enter the number of trades you want: ");

        int numTrades = scanner.nextInt();

        scanner.nextLine();

        System.out.println("Enter a list of trades (one trade per line):");

 

        while (trades.size() < numTrades) {

            String input = scanner.nextLine();

            trades.add(input);

        }

        try {

            List<String> activeTraders = mat.findMostActiveTraders(trades, numTrades);

            mat.displayActiveTraders(activeTraders);

        } catch (IllegalArgumentException e) {

            System.out.println("Error: " + e.getMessage());

        }

        scanner.close();

    }

Thank you

Venkata kishore T(Intern)

Shield Warriors,

Data Shield Team,

Enterprise Minds.

No comments:

Post a Comment

EM-Tirupati Codeathon Series #08

[Question] ROBOTIC CRICKET MATCH you should write a program that simulate an automatic cricket match between India and Sri Lanka. The fo...