Question

how to pass in an object from an array of objects in a binary search on...

how to pass in an object from an array of objects in a binary search on swift? I have the following:

   func binarySearchPrefix(array: [String], target: String) -> Bool {
      
        var left = 0
        var right = array.count - 1
      
        while (left <= right) {
            let mid = (left + right) / 2
            let value = array[mid]
            if (value.hasPrefix(target)) {
                return true
            }
            if (value < target) {
                left = mid + 1
            }
          
            if (value > target) {
                right = mid - 1
            }
        }
        return false
    }

how can i call this using object

struct someObject {

    var value: String = "hello"
    var key: String = "hello"
}

var Thing = someObject()

var Thing1 = someObject()

var Thing2 = someObject()

var arrayOfObjects [Thing, Thing1, Thing2,]

I want to call the function and pass in Thing.value and Thing.key into the binary search, but from the arrayofObjects, not directly. How would I do it?

I tried

binarySearchPrefix(array: arrayOfObjects, target: "hello" ) but it says it's looking for a String, it seems like I need to modify my binary search. Any ideas?

Homework Answers

Answer #1

Hi first it is not a question of Algebra.Its Computer Science.

You are trying it incorrectly.In your function binarySearchPrefix the parameter is array of String( func binarySearchPrefix(array: [String], target: String) -> Bool).

But when you call this function you pass an array of object.

You have two option

1. By Modify func as:

func binarySearchPrefix(array: [someObject], target: String) -> Bool

In this case you also need to change logic of function.

2. in function call pass array of String.

var arrayOfObjects = [Thing.value, Thing1.value, Thing2.value]

and pass it to function.

Please Thumbs up.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In this lab, you will write a program that creates a binary search tree based on...
In this lab, you will write a program that creates a binary search tree based on user input. Then, the user will indicate what order to print the values in. **Please write in C code** Start with the bst.h and bst.c base code provided to you. You will need to modify the source and header file to complete this lab. bst.h: #ifndef BST_H #define BST_H typedef struct BSTNode { int value; struct BSTNode* left; struct BSTNode* right; } BSTNode; BSTNode*...
JAVA: Design and implement a recursive version of a binary search.  Instead of using a loop to...
JAVA: Design and implement a recursive version of a binary search.  Instead of using a loop to repeatedly check for the target value, use calls to a recursive method to check one value at a time.  If the value is not the target, refine the search space and call the method again.  The name to search for is entered by the user, as is the indexes that define the range of viable candidates can be entered by the user (that are passed to...
Create an add method for the BST (Binary Search Tree) class. add(self, new_value: object) -> None:...
Create an add method for the BST (Binary Search Tree) class. add(self, new_value: object) -> None: """This method adds new value to the tree, maintaining BST property. Duplicates must be allowed and placed in the right subtree.""" Example #1: tree = BST() print(tree) tree.add(10) tree.add(15) tree.add(5) print(tree) tree.add(15) tree.add(15) print(tree) tree.add(5) print(tree) Output: TREE in order { } TREE in order { 5, 10, 15 } TREE in order { 5, 10, 15, 15, 15 } TREE in order {...
Binary Search Tree with multiple structs? Hi, I am having an issue with trying to create...
Binary Search Tree with multiple structs? Hi, I am having an issue with trying to create a binary search tree while having multiple structs. The struct code provided is provided for us. #define CAT_NAME_LEN 25 #define APP_NAME_LEN 50 #define VERSION_LEN 10 #define UNIT_SIZE 3 struct app_info{ char category[CAT_NAME_LEN]; // name of category char app_name[APP_NAME_LEN]; // name of application char version[VERSION_LEN]; // version number float size; // size of application char units[UNIT_SIZE]; // GB or MB float price; // price in...
In Java is there a way I can put a value in a linkList = array[...
In Java is there a way I can put a value in a linkList = array[ ] string (I don't know how to word it here what I have so far with my code) * Example object 51 I want to equal Array [0]; // Project code below public String getCardRank() { String values[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"}; return "RRR"; } // this part is in another .java file private...
How do you pass two class objects as parameters? Also, if the object sq can be...
How do you pass two class objects as parameters? Also, if the object sq can be used to call getters, then what is the purpose of the move object? I would like an example with an explanation of how to do this in java since I don't understand this. Here is this example of a method with two objects as parameters. private void move(Board sq, Board move){ ... space[sq.getX()][sq.getY()]; }
1. Write a function named "countPositiveNegative" that accepts an array of integers, its size and return...
1. Write a function named "countPositiveNegative" that accepts an array of integers, its size and return the number of positive and negative in the array through the use of parameters. In addition, it also returns the number of 0s in the array as the return value. For example, array of {10, 20, -30, -40, 50, 0, 60} will return 4 for positives, 2 for negatives and 1 for 0's as the return value. 2. Write a function named "rotateRight" that...
You are given a reference to the root node of a binary search tree, that implements...
You are given a reference to the root node of a binary search tree, that implements a dictionary data structure. Please print all the elements in depths 500 through 510, all in sorted order. A node in a binary search tree is at depth x, if it takes x hops to get from the root. So the root is at depth 0, the children of the root are at depth 1, and so on. The class TreeNode defines a single...
Java program question! If I pass null to a method, how can I use the passed...
Java program question! If I pass null to a method, how can I use the passed value to compare. Here is example code, it might be wrong, please make it could run on eclipse. Public class hw{ public static void main (String args[]) { method1(); } private static void method1() { System.out.println(method(null)); } public static String method(int[] a) { return null; } What I want to get in the output is just "()". It is not just to type the...
Please answer in JAVA IDS 401 Assignment 4 Deadline In order to receive full credit, this...
Please answer in JAVA IDS 401 Assignment 4 Deadline In order to receive full credit, this assignment must be submitted by the deadline on Blackboard. Submitting your assignment early is recommended, in case problems arise with the submission process. Late submissions will be accepted (but penalized 10pts for each day late) up to one week after the submission deadline. After that, assignments will not be accepted. Assignment The object of this assignment is to construct a mini-banking system that helps...