Check if array contains a value
Author |
Message |
Jeffmagma
|
Posted: Wed Dec 23, 2015 7:13 pm Post subject: Check if array contains a value |
|
|
What is it you are trying to achieve?
I'm trying to check if an array of strings contains a certain value
What is the problem you are having?
I'm not exactly sure how to do this
Describe what you have tried to solve this problem
I've tried to make a procedure that checks each individual part of the array for a match, but the array is extremely long and was wondering if there was an easier way to do this
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Turing: | var found : boolean
proc find
for i: 1.. upper(array)
if number = array(i ) then
found := true
return
end if
end for
end find
if found then
end if
|
Please specify what version of Turing you are using
OpenTuring 1.0.1 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Insectoid
|
Posted: Wed Dec 23, 2015 8:12 pm Post subject: RE:Check if array contains a value |
|
|
There are many, many search algorithms of varying degrees of complexity and efficiency. Your algorithm is pretty much the simplest to code though least efficient. A really long array won't make it harder to write, in fact it doesn't matter at all. It'll just take longer to execute, and unless your array has millions of elements, it will complete the search very quickly.
The code you posted doesn't look complete, though the logic is sound. If you solve the syntax errors (and Turing will tell you what they are), it will probably work. |
|
|
|
|
|
Jeffmagma
|
Posted: Wed Dec 23, 2015 8:20 pm Post subject: Re: Check if array contains a value |
|
|
yeah the code isn't complete, it was just like an example, but thanks for the reply. I was just wondering since some other languages seem to have a function that does this, but Turing seems to be kind of limited |
|
|
|
|
|
Insectoid
|
Posted: Wed Dec 23, 2015 9:10 pm Post subject: RE:Check if array contains a value |
|
|
Turing is designed for learning. A lot of these functions are left out, so you are forced to learn how they work and make them yourself. Other languages are designed for developers who don't have time to reinvent the wheel. |
|
|
|
|
|
nulldev
|
Posted: Wed Jan 06, 2016 9:09 pm Post subject: Re: RE:Check if array contains a value |
|
|
Insectoid @ Wed Dec 23, 2015 8:12 pm wrote: There are many, many search algorithms of varying degrees of complexity and efficiency. Your algorithm is pretty much the simplest to code though least efficient.
Actually, it is mathematically impossible to check if an array contains a value without checking EVERY element in the array. So actually your algorithm is the best.
This question is similar to this one (https://www.quora.com/What-is-the-fastest-algorithm-to-find-the-largest-number-in-an-unsorted-array) where instead of looking for the highest element, we are looking for the instance of an element. In both cases, the best solution is the simplest one.
There is a way to improve performance theoretically by storing the array in chunks and searching each chunk in parallel but it only improves performance in really, really big arrays. It actually decreases performance in smaller arrays. |
|
|
|
|
|
Insectoid
|
Posted: Thu Jan 07, 2016 2:14 pm Post subject: RE:Check if array contains a value |
|
|
That depends if the array is sorted or not, which OP didn't specify. |
|
|
|
|
|
|
|