Friday, 30 March 2018

javascript - check to see if exactly two out of three booleans are true?



I need to test to see if exactly two out of three booleans are true.



Something like this:




if((a && b && !c) || (a && !b && c) || (!a && b && c)){
//success
}


Is this the most direct way to go about this? Does anyone know of a shortcut / shorthand?


Answer



To check if exactly two are equal to true:



[a, b, c].filter(Boolean).length === 2;



References:




Thursday, 29 March 2018

Match (a AND b) vs (a OR b) in PHP regex

I'm trying to search through some files in PHP and I want to give the user a choice between matching any or every term.



E.g.: Search terms: [apple,banana]



Is it possible to return only files that have both words in it?




Also my OR would use the following:



$s_terms = "(" . implode("|", explode(",", $s_terms)) . ")";



I want to separate terms with , and use it in a RegEx match like (apple|banana). Does the above code do that?



Thank you in forward



EDIT:




The other question did not answer my question, but I found something that works:
(.*apple.*banana.*)

sql - Characters that must be escaped in Tsql

I was looking for a list of special characters that must be escaped in ms sql server but could not find one and most of answers I saw for the similar questions advised to use Parameterised queries.. which I am already doing but the framework I am using does not does any escaping for me.



Thus I thought I will give a bunch of those a try and see which one fails.... and I tried a simple query



select * from x where value = ''


in such query I tried almost all the characters I could find on my keyboard and all of them seem to work... besides the Singe Quote.. that one fails.




Thus I want to know the list of characters that are invalid and must be escaped in ms sql server - tsql and do not want to take the risk of just escaping the single quote and leave the rest that could cause trouble



Appreciate your help

Can interfaces have static methods or java class stati'fy interface method?

The following is allowed:-




public interface JustImplementMe
{
public void ClimbMountain();
}


The following is not allowed:-



public interface StaticMethodInterface
{

public static void Climb();
}


The following is allowed:-



public class Classic implements JustImplementMe
{
public void ClimbMountain()
{


}
}


The following is not allowed:-



 public class ClassicMusic implements JustImplementMe    
{
public static void ClimbMountain()

{
// static method cannot hide the instance method in JustImplementMe
}
}


Why is it so? Thanks.

analysis - What was Frank? - Movies & TV



During the film Donnie Darko, Donnie occasionally talks with Frank, who we end up finding out at the end of the film is a guy that Donnie shot because he accidentally ran over his girlfriend. Is Frank a manifestation of Donnie's consience, having premonitioned what was going to happen in the future? If not, what was Frank supposed to be?


Answer




The real-life Frank that runs over Donnie's girlfriend is Elizabeth's (Donnie's sister) boyfriend. We see him drive past Donnie at the start of the movie after dropping her off at home shortly before the engine crashes into the house. You can see this sequence of events more clearly at the end of the film.



The Frank that Donnie speaks to throughout the movie is a Messenger sent to warn Donnie of the impending end of the world. The characters that are killed in the alternate universe are manipulated by whomever is helping Donnie save the world. They are known as Manipulated Dead and are briefly explained in the book from the director's cut.


C++ adding numbers from input file




I am trying to open files with list of int numbers. I just want to output the total number of numbers in the file, and add together all the numbers from the file.



This is the part that I am having problems with:



void workFunc(ifstream& myInput, string fileName, int& count, int& sum)

{
//int count = 0;

//int sum = 0;
int num;
myInput >> num;

while (myInput.good())
{
myInput >> num;
count++;
sum += num;
}


}


It is giving me the right count, but not adding the numbers together properly every time.


Answer



You don't count the first thing you read, but you count the last thing twice. So you get the count right by accident, but of course get the sum wrong unless the first and last values happen to be the same.



You read something into num and then enter the loop. The first thing your loop does is read into num again, discarding the previous value. So the first value doesn't get counted. You then increment count and add it to sum whether or not the read succeeded, which counts the last value twice.




You want this:



myInput >> num;

while (myInput.good())
{
// First count the value that we just read
count++;
sum += num;


// Then read the next value
myInput >> num;
}

bash - Difference between single and double quotes in awk



I have this awk statement:



glb_library="my_library"
awk "

/^Direct Dependers of/ { next }
/^---/ { next }
/^$glb_library:/ { ver=\$0; next }
{ gsub(/[[:space:]]/, '', \$0); print ver':'\$0 }
" file


Basically, I have enclosed the awk code in double quotes so that the shell variable glb_library is expanded. I have made sure to escape the $ character to prevent the shell from expanding $0. Followed the guidance from here.



awk gives me this error:




awk: syntax error at source line 5
context is
{ gsub(/[[:space:]]/, >>> ' <<<


I want to understand:




  • Is it legal to use single quotes inside awk? Why is '' not a null string like "" is?


  • Does awk treat single and double quotes differently?



My code worked after I escaped the single quotes with backslashes and used \"\" to represent the null string instead of ''.


Answer



Based on the comments above by awk experts and some research, I am posting this answer:




  • awk strings are enclosed in double quotes, not single quotes; more precisely: single quotes are not string delimiters in awk, unlike shell

  • awk attaches no special meaning to single quotes and they need to be enclosed in double quotes if used in string literals


  • it is best to use single quotes to wrap awk statements on command line, unlike OP's code that's using double quotes (Ed pointed this out clearly)



Further clarification:




  • "" is the null string in awk, not ''

  • to use single quotes in an awk string literal, enclose them in double quotes, as in "Ed's answers are great!"

  • other techniques followed while handling single quotes in awk are:




    a) use a variable, as in awk -v q="'" '{ print q }' ...



    b) use octal or hex notation, as in awk '{ print "\047"$0"\047" }' ...







Relevant documentation here.


casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...