相対パスを印刷する


15

説明

ソースパスとデスティネーションパスを指定して、ソースに対するデスティネーションへの相対パスを出力します。

ルール

  1. 入力は、stdinから、またはプログラム/関数への引数として取得できます。

  2. WindowsスタイルのパスとUnixスタイルのパスの両方をサポートする必要があります。

  3. 出力パスは、パス区切り文字を使用/したり\、パス区切り文字に使用したりできます(両方を選択して組み合わせても構いません)。

  4. 相対パスが可能であると想定できます。

  5. 相対パスを計算するために作成された外部プログラム、組み込み関数、またはライブラリ関数の使用は禁止されています(例:Pythonのos.path.relpath

  6. これは

    編集:コメントからの新しいルール。

  7. 相対パスは、可能な限り最短の相対パスでなければなりません。

  8. 宛先パスがソースパスと異なると仮定します。

例1

# In
/usr/share/geany/colorschemes
/usr/share/vim/vim73/ftplugin

# Out
../../vim/vim73/ftplugin

例2

# In
C:\Windows\System32\drivers
C:\Windows\System32\WindowsPowerShell\v1.0

# Out
..\WindowsPowerShell\v1.0

ルール#3に関して-混合物は大丈夫ですか?例../../vim\vim73\ftplugin
ダンカンジョーンズ14年

1
最短の相対パスを返す必要がありますか、それともパスを生成しても大丈夫ですか?
ハワード14年

@ダンカンはい、ミックスは大丈夫です。
リナント14年

1
@Howard、それは最短の相対パスでなければなりません。
リナント14年

最初の例ではあり../vim/vim73/ftpluginませんか?
マルタイン

回答:


2

CJam、46バイト

ll]{'\/'/f/:~}/W{)__3$=4$@==}g@,1$-"../"*o>'/*

オンラインでお試しください。

$ echo '/usr/share/geany/colorschemes
> /usr/share/vim/vim73/ftplugin' | cjam path.cjam; echo
../../vim/vim73/ftplugin
$ echo 'C:\Windows\System32\drivers
> C:\Windows\System32\WindowsPowerShell\v1.0' | cjam path.cjam; echo
../WindowsPowerShell/v1.0

使い方

ll]         " Read two lines from STDIN and wrap them in an array.                       ";
{           " For each line:                                                             ";
  '\/       " Split by “\”.                                                              ";
  '/f/      " Split each chunk by “/”.                                                   ";
  :~        " Flatten the array of chunks.                                               ";
}/          "                                                                            ";
W           " Push -1 (accumulator).                                                     ";
{           "                                                                            ";
  )__       " Increment and duplicate twice.                                             ";
  3$=       " Extract the corresponding chunk from the first line.                       ";
  4$@=      " Extract the corresponding chunk from the second line.                      ";
  =         " If the are equal,                                                          ";
}g          " repeat the loop.                                                           ";
@,          " Rotate the array of chunks of the first line on top and get its length.    ";
1$-         " Subtract the value of the accumulator.                                     ";
"../"*o     " Print the string “../” repeated that many times.                           ";
>           " Remove all chunks with index less than the accumulator of the second line. ";
'/*         " Join the chunks with “/”.                                                  ";

1
バグがあります。試してみてください/aa/x/ab/y
jimmy23013 14

@ user23013:修正されました。
デニス14

2

Bash + coreutils、116

ボールを転がすためのシェルスクリプトを次に示します。短い答えがあることを確認してください:

