Jump to content
aveeva

How to print largest even number in given string?

Recommended Posts

String: NodeJs is one of the best technology

Quote

NodeJs        is      one       of      the      best      technology
    6               2          3        2         3           4            10


From above, how to print technology which largest even number.

How to print the largest even number?

Share this post


Link to post
Share on other sites

What have you tried besides posting this on every coding forum?

  • Like 1

Share this post


Link to post
Share on other sites
Posted (edited)

WHAT  is this?  You have a tendency to NOT be very specific in your posts.  You don't explain things, you don't ask questions.  You simply post something and then hope forum users can figure out what you are trying to say.   Please say what you want!  For instance - whatever does 'NodeJS' have to do with this list of numbers? And what are the words there for?  None of this is obvious to us.

Edited by ginerjm

Share this post


Link to post
Share on other sites

First create an array of even numbers. You can find which are even by using the remainder operator dividing by 2. A zero remainder means it is even. Then use the max function to find the largest.

Share this post


Link to post
Share on other sites
$numbers = array(1,3,7,8,10,12); 
foreach($numbers as $number) { 
	if($number%2==0) {
		if($number > $old_number) { 
			$max_number = $number;
		}
		$old_number = $number; 
	}
}
echo $max_number; 

Not sure what nodejs has to do with anything. This is a php forum. Something like this would do it in php. 

Share this post


Link to post
Share on other sites

I didn't even think about using the max function. 

$numbers = array(1,3,7,8,10,12); 
foreach($numbers as $number) { 
	if($number%2==0) {
		$evens[]=$number; 
	}
}
$max_number = max($evens); 
echo $max_number; 

 

Share this post


Link to post
Share on other sites
$numbers = array(1,3,7,8,10,13); 

$max = max(array_filter($numbers, function($v) { return $v%2==0; })) ;

 

  • Like 2

Share this post


Link to post
Share on other sites
2 hours ago, Barand said:

$numbers = array(1,3,7,8,10,13); 

$max = max(array_filter($numbers, function($v) { return $v%2==0; })) ;

 

This is the modern/functional programming way of handling a problem like this.  I'm not a huge javascript fan, but having to practice it on occasion certainly opened my eyes to the use of filter/map/reduce and other mainstays of functional programming.    I've also found this guy's youtube channel to be both educational and inspirational.  You do have to do a bit of research for the php functions that are similar, but in the case of arrays there are ones like array_filter that I find are great as glue for so many smallish tasks as demonstrated by Barand's code.

Share this post


Link to post
Share on other sites

array_reduce is a great function for this. Quite rare to see it in the real world, which means bonus points for actually using it.

$numbers = array(1, 3, 7, 8, 10, 13);

// assuming there is at least one even number >= 0
$max = array_reduce($numbers, function($max, $n) { return ($n % 2) == 0 && $n > $max ? $n : $max; }, 0);

Or with PHP 7.4's arrow functions,

$max = array_reduce($numbers, fn($max, $n) => ($n % 2) == 0 && $n > $max ? $n : $max, 0);

 

  • Like 1

Share this post


Link to post
Share on other sites
7 hours ago, requinix said:

Or with PHP 7.4's arrow functions,


$max = array_reduce($numbers, fn($max, $n) => ($n % 2) == 0 && $n > $max ? $n : $max, 0);

 

There's a part of me that thinks they're developing future PHP specifically to make the kids think they're programming in JavaScript... I mean, I actually really like fat arrow functions in JS, but every time I see an example of it in PHP I have to pause a second and readjust my brain.

Share this post


Link to post
Share on other sites
10 minutes ago, maxxd said:

There's a part of me that thinks they're developing future PHP specifically to make the kids think they're programming in JavaScript... I mean, I actually really like fat arrow functions in JS, but every time I see an example of it in PHP I have to pause a second and readjust my brain.

Emphasis on "think". The nicest thing arrow functions have to offer is implicit variable binding.

$a = 1;
$add = fn($b) => $a + $b; // function($b) use ($a) { return $a + $b; }

echo $add(2); // 3

The bad thing is that the binding is strictly by-value. As in a regular use() and by a by-ref use(&). Which means it's very much not like how Javascript works (which goes by scope, not bindings).

$a = 2;
echo $add(2); // 3 because $add still thinks $a=1
var a = 1;
var add = (b) => a + b;

console.log(add(2)); // 3

a = 2;
console.log(add(2)); // 4

 

Share this post


Link to post
Share on other sites

maxxd, 

From what I've seen, it's more a matter of the php team wanting to give developers the same tools and capabilities that exist in other languages.  Adding syntax to easily use anonymous functions is yet another step in furthering that longtime goal.  With that said, the associative array assignment syntax does make this a change that will cause some head scratching for long time PHP developers.

Share this post


Link to post
Share on other sites
2 hours ago, gizmola said:

With that said, the associative array assignment syntax does make this a change that will cause some head scratching for long time PHP developers.

As I recall slash can find in my email archive (~summer 2017 was when the discussion first started),

==> was the preferred syntax for a while because Hack uses it, however PHP couldn't implement closures in the same way Hack does (IIRC) so this would break compatibility with HHVM. Plus there would be difficulties in coercing the parser into reading it. And there was a bit of "let's please not establish a precedence of copying Hack". So it couldn't work.

=> was proposed early but was initially rejected because it would conflict with arrays. Similarly -> would conflict with objects.

Another option was ~> but too many people thought it looked weird, and apparently it too had some parser problems. Ruby's |x| form didn't last long in the conversation either. \()=> came up for a while but enough people objected to Yet Another Backslash. And there were other assorted arrangements of symbols - braces, brackets, pipes, you name it.

But => did win out once people realized that the ambiguity problem could be resolved by using it as keyword()=>. So then the question was what keyword. "function" already exists but was too long for something that's supposed to be short and easy to type. "lambda" was another but was maybe sounded too technical. "fn" finally won as being short and concise. Whether fn()==> could also have worked, I don't know...

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.