I was asked to write a program in php where all the occurrences of palindromes in a given string need to be printed.
Example: For the string I O M K I L O L I K T C J I O P L L P O
The answer would be:
O P L L P O and
K I L O L I K
While doing this it should kept in mind that every palindrome that is more than 3 characters in length can be broken down into more palindromes but you just need to print out the longest size possible for each set (so, for the example string, you SHOULD NOT print LOL, LL , PLLP and ILOLI)
I tried it but could only manage to do this:
$data = 'I O M K I L O L I K T C J I O P L L P O';
$data = str_replace(' ', '', $data);
$palindromes = [];
for($i=0; $i<strlen($data); $i++ ) {
for($j=3; $j<=(strlen($data)-$i); $j++){
$word = substr($data, $i, $j);
$reverse_word = strrev($word);
if($word == $reverse_word){
print "Word: ".$word."<br/>";
}
}
}
Which gives me the following output:
Word: KILOLIK
Word: ILOLI
Word: LOL
Word: OPLLPO
Word: PLLP
Which is not the expected ouput. What should I to get rid of the strings like ILOLI, LOL PLLP because I am expected to get the longest palindrome.
According your example, you mention the answer would be O P L L P O (six characters long) and K I L O L I K (seven characters long), but later you imply only the longest palindrome should be returned?
Assuming you mean the later case, you can first assign all the palindromes to array and then sort the array, like this:
function str_length_sort($first, $second) {
return strlen($second) - strlen($first);
}
$data = 'I O M K I L O L I K T C J I O P L L P O';
$data = str_replace(' ', '', $data);
$palindromes = [];
for($i=0; $i<strlen($data); $i++ ) {
for($j=3; $j<=(strlen($data)-$i); $j++){
$word = substr($data, $i, $j);
$reverse_word = strrev($word);
if($word == $reverse_word){
$palindromes[] = $word;
}
}
}
usort($palindromes, 'str_length_sort');
$max = strlen($palindromes[0]);
foreach ($palindromes as $p) {
$length = strlen($p);
if ($length >= $max) {
echo $p;
}
}
Hope this helps.
Related
<?php
for ($i = 'a'; $i <= 'z'; $i++)
echo "$i\n";
This snippet gives the following output (newlines are replaced by spaces):
a b c d e f g h i j k l m n o p q r s t u v w x y z aa ab ac ad ae af ag ah ai aj ak al am an ao ap aq ar as at au av aw ax ay az ba bb bc bd be bf bg bh bi bj bk bl bm bn bo bp bq br bs bt bu bv bw bx by bz ca cb cc cd ce cf cg ch ci cj ck cl cm cn co cp cq cr cs ct cu cv cw cx cy cz da db dc dd de df dg dh di dj dk dl dm dn do dp dq dr ds dt du dv dw dx dy dz ea eb ec ed ee ef eg eh ei ej ek el em en eo ep eq er es et eu ev ew ex... on to yz
From the docs:
PHP follows Perl's convention when dealing with arithmetic operations on character variables and not C's.
For example, in Perl 'Z'+1 turns into 'AA', while in C 'Z'+1 turns into '[' ( ord('Z') == 90, ord('[') == 91 ).
Note that character variables can be incremented but not decremented and even so only plain ASCII characters (a-z and A-Z) are supported.
From Comments:-
It should also be noted that <= is a lexicographical comparison, so 'z'+1 ≤ 'z'. (Since 'z'+1 = 'aa' ≤ 'z'. But 'za' ≤ 'z' is the first time the comparison is false.) Breaking when $i == 'z' would work, for instance.
Example here.
Because once 'z' is reached (and this is a valid result within your range, the $i++ increments it to the next value in sequence), the next value will be 'aa'; and alphabetically, 'aa' is < 'z', so the comparison is never met
for ($i = 'a'; $i != 'aa'; $i++)
echo "$i\n";
Others answers explain the observed behavior of the posted code. Here is one way to do what you want (and it's cleaner code, IMO):
foreach (range('a', 'z') as $i)
echo "$i\n";
In response to ShreevatsaR's comment/question about the range function: Yes, it produces the "right endpoint", i.e. the values passed to the function are in the range. To illustrate, the output from the above code was:
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
Others already said why PHP doesn't show what you expect. Here's how you get the result you might want:
<?php
for ($i = ord('a'); $i <= ord('z'); $i++)
echo chr($i);
?>
Why not just use range('a','z')?
Try this code. I think this code will be helpful to you.
$alphas = range('A', 'Z');
foreach($alphas as $value){
echo $value."<br>";
}
Display 26 letters in sequence.
<?php
$i = 'a';
do {
echo ($j=$i++),"\r\n";
} while (ord($j) < ord($i));
?>
Also this can be used:
for ($i = 'a'; $i <= 'z'; $i=chr(ord($i)+1))
echo "$i\n";
PHP has the function of looping letters and can exceed beyond single characters; the rest will be done this way: aa ab ac... zz, and so on.
Try this:
<?php
for ($i = 'a'; $i !== 'aa'; $i++)
echo "$i\n";
?>
While the above answers are insightful to what's going on, and pretty interesting (I didn't know it would behave like this, and its good to see why.
The easiest fix (although perhaps not the most meaningful) would be just to change the condition to $i != 'z'
<?php
for ($i = 'a'; $i != 'z'; $i++)
echo "$i\n";
?>
The PHP does not consider 'AA' less than 'Z'.
The best way to make this is:
for($i = 'a'; $i != 'aa'; $i++) {
echo $i;
}
abcdefghijklmnopqrstuvwxyz
Perhaps this code will work. It’s easy & can be understood:
<?php
$ascii_val = ord("a");
for($i=$ascii_val;$i<$ascii_val+26;$i++){
echo chr($i)."\n";
}
?>
where 26 is the total number of letters in the alphabet.
There are several ways to do that.
1. First you can take the range from 'a' to 'z'. Then iterate a loop over it.
foreach(range('a', 'z') as $i)
{
echo $i . "\n";
}
2. You can print the letters using asci value of the characters.
for($i = 97 ; $i<=122; $i++)
{
echo chr($i) . "\n";
}
3. You can take the asci value of 'a' and run a loop till the asci value of 'z':
for ($x = ord('a'); $x <= ord('z'); $x++)
{
echo chr($x) . "\n";
}
4. For printing a new line in html you can append the to the end of each characters.
for($i='a';$i<='z';$i++)
{
echo $i. "<br />";
}
this code will work. It’s easy & can be understood:
<?php
// print form A to ZZ like this
// A B C ... AA AB AC ... ZA .. ZZ
$pre = "";
$i = ord('a');
for ($x = ord('a'); $pre . strtoupper(chr($x-1)) != 'AH'; $x++)
{
echo "". $pre . strtoupper(chr($x)) . "\n";
if(chr($x) === "z"){
$pre = strtoupper(chr($i++));
$x = ord('a');
$x--;
}
}
One more way to get the display of characters a-z:
<?php
for ($i = 'a'; $i < 'z'; $i++){
echo "$i\n";
}
echo $i; // 'z'
See live code
Once the conditional loop becomes false, $i will have already been incremented and have the string value corresponding to 'z' which can be displayed.
Wow I really didn't know about this but its not a big code you can try echo "z" after loop Mark is Absolutely Right I use his method but if you want alternative then this may also you can try
<?php
for ($i = "a"; $i = "y"; $i++) {
echo "$i\n";
if ($i == "z") {}
}
echo "z";
?>
How can I explode a string by one or more spaces or tabs?
Example:
A B C
D E F
G H I
J K L
M N O
I have successfully tokenize on spaces and tab's with this code:
$parts = preg_split('/\s+/', $str);
but the issue rises when there is new line.
I want to make this an array so that i can run mysqlquery as well.
If you want to get a matrix of characters, split by newline first:
<?php
$str = "A B\tC\nD E\tF";
$parts = preg_split("/\n+/", $str);
foreach($parts as $key => $line)
{
$parts[$key] = preg_split("/\s+/", $line);
}
//output
$n = count($parts);
for ($i = 0; $i < $n; ++$i)
{
$m = count($parts[$i]);
for ($j = 0; $j < $m; ++$j)
{
print $parts[$i][$j]." ";
}
print "\n";
}
Output:
A B C
D E F
I was trying to create a simple password generator and noted that array_rand() returns the same results. Here is the code:
<?php
function generatePass() {
$password = '';
$a = explode(' ', 'q w e r t y u i o p a s d f g h j k l z x c v b n m');
for($i=0; $i < rand(1,200); $i++) {
$password .= $a[array_rand($a)];
}
return $password;
}
$r = 0;
while ($r <= 10000) { #generating 10 000 passwords
$total[] = generatePass();
$r++;
}
echo '<pre>';
print_r($total);
echo '</pre>';
?>
The $total array basically contains the same results repeated over and over again; if I refresh the page, only the order of elements changes, and not their values.
The question is: is this an expected behaviour or am I doing something wrong?
Thak you for your attention.
Reseed the random number generator with srand() just prior to calling array_rand.
srand();
$password .= $a[array_rand($a)];
I think it should be your PHP version. I just copied your code into my localhost server and it worked fine. I'm using PHP 5.5.9-1ubuntu4.7, you should try it or a newer version.
By the way, if you can't update your PHP version, use this modified version of your code:
<?php
function generatePass() {
$password = '';
// Put all letters into an array.
$letters = array('q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c','v','b','n','m');
for($i=0; $i < rand(1,200); $i++) {
// Select a random letter with rand() command inside the brackets of the array.
$password .= $letters[rand(0,25)];
}
return $password;
}
$r = 0;
while ($r <= 10000) { #generating 10 000 passwords
$total[] = generatePass();
$r++;
}
echo '<pre>';
print_r($total);
echo '</pre>';
?>
Seeding the random generator isn't needed since version 4.2.0
Try using mt_rand() for a better generation.
http://php.net/manual/en/function.mt-rand.php
Edit:
I think you actually want
$a = explode(' ', 'q w e r t y u i o p a s d f g h j k l z x c v b n m');
$end = mt_rand(1,200);
$password = '';
for($i=0; $i < $end; $i++) {
$password .= $a[array_rand($a)];
}
I took the time for you and wrote the generator.
I got it to generate 10000 unique passwords and code is efficient:
<?php
function generatePass() {
$password = '';
$lib = 'q w e r t y u i o p a s d f g h j k l z x c v b n m';
$a = explode(' ', $lib);
//remove random char
for($i=0; $i < mt_rand(1,49); $i++) {
shuffle($a);
//get Random Char
$password .= $a[mt_rand(0,count($a)-1)];
}
return $password;
}
$len = 10000; // total number of numbers
$range = array();
foreach(range(0, $len - 1) as $i){
while(in_array($num = generatePass(), $range)){}
$range[] = $num;
}
echo '<pre>';
print_r($range);
echo '</pre>';
Sorry if a solution exists anywhere else, but I couldn't find one.
I have the following array:
$data = array(
array('a', 'b', 'c'),
array('e', 'f', 'g'),
array('w', 'x', 'y', 'z'),
);
I am trying hard to write a function that will give an array like:
a
e
w
x
y
z
f
w
x
y
z
g
w
x
y
z
b
e
w
x
y
z
f
w
x
y
z
g
w
x
y
z
c
e
w
x
y
z
f
w
x
y
z
g
w
x
y
z
The major problem here is that the number of source arrays and their lengths keep changing always. So a function should be capable of handling any data given to it.
I tried to come up with something like this:
function testfunc($data){
$arrayDepth = count($data);
foreach($data as $key=>$d){
foreach($d as $e){
echo $e . "\n";
if($key < $arrayDepth){
array_shift($data);
testfunc($data);
}
}
}
}
And the output I got was:
a
e
w
x
y
z
f
g
w
x
y
z
b
w
x
y
z
c
e
f
g
w
x
y
z
I am stuck for almost a day with no proper solution. Any help would be great! Thanks!
Recursion [Wikipedia] is your friend:
function product($arrays) {
if(count($arrays) === 1) {
return $arrays[0];
}
else {
$result = array();
foreach($arrays[0] as $value) {
$result[$value] = product(array_slice($arrays, 1));
}
return $result;
}
}
DEMO
Non-recursive version. This should run fast!
$result = end($data);
if ($result === false)
{
return false; // or Array or what makes sense for an empty array.
}
$higherArr = prev($data);
while ($higherArr !== false)
{
// Set the orignal array to be the one that you built previously.
$orig = $result;
$result = array();
foreach ($higherArr as $higherKey)
{
$result[$higherKey] = $orig;
}
$higherArr = prev($data);
}
echo 'Finished with: ' . var_export($result, true);
<?php
for ($i = 'a'; $i <= 'z'; $i++)
echo "$i\n";
This snippet gives the following output (newlines are replaced by spaces):
a b c d e f g h i j k l m n o p q r s t u v w x y z aa ab ac ad ae af ag ah ai aj ak al am an ao ap aq ar as at au av aw ax ay az ba bb bc bd be bf bg bh bi bj bk bl bm bn bo bp bq br bs bt bu bv bw bx by bz ca cb cc cd ce cf cg ch ci cj ck cl cm cn co cp cq cr cs ct cu cv cw cx cy cz da db dc dd de df dg dh di dj dk dl dm dn do dp dq dr ds dt du dv dw dx dy dz ea eb ec ed ee ef eg eh ei ej ek el em en eo ep eq er es et eu ev ew ex... on to yz
From the docs:
PHP follows Perl's convention when dealing with arithmetic operations on character variables and not C's.
For example, in Perl 'Z'+1 turns into 'AA', while in C 'Z'+1 turns into '[' ( ord('Z') == 90, ord('[') == 91 ).
Note that character variables can be incremented but not decremented and even so only plain ASCII characters (a-z and A-Z) are supported.
From Comments:-
It should also be noted that <= is a lexicographical comparison, so 'z'+1 ≤ 'z'. (Since 'z'+1 = 'aa' ≤ 'z'. But 'za' ≤ 'z' is the first time the comparison is false.) Breaking when $i == 'z' would work, for instance.
Example here.
Because once 'z' is reached (and this is a valid result within your range, the $i++ increments it to the next value in sequence), the next value will be 'aa'; and alphabetically, 'aa' is < 'z', so the comparison is never met
for ($i = 'a'; $i != 'aa'; $i++)
echo "$i\n";
Others answers explain the observed behavior of the posted code. Here is one way to do what you want (and it's cleaner code, IMO):
foreach (range('a', 'z') as $i)
echo "$i\n";
In response to ShreevatsaR's comment/question about the range function: Yes, it produces the "right endpoint", i.e. the values passed to the function are in the range. To illustrate, the output from the above code was:
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
Others already said why PHP doesn't show what you expect. Here's how you get the result you might want:
<?php
for ($i = ord('a'); $i <= ord('z'); $i++)
echo chr($i);
?>
Why not just use range('a','z')?
Try this code. I think this code will be helpful to you.
$alphas = range('A', 'Z');
foreach($alphas as $value){
echo $value."<br>";
}
Display 26 letters in sequence.
<?php
$i = 'a';
do {
echo ($j=$i++),"\r\n";
} while (ord($j) < ord($i));
?>
Also this can be used:
for ($i = 'a'; $i <= 'z'; $i=chr(ord($i)+1))
echo "$i\n";
PHP has the function of looping letters and can exceed beyond single characters; the rest will be done this way: aa ab ac... zz, and so on.
Try this:
<?php
for ($i = 'a'; $i !== 'aa'; $i++)
echo "$i\n";
?>
While the above answers are insightful to what's going on, and pretty interesting (I didn't know it would behave like this, and its good to see why.
The easiest fix (although perhaps not the most meaningful) would be just to change the condition to $i != 'z'
<?php
for ($i = 'a'; $i != 'z'; $i++)
echo "$i\n";
?>
The PHP does not consider 'AA' less than 'Z'.
The best way to make this is:
for($i = 'a'; $i != 'aa'; $i++) {
echo $i;
}
abcdefghijklmnopqrstuvwxyz
Perhaps this code will work. It’s easy & can be understood:
<?php
$ascii_val = ord("a");
for($i=$ascii_val;$i<$ascii_val+26;$i++){
echo chr($i)."\n";
}
?>
where 26 is the total number of letters in the alphabet.
There are several ways to do that.
1. First you can take the range from 'a' to 'z'. Then iterate a loop over it.
foreach(range('a', 'z') as $i)
{
echo $i . "\n";
}
2. You can print the letters using asci value of the characters.
for($i = 97 ; $i<=122; $i++)
{
echo chr($i) . "\n";
}
3. You can take the asci value of 'a' and run a loop till the asci value of 'z':
for ($x = ord('a'); $x <= ord('z'); $x++)
{
echo chr($x) . "\n";
}
4. For printing a new line in html you can append the to the end of each characters.
for($i='a';$i<='z';$i++)
{
echo $i. "<br />";
}
this code will work. It’s easy & can be understood:
<?php
// print form A to ZZ like this
// A B C ... AA AB AC ... ZA .. ZZ
$pre = "";
$i = ord('a');
for ($x = ord('a'); $pre . strtoupper(chr($x-1)) != 'AH'; $x++)
{
echo "". $pre . strtoupper(chr($x)) . "\n";
if(chr($x) === "z"){
$pre = strtoupper(chr($i++));
$x = ord('a');
$x--;
}
}
One more way to get the display of characters a-z:
<?php
for ($i = 'a'; $i < 'z'; $i++){
echo "$i\n";
}
echo $i; // 'z'
See live code
Once the conditional loop becomes false, $i will have already been incremented and have the string value corresponding to 'z' which can be displayed.
Wow I really didn't know about this but its not a big code you can try echo "z" after loop Mark is Absolutely Right I use his method but if you want alternative then this may also you can try
<?php
for ($i = "a"; $i = "y"; $i++) {
echo "$i\n";
if ($i == "z") {}
}
echo "z";
?>