Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,632 members, 7,816,609 topics. Date: Friday, 03 May 2024 at 01:51 PM

Can You Solve PHP Problems? Let's Find Out.. - Programming (2) - Nairaland

Nairaland Forum / Science/Technology / Programming / Can You Solve PHP Problems? Let's Find Out.. (3399 Views)

Can You Solve This Tricky Question? / Number Generator Challenge : Can You Solve This? / Can You Solve This? (2) (3) (4)

(1) (2) (Reply) (Go Down)

Re: Can You Solve PHP Problems? Let's Find Out.. by Kodejuice: 3:59pm On Oct 08, 2016
toheebDOTcom:
^^^kodejuice

$foo++ returns the value before incrementing.

++$foo increments before returning the value...

That only matters when your echo-ing it, i mean when you do:


$foo = "bar";
$foo++;
echo $foo;


AND


$foo = "bar";
++$foo;
echo $foo;


it outputs d same tin
Re: Can You Solve PHP Problems? Let's Find Out.. by Kodejuice: 4:05pm On Oct 08, 2016
FrankLampard:
Use any language to solve this very simple.

Question.

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10 001st prime number?

Euler? eww
Re: Can You Solve PHP Problems? Let's Find Out.. by Nobody: 4:05pm On Oct 08, 2016
FrankLampard:
Use any language to solve this very simple.

Question.

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10 001st prime number?

Nawa oo, this guy wants me to flex muscle sha..Okay here's my shot:

I made this re-usable for anyone that needs it -
...Loading, brb have to get something done real quick. I'd be back.
Re: Can You Solve PHP Problems? Let's Find Out.. by Nobody: 4:06pm On Oct 08, 2016
Kodejuice:

Euler? eww
I knew it was from Euler, just laughed inside of me
Re: Can You Solve PHP Problems? Let's Find Out.. by Kodejuice: 4:15pm On Oct 08, 2016
make una try this one, saw it in a book i'm reading:

Write a method/function which finds the maximum of two numbers. Don use if-else or any other comparison operator.

EXAMPLE
5, 10
=> 5
Re: Can You Solve PHP Problems? Let's Find Out.. by Nobody: 4:41pm On Oct 08, 2016
Kodejuice:
make una try this one, saw it in a book i'm reading:

Write a method/function which finds the maximum of two numbers. Don use if-else or any other comparison operator.

EXAMPLE
5, 10
=> 5
Including PHP's max() ?
Re: Can You Solve PHP Problems? Let's Find Out.. by Kodejuice: 4:45pm On Oct 08, 2016
DanielTheGeek:

Including PHP's max() ?
of cuz, dat one na cheating na.

1 Like

Re: Can You Solve PHP Problems? Let's Find Out.. by noordean(m): 12:36am On Oct 09, 2016
FrankLampard:
Use any language to solve this very simple.

Question.

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10 001st prime number?

My solution:
https://ideone.com/6BARWK

My program does not loop beyond around 3000th position but I'm sure my algorithm is correct , I guess this is due to the strong looping process involved.
I'm still trying it on online editor. When my system is up, I'll check and see.
Re: Can You Solve PHP Problems? Let's Find Out.. by guente02(m): 3:19am On Oct 09, 2016
What needs to be changed to make the above code work as expected?

Just add ! at the beginning of if-else condition. Just a hack tho. Still trying to figure out the why.

cc: DanielTheGeek


$first_string = 'DanielTheGeek' ;
$second_string = 'Daniel' ;
if (!strpos($first_string, $second_string)) {
echo "\"" . $first_string . "\" contains \"" . $second_string . "\"" ;
} else {
echo "\"" . $first_string . "\" does not contain \"" . $second_string . "\"" ;
}
Re: Can You Solve PHP Problems? Let's Find Out.. by FincoApps(m): 5:38am On Oct 09, 2016
DanielTheGeek, I'll appreciate if you created a similar thread but for Js. I feel it's easier to test Js codes, like right from the browser..... for php, small English go dey.

Except the challenge is like who can write a working code without testing it (I would LOVE that too)
Re: Can You Solve PHP Problems? Let's Find Out.. by Nobody: 6:00am On Oct 09, 2016
FincoApps:
DanielTheGeek, I'll appreciate if you created a similar thread but for Js. I feel it's easier to test Js codes, like right from the browser..... for php, small English go dey.

Except the challenge is like who can write a working code without testing it (I would LOVE that too)

Of course, another thread for JS will be up soon.

But I doubt anyone has been testing the above codes, na eye me I dey use debug am.
Re: Can You Solve PHP Problems? Let's Find Out.. by Nobody: 6:05am On Oct 09, 2016
guente02:

What needs to be changed to make the above code work as expected?

Just add ! at the beginning of if-else condition. Just a hack tho. Still trying to figure out the why.

cc: DanielTheGeek

Strpos() will return 0 if matched and 0 is false except you check against the type using !== or !strpos(), so what you're doing is a good hack saying, hey, if strpos() does not evaluate to true then that's what I'm looking for.
Re: Can You Solve PHP Problems? Let's Find Out.. by Nobody: 6:08am On Oct 09, 2016
noordean:


My solution:
https://ideone.com/6BARWK

My program does not loop beyond around 3000th position but I'm sure my algorithm is correct , I guess this is due to the strong looping process involved.
I'm still trying it on online editor. When my system is up, I'll check and see.



I'm happy you gave it a shot. From visual debugging, that looks like a correct solution.
Re: Can You Solve PHP Problems? Let's Find Out.. by noordean(m): 6:55am On Oct 09, 2016
DanielTheGeek:


I'm happy you gave it a shot. From visual debugging, that looks like a correct solution.
Thanks boss.

1 Like

Re: Can You Solve PHP Problems? Let's Find Out.. by FrankLampard: 7:38am On Oct 09, 2016
noordean:


My solution:
https://ideone.com/6BARWK

My program does not loop beyond around 3000th position but I'm sure my algorithm is correct , I guess this is due to the strong looping process involved.
I'm still trying it on online editor. When my system is up, I'll check and see.

Though I'm not a Python guy. But I can read your code slightly.

You tried in your implementation, but you need to read the question and answer the question again.

From your code you got 27437 (Not sure). The answer supposed to be 104743. It has been long since I solve that particular question. But you can effectively answer the question using Dynamic Programming DP, simply Google "sieve of eratosthenes".

Hint:

Use Sieve method to iterate all the prime number, then check if when the iteration has gotten to it 10001 number , then break out of the loop.

Happy coding.
Re: Can You Solve PHP Problems? Let's Find Out.. by ANTONINEUTRON(m): 5:06pm On Oct 09, 2016
abeg diz might be off topic but i notice a problem in php.

For Example::
$i = 1;
while($i<$antoniage){
$i++;
echo $i;
}

once d loop above run finish, if i want to use $i again in another loop(i intend it to still be equal to 1)
it return nothing or False


What might be d problem



Am just a beginner
Re: Can You Solve PHP Problems? Let's Find Out.. by FrankLampard: 5:32pm On Oct 09, 2016
ANTONINEUTRON:
abeg diz might be off topic but i notice a problem in php.

For Example::
$i = 1;
while($i<$antoniage){
$i++;
echo $i;
}

once d loop above run finish, if i want to use $i again in another loop(i intend it to still be equal to 1)
it return nothing or False


What might be d problem



Am just a beginner

Make your question clear.
Re: Can You Solve PHP Problems? Let's Find Out.. by Kodejuice: 7:24pm On Oct 09, 2016
ANTONINEUTRON:
abeg diz might be off topic but i notice a problem in php.

For Example::
$i = 1;
while($i<$antoniage){
$i++;
echo $i;
}

once d loop above run finish, if i want to use $i again in another loop(i intend it to still be equal to 1)
it return nothing or False


What might be d problem



Am just a beginner

Scope!, you're declaring the variable in an outer scope and incrementing it in the same outer scope, so its changed everywhere, try this

<?php

$i = 1;
$antoniage = 10; //eg

function fn1($arg) {
$i = $arg;
global $antoniage; //make it accessible from this fn

while ($i < $antoniage) {
$i++;
echo $i;
}
}

fn1($i);

//$i is still 1

1 Like

Re: Can You Solve PHP Problems? Let's Find Out.. by ANTONINEUTRON(m): 7:30pm On Oct 09, 2016
Kodejuice:


Scope!, you're declaring the variable in an outer scope and incrementing it in the same outer scope, so its changed everywhere, try this

<?php

$i = 1;
$antoniage = 10; //eg

function fn1($arg) {
$i = $arg;
global $antoniage; //make it accessible from this fn

while ($i < $antoniage) {
$i++;
echo $i;
}
}

fn1($i);

//$i is still 1
it worked
Re: Can You Solve PHP Problems? Let's Find Out.. by Nobody: 7:59pm On Oct 09, 2016
I like what I see here, thumbs up bros. I've been choked with work lately, keep the questions coming in.
Re: Can You Solve PHP Problems? Let's Find Out.. by guente02(m): 11:05pm On Oct 09, 2016
DanielTheGeek:
I like what I see here, thumbs up bros. I've been choked with work lately, keep the questions coming in.
How do i implement a pin/serial number generator.
I want the script to check for the existence of the pin in the db after every loop iteration.

Re: Can You Solve PHP Problems? Let's Find Out.. by Nobody: 6:19am On Oct 10, 2016
guente02:

How do i implement a pin/serial number generator.
I want the script to check for the existence of the pin in the db after every loop iteration.

You can check this CodeIgniter library I did. You can make that to work out of CodeIgniter, just add a method that checks if it exists in the db, if true, re-generate, else use the generated hash.
Re: Can You Solve PHP Problems? Let's Find Out.. by kenpanda(m): 10:19am On Oct 10, 2016
Check your parameters, you need three parameter passed
strpos (string $haystack, mixed $needle [, int $offset = 0]): int
Re: Can You Solve PHP Problems? Let's Find Out.. by Nobody: 5:33pm On Sep 14, 2018
FrankLampard:
Use any language to solve this very simple.

Question.

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10 001st prime number?

can you please help me with the solution to this?
Re: Can You Solve PHP Problems? Let's Find Out.. by segbolon: 3:43am On Sep 15, 2018
Have you been coding PHP for many years but have not gone beyond few functions?, this thread tests your practical knowledge of the language.
Please, don't post off-topic here, just serious contributions involving code and useful comments..

I'd take this from easy to hard and anybody can drop in a question at anytime.

Now, first question:

$first_string = 'DanielTheGeek' ;
$second_string = 'Daniel' ;
if (strpos($first_string, $second_string)) {
echo "\"" . $first_string . "\" contains \"" . $second_string . "\"" ;
} else {
echo "\"" . $first_string . "\" does not contain \"" . $second_string . "\"" ;
}

If you run the above code, you'd get:
"DanielTheGeek" does not contain "Daniel"

Why?

What needs to be changed to make the above code work as expected?

UPDATE: Five points for the first correctly answered question, a leaderboard will be published with the names of people that have high points.

eww

$ss = "Daniel";
$fs = "DanielTheGeek";
$conj = strstr($ss, $fs) ? "does not" : null;
echo "'$fs' $conj contains '$ss'\n" ;
Re: Can You Solve PHP Problems? Let's Find Out.. by segbolon: 4:11am On Sep 15, 2018
chukwukahenry:


can you please help me with the solution to this?
Here's a naive solution,

<?php
$count = 0;
$num = 2;
$index = 10001;

function isPrime($num) {
for($i = 2; $i < $num; $i++) {
if (is_int($num/$i)) {
return false;
}
}
return true;
}

while (true) {
if (isPrime($num)) {
echo "$num is prime $count\n";
$count++;
}
if ($count == $index) { break; }
$num++;
}
echo "prime $num is count of $count\n";

Re: Can You Solve PHP Problems? Let's Find Out.. by segbolon: 4:16am On Sep 15, 2018
chukwukahenry:


can you please help me with the solution to this?
A non native approach which is 100x faster than the previous


$count = 1;
$num = 2;
$index = 10001;

function isPrime($num) {
static $m = [];
foreach ($m as $i) {
if (is_int($num/$i)) {
return false;
}
}
$m[] = $num;
return true;
}

while (true) {
if (isPrime($num)) {
echo "$num is prime $count\n";
$count++;
}
if ($count == $index) { break; }
$num++;
}
echo "prime $num is count of $count\n";
Re: Can You Solve PHP Problems? Let's Find Out.. by Nobody: 12:35am On Sep 16, 2018
thanks @segbolon
Re: Can You Solve PHP Problems? Let's Find Out.. by citicapfin: 7:59pm On Sep 03, 2020
best digital marketing online courses

(1) (2) (Reply)

Django Programmers Thread / Composer Installation Giving Errors , Please Help / Please What Are The Benefits Of Learning Php And Mysql. Please I Need Advice

(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. 39
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.