php


Effeciently test ifany three of four numbers are equal?


I am trying to determine a quick way to select sets of numbers (they'll be in arrays). I only need to keep sets where two or more of the numbers are greater than zero; therefore, sets that include zero three times can be discarded. I have come up with the following code but it doesn't have the expected outcomes, any advice would be greatly appreciated.
<?php
function testFour($a, $b, $c, $d)
{
if(($a + $b + $c + $d) == ($a || $b || $c || $d)) {
echo $a.", ".$b.", ".$c.", ".$d." => exclude<br>";
} else {
echo $a.", ".$b.", ".$c.", ".$d." => keep<br>";
}
}
echo "<pre>";
testFour(0,0,0,5); // true
testFour(1,0,0,5); // false
testFour(0,4,2,0); // false
testFour(1,1,0,5); // false
testFour(0,2,0,0); // true
testFour(2,3,0,0); // false
echo "</pre>";
Give this a try:
<?php
function testFour($a, $b, $c, $d)
{
$t = ($a == 0 ? 1 : 0) + ($b == 0 ? 1 : 0) + ($c == 0 ? 1 : 0) + ($d == 0 ? 1 : 0);
return t < 3;
}
echo "<pre>";
echo "Test1: ".testFour(0,0,0,5)."\n"; // true
echo "Test2: ".testFour(1,0,0,5)."\n"; // false
echo "Test3: ".testFour(0,4,2,0)."\n"; // false
echo "Test4: ".testFour(1,1,0,5)."\n"; // false
echo "Test5: ".testFour(0,2,0,0)."\n"; // true
echo "Test6: ".testFour(2,3,0,0)."\n"; // false
echo "</pre>";

Related Links

HybridAuth (Anvard) - How to log out & log in as different user?
extract order id from a string using preg match in php
What is the best way in sql to select 3 users from data in another table? [closed]
Codeigniter random session logouts (Already tried override session class AND raising session_time_to_update
SimpleSamlphp Error: State information lost, and no way to restart the request
Geting img url and video url from a string
Is there a way to send e-mail from github.io without external hosting?
Why are my links not being shortened?
Zend SQL statement to find and order results by occurances
Why does Auto_Increment return null?
PHP redirection to multiple pages [closed]
Invalid argument supplied for foreach() php warning
Passing a number as a string parameter
Limiting character length of output
How to add/set images on PHPOffice/PHPWord Template?
PHP - Multidimensional Array

Categories

HOME
opengl
apache-nifi
weblogic12c
tomcat
facebook-messenger
mousemove
code-formatting
whmcs
turbojpeg
stored-procedures
firebase-database
phpseclib
sharepoint-online
log4j2
clickable-image
graphdb
cname
spring-jpa
dhcp
mailgun
texas-instruments
uima
checkout
xsl-fo
deepfreeze
mime-types
substring
firemonkey-style
react-dnd
managed-c++
strophe.js
hdmi
ibm-connections
mdns
social-networking
winsock
instructions
memory-fences
rust-cargo
riotjs
apptentive
char-pointer
ftp-server
cartopy
raytracing
dagger
vungle-ads
nomad
react-intl
rightnow-crm
deployd
sonicmq
nusoap
cordova-ios
sharpdx
gestures
android-sharing
webvtt
asp.net-web-api-routing
business-rules
prefetch
finder
petsc
yui3
s-function
jsonix
nunit-console
android-sdcard
eclemma
jeditable
django-redis
openmrs
webgrind
outlook.com
bayesglm
dnx50
extend
supersocket.net
algebraixlib
textscan
whitespace-language
fuseesb
innerhtml
signed
python-c-extension
ssmtp
nhibernate-criteria
comctl32
rubber
global-scope
object-code
network-printers
stripe.net
radius-protocol
smips
pbkdf2
grails-2.3
.net-remoting
spiral
oscilloscope
contextswitchdeadlock
red-system
lynx
wndproc
platform-independent
monocross
autosize
fitch-proofs
dropshadow
datacontracts
qt-mobility
multiple-users
tabbarcontroller
webclient.uploaddata

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App