Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,749 members, 7,817,072 topics. Date: Saturday, 04 May 2024 at 03:11 AM

Who Can Solve This In PHP? - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Who Can Solve This In PHP? (1439 Views)

Can You Solve This Google Coding Interview Problem? (pics) / Solve This Amazon Interview Question / Who Can Solve This Problem Using Java (2) (3) (4)

(1) (Reply) (Go Down)

Who Can Solve This In PHP? by WebLab: 10:17pm On Feb 01, 2021
Please house, I need solutions to these two guys in PHP

1) Write a function that would return the Nth largest element from start or the end (default from the end) in an array, and then provide a set of test cases against that function.
- Note: That is the Nth largest element in the sorted order, not the Nth distinct element.
For Example: Given [3,2,1,5,6,4], n = 2, from = end, return 5.

2) Write a function that would: arrange an array of positive integers to form the largest number or the smallest number. (default largest)
- Note: Return a string instead of an integer in case the result is too large.
For example, given [2, 20, 24, 6, 8], type = largest, the largest formed number is 8624220.
Re: Who Can Solve This In PHP? by etoluw: 7:13am On Feb 02, 2021
WebLab:
Please house, I need solutions to these two guys in PHP

1) Write a function that would return the Nth largest element from start or the end (default from the end) in an array, and then provide a set of test cases against that function.
- Note: That is the Nth largest element in the sorted order, not the Nth distinct element.
For Example: Given [3,2,1,5,6,4], n = 2, from = end, return 5.

2) Write a function that would: arrange an array of positive integers to form the largest number or the smallest number. (default largest)
- Note: Return a string instead of an integer in case the result is too large.
For example, given [2, 20, 24, 6, 8], type = largest, the largest formed number is 8624220.

how much u go pay?
Re: Who Can Solve This In PHP? by WebLab: 8:11am On Feb 02, 2021
etoluw:


how much u go pay?

Solve it first to show skills
Re: Who Can Solve This In PHP? by Karleb(m): 9:06am On Feb 02, 2021
WebLab:
Solve it first to show skills
And you'll copy his code and not pay him?
Re: Who Can Solve This In PHP? by WebLab: 10:05am On Feb 02, 2021
Karleb:


And you'll copy his code and not pay him?

Like I said, I don't think I attached price to the questions. It's a challenge from one of my students and I have solved some of the questions but these two I are left. If anyone can solve it, I will learn and thank him and someone else might also learn through it in the future. If no one solves, I will keep cracking it till it dissolves.
As Programmers, we solve questions on Stackoverflow without asking for a penny. He's not at faults for asking for money though. Everyone to his view.
Thanks.

1 Like

Re: Who Can Solve This In PHP? by etoluw: 12:14pm On Feb 02, 2021
WebLab:


Like I said, I don't think I attached price to the questions. It's a challenge from one of my students and I have solved some of the questions but these two I are left. If anyone can solve it, I will learn and thank him and someone else might also learn through it in the future. If no one solves, I will keep cracking it till it dissolves.
As Programmers, we solve questions on Stackoverflow without asking for a penny. He's not at faults for asking for money though. Everyone to his view.
Thanks.

okay
I'll try to solve it later in the day and will get back to you
Re: Who Can Solve This In PHP? by WebLab: 12:50pm On Feb 02, 2021
Alright etoluw
Re: Who Can Solve This In PHP? by bassdow: 11:57pm On Feb 03, 2021
WebLab:
Please house, I need solutions to these two guys in PHP

1) Write a function that would return the Nth largest element from start or the end (default from the end) in an array, and then provide a set of test cases against that function.
- Note: That is the Nth largest element in the sorted order, not the Nth distinct element.
For Example: Given [3,2,1,5,6,4], n = 2, from = end, return 5.

2) Write a function that would: arrange an array of positive integers to form the largest number or the smallest number. (default largest)
- Note: Return a string instead of an integer in case the result is too large.
For example, given [2, 20, 24, 6, 8], type = largest, the largest formed number is 8624220.


Not a fan of wasting time unnecessarily but if you're asking to get a Clue, a simple foreach loop + basic arithmetic logic should handle them all.
And if you like to perform Premature Optimizations and/ or write less, a single loop can handle both Question 1 and 2 .

Hopefully the arrays would be simple array, not multi-dimensional array else you've got to a function that recursively handles them
Re: Who Can Solve This In PHP? by Springboot: 6:35am On Feb 04, 2021
etoluw:


how much u go pay?

Is this poverty or you just don't have an idea about the code. What is there to pay? Is this a project?
Re: Who Can Solve This In PHP? by WebLab: 7:07am On Feb 04, 2021
bassdow:



Not a fan of wasting time unnecessarily but if you're asking to get a Clue, a simple foreach loop + basic arithmetic logic should handle them all.
And if you like to perform Premature Optimizations and/ or write less, a single loop can handle both Question 1 and 2 .

Hopefully the arrays would be simple array, not multi-dimensional array else you've got to a function that recursively handles them
I think the time spent writing this is more than enough to write the SIMPLE FOREACH LOOP.
Aunty, solve these questions make your boys learn from you asap.
Re: Who Can Solve This In PHP? by etoluw: 9:41am On Feb 04, 2021
@WebLab Have you been able to fix it?
Re: Who Can Solve This In PHP? by WebLab: 11:15am On Feb 04, 2021
etoluw:
@WebLab Have you been able to fix it?
Not yet
Re: Who Can Solve This In PHP? by etoluw: 4:24pm On Feb 04, 2021
The solution to the first one
function getN( $arr, $n = 3, $end = true ) {
$count = count($arr);
if($end) $n = $count - $n; else $n--;
sort($arr);
return $arr[$n]; }
Re: Who Can Solve This In PHP? by spartan117(m): 4:25pm On Feb 04, 2021
WebLab:

Not yet
Can you provide more edge cases for the first challenge.

Also if I solve it in JavaScript will you be able to translate to php?
Re: Who Can Solve This In PHP? by WebLab: 4:45pm On Feb 04, 2021
etoluw:
The solution to the first one

function getN( $arr, $n = 3, $end = true ) {

$count = count($arr);

if($end) $n = $count - $n;
else $n--;

sort($arr);

return $arr[$n];
}

Weldone bro. But why did you initialize $n with 3?
Re: Who Can Solve This In PHP? by WebLab: 4:54pm On Feb 04, 2021
spartan117:

Can you provide more edge cases for the first challenge.

Also if I solve it in JavaScript will you be able to translate to php?

I think the first question needs to be sorted in ascending order first and then locate the largest element of the array. After this, the positioning takes place depending on the nth values passed into the function. For instance, after sorting the array in the example we have [1, 2, 3, 4, 5, 6]. Locating the largest element will give us 6 which array[5]. Then counting two positions backward starting from 6 will give us 5 as 6 itself will be the first position backwards judging from n=2 test case from the example. The algorithm is to first locate the largest element of the array and count backward from it depending on the test case given. That's how I see the stuff personally.
Re: Who Can Solve This In PHP? by spartan117(m): 5:01pm On Feb 04, 2021
WebLab:


I think the first question needs to be sorted in ascending order first and then locate the largest element of the array. After this, the positioning takes place depending on the nth values passed into the function. For instance, after sorting the array in the example we have [1, 2, 3, 4, 5, 6]. Locating the largest element will give us 6 which array[5]. Then counting two positions backward starting from 6 will give us 5 as 6 itself will be the first position backwards judging from n=2 test case from the example. The algorithm is to first locate the largest element of the array and count backward from it depending on the test case give. That's how I see the stuff personally.
Looks like you have already solved it.
Re: Who Can Solve This In PHP? by WebLab: 5:05pm On Feb 04, 2021
spartan117:

Looks like you have already solved it.
I haven't actually.
Re: Who Can Solve This In PHP? by spartan117(m): 5:45pm On Feb 04, 2021
WebLab:

I haven't actually.
Writing the code is the easy part it's just a matter of syntax. Figuring out how to approach the problem and breaking it into parts is the hard part and from what I've seen you've already done that and arrived at a solution that would work.
Re: Who Can Solve This In PHP? by etoluw: 7:01pm On Feb 04, 2021
WebLab:

Weldone bro. But why did you initialize $n with 3?
it's just a random number
Re: Who Can Solve This In PHP? by etoluw: 7:45pm On Feb 04, 2021
cloudflare is blocking me from posting the solution to the second question

(1) (Reply)

Latest Programming Lang In The IT Word Is: / Ideas On How To Manage My Blog / Write Code To Implement Polygon In Java

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 31
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.