Why does this list always grab the first item? - list

This function is supposed to go through all of the list items and check whether the argument coordinates are close to the position in the list items (the second and third numbers in the nested lists), but from the readout I can tell that it only ever checks the first one.
function CapitalProximity(x,y)
local positions = {{"lusitani",31,328},{"ebdani",78,592},{"carpetani",101,329}}
for i = 1, #positions do
local dist = distance_2D(x,y,positions[i][2],positions[i][3])
print("position is "..dist.." from "..positions[i][1])
if dist < 20 then
return true
else
return false
end
end
end

Since both branches of the if return from inside the loop, the loop can never reach a second iteration. To get results based on all elements from the positions array, you need to make a table to store them:
function CapitalProximity(x,y)
local positions = {{"lusitani",31,328},{"ebdani",78,592},{"carpetani",101,329}}
local result = {} -- This will hold all results.
for i = 1, #positions do
local dist = distance_2D(x,y,positions[i][2],positions[i][3])
print("position is "..dist.." from "..positions[i][1])
result[i] = dist < 20 -- Store the current result.
end
return result
end

Related

Program to generate nCk combinations of k numbers varying from 1 to n

My function definition looks like
nxt_comb[] = Combination(comb[1:k],n)
This function should give the next combination which comes after the input combination comb (array). The n elements of comb takes values from 1 to n.
Example:
if function is called as a = Combination([1,3,4,6],8), then a = [1,3,4,7]
if function is called as a = Combination([1,3,4,8],8), then a = [1,3,5,6]
if function is called as a = Combination([1,3,7,8],8), then a = [1,4,5,6]
if function is called as a = Combination([3,6,7,8],8), then a = [4,5,6,7]
The input combination will never be the last combination. That is, in the above case the input will be never [5,6,7,8].
Also, if the input is all zeros, the function must output the first combination that is [1,2,....,k].
Edit: What I am looking for is the logic. The implementation can be in either C/C++ or MATLAB.
Leaving aside the last requirement about starting with all 0's, which is a trivial extra check, the algorithm is:
By searching backwards, find the largest i such that comb[i]<i+n-k (using 1-based indexing; adjust if you're using C). If you can't find one, you're at the last combination.
Increment comb[i], and then working from j = i+1 up to k, set comb[j] to comb[j-i]+1

Adding Values to a List from a function in Matlab

I'm trying to write a code that searches a list and matches a term. In this case 'bus' what I am then trying to do is to get the values for distance and time for that method and add them to separate lists. Attached is my code
distanceb = [];
timeb = [];
for i =1:n
if strcmp(method(i),'bus') == 1
distanceb = (x(i))
timeb = time(i)
end
end
I can get the values for the x and time but the code seems to overwrite everytime it adds to the list and I get only one answer for distanceb and timeb. Is there a way to add the values to the list without overwriting the previous value?
You are assigning new values to distanceb and timeb as scalars and not as lists/vectors.
You need to append values:
distanceb(end+1) = x(i);
timeb(end+1) = time(i);
A few remarks:
If you know the final size of distanceb and timeb it is best to pre-allocate them and not grow them inside the loop.
It is best not to use i as a variable name in Matlab.

simple hash map with vectors in C++

I'm in my first semester of studies and as a part of my comp. science assignment I have to implement a simple hash map using vectors, but I have some problems understanding the concept.
First of all I have to implement a hash function. To avoid collisions I thought it would be better to use double hashing, as follows:
do {
h = (k % m + j*(1+(k % (m-2)));
j++;
} while ( j % m != 0 );
where h is the hash to be returned, k is the key and m is the size of hash_map (and a prime number; they are all of type int).
This was easy, but then I need to be able to insert or remove a pair of key and the corresponding value in the map.
The signature of the two functions should be bool, so I have to return either true or flase, and I'm guessing that I should return true when there is no element at position h in the vector. (But I have no idea why remove should be bool as well).
My problem is what to do when the insert function returns false (i.e. when there is already a key-value pair saved on position h - I implemented this as a function named find). I could obviously move it to the next free place by simply increasing j, but then the hash calculated by my hash function wouldn't tell us anymore at which place a certain key is saved, causing wrong behaviour of remove function.
Is there any good example online, that doesn't use the pre defined STD methods? (My Google behaves wierdly in the past few days and only reutrns me unuseful hits in the local language)
I've been told to move my comment to an answer so here it is. I am presuming your get method takes the value you are looking for an argument.
so what we are going to do is a process called linear probing.
when we insert the value we hash it as normal lets say our hash value is 4
[x,x,x,,,x,x]
as we can see we can simply insert it in:
[x,x,x,x,,x,x]
however if 4 is taken when we insert it we will simply move to the next slot that is empty
[x,x,x,**x**,x,,x,x]
In linear probing if we reach the end we loop back round to the beginning until we find a slot. You shouldn't run out of space as you are using a vector which can allocate extra space when it starts getting near full capacity
this will cause problems when you are searching because the value at 4 may not be at 4 anymore (in this case its at 5). To solve this we do a little bit of a hack. Note that we still get O(1) run time complexity for inserting and retrieval as long as the load balance is below 1.
in our get method instead of returning the value in the array at 4 we are instead going to start looking for our value at 4 if its there we can return it. If not we look at the value at 5 and so on till we find the value.
in psudo code the new stuff looks like this
bool insert(value){
h = hash(value);
while(node[h] != null){
h++;
if( h = node.length){
h = 0;
}
}
node[h] = value;
return true;
}
get
get(value){
h = hash(value);
roundTrip = 0; //used to see if we keep going round the hashmap
while(true){
if(node[h] == value)
return node[h];
h++;
if( h = node.length){
h = 0;
roundTrip++;
}
if(roundTrip > 1){ //we can't find it after going round list once
return -1;
}
}
}

Trouble Understanding C++ Sequential Search

I am new to C++ and am having difficulty understanding this function. Could someone walk me through it?
int seqSearch(int [ ] list, int target)
{
//precondition: The list is in non-decreasing order
//postcondition: The method returns -1 if the target is not in the list. If the target
//is in the list, the method returns the index of the first occurrence of the target.
int result = -1;
boolean foundBigger = false;
int i = 0;
while (result == -1 && !foundBigger && i < list.length)
{
if (target == list[i])
result = i;
else if (list[i] > target)
foundBigger = true;
//endif
i++;
} //endwhile
return result;
}
It is trying to find if a target number is present in the list, where the number in list are stored in descending order.
The loop continues,
Till the target is not found. (Condition:result == -1)
( If target found then result != -1 and the loop breaks, returning the index of the element.
or Till the element in the list is bigger than the target. ( Condition:!foundBigger )
( As the list is in descending order, if it finds a number which is less than the target, so their would be no chance of finding the number in the remaining list. So this means it is not present in the list and the loop should break.)
or Till the whole list is rendered and its not found. (Condition: i < list.length)
Hope its clear now.
Hmm, "non-decreasing order". A better word for it is ascending order :-)
The function assumes the list is in ascending order. Starting with the first item in the list (list[0]) , compare with the item you are looking for (ie, "target"). If equal, set result to index "i". If not, increment i and continue loop. Go through each item one by one until:
(a) you find "target", OR
(b) the current item is bigger than your "target" (quit at this point since no point goind on since your list is ordered)
Return value is the index in the list where you found "target" or -1 if not found.
when result is == -1 and
foundBigger is true and
then the size of the array that you pass to the function is more than 0
then it goes inside the while loop
if even of the one of the above criteria is not full filled then it would not go inside the loop it would just return the value of the return
then if the value that you pass to the function using target parameter equals to i value of array list then you assign result to he value of i
if the target value is less than to i value of array list then the foundBigger is assigned true so again when you come into the while loop you cannot fulfill the above criteria
if none of the above works then i is incremented by 1 and it comes out of the while loop
it goes until you find the in which position the target value is saved if target value is not in the array then it would return result -1
or else it would return the location
// precondition: The list is in non-decreasing order
// postcondition: The method returns -1 if the target is not in the list. If the target
// is in the list, the method returns the index of the first occurrence of the target.
int seqSearch(int [ ] list, int target)
{
int result = -1; // Set result to 'not-found' value
// Also we want to stop search if smth bigger than `target' will be found
// Remember the original list is assumed to be sorted, so first value
// bigger than `target' means: no need to continue, it is guarantied that `target'
// is nto here!
boolean foundBigger = false;
int i = 0; // Initial index to start search
// Repeat 'till `target' not found AND not found smth bigger that requested target
// and current index less than the list size...
while (result == -1 && !foundBigger && i < list.length)
{
if (target == list[i]) // Is current item equal to the requested?
result = i; // Remember result index (this will break the loop)
else if (list[i] > target) // Is current item bigger than the requsted?
foundBigger = true; // Yep, will break the loop w/ no result to return
//endif
i++; // move to next item in the list
} //endwhile
return result; // Return the result, whatever it is
}
If this is working code then its DEFINETELY not c++. The boolean operations in the while loop would have to be enclosed in separate parenthesis for order of operations and c++ doesn't have a .length property for arrays (java allows for both of these which makes me thing it's written in java).The logic behind it still remains the same though.
You first initialize your result int(the one that's to be returned) and a boolean that checks if you've passed the element that you want. As long as you haven't found what your looking for and you haven't 'passed' the target item you keep going.
If you've found your target then your result int is changed to the correct index and the next time the loop checks its conditions the index will be over -1 and represent a real number so the method will end and return an index.
The second statement checks to see if you've passed the int that you're looking for. You know the list is sequential so if the value you're currently on is higher than the value you're looking for and you didn't find the item then you change the foundBigger boolean which will end the loop next time the conditions are checked.
If none of these conditions are met then you will eventually go past the end of the list searching so once you've reached the end of your list you know the item wasn't in the list so you still return -1.

How do I find the mode of a sorted array?

I need to write a function to find the mode of a array. I'm not good at coming up with algorithms however and I'm hoping someone else knows how to do this.
I know the size of the array and the values in each element, and I have the array sorted from least to greatest.
array would be passed to the mode function like
mode = findMode(arrayPointer, sizePointer);
UPDATE:
After reading the comments I've tried this
int findMode(int *arrPTR, const int *sizePTR)
{
int most_found_element = arrPTR[0];
int most_found_element_count = 0;
int current_element = arrPTR[0];
int current_element_count = 0;
int count;
for (count = 0; count < *sizePTR; count++)
{
if(count == arrPTR[count])
current_element_count++;
else if(current_element_count > most_found_element)
{
most_found_element = current_element;
most_found_element_count = current_element_count;
}
current_element = count;
current_element_count=1;
}
return most_found_element;
}
I'm still having problems grasping this algorithm though if anyone can sort me out.
I've never used vectors so don't really understand the other examples.
You have almost everything.
You can take advantage of the fact that the array is sorted.
Just go through the array keeping track of both the current equal consecutive numbers, and the greatest number of equal consecutive numbers you have found until that point (and which number produced it). In the end you will have the greatest number of equal consecutive numbers and which number produced it. That will be the mode.
Note: For a solution which does not require the array to be sorted, see for example one based in the histogram approach in a related question.
set most_found_element to the first element in the array
set most_found_element_count to zero
set current_element to the first element of the array
set current_element_count to zero
for each element e in the array
if e is the same as the current_element
increase current_element_count by one
else
if current_element_count is greater than most_found_element_count
set most_found_element to the current_element
set most_found_element_count to current_element_count
set current_element to e
set current_element_count to one
if current_element_count is greater than most_found_element_count
set most_found_element to the current_element
set most_found_element_count to current_element_count
print most_found_element and most_found_element_count
I thought the names would explain it, but here we go:
When we start, no element has been found the most times
so the "high-score" count is zero.
Also, the "current" value is the first, but we haven't looked at it yet
so we've seen it zero times so far
Then we go through each element one by one
if it's the same as "current" value,
then add this to the number of times we've seen the current value.
if we've reached the next value, we've counted all of the "current" value.
if there was more of the current value than the "high-score"
then the "high-score" is now the current value
and since we reached a new value
the new current value is the value we just reached
Now that we've seen all of the elements, we have to check the last one
if there was more of the current value than the "high-score"
then the "high-score" is now the current value
Now the "high-score" holds the one that was in the array the most times!
Also note: my original algorithm/code had a bug, we have to do an extra check of "current" after the loop ends, as it never finds "the one after the last".
Hints:
Q: How do you define the mode?
A: The number whose count is greatest within the array.
Q: How do you count numbers in an ordered array?
A: Iterate through the array, and while the next item is equal to the previous, increment the count for that value.
Q: If the count of the previous value is less than the count of the current value, then can the previous value be the mode?
A: No
If the input array is sorted, here is the same approach as described in other answers but implemented in a different way, a better and easy to understand way.
Run a loop over the input array.
Keep global mode value and mode count.
Run a sliding window till you find equal elements.
If local equal element count is greater than global mode count, then update global mode and mode count.
Here is working and tested code in C++.
int mode(vector<int> a, int N)
{
int mode = a[0];
int mode_count = 1;
int i = 0;
while (i < N - 1) {
int cur = a[i];
int cur_count = 1;
while (a[i] == a[i + 1]) {
i++;
cur_count++;
}
if (cur_count > mode_count) {
mode_count = cur_count;
mode = a[i];
}
i++;
}
return mode;
}