Solved

Count Sequence

Posted on 2008-10-24
18
358 Views
Last Modified: 2013-12-13
I have a string of characters e.g. "abds236789plmn".

No character is ever repeated twice in the string.

What I am after doing is creating a function which converts a number to a value from the string.

If i call function sequence(1) it will return a.

1 = a
14 = n
16 = bn
28 = nn

I tried using str_length and treating the string as array and adding 1 to the index.

Thanks for reading.
0
Comment
Question by:ncoo
  • 7
  • 5
  • 4
  • +1
18 Comments
 
LVL 73

Expert Comment

by:sdstuber
ID: 22795979
given n, and a string of length l

looks like you are trying to do this...

  the character at the mod(n,l) position (position 0 is no character) followed by
  floor(n/l) copies of the last letter

with your examples above ...

mod(1,14) = 1 =>"a",     floor(n/l) = 0  => "",        final answer "a"
mod(14,14) = 0 => "",    floor(n/l) = 1   => "n",     final answer "n"
mod(16,14) = 2 => "b",  floor(16/14) = 1 => "n",  final answer "bn"
mod(28,14) = 0 => "",    floor(28,14) = 2  => "nn", final answer "nn"
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 22796122
Try this function:
function sequence($x,$str="abds236789plmn") {

  $res = '';

  $len = strlen($str);

  while($x > 0) {

    $v = ($x % $len);

    if(!$v)$v=$len;

    $res .= $str[$v-1];

    $x -= $v;

  }

  return $res;

}

Open in new window

0
 
LVL 15

Author Comment

by:ncoo
ID: 22796245
I think I may have wording of the question could have been a little better.

The example string length is: 14

Which means for my new string of length 2 there should be 196 combinations of the string (14^2)

I think I may need to have a zero value for this to work.

Like Hex but with a variable set of charaters. So instead of 0-9 and a-f that example string.
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 22796516
Hex has as you probably know 16 digits, but you can easily do math with any number of digits, including 14. If this is what you are doing, then yes, you will need a zero. By tradition numbers are "growing" from right to left, you are going left to right, but that is not a problem for the math.

Maybe it is easier to work with a "normal" sequence 0-9abcd, and then at the end map the result to your random string of 14 characters, and reverse it?
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 22796604
does the algorithm I posted above not do it?

Sorry I don't know php syntax.  but given the description, hopefully somebody can do the actual coding.  I imagine it would be 1 or 2 lines of code tops.
0
 
LVL 15

Author Comment

by:ncoo
ID: 22796933
If I'm reading it correctly sbstuber I'm certain you end up with the same thing as the PHP code cxr originally posted results below, which is not what I am after.

Yes for convention stick with normall numbering order.

If you want to try writing pseudocode I can try to convert to PHP sdstuber.
0 = , 1 = a, 2 = b, 3 = d, 4 = s, 5 = 2, 6 = 3, 7 = 6, 8 = 7, 9 = 8, 10 = 9, 11 = p, 12 = l, 13 = m, 14 = n, 15 = an, 16 = bn, 17 = dn, 18 = sn, 19 = 2n, 20 = 3n, 21 = 6n, 22 = 7n, 23 = 8n, 24 = 9n, 25 = pn, 26 = ln, 27 = mn, 28 = nn, 29 = ann, 30 = bnn, 31 = dnn, 32 = snn, 33 = 2nn, 34 = 3nn, 35 = 6nn, 36 = 7nn, 37 = 8nn, 38 = 9nn, 39 = pnn, 40 = lnn, 41 = mnn, 42 = nnn, 43 = annn, 44 = bnnn, 45 = dnnn, 46 = snnn, 47 = 2nnn, 48 = 3nnn, 49 = 6nnn,  etc...

Open in new window

0
 
LVL 15

Author Comment

by:ncoo
ID: 22796961
New string so it's easier to read:

0123456789abcdefgh

Which should give the output:

0 = 0, 1 = 1, 2 = 2, 3 = 3, 4 = 4, 5 = 5, 6 = 6, 7 = 7, 8 = 8, 9 = 9, 10 = a, 11 = b, 12 = c, 13 = d, 14 = e, 15 = f, 16 = g, 17 = h, 18 = 10, 19 = 11, 20 = 12, 21 = 13, 22 = 14, 23 = 15, 24 = 16, 25 = 17, 26 = 18, 27 = 19, 28 = 1a, 29 = 1b, 30 = 1c, 31 = 1d, 32 = 1e, 33 = 1f, 34 = 1g, 35 = 1h, 36 = 20, 37 = 21, 38 = 22, 39 = 23 etc...
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 22797378
Then why is 16=bn in your original question? Wouldn't it be 15=aa and 16=ab?

This last example seems to be just "normal" base-18 numbers? Same as hex, but 18 digits?
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 22797409
ncoo, could you please explain your question a bit more, I don't think I understand what you want.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 15

Author Comment

by:ncoo
ID: 22797545
I think it would be the following,
1 = b
14 = ba
16 = bd

Lets work with the easier string though first.

Yes base 18 for the easier string and base 14 for the first string.

What I am after is 'base of string length' for a string.

Still not sure how much sense that makes.

0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22797553
Based on your sample in 22796961:
<pre>

<?php

function numconv($num,$str){

  $len=strlen($str);

	if($num<$len){

	  return $str[$num];

	} else {

	  $a=intval($num/$len);

		$b=$num-($len*$a);

		return $a.$str[$b];

	}

}

$string="0123456789abcdefgh";

echo '1 "1":'.numconv(1,$string).'<hr>';

echo '13 "d":'.numconv(13,$string).'<hr>';

echo '19 "11":'.numconv(19,$string).'<hr>';

echo '37 "21":'.numconv(37,$string).'<hr>';

?>

Open in new window

0
 
LVL 15

Author Comment

by:ncoo
ID: 22797998
Thanks that is almost it ddrudik.

Sample output below goes wrong at 180.
173 = 9b

174 = 9c

175 = 9d

176 = 9e

177 = 9f

178 = 9g

179 = 9h

180 = 100 should be a0

181 = 101 '' a1

182 = 102 '' a2

183 = 103 '' a3

184 = 104 '' a4

Open in new window

0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
ID: 22797999
I believe this is what you mean:
function BaseX($n,$str="abds236789plmn") {

  $res = '';

  $base = strlen($str);

  while($n > 0) {

    $res = $str[$n % $base].$res;

    $n = floor($n/$base);

  }

  return $res;

}

Open in new window

0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22798047
If a in a0 is hex of 10 then:
<pre>

<?php

function numconv($num,$str){

  $len=strlen($str);

	if($num<$len){

	  return $str[$num];

	} else {

	  $a=intval($num/$len);

		$b=$num-($len*$a);

		return dechex($a).$str[$b];

	}

}

$string="0123456789abcdefgh";

echo '1 "1":'.numconv(1,$string).'<hr>';

echo '13 "d":'.numconv(13,$string).'<hr>';

echo '19 "11":'.numconv(19,$string).'<hr>';

echo '179 "9h":'.numconv(179,$string).'<hr>';

echo '180 "a0":'.numconv(180,$string).'<hr>';

?>

Open in new window

0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22798061
cxr's last post worked for me as well.
0
 
LVL 15

Author Closing Comment

by:ncoo
ID: 31509639
Thank you
0
 
LVL 15

Author Comment

by:ncoo
ID: 22798135
Thanks cxr.

Just tried your new code ddrudik almost it now gets to 288.
286 = fg = fg

287 = fh = fh

288 = 100 = g0

289 = 101 = g1

290 = 102 = g2

291 = 103 = g3

292 = 104 = g4

Open in new window

0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22798382
ncoo, seems it was beyond hex then, thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The greatest common divisor (gcd) of two positive integers is their largest common divisor. Let's consider two numbers 12 and 20. The divisors of 12 are 1, 2, 3, 4, 6, 12 The divisors of 20 are 1, 2, 4, 5, 10 20 The highest number among the c…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now