Jump to content

Issue with my programming logic?


Aeternitas

Recommended Posts

This is probably less of a language-specific question and more one related to logic, but since it's written in PHP and it's been vexing me for a few hours now, I figured I'd take a shot asking about here!

 

Simple problem I'm trying to figure out for a class; I have a simple string of characters in the form "111-AAA" (three digits, hyphen, three letters) and I'm supposed to increment the string in a particular order.  I increment the digits first, and after they've passed 999, I reset them to 000, increment the right-most column on the letters' side, and start again.  I assumed this would be pretty simple, but the code I've written executes with some fairly strange results.  With the file posted below, eight iterations into the outermost for() loop ought to echo "119-AAA", but I get "129-AAA" instead.  And that's just the beginning - I started off with a thousand iterations so I could see just how drastically this little issue snowballs down the line.  I see what's happening: before the value in the column being incremented is reset to 0 or A, the next column to be changed jumps the gun and gets incremented early. But no matter where I stick the increment expression, the same thing happens.  I'm sure the problem is right here under my very nose, but I'm completely missing it...could anyone possibly show me what I'm not seeing?

 

<html>

<title>Test</title>

<body>

<?php

$oldString = "111-AAA";

echo "Below this line is the string to be modified:<p>";

echo $oldString;

echo "<p>And below this line is the modified string:<p>";

$newString = array($oldString[2], $oldString[1], 
$oldString[0], $oldString[6], $oldString[5], 
$oldString[4]);

for($i=0; $i<1000; $i++) {

for ($count = 0; $count < 6; $count++) {

  $newString[$count]++;

  if (($newString[$count] > 9) && ($count < 3)) {
    $newString[$count] = 0;
    break;
  }

  if (($newString[$count] == 'AA') && ($count > 2)) {
    $newString[$count] = 'A';
    break;
  }

  if (($newString[$count] != 'AA') && ($newString[$count] != 9)) {
    break;
  }

}

echo $newString[2] . $newString[1] . $newString[0] . '-' .
$newString[5] . $newString[4] . $newString[3] . "<br>";

}

?>

</body>

</html>

 

 

Link to comment
Share on other sites

Treat the two parts like two numbers: the first three characters form a base 10 (0-9) number and the last three form a base 26 (A-Z) number. If you can convert between the decimal forms and the base X forms then you're done.

 

As in,

1. Convert 111 to a number (spoiler: it already is)
2. Add 1 and modulo 1000.
3. If you end up with 0 (ie, it wrapped around) then
a) Convert AAA to a number 
  i-  A=0, B=1, C=2... Y=24, Z=25
  ii- "AAA" = 26^2 * "A" + 26^1 * "A" + 26^0 * "A" = 0
b) Add 1 and (presumably) modulo BAAA (=17576)
4. Add the two parts together again, adding 0-padding to the first and A-padding to the second

Link to comment
Share on other sites

Absolutely, and that's another example of complete tunnel vision on my part, since my code already increments characters...and it is my understanding that the difference between a string and a character in php is a semantic one. I should have been able to see that, but no: under stress, I get mired in singular details. :-P

 

Thanks for the help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.