Add tags

Add tags to the following comment
 パラメータとして渡すリストは文字列型としてください(0で始まるリスト対応)。
 数字のみでなく英字も受け付ける関数仕様になっています。
 また、あみだくじの特性上、重複した文字が含まれるリストを受け付けません。

 リストをソート(変則的なバブルソート)する過程を記録し、過程を逆転させたものをあみだくじのパターンとするロジックです。

ex)
echo '<pre>'.CreateAmida('9876543210').'</pre>';
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
function CreateAmida($org)
{
    if (!ctype_alnum($org) ||
        count(array_unique(str_split($org))) != count(str_split($org))) {
        return NULL;
    }
    $bridge_num = strlen($org)-1;
    $p = $org;
    $step = array();
    while (1) {
        $chg = FALSE;
        $step_buf = array_fill(0, $bridge_num, ' ');
        for ($i = 0; $i < $bridge_num; $i++) {
            if ($p[$i] > $p[$i+1]) {
                $sub = array($p[$i]=>$p[$i+1], $p[$i+1]=>$p[$i]);
                $p = strtr($p, $sub);
                $step_buf[$i] = '-';
                $i++;
                $chg = TRUE;
            }
        }
        if ($chg) {
            $step[] = '|'.implode('|', $step_buf).'|';
        } else {
            break;
        }
    }
    $res = implode(' ', str_split($p))."\n";
    $res .= implode("\n", array_reverse($step))."\n";
    $res .= implode(' ', str_split($org))."\n";
    return $res;
}
?>

Add tags

The input will be splited to tags with space.

Index

Feed

Other

Link

Pathtraq

loading...