count controlled loops of palindrome functions - loops

Create function is_palindrome(s). which returns True if string s in a palindrome, otherwise, returns False as ANA,madam
example
is_palindrome("aba") => True

Related

Suppose You have an array that contains some strings as name of something. Now,you have to find one string that contains odd letter by java script

const array=['abul','babu','rony','babul','suny'];
function{enter code here}
I think you would want to use the remainder operator to determine if the string length is odd and Array.prototype.find() to find the value in the list
The remainder operator (%) returns the remainder left over when one operand is divided by a second operand.
The find() method returns the value of the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.
const names = ['abul','babu','rony','babul','suny']
// function that takes a string and returns true if the string has odd length
// determines whether a string's length is odd using modulo operator and checking remainder
const hasOddLength = str => str.length % 2 !== 0
// uses hasOddLength as callback for find()
const findStringWithOddLength = (list) => (
list.find(str => hasOddLength(str))
)
console.log(findStringWithOddLength(names)) // "babul"
Note: if there are multiple names with an odd length, find() will only return the first one.

My for loop and conditional return confusion

I wrote some code to return True for all the even numbers in a given list. It only returns the first number.
When I write the function iseven(L), and I define it as:
for i in L: print i;
I print the function and get all the numbers in my list. But when I do:
for i in L, if i%2==0, return True
suddenly it only returns the first number in the list.
def iseven(L):
for i in L:
print (i)
#if i%2==0:
#return True
#else:
#return ''
The commented out lines are what I want my code to be, but since it's only spitting out the first number in the list, I checked the code with print (i) and it shows every number.
If-statements are a once-and-done deal. What you're looking for is a for-loop that goes through each number or object you have and can run code for each - in this case, your if and print statements. After the for-loop is done then you can exit.
In Python:
def iseven:
found = False
for i in list:
if i % 2:
print(i)
found = True
return found
Note: The value of found is only returned once. If you want to return multiple values, not just print them like with i, then you need a data structure like an array.
Def number(list):
for num in list:
# checking condition
if num % 2 == 0:
print(num, end = " ")

Checking if an array contains a string

I'm trying to evaluate if an entered string partially matches any item in an array. When I use the following method in playgrounds it seems to work properly. However, when I use the exact same method in Xcode 9.0 beta 6 (9M214v) it doesn't return the correct answer.
func isValid(_ item: String) -> Bool {
let whitelist = ["https://apple.com","https://facebook.com","https://stackoverflow.com"]
return whitelist.contains(where: {$0 <= item ? true : false })
}
There's also anomalies like when I passed in "https://twitter.com" it'll return true. Am I nuts? And while I'm here, anyone have a different approach to solve this problem?
theres also anomalies like when I passed in "https://twitter.com"
it'll return true.
Whether the version of Swift is 3 or 4, based on your code snippet you should get the same true result! Why?
because the logic of contains(where:) of doing the comparison is related to the logic of the equality of the given elements, i,e you cannot use contains with array of non-equatable elements. To make it more clear:
"https://apple.com" <= "https://twitter.com"
"https://facebook.com" <= "https://twitter.com"
"https://stackoverflow.com" <= "https://twitter.com"
the result would be true for all statements ('t' character is greater than 'a', 'f' and 's').
Thus:
"https://zwebsite" <= "https://twitter.com"
would returns false ('t' is less than 'z').
However, to get the expected result, you could implement your function like this:
func isValid(_ item: String) -> Bool {
let whitelist = ["https://apple.com","https://facebook.com","https://stackoverflow.com"]
return whitelist.contains { $0 == item }
}
Or for even a shorter contains:
return whitelist.contains(item)
Which leads to let
isValid("https://twitter.com")
to returns false.
Just return the following, which will return true or false:
return whitelist.contains(item)

How to check all array equal to some value in angular js?

self.scope.savesectioneditmain = [self.scope.savesection2details,
self.scope.editsection2,
self.scope.savesection1details,
self.scope.editsection1];
Based on above codes, I want to check all array value equal to true value.
if($.inArray(true, self.scope.savesectioneditmain) == 0)
I have tried $inArray, but $inArray checks only one condition, but I want to check all array value should be equal to true.
You can use Array.prototype.every()
The every() method tests whether all elements in the array pass the test implemented by the provided function.
if(self.scope.savesectioneditmain.every(x => x == true)){
//All elements are true
}

Replace ONLY next to each other duplicates

How to make replacement of next to each other ONLY false duplicates in array but don't touch the ones that seperate.
From this:
{false, false, false, false, true, true, false, true, false, false};
to this:
{false, true, true, false, true, false}
I think a smart method would be the following.
boolean[] myArray = {false, false, false, false, true, true, false, true, false, false};
// Introduce myArray[0] to your array result.
for (int i = 1; i < myArray.length; i++) {
if (myArray[i-1] || myArray[i]) // Introduce myArray[i] to your array result.
}
You introduce the value if current value is true, or if the last one (controlled by introduce variable) was not false. First element is always introduced, because wether is true or false, the logic problem says to introduce it.
Also, I think it would go faster if you don't short-circuit myArray[i-1] || myArray[i] and let myArray[i-1] | myArray[i]. It is faster to check the second value of the or operation than make the if instruction at the compiler level to see if the first value is already true.
You can check the next element and remove the current false only if next is false.At the same time you can make changes to the same array to remove duplicates.
int index=0;
for(int i=0;i<arr.size()-1;i++)
{
if(arr[i]==false && arr[i+1]==false)
continue;
else //this should exist in array
{
arr[index]=arr[i];
index++;
}
}
arr[index++]=arr[arr.size()-1];//for the last element
//Last element added irrespective of being true or false
//Now you can remove the elements from index to end
std::remove(arr.begin()+index,arr.end());
Using C++ and the standard library:
v.erase(std::unique(v.begin(), v.end(),
[](bool a, bool b){ return !(a||b); }),
v.end());
The standard library std::unique function compresses the collection by overwriting all but the first of a sequence of duplicates. In this case, we use a lambda function in order to avoid counting two consecutive true values as duplicates; only a pair of values such that !(a||b) is true counts, and that expression is only true if both a and b are false. (!a && !b would have worked just as well and very likely generates identical code.)
std::unique returns the end point of the compressed range; it is then necessary (or at least useful) to reduce the size of the container, which is what the erase member function does, assuming that it is implemented for the container v (here, I'm assuming that v is a std::vector but a std::deque or std::list would work as well).
Live on ideone

Resources