n=`cmp <(echo $1) <(echo $2)|grep -Po "\d+(?=,)"`
printf -vs %`grep -o /<<<${1:n-1}|wc -l`s
echo ${s// /../}${2:n-1}

出力:

$ ./rel.sh /usr/share/geany/colorschemes /usr/share/vim/vim73/ftplugin
../vim/vim73/ftplugin
$ ./rel.sh /usr/share/geany/colorschemes/ /usr/share/vim/vim73/ftplugin/
../../vim/vim73/ftplugin/
$ ./rel.sh /usr/share/vim/vim73/ftplugin /usr/share/geany/colorschemes
../../geany/colorschemes
$ 

文字列ftpluginがファイルかディレクトリかをスクリプトが判断する方法がないことに注意してください。/上記の例のようにを追加することにより、明示的にディレクトリを提供できます。

空白またはその他の面白い文字を含むパスを処理しません。それが要件であるかどうかはわかりません。いくつかの追加の引用符が必要になります。


2

Javascript(E6)104

出力用に追加されたアラートを編集

R=(s,d)=>alert(s.split(x=/\/|\\/).map(a=>a==d[0]?d.shift()&&'':'../',d=d.split(x)).join('')+d.join('/'))

非ゴルフ

R (s,d) => // a single espression is returned, no {} or () needed
  s.split(x=/\/|\\/) // split string at / or \, save regexp in X for later
  .map( // create a new array from s
     a => a == d[0] // check if current of s and d equals
          ? d.shift() && '' // map to '' and cut 1 element of d
          : '../', // else map to '../'
     d=d.split(x)) // second param of map is useless, so split d here
  .join('')+d.join('/') // join map and concat to rest of d adding separators

テスト

R('C:\\Windows\\System32\\drivers','C:\\Windows\\System32\\WindowsPowerShell\\v1.0')

../WindowsPowerShell/v1.0

R('/usr/share/geany/colorschemes','/usr/share/vim/vim73/ftplugin')

../../vim/vim73/ftplugin


2

ルビー> = 1.9、89 94 キャラクター

$;=/\\|\//
a,b=$*.map &:split
puts"../"*(a.size-r=a.index{a[$.+=1]!=b[$.]}+1)+b[r..-1]*?/

コマンドライン引数を介した入力。フォルダー名が繰り返されるパスを含む、UNIXスタイルとWindowsスタイルの両方のパスで機​​能します。

$ ruby relpath.rb /usr/share/geany/colorschemes /usr/share/vim/vim73/ftplugin
../../vim/vim73/ftplugin
$ ruby relpath.rb 'C:\Windows\System32\drivers' 'C:\Windows\System32\WindowsPowerShell\v1.0'
../WindowsPowerShell/v1.0
$ ruby relpath.rb /foo/bar/foo/bar /foo/qux/foo/bar
../../../qux/foo/bar

2

J-63文字

左側の古いパスと右側の新しいパスを使用する関数。

}.@;@(c=.c&}.`(,~(<'/..')"0)@.(~:&{.))&('/'<;.1@,'\/'&charsub)~

このソリューションは、次のような3つの部分で構成されていpost@loop&pre~ます。爆発の説明:

post @ loop & pre ~   NB. the full golf
                  ~   NB. swap the arguments: new on left, old on right
            & pre     NB. apply pre to each argument
       loop           NB. run the recursive loop on both
post @                NB. apply post to the final result

'/'<;.1@,'\/'&charsub  NB. pre
         '\/'&charsub  NB. replace every \ char with /
'/'     ,              NB. prepend a / char
   <;.1@               NB. split string on the first char (/)

c=.c&}.`(,~(<'/..')"0)@.(~:&{.)  NB. loop
                      @.(~:&{.)  NB. if the top folders match:
    &}.                          NB.   chop off the top folders
   c                             NB.   recurse
       `                         NB. else:
           (<'/..')"0            NB.   change remaining old folders to /..
         ,~                      NB.   append to front of remaining new folders
c=.                              NB. call this loop c to recurse later

}.@;  NB. post
   ;  NB. turn the list of folders into a string
}.@   NB. chop off the / in the front

/分割する前に各パスに先頭を追加することに注意してください。これによりC:、「フォルダ」にすることでWindowsスタイルのパスを処理できます。これにより、Unixスタイルのパスの先頭に空のフォルダーが作成されますが、ループによって常に削除されます。

実際にご覧ください:

   NB. you can use it without a name if you want, we will for brevity
   relpath =. }.@;@(c=.c&}.`(,~(<'/..')"0)@.(~:&{.))&('/'<;.1@,'\/'&charsub)~
   '/usr/share/geany/colorschemes' relpath '/usr/share/vim/vim73/ftplugin'
../../vim/vim73/ftplugin
   'C:\Windows\System32\drivers' relpath 'C:\Windows\System32\WindowsPowerShell\v1.0'
../WindowsPowerShell/v1.0

tryj.tkで自分で試すこともできます。


2

バッシュ、69 66

誰かがそれをもっとうまくできるはずだと思ったので、私はこれを投稿しませんでした。しかし、どうやら簡単ではありません。

sed -r 'N;s/(.*[/\])(.*)\n\1/\2\n/'|sed '1s/[^/\]*/../g;N;s!\n!/!'

Nsed2行を一致させます。最初の式は、/orで終わる共通プレフィックスを削除し\ます。2番目の式は..、最初の行のディレクトリ名を置き換えます。最後に、2行を区切り文字で連結します。

3文字のHasturkunに感謝します。


おもしろそう!説明を追加できますか?
デジタル外傷14年

1
@DigitalTraumaが追加されました。ただし、基本的には単なる正規表現です。
jimmy23013 14年

ありがとう!次回ターミナルにいるときはこれで遊ぶつもりです
デジタルトラウマ14年

本当にsed2回実行する必要はありません。1 つのスクリプトで実行できます。
ハスタークン14

@Hasturkunしかし、私はそれを動作させる方法を見つけられませんでしたN。方法を知っていれば、この答えを編集できるかもしれません。
jimmy23013

1

C、119 106

void p(char*s,char* d){while(*s==*d){s++;d++;}s--;while(*s){if(*s==47||*s==92)printf("../");s++;}puts(d);}

p(char*s,char*d){for(;*s;)*s++-*d?*s-47||printf("../"):d++;puts(d);}68文字/ OバックスラッシュW
BEBE

ありがとう!ただし、ルール2では、両方をサポートする必要があります。どちらかを選択できるのは出力です(ルール3)。
kwokkie 14年

1

Python 3、120

a,b=(i.split('\\/'['/'in i])for i in map(input,'  '))
while[]<a[:1]==b[:1]:del a[0],b[0]
print('../'*len(a)+'/'.join(b))

例:

$ python3 path.py
 /usr/share/geany/colorschemes
/usr/share/vim/vim73/ftplugin 
../../vim/vim73/ftplugin

行1を使用execして文字列操作を行うより短い方法がありますか?
xnor 14年

@xnorたぶん、しかし、私はそれを見ることができません。
GRC

かもしれないmap(input,' ')`(入力()、入力())のために働きますか?(私は自分でテストすることはできません)
xnor

@xnorええ、ありがとうございます!
grc 14年

1

ルビー-89

r=/\/|\\/
s = ARGV[0].split r
d = ARGV[1].split r
puts ("../"*(s-d).size)+((d-s).join"/")

使用法:

ruby relative.rb working/directory destination/directory

3
/foo/bar/foo/barやなどの引数では失敗します/foo/qux/foo/bar
ヴェンテロ14年

およびWindowsスタイルのパスに障害が発生した
edc65

@ edc65ルールは、両方のパス形式をサポートする必要があるとは言っていませんが、どちらか一方を実行できます。
nderscore 14年

@nderscoreルール2 WindowsとUnixスタイルの両方のパスをサポートする必要があります。
edc65 14年

1
@Jwosty:まあ、それが美しさですね。短く正しいソリューションを考え出す。過去に、見落とされたエッジケースのために、答えを完全に修正しなければならなかったケースがありました。さて、このケースでは、すべてのタスクにしっかりしたテストケースセットが付随する必要があると考えているため、タスクにも一部責任を負わせています。
ジョーイ14年

0

JavaScript-155

function p(s,d){s=s.split(/\\|\//g);d=d.split(/\\|\//g);o=[];for(i=0;s[i]==d[i];i++);for(j=s.length-i;j--;)o[j]="..";return o.concat(d.slice(i)).join("/")}

パス形式を解析しますが、/セパレータ付きで出力します。

console.log(p("/usr/share/geany/colorschemes","/usr/share/vim/vim73/ftplugin"));
../../vim/vim73/ftplugin
console.log(p("/usr/share/geany/colorschemes/test/this","/usr/share/vim/vim73/ftplugin/this/is/a/test"));
../../../../vim/vim73/ftplugin/this/is/a/test
console.log(p("c:\\windows\\system32\\drivers\\etc\\host","c:\\windows\\system\\drivers\\etc\host"));
../../../../system/drivers/etchost

0

PHP、158 151

function r($a,$b){$a=explode('/',$a);$b=explode('/',$b);foreach($a as $k=>$v){if($v==$b[$k])$b[$k]='..';else break;}unset($b[0]);echo implode('/',$b);}

ゴルフをしていない:

function r($a,$b){
    $a=explode('/',$a);
    $b=explode('/',$b);
    foreach($a as $k=>$v){
        if($v==$b[$k])$b[$k]='..';
        else break; 
    }
    unset($b[0]);
    echo implode('/',$b);
}
// these lines are not included in count:
r('/test/test2/abc','/test/test3/abcd'); // ../test3/abcd
r('/test/test2/abc','/test/test2/abcd'); // ../../abcd

あなたの答えは正しくありません。このdirsを作成してみてくださいcd:)
core1024 14

0

Groovy-144文字

1つの解決策:

x=args[0][1]!=':'?'/':'\\'
f={args[it].tokenize x}
s=f 0
d=f 1
n=0
while(s[n]==d[n++]);
u="..$x"*(s.size-(--n))
println "$u${d.drop(n).join x}"

出力例:

bash$ groovy P.groovy C:\\Windows\\System32\\drivers C:\\Windows\\System32\\WindowsPowerShell\\v1.0
..\WindowsPowerShell\v1.0

bash$ groovy P.groovy /usr/share/geany/colorschemes /usr/share/vim/vim73/ftplugin
../../vim/vim73/ftplugin

bash$ groovy P.groovy /foo/bar/foo/bar /foo/qux/foo/bar
../../../qux/foo/bar

なし:

// fs = file seperator, / or \
fs = args[0][1]!=':'?'/':'\\'

s = args[0].tokenize fs
d = args[1].tokenize fs

// n = # of matching dirs from root + 1
n = 0
while (s[n] == d[n++]) ;

// up = the up-prefix. e.g. "../../..", for instance 
n--
up = "..${fs}" * (s.size-n)

println "$up${d.drop(n).join fs}"
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.