给定源路径和目标路径,输出相对于源的相对路径。
/和/或\作为路径分隔符(两者的选择和组合都可以)。os.path.relpath)。# In
/usr/share/geany/colorschemes
/usr/share/vim/vim73/ftplugin
# Out
../../vim/vim73/ftplugin# In
C:\Windows\System32\drivers
C:\Windows\System32\WindowsPowerShell\v1.0
# Out
..\WindowsPowerShell\v1.0发布于 2014-06-26 10:04:01
编辑添加的输出警报
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 separatorsR('C:\\Windows\\System32\\drivers','C:\\Windows\\System32\\WindowsPowerShell\\v1.0')../WindowsPowerShell/v1.0
R('/usr/share/geany/colorschemes','/usr/share/vim/vim73/ftplugin')././vim/vim 73/ftplugin
发布于 2014-06-26 05:15:56
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发布于 2014-06-26 07:26:03
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/ftpluginhttps://codegolf.stackexchange.com/questions/32568
复制相似问题