Search in sources :

Example 16 with Queue

use of com.google.cloud.tasks.v2.Queue in project algorithms-sedgewick-wayne by reneargento.

the class BucketSort method bucketSort.

public void bucketSort(String[] strings) {
    int alphabetSize = 256;
    Queue<String>[] buckets = new Queue[alphabetSize + 1];
    for (int bucket = 0; bucket < buckets.length; bucket++) {
        buckets[bucket] = new Queue();
    }
    // 1- Put strings in buckets
    for (int i = 0; i < strings.length; i++) {
        int leadingDigitIndex = strings[i].charAt(0);
        buckets[leadingDigitIndex].enqueue(strings[i]);
    }
    // 2- Sort the sublists
    for (int bucket = 0; bucket < buckets.length; bucket++) {
        if (!buckets[bucket].isEmpty()) {
            sortBucket(buckets[bucket]);
        }
    }
    // 3- Stitch together all the buckets
    int arrayIndex = 0;
    for (int r = 0; r <= alphabetSize; r++) {
        while (!buckets[r].isEmpty()) {
            String currentString = buckets[r].dequeue();
            strings[arrayIndex++] = currentString;
        }
    }
}
Also used : Queue(chapter1.section3.Queue)

Example 17 with Queue

use of com.google.cloud.tasks.v2.Queue in project algorithms-sedgewick-wayne by reneargento.

the class Exercise47_CatenableCollections method testQueueCatenation.

private static void testQueueCatenation(Exercise47_CatenableCollections<Integer> catenableCollections) {
    Queue<Integer> queue1 = new Queue<>();
    queue1.enqueue(0);
    queue1.enqueue(1);
    queue1.enqueue(2);
    queue1.enqueue(3);
    Queue<Integer> queue2 = new Queue<>();
    queue2.enqueue(7);
    queue2.enqueue(8);
    queue2.enqueue(9);
    LinkedListCircular<Integer> linkedListCircular = catenableCollections.catenationQueues(queue1, queue2);
    StringJoiner linkedListItems = new StringJoiner(" ");
    for (int item : linkedListCircular) {
        linkedListItems.add(String.valueOf(item));
    }
    StdOut.println("Result after catenation: " + linkedListItems.toString());
    StdOut.println("Expected: 0 1 2 3 7 8 9");
    StdOut.println();
    StringJoiner queue1Items = new StringJoiner(" ");
    for (int item : queue1) {
        queue1Items.add(String.valueOf(item));
    }
    StdOut.println("Queue 1 after catenation: " + queue1Items.toString());
    StdOut.println("Expected: ");
    StdOut.println();
    StringJoiner queue2Items = new StringJoiner(" ");
    for (int item : queue2) {
        queue1Items.add(String.valueOf(item));
    }
    StdOut.println("Queue 2 after catenation: " + queue2Items.toString());
    StdOut.println("Expected: ");
    StdOut.println();
}
Also used : Queue(edu.princeton.cs.algs4.Queue) StringJoiner(java.util.StringJoiner)

Example 18 with Queue

use of com.google.cloud.tasks.v2.Queue in project algorithms-sedgewick-wayne by reneargento.

the class Exercise17 method readAllTransactions.

public static Transaction[] readAllTransactions(String fileName) {
    In in = new In(fileName);
    Queue<Transaction> queue = new Queue<>();
    while (!in.isEmpty()) {
        queue.enqueue(new Transaction(in.readLine()));
    }
    int queueSize = queue.size();
    Transaction[] transactions = new Transaction[queueSize];
    for (int i = 0; i < queueSize; i++) {
        transactions[i] = queue.dequeue();
    }
    return transactions;
}
Also used : Transaction(edu.princeton.cs.algs4.Transaction) In(edu.princeton.cs.algs4.In) Queue(edu.princeton.cs.algs4.Queue)

Example 19 with Queue

use of com.google.cloud.tasks.v2.Queue in project algorithms-sedgewick-wayne by reneargento.

the class Exercise16 method readAllDates.

private static Date[] readAllDates(String fileName) {
    In in = new In(fileName);
    Queue<Date> queue = new Queue<>();
    while (!in.isEmpty()) {
        queue.enqueue(new Date(in.readString()));
    }
    int n = queue.size();
    Date[] dates = new Date[n];
    for (int i = 0; i < n; i++) {
        dates[i] = queue.dequeue();
    }
    return dates;
}
Also used : In(edu.princeton.cs.algs4.In) Queue(edu.princeton.cs.algs4.Queue) Date(edu.princeton.cs.algs4.Date)

Example 20 with Queue

use of com.google.cloud.tasks.v2.Queue in project algorithms-sedgewick-wayne by reneargento.

the class Exercise22_TransactionSortTest method readAllTransactions.

public static Transaction[] readAllTransactions(String fileName) {
    In in = new In(fileName);
    Queue<Transaction> queue = new Queue<>();
    while (!in.isEmpty()) {
        queue.enqueue(new Transaction(in.readLine()));
    }
    int queueSize = queue.size();
    Transaction[] transactions = new Transaction[queueSize];
    for (int i = 0; i < queueSize; i++) {
        transactions[i] = queue.dequeue();
    }
    return transactions;
}
Also used : Transaction(edu.princeton.cs.algs4.Transaction) In(edu.princeton.cs.algs4.In) Queue(edu.princeton.cs.algs4.Queue)

Aggregations

CloudTasksClient (com.google.cloud.tasks.v2.CloudTasksClient)10 Task (com.google.cloud.tasks.v2.Task)8 In (edu.princeton.cs.algs4.In)5 Queue (edu.princeton.cs.algs4.Queue)5 Queue (chapter1.section3.Queue)4 LocationName (com.google.cloud.tasks.v2.LocationName)3 Queue (com.google.cloud.tasks.v2.Queue)3 ByteString (com.google.protobuf.ByteString)3 Test (org.junit.jupiter.api.Test)3 HashSet (chapter3.section5.HashSet)2 AppEngineHttpRequest (com.google.cloud.tasks.v2.AppEngineHttpRequest)2 Transaction (edu.princeton.cs.algs4.Transaction)2 StringJoiner (java.util.StringJoiner)2 UnionFind (chapter1.section5.UnionFind)1 PriorityQueueResize (chapter2.section4.PriorityQueueResize)1 SeparateChainingHashTable (chapter3.section4.SeparateChainingHashTable)1 ApiCallContext (com.google.api.gax.rpc.ApiCallContext)1 UnaryCallable (com.google.api.gax.rpc.UnaryCallable)1 CreateTaskRequest (com.google.cloud.tasks.v2.CreateTaskRequest)1 QueueName (com.google.cloud.tasks.v2.QueueName)1