gpt4 book ai didi

how to substract and add id's with decimal and alphanumeric(如何用十进制和字母数字减去和添加id)

转载 作者:bug小助手 更新时间:2023-10-22 17:34:47 26 4
gpt4 key购买 nike



Im stuck on this for quite some time. I have to work with identifiers that are always 4 characters long and consist of 0-9a-z. An example would be iulz. So the range would be something like:

我在这个问题上坚持了很长一段时间。我必须使用总是4个字符长并且由0-9a-z组成的标识符。iulz就是一个例子。所以范围大概是:


0000 0001 0002 ... 0009 000a 000b ... 000z 0010 ... zzzz

0000 0001 0002。。。0009 000a 000b。。。000z 0010。。。zzzz


So in the case of my example, iulz I know that the ID before that would be iuly and after that ium0. But i cannot seem to work out how to calculate this. The functions hexdec and dechex would help me if they would include the whole alphabet.

所以在我的例子中,iulz,我知道在那之前的ID是iuly,在那之后是iu0。但我似乎不知道该怎么计算。如果函数hexdec和dechex包括整个字母表,它们会对我有所帮助。


My first try was to make a logical list with some for loops but this takes an impossible amount of memory. I am looking for a way to make the following two functions:

我的第一次尝试是用一些for循环制作一个逻辑列表,但这需要大量的内存。我正在寻找一种实现以下两个功能的方法:


function getNext(string $id) : string { ... }
function getPrev(string $id) : string { ... }
$this->getNext('iulz') // returns 'ium0'
$this->getPrev('iulz') // returns 'iuly'

Can anyone get me on the right track here? Thnxz!

有人能让我走上正轨吗?Thnxz!


更多回答

Can you tell me how did you arrive at 000a from 0009?

你能告诉我你是如何从0009到达000a的吗?

@nice_dev I believe he created his own count base, where a is the next figure after 9, and so on with the rest of the english alphabet

@nice_dev我相信他创建了自己的计数基数,其中a是9之后的下一个数字,以此类推

@nice_dev we are counting from 0 to 9 and then a to z.

@nice_dev我们从0计数到9,然后从a计数到z。

@DJQ Wasn't really helpful though.

@DJQ并没有真正的帮助。

优秀答案推荐

Your encoding scheme contains 36 symbols (digits from 0 to 9 and letters from a to z), so a possible approach is to implement a math with Base36. PHP supports base_convert() function to convert a number between arbitrary bases.

您的编码方案包含36个符号(数字从0到9,字母从a到z),因此一种可能的方法是使用Base36实现数学运算。PHP支持base_convert()函数在任意基数之间转换数字。


<?php

function getNext($id) {
$n = base_convert($id, 36, 10);
$n = $n + 1;
$n = base_convert($n, 10, 36);
$n = str_pad($n, 4, '0', STR_PAD_LEFT);
return $n;
}

function getPrev($id) {
$n = base_convert($id, 36, 10);
$n = $n - 1;
$n = base_convert($n, 10, 36);
$n = str_pad($n, 4, '0', STR_PAD_LEFT);
return $n;
}

$id = 'iulz';
echo "ID: " . $id . ", next ID: " . getNext($id) . ", previous ID: " . getPrev($id) . ".";

?>

Result:

结果:


ID: iulz, next ID: ium0, previous ID: iuly.

As an additional note, you need to define the expected behaviour of the functions, if the $id is outside the [0000 .. zzzz] range.

另外需要注意的是,如果$id在[000.zzzz]范围之外,则需要定义函数的预期行为。



function previousId(string $alphabet, string $id)
{
$length = strlen($id);
$first = $alphabet[0];
$last = $alphabet[strlen($alphabet) - 1];
if ($id === str_repeat($first, $length)) {
return '––––';
}
for ($len = $length - 1; $len >= 0; $len--) {
if (str_ends_with($id, str_repeat($first, $len))) {
$pos = $length - $len - 1;
$char = $alphabet[strpos($alphabet, $id[$pos]) - 1];
return substr($id, 0, $pos) . $char . str_repeat($last, $len);
}
}
}

function nextId(string $alphabet, string $id)
{
$length = strlen($id);
$first = $alphabet[0];
$last = $alphabet[strlen($alphabet) - 1];
if ($id === str_repeat($last, $length)) {
return '––––';
}
for ($len = $length - 1; $len >= 0; $len--) {
if (str_ends_with($id, str_repeat($last, $len))) {
$pos = $length - $len - 1;
$char = $alphabet[strpos($alphabet, $id[$pos]) + 1];
return substr($id, 0, $pos) . $char . str_repeat($first, $len);
}
}
}

These two functions take an arbitrary alphabet (of unique characters). The id can be of any length smaller than the length of the alphabet string. Both error condition could and should be added to these function.

这两个函数采用(唯一字符的)任意字母表。id可以是比字母字符串的长度小的任何长度。这两个错误条件都可以而且应该添加到这些函数中。


If there is no preceding value for previousId the function currently returns the message '----' for illustration purposes. Possible real values: false, null, wrap around to the last value ('zzzz'), or maybe throw a RuntimeException or test the input value before calling previousId. Same applies for a call of nextId with an id of 'zzzz', which could return '0000', or false, or null, etc.

如果previousId没有前面的值,则函数当前会返回消息“----”以供说明。可能的实际值:false、null、换行到最后一个值(“zzzz”),或者可能引发RuntimeException或在调用previousId之前测试输入值。同样适用于id为“zzzz”的nextId调用,该调用可能返回“0000”、false或null等。


$alphabet = '0123456789abcdefghijklmnopqrstuvwxyz';
$id = 'iulz';

echo previousId($alphabet, $id) . " < $id > " . nextId($alphabet, $id) . "\n";

// Outputs: iuly < iulz > ium0

更多回答

Don't forget to pad the result (e.g. with str_pad($value, 4, '0', STR_PAD_LEFT)).

不要忘记填充结果(例如,使用str_pad($value,4,'0',str_pad_LEFT))。

@Olivier, thanks, very important note.

@奥利维尔,谢谢,非常重要的提示。

With 'zzzz' this will return '10000', which might not be what the OP wants. Maybe he wants an exception thrown, or restart with '0000'. Nonetheless an elegant solution.

有了“zzzz”,这将返回“10000”,这可能不是OP想要的。也许他想要抛出一个异常,或者用“0000”重新启动。尽管如此,这是一个优雅的解决方案。

@lukas.j, thanks, I added a note in the answer.

@lukas.j,谢谢,我在回答中加了一个注释。

@Zhorov That is of course maybe even better. But of course the OP should state what the functions should return in these two edge cases.

@Zhorov那当然可能更好。当然,OP应该说明在这两种边缘情况下函数应该返回什么。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com