Skip to content

Latest commit

 

History

History
220 lines (174 loc) · 6.36 KB

File metadata and controls

220 lines (174 loc) · 6.36 KB

Grouping Transactions by Items' Names

For a given array of transactions, group all of the transactions by item name. Return an array of strings where each string contains the item name followed by a space and the number of associated transactions.

Note: Sort the array descending by transaction count, then ascending alphabetically by item name for items with matching transaction counts.

Example: transactions = ['notebook', 'notebook', 'mouse', 'keyboard', 'mouse']

There are two items with 2 transactions each: 'notebook' and 'mouse'. In alphabetical order, they are 'mouse', 'notebook'.

There is one item with 1 transaction: 'keyboard'.

The return array, sorted as required, is ['mouse 2', 'notebook 2', 'keyboard 1'].

Function Description: Complete the function groupTransactions in the editor below.

groupTransactions has the following parameter(s):     string transactions[n]: each transactions[i] denotes the item name in the ith transaction

Returns:     string[]: an array of strings of "item name[space]transaction count" sorted as described

Constraints:

  • 1 ≤ n ≤ 105
  • 1 ≤ length of transactions[i] ≤ 10
  • transactions[i] contains only lowercase English letters, ascii[a-z]

Input Format Format for Custom Testing

Input from stdin will be processed as follows and passed to the function.

The first line contains a single integer, n, the size of transactions.

Each of the next n lines contains a string, the item name for transactions[i].

Sample Case 0

Sample Input 0:

STDIN     Function
-----	  -----
4      →  transactions[] size n = 4
bin    →  transactions = ['bin', 'can', 'bin', 'bin']
can
bin
bin

Sample Output 0:

bin 3
can 1

Explanation:

  • There is one item 'bin' with 3 transactions.
  • There is one item 'can' with 1 transaction.
  • The return array sorted descending by transaction count, then ascending by name is ['bin 3', 'can 1']. Sample Case 1Sample Input

Sample Case 1

Sample Input 1:

STDIN     Function
-----	  -----
3      →  transactions[] size n = 3
banana →  transactions = ['banana', 'pear', 'apple']
pear
apple

Sample Output 1:

apple 1
banana 1
pear 1

Explanation:

  • There is one item 'apple' with 1 transaction.
  • There is one item 'banana' with 1 transaction.
  • There is one item 'pear' with 1 transaction.
  • The return array sorted descending by transaction count, then ascending by name is ['apple 1', 'banana 1', 'pear 1'].

Starter Code:

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;



class Result {

    /*
     * Complete the 'groupTransactions' function below.
     *
     * The function is expected to return a STRING_ARRAY.
     * The function accepts STRING_ARRAY transactions as parameter.
     */

    public static List<String> groupTransactions(List<String> transactions) {
    // Write your code here
    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int transactionsCount = Integer.parseInt(bufferedReader.readLine().trim());

        List<String> transactions = IntStream.range(0, transactionsCount).mapToObj(i -> {
            try {
                return bufferedReader.readLine();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        })
            .collect(toList());

        List<String> result = Result.groupTransactions(transactions);

        bufferedWriter.write(
            result.stream()
                .collect(joining("\n"))
            + "\n"
        );

        bufferedReader.close();
        bufferedWriter.close();
    }
}

Finished Code:

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;



class Result {

    /*
     * Complete the 'groupTransactions' function below.
     *
     * The function is expected to return a STRING_ARRAY.
     * The function accepts STRING_ARRAY transactions as parameter.
     */

    public static List<String> groupTransactions(List<String> transactions) {
    // Write your code here
        Map<String, Integer> matches = new TreeMap<>();
       List<String> output = new ArrayList<>();
        int count = 1;
        for(int i = 0; i < transactions.size(); i++){
            if (!matches.containsKey(transactions.get(i))){
                matches.put(transactions.get(i), count);
            } else if (matches.containsKey(transactions.get(i))){
                    matches.put(transactions.get(i), matches.get(transactions.get(i)) + 1);
                }
        }
        for(Map.Entry<String, Integer> item : matches.entrySet()) {
            output.add(item.getKey() + " " + item.getValue());
        }
        return output;
    }
}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int transactionsCount = Integer.parseInt(bufferedReader.readLine().trim());

        List<String> transactions = IntStream.range(0, transactionsCount).mapToObj(i -> {
            try {
                return bufferedReader.readLine();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        })
            .collect(toList());

        List<String> result = Result.groupTransactions(transactions);

        bufferedWriter.write(
            result.stream()
                .collect(joining("\n"))
            + "\n"
        );

        bufferedReader.close();
        bufferedWriter.close();
    }
}