if at least one element of array in Perl [duplicate] - arrays

This question already has answers here:
How can I check if a Perl array contains a particular value?
(12 answers)
Closed 7 years ago.
Maybe my question is really easy, but I would like to know the best way and efficient one.
Let's we have an array of strings and we want to compare it with another string.
say,
my #array = {"hi","bye","you","shadow", "hi"}
Now I want to check if at least one element of the array equals hi then there is some condition.
May I know your idea about it. I know that within a for loop one could do it easily, but Would you would suggest as a good one?

Something like
my #array = qw (hi bye you shadow hi);
my $hi_count = scalar(grep {$_ eq 'hi'} #array);
print $hi_count;
This will print 2 as there are two words that equal hi.

Related

Any append (Python) or push_back (C++) equivalent in C? [duplicate]

This question already has an answer here:
Python-like array filling - C equivalent
(1 answer)
Closed 2 years ago.
I'm looking for an "append" (Python) or "push_back" (C++) equivalent in C to fill an array of strings (char). Does it exists?
The question doesn't make sense because you cannot add elements to an array in C. In C, you set the size of the array when you create it, and the size cannot change.
If you want to "append", you have to create a big array (e.g. 1000 items), and use a separate variable to remember how many items you're actually using (the rest are spare).

Two words inside brackets in Matlab? [duplicate]

This question already has an answer here:
What is the difference between comma separated or not MATLAB function return?
(1 answer)
Closed 6 years ago.
What's that syntax for?
[sbox idx] = sort(box);
After reading Sort array elements, I would expect to see something like this:
[sbox, idx] = sort(box);
but that's not the case as you can see (it's an open sourced project, so it's not mine code). What does it mean?
Both lines do exactly the same, like [1 2 3 4] and [1,2,3,4] define the same vector.

Algorithms Programming [duplicate]

This question already has answers here:
Quickest way to find missing number in an array of numbers
(31 answers)
Closed 8 years ago.
Can someone explain what this question is asking for?
Array A contains n‐1 unique integers in the range [0,n‐1] and there is one
number from this range that is not in A. Design an O(n) algorithm for finding that
number. You are allowed to use only O(1) additional space besides the array A itself.
Does this question means the length of the array is (for example: 5). And array contains = {0,1,x,3,4}. Find x?
What is O(n) and O(1)? How do I find the missing number in the array
using O(n) algorithm?
Help is appreciated. Thanks.
Does this question means the length of the array is (for example: 5). And array contains = {0,1,x,3,4}. Find x?
Yes. (More or less)
What is O(n) and O(1)?
Read your algorithmics text book. Or your lecture notes. Or http://en.wikipedia.org/wiki/Big_O_notation.
How do I find the missing number in the array using O(n) algorithm?
That would be solving the problem for you!
Hint: what is the formula for the sum of 1 to N?
Does this question means the length of the array is (for example: 5). And array contains = {0,1,x,3,4}. Find x?
not exactly: if n was 5, the array would contain four unique naturals from 0 to 4 (n-1): {3, 1, 4, 0}.

How to swap two variables in one line in C? [duplicate]

This question already has answers here:
Interchanging values of two variables by xor operator using references or pointers
(2 answers)
Swapping two variable value without using third variable
(31 answers)
Closed 9 years ago.
I want to know that is there any other way of swapping 2 numbers in one line and of course without 3rd variable.
I know one way of doing this:
b=a+b-(a=b)
or
a=a+b-(b=a)
both are same(approximately). If you know then please help me out.
The frequently cited classic answer that you are probably looking for is:
a^=b^=a^=b;
But, it is technically wrong, because it changes the same variable more than once before a sequence point.
Use bit twiddling in C. Following swap two variables:
if (a != b) {
a ^= b ^= a ^= b;
}

What is the history of using i in for loops? [duplicate]

This question already has answers here:
Why are variables "i" and "j" used for counters?
(23 answers)
Closed 10 years ago.
In all the programming languages I have come across there seems to be the best practice to use variable i in for loop iterations. Usually i is followed by l in the nested loop. This seem to apply both for statically compiled and scripting languages.
What is the history of this practice? Does i mark for integer, index, or something else? Why, for example, we don't use x which would be more common, considering math background.
I've got two theories: i can stand for 1) index 2) integer (value of integer type)
It makes most sense that i stands for index because the loop is over each element of an array and each element is indexed.

Resources