I have this code that for some reason it keeps in an infinite loop, when it is suppose to just print all possible substrings. This is part of the complete function. The purpose of the function is to return the indexes of the substring from string $str2 found in string $str1. Thanks a lot for the help.
$str1='QYDIKYTWNVPKIAPKS';
$str2='KYTWNVPKSS';
print($str1);echo"</br>";print($str2);echo"</br>";
function overlapping($str1,$str2) {
$peptide1 = str_split($str1);
$peptide2 = str_split($str2);
$longest_seq=array();
$len=count($peptide2)-1;
for ($i = 0; $i < count($peptide1); ++$i) {
for ($j = 0; $j < count($peptide2); ++$j) {
if ($peptide2[$j]==$peptide1[$i]){
$k=$j;
$start=$j;
$l=$i;
$tmp=array();
while ($peptide2[$k]==$peptide1[$l]){
array_push($tmp, $peptide2[$k]);
$substring=implode($tmp);
print $substring;
echo"</br>";
$k=$k+1;
$l=$l+1;
}
}
}
}
}
Maybe you have another idea of how to extract the index of a matching substring, I can also try that
You never check to insure that $k and $l are valid offsets. Check for it here :
while (isset($peptide2[$k]) && isset($peptide1[$l]) && $peptide2[$k]==$peptide1[$l]){
instead of
while ($peptide2[$k]==$peptide1[$l]){
I'm not sure if this has anything to do with your issue, but I always post-increment in my for loops.
for ($i = 0; $i < count($peptide1); $i++) {
// do stuff
}
Related
i have some problem.
i just want my loop to run, but when i try to do it, it fails, it has to increment each letter by a few, but it doesn't take any new letters at all, why is this happening and what is the reason? in c ++ such code would work.
function accum('ZpglnRxqenU') {
// your code
$result = '';
$letters_result = '';
$letter_original = '';
$num_if_str = strlen($s);
$j = 0;
for ( $i=0; $i <= $num_if_str; $i++ )
{
$letter_original = substr($s, $i, $i+1);
$j = 0;
while($j == $i)
{
$letters_result = $letters_result . $letter_original;
$j++;
}
if($i != strlen($s))
{
$letters_result = $letters_result . '-';
}
}
return $letters_result;
}
It returns
- Expected: 'Z-Pp-Ggg-Llll-Nnnnn-Rrrrrr-Xxxxxxx-Qqqqqqqq-Eeeeeeeee-Nnnnnnnnnn-Uuuuuuuuuuu'
Actual : 'Z-----------'
what problem with what PHP code?
There are a number of problems here:
you're using $s but never initialise it
Your call to substr() uses an incorrect value for the length of substring to return
you're inner loop only runs while $i = $j, but you initialise $j to 0 so it will only run when $i is zero, i.e. for the first letter of the string.
There is a simpler way to do this. In PHP you can address individual characters in a string as if they were array elements, so no need for substr()
Further, you can use str_repeat() to generate the repeating strings, and if you store the expanded strings in an array you can join them all with implode().
Lastly, combining ucwords() and strtolower() returns the required case.
Putting it all together we get
<?php
$str = "ZpglnRxqenU";
$output = [];
for ($i = 0;$i<strlen($str);$i++) {
$output[] = str_repeat($str[$i], $i+1);
}
$output = ucwords(strtolower(implode('-',$output)),"-");
echo $output; // Z-Pp-Ggg-Llll-Nnnnn-Rrrrrr-Xxxxxxx-Qqqqqqqq-Eeeeeeeee-Nnnnnnnnnn-Uuuuuuuuuuu
Demo:https://3v4l.org/OoukZ
I don't have much more to add to #TangentiallyPerpendicular's answer as far as critique, other than you've made the classic while($i<=strlen($s)) off-by-one blunder. String bar will have a length of 3, but arrays are zero-indexed [eg: [ 0 => 'b', 1 => 'a', '2' => 'r' ]] so when you hit $i == strlen() at 3, that's an error.
Aside from that your approach, when corrected and made concise, would look like:
function accum($input) {
$result = '';
for ( $i=0, $len=strlen($input); $i < $len; $i++ ) {
$letter = substr($input, $i, 1);
for( $j=0; $j<=$i; $j++ ) {
$result .= $letter;
}
if($i != $len-1) {
$result .= '-';
}
}
return $result;
}
var_dump(accum('ZpglnRxqenU'));
Output:
string(76) "Z-pp-ggg-llll-nnnnn-RRRRRR-xxxxxxx-qqqqqqqq-eeeeeeeee-nnnnnnnnnn-UUUUUUUUUUU"
Also keep in mind that functions have their own isolated variable scope, so you don't need to namespace variables like $letters_foo which can make your code a bit confusing to the eye.
Below is the code i tried, where count is showing improper.
Please help me to get where i am missing the logic.
I am attaching the code which i have tried so far.
PS Note:- I am not intended to use more built in function of php and so I created function for string length.
error_reporting(E_ALL);
$string = "ssddk";
function checkString($addinString, &$stringBK) {
if (empty(count($stringBK))) {
$stringBK[] = $addinString;
return false;
}
foreach ($stringBK as $key => $val) {
if ($addinString == $val) {
return true;
}
}
$stringBK[] = $addinString;
return false;
}
for ($i = 0; $i < checkstrlength($string); $i++) {
$count = 0;
for ($j = 0; $j < checkstrlength($string); $j++) {
if ($string[$i] == $string[$j]) {
if (checkString($string[$i], $stringBK)) {
continue 2;
}
$count++;
echo "Column => " . $string[$j] . " for count" .$count . "<br>";
}
}
}
function checkstrlength($string) {
$count = 0;
for ($i = 0; $string[$i] != ""; $i++) {
$count++;
}
return $count;
}
It gives below output ,
Column => s for count1
Column => d for count1
Column => k for count1
I am expecting it as ,
Column => s for count 2
Column => d for count 2
Column => k for count 1
Ok, there a couple of things to look at here.
The checkstrlength() has the below loop.
for ($i = 0; $string[$i] != ""; $i++) {
Formally speaking, we usually look at \0 terminating character in the string to terminate our loop. But in PHP, everything is a string. So, \0 is now a string to match on rather than a character match. Better, we do an isset check to stop our loop. So, code would look like:
for ($i = 0; isset($string[$i]); $i++) {
Second is your not caching the result which you got from checkstrlength(). Do it. Also, you can start the inner loop from $i itself. There is no need to go from start again. So, for loop would look like:
$length = checkstrlength($string);
for ($i = 0; $i < $length; $i++) {
for ($j = $i; $j < $length; $j++) {
Third is that there is no need of empty and count checks in checkString. This also reduces inbuilt function calls. You can simply loop over and return true if found. If not found, we are adding it anyway. So it would look like:
function checkString($addinString, &$stringBK) {
foreach ($stringBK as $key => $val) {
if ($addinString == $val) {
return true;
}
}
$stringBK[] = $addinString;
return false;
}
Now, in your nested loop, you add it to $stringBK outside of the inner loop, because there is no point in checking with the inner loop when chars match. This is because if some character was visited, why initialize the inner loop at all. Just have a check above and continue the search and count. Also note that you are having echo statements inside the inner loop which doesn't make sense because we haven't finished the count yet. Let's do and print it outside of the inner loop at the end. Snippet as follows:
for ($i = 0; $i < $length; $i++) {
$count = 0;
if (checkString($string[$i], $stringBK)) {
continue;
}
for ($j = $i; $j < $length; $j++) {
if ($string[$i] == $string[$j]) {
$count++;
}
}
echo "Column => " . $string[$i] . " for count : " .$count,PHP_EOL;
}
Final Code Demo: https://3v4l.org/4dpST
I would like to write a function that can count the number of times a substring occurs in a string using strlen+substr+strpos ONLY ,in PHP.
Without using substr_count!
example: fn('iwritecodeiwritecode-','i');
Thanks Ahead
You will not need strlen() or substr() for this task.
Merely iterate your string with a while loop, advance the output of strpos() with every successful found needle and count the number of successful matches.
The "magic" in this technique is using the previous strpos() value (plus 1) as the the starting point for all subsequent strpos() calls.
Code: (Demo)
function countSubstrings($haystack,$needle) {
$pos = -1; // start at -1 so that first iteration uses $pos of 0 as starting offset
$tally = 0;
while (($pos = strpos($haystack, $needle, ++$pos)) !== false) {
++$tally;
}
return $tally;
}
echo countSubstrings('iwritecodeiwritecodeiwritecode', 'i'); // 6
echo countSubstrings('iwritecodeiwritecodeiwritecode', 'Perumal'); // 0
echo countSubstrings('iwritecodeiwritecodeiwritecode', 'write'); // 3
A note to future readers, this question is not best practice. The correct method would be a simple call of the pre-existing php function substr_count().
echo substr_count('iwritecodeiwritecodeiwritecode', 'i');
Or, less efficient versus substring_count() would be preg_match_all() which returns the number of matches.
echo preg_match_all('/i/', 'iwritecodeiwritecodeiwritecode'); // 6
function fn($string, $char){
$count=0;
for($i=0; $i<strlen($string);$i++){
if($string[$i] == $char){
$count++;
}
}
print($count);
}
fn('iwritecodeiwritecode-','i');
I hope it helps Cheers!
I've come up with my own best solution.
<?php
$str = "iwritecodeiwritecode";
function find_substr_count($str, $substr) {
$substr_len = strlen($substr);
$substr_count = 0;
for($i = 0; $i < strlen($str); $i++) {
$substr_temp = '';
for($j = $i; $j < $i + $substr_len; $j++) {
if($j < strlen($str)) {
$substr_temp .= $str[$j];
}
}
if($substr_temp == $substr) {
$substr_count += 1;
}
}
return $substr_count;
}
echo find_substr_count($str, "i");
?>
It doesn't only work for single character. You can also try passing two or more characters in the function like:
echo find_substr_count($str, "write");
I've given my best to help you.
Hope it helps!
The code below basically helps in finding out if a number is a Palindromic Number or not. Although I get my execution done with the output, I just can seem to handle all the "screams" and fatal errors that I get. How do I handle this. Just a beginner and trust you can explain in a way that I may be able to understand..
<?php
for ($num = 1; $num <= 20; ++$num){
$_array1 = str_split($num);
//print_r($_array1);
//echo "<br/>";
$_array2 = array_reverse($_array1);
//print_r($_array2);
//echo "<br/>";
$i = 0;
$j = 0;
while ($i < sizeof($_array1) && $j < sizeof($_array2)){
if ($_array1[$i] == $_array2[$j]){
++$i;
++$j;
}
}
if ($_array1[$i] == $_array2[$j]){
echo "The number $num is a Palindrome Number";
}
}
?>
You get to the size of elements, which is 1. However, if your array has only one element, which is the case for 1-digit numbers, then sizeof($_array) === 1. Which means that the biggest possible index you can use is 0. You need to change your code to something like this:
<?php
for ($num = 1; $num <= 20; ++$num){
$_array1 = str_split($num);
//print_r($_array1);
//echo "<br/>";
$_array2 = array_reverse($_array1);
//print_r($_array2);
//echo "<br/>";
$i = 0;
$j = 0;
$different = false;
while ((!$different) && ($i < sizeof($_array1))){
if ($_array1[$i] == $_array2[$j]){
++$i;
++$j;
} else {
$different = true;
}
}
if (!$different){
echo "The number $num is a Palindrome Number";
}
}
?>
But you are inversing the array without a need to do so and you are looping for unnecessarily long. I propose this function to determine whether an array is a palindrome:
function isPalindrome($input) {
$size = count($input);
for ($index = 0; $index < $size / 2; $index++) {
if ($input[$index] != $input[$size - $index - 1]) {
return false;
}
}
return true;
}
Note, that:
the function assumes that the keys of the array are numbers
the function uses a single array
the size of the array is stored into a local variable to not calculate it repeatedly
the cycle cycles until half of the array, since going beyond that is unnecessary, due to the symmetrical nature of the != operator
the function returns false when the first difference is found, to further optimize the checking
if there were no differences, the function returns true, representing that the input is a palindrome
what I want to do is run this for loop and within there is a foreach searching the positions. what I want to do is once there it returns false I want it to break and save the position of $i in a variable. I'm using simple_html_dom.php but I don't think that matters since this is more of a basic programming problem.
for($i = $0; $i < $20; $i++){
foreach($html->find('div[class=cate_link]',$i) as $a){
if (strpos($a->plaintext,'+info') == false){
break;
}
}
//this is not valid, but essentialy this is what I want to do.
$stop = $i;
}
To break multiple levels in a loop you simply specify the levels, eg, break 2 - see the manual on break - http://www.php.net/manual/en/control-structures.break.php.
As such your code might work as
for($i = $0; $i < $20; $i++){
foreach($html->find('div[class=cate_link]',$i) as $a){
if (strpos($a->plaintext,'+info') == false){
$stop = $i; // Set variable
break 2; // break both loops
// or alternatively force the outer loop condition to expire
//$i = 21; // Force the outer loop to exit
//break;
}
}
}
I have expanded to question to set $i = 21 to break the outer loop with a single break.
Untested code but syntax checked...
<?php
// Untested code...
// Assume that you WILL break out of the loops...
$currentForIdx = -1; // so we can test that 'for' loop actually ran
$quitLoops = false;
for($i = 0; $i < $20 && !quitLoops; $i++) {
$currentForIdx = $i; // in case we break out of the loops
foreach($html->find('div[class=cate_link]',$i) as $a){
if (strpos($a->plaintext,'+info') == false) {
$quitLoops = true;
break;
}
}
}
// test $quitLoops and $currentForIdx to work out what happened...
?>
I havent tested this, but I would try something like this:
for($i = $0; $i < $20; $i++){
$stop = false;
foreach($html->find('div[class=cate_link]',$i) as $a){
if (strpos($a->plaintext,'+info') == false){
$stop = $i;
}
}
if ($stop !== false) {break;}
}