i need to make code sequences
Eg. Start with A5B, next code will be A5C,A5D...
Until A59 then next A6A,A6B...A99 then next BAA,BAB,BAC
the sequences is A-Z then continuous with 2-9
$x = $last; // Get Last Value From DB
$a = substr($x,0,1); // Get First String
$b = substr($x,1,1); // Get Middle String
$c = substr($x, -1); // Get Last String
if($c == 'Z'){
$x = $a.$b.'0';
}
elseif($c == '9'){
if ($b == 'Z'){
$x = $a.'0'.'A';
}
elseif($b == '9'){
$a++;
$x = $a.'A'.'A';
}
else{
$b++;
$x = $a.$b.'A';
}
}
else{
$x++;
}
its works but the problem is how to make the code sequences without using 0,1,o & i
please help
sorry for my english
If you're not worried about performance, an easy fix is to just use a do-while loop to keep iterating until $x no longer contains any of the unwanted characters:
$x = $last; // Get Last Value From DB
do {
$a = substr($x, 0, 1); // Get First String
$b = substr($x, 1, 1); // Get Middle String
$c = substr($x, -1); // Get Last String
if ($c == 'Z') {
$x = $a.$b.'0';
} elseif ($c == '9') {
if ($b == 'Z') {
$x = $a.'0'.'A';
} elseif ($b == '9') {
$a++;
$x = $a.'A'.'A';
} else {
$b++;
$x = $a.$b.'A';
}
} else {
$x++;
}
} while (preg_match('/[01OI]/', $x));
Related
Using while loop to print 10-20 odd numbers on the screen, with <-> (exapmle 11-13-15-17-19, <-> not after 19).How to take the last number so as not to put a -. Thank you.
<?php
$x = 10;
while($x < 20){
$x++;
if($x % 2){
echo $x. "-";
}
}
You can push the odd values to an array and after the loop you can convert the array to a string with the implode (https://www.php.net/manual/en/function.implode.php) function.
$x = 10;
$arr = [];
while($x<20){
$x++;
if ($x%2){
$arr[] = $x;
}
}
echo implode(",", $arr);
// output will be a comma seperated string
without (helper)array
You can use rtrim() (https://www.php.net/manual/de/function.rtrim.php) funktion.
$str = "1,2,3,";
echo rtrim($str,",");
As mentioned in the comments, have a boolean variable say firstTime. If this is true, don't prepend a hyphen, else if it is false, prepend it with a hyphen.
<?php
$x = 10;
$firstTime = true;
while($x < 20){
$x++;
if($x % 2){
echo ($firstTime ? "" : "-") . $x;
$firstTime = false;
}
}
Turn the limit number into a variable and use a ternary operator to print out a dash only if $x + 1 < $limit
https://paiza.io/projects/EDj6-u-FAcYxYoR7ON_cvg
<?php
$x = 10;
$y = 20;
while($x < $y){
$x++;
if($x % 2){
echo ($x + 1 === $y) ? $x : $x. "-";
}
}
?>
Simple approach
Imagine you are printing a list of numbers from 10 to 20. If it's an even number, print the "-" symbol and if it's odd number, print the number.
<?php
$start = 10;
$end = 20;
while($start<$end)
{
$n=$start;
if($n>10)
echo ($n%2==0)?"-":$n; //the gist is here
$start++;
}
This is the code I have. It currently works as is, However I'm experimenting with loops and want to see it can be done with a while loop and how it would be done. With this code I can take 2 input numbers and display them, then point out all odds, add all evens, and add all the squares of the odds.
define ("B","<br/>");
$firstNum = $_POST["firstNum"];
$secondNum = $_POST["secondNum"];
if ($firstNum < $secondNum)
{
$firstNum = true;
}
elseif ($firstNum >= $secondNum)
{
$firstNum = "You didn't listen, dumb dumb!".'<br/>GO BACK';
}
echo "First Number: ".$firstNum."<br/>"."Second Number: ".$secondNum;
echo B;
echo B;
$numbers = array();
$numbers = range($firstNum, $secondNum);
$length = count($numbers);
$odds = array();
$sumSqOdds = 0;
$sumEven = 0;
$j = 0;
for ($x = 0; $x < $length; $x++)
{
if (($numbers[$x] % 2) == 1)
{
$odds[$j] = $numbers[$x];
$sumSqOdds = $sumSqOdds + pow ($numbers[$x], 2);
$j++;
}
else
{
$sumEven = $sumEven + $numbers[$x];
}
}
$x = 0;
$y = 0;
printf("The odd numbers between your integers are: ");
for ($x = 0; $x < $j; $x++)
{
echo $odds[$x];
echo ' ';
$y++;
if (($y % 10) == 0)
{
echo B;
}
}
echo B;
echo B;
printf("The sum of all even numbers between your integers is: ".$sumEven);
echo B;
echo B;
printf("The sum of the square of the odd numbers between your integers is: ".$sumSqOdds);
Here is my while loop but it seems to be infinite...
$numW = array ();
$numW = range ($firstNum, $secondNum);
$lengthW = count ($numW);
$oddsW = array ();
$sumSqOddsW = 0;
$sumEvenW = 0;
$j = 0;
$x = 0;
while ($x < $lengthW)
{
if (($numW[$x] % 2) == 1)
{
$oddsW[$j] = $numW[$x];
$sumSqOddsW = $sumSqOddsW + pow ($numW[$x], 2);
$x++;
$j++;
}
else
{
$sumEvenW = $sumEvenW + $numW[$x];
}
}
$x = 0;
$y = 0;
printf ("The odd numbers between your integers are: ");
while ($x < $j)
{
$x++;
echo $oddsW[$x];
echo "nbsp;";
$y++;
if (($y % 10) == 0)
{
echo B;
}
}
Equivalent loops:
for ($i = 0; $i < 10; $i++) {
echo $i;
}
$i = 0;
while ($i < 10) {
echo $i;
$i++;
}
For a loop to ever finish it has to change one of the two evaluating variables. So either $x, or $lengthW would have to change during iteration. You made an if statment, in the first case you define that X increases by 1, but in the else case you do not change any variable that then has an effect on either $x, or $lengthW
Nor is there any check that sees if the else state has been reached and to catch that by either changing $x or $lengthW in a later iteration.
As such there's an infinite loop as soon as you reach the else case.
The if statement uses the same $x value as the last iteration checking the same position of the $numW, as such nothing has changed since the last iteration and you'll hit the else again, and again, and so on.
while ($x < $lengthW)
{
if (($numW[$x] % 2) == 1)
{
$oddsW[$j] = $numW[$x];
$sumSqOddsW = $sumSqOddsW + pow ($numW[$x], 2);
$x++; //$x is increased by one, and as such, the loop will progress.
// remove this $x++ if you place it outside the if else statement.
$j++;
}
else // reached when ($numW[$x] %2) != 1
{
$sumEvenW = $sumEvenW + $numW[$x];
// No changes to $x or $lengthW as such you'll hit the else again
// this could be solved by either adding $x++; here.
}
// or by adding $x++; here
// (if you do add it here, remove it in the if case above,
// or you risk increasing it by 2 every iteration
}
it says an error undefined offset i dont know what causes it
im trying to shuffle $numberarray without repeating the number
heres the code
$numberarray = array(1,2,3,4,5,6,7,8,9,10);
for($counter=0;$counter<=9;$counter++)
{
$b = $counter - 1;
$a = $numberarray[$counter];
$numberarray[$counter] = rand(1,10);
do
{
$numberarray[$counter] = rand(1,10);
while($b != 0)
{
if($numberarray[$counter] == $numberarray[$b])
{
$numberarray[$counter] = rand(1,10);
$b = $counter - 1;
//echo $b;
}
else
{
$b--;
}
}
}while($a == $numberarray[$counter]);
echo $numberarray[$counter].", ";
}
sample output $numberarray = {3,4,5,1,2,7,9,10,8,5}
This would be better:
(By the way you don't need an extra condition before the while since while act like a condition itself)
$numberarray = array(1,2,3,4,5,6,7,8,9,10);
for($counter=0;$counter<=9;$counter++)
{
$b = $counter - 1;
$a = $numberarray[$counter];
$numberarray[$counter] = rand(1,10);
do
{
$numberarray[$counter] = rand(1,10);
while($b >0)
{
if($numberarray[$counter] == $numberarray[$b])
{
$numberarray[$counter] = rand(1,10);
$b = $counter - 1;
//echo $b;
}
else
{
$b--;
}
}
}while($a == $numberarray[$counter]);
echo $numberarray[$counter].", ";
}
Even if there is no more error, your code repeat numbers sometimes, so I would symply do it like this using shuffle:
$numberarray2 = array(1,2,3,4,5,6,7,8,9,10);
shuffle($numberarray2);
print_r($numberarray2);
The problem is that in the first $b is equal to -1 that's why you get the error so i think you should delete the if condition and edit the while statement to while($b!=-1).
for ($i = 1; $i <= $numITER; $i++) {
$val = rand(2,36);
switch (TRUE) {
case ($val<=10):
$vercodeTEMP.=$val;
break;
case ($val>10):
if ($val != 25 && $val != 19) {
$vercodeTEMP.=chr(54+$val);
} else {
$i=$i-1;
}
break;
}
}
I'm basically trying to avoid 0, 1, and the letters O and I. How can this possibly be giving me zero's when the rand range is 2 to 36?
If $val == 10, then you will append $val onto $vercodeTEMP.
Try:
for ($i = 1; $i <= $numITER; $i++) {
$val = rand(2,36);
if ($val<10) {
$vercodeTEMP.=$val;
} else if ($val != 25 && $val != 19) {
$vercodeTEMP.=chr(54+$val);
} else {
$i=$i-1;
}
}
I'm basically trying to avoid 0, 1, and the letters O and I
What about not messing with magic numbers (besides position) and using PHP's range()?
$numbers = range(2, 9);
$letters = range('a', 'z');
unset($letters[8], $letters[14]);
$letters = array_merge($letters, array_map('strtoupper', $letters));
$pool = array_merge($numbers, $letters);
shuffle($pool);
$captcha = join(array_slice($pool, 0, $numITER)); // e.g. 2ESQcnMTNy
CodePad.
The following code is displaying INF as the result. How can I fix it?
<?php
function fibonacci($n)
{
$a = 1;
$b = 1;
$result = 0;
for ($i = 0; $i < $n; $i=$i+1)
{
$sum = $a + $b;
$a = $b;
$b = $sum;
if ($a % 2 == 0)
{
$result = $result + $a;
}
}
echo "<br/>" . $result;
}
echo fibonacci(400000);
?>
The number is too big to display, and INF is a pretty good guess :) (fibonacci(1000) gives you a number with 210 digits).
100: 22 digits, 110: 24 digits, 120: 25 digits, 130: 27 digits
If you extrapolate that, you would end up with about (400000 / 10) * 2 = 80000 digits.
The following implements your logic using bcmath to prevent the INF error.
function fibonacci($n)
{
$a = '1'; $b = '1'; $result = '0';
for ($i = 0; $i < $n; $i++) {
$sum = bcadd($a,$b);
$a = $b;
$b = $sum;
if (bcmod($a,'2') == '0') {
$result = bcadd($result,$a);
}
}
echo "<br />".$result;
}
As your fibonacci function doesn't actually return any value, there's no point in echo fibonacci(400000)
EDIT
However, your logic is completely flawed. The following should give you the correct result for the problem you're trying to solve (again using bcmath):
function fibonacci($n)
{
$a = '0'; $b = '1'; $sum = '0';
$sum = '0';
do {
$fib = bcadd($a,$b);
$a = $b;
$b = $fib;
if (bccomp($fib,$n) == -1) {
if (bcmod($fib,'2') == '0') {
$sum = bcadd($sum,$fib);
}
}
++$i;
} while (bccomp($fib,$n) == -1);
return $sum;
}
echo fibonacci(4000000);
Rather than simply executing it to get the result, look to see how it works and what it's actually doing