Last active
September 23, 2019 02:06
-
-
Save TheRealJAG/0acc8b81be8012f14a6b58808a0de26d to your computer and use it in GitHub Desktop.
Write a function that provides change directory (cd) function for an abstract file system.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Path { | |
public $currentPath; | |
function __construct($path) { | |
$this->currentPath = $path; | |
} | |
public function cd($newPath) { | |
$innerCounter = 0; | |
$strOut= ''; | |
$newPath = explode('/',$newPath); | |
$oldPath = explode('/', $this->currentPath); | |
foreach($newPath as $str) { | |
echo $str.'<P>'; | |
if($str == '..') $innerCounter++; | |
} | |
$oldLength = count($oldPath); | |
for($i=0;$i<($oldLength - $innerCounter);$i++) | |
$strOut .= $oldPath[$i]."/"; | |
$newLength = count($newPath); | |
for($i=0;$i<$newLength;$i++){ | |
if($newPath[$i] !='..'){ | |
$strOut = $strOut.$newPath[$i].''; | |
} | |
} | |
$this->currentPath = $strOut; | |
return $this; | |
} | |
} | |
$path = new Path('/a/b/c/d'); | |
echo $path->cd('../x')->currentPath; |
`<?php
class Path
{
public $currentPath;
function __construct($path)
{
$this->currentPath = $path;
}
public function cd(string $newPath)
{
$hiearchy = explode('/', $this->currentPath);
unset($hiearchy[0]);
$this->checkNameDirectory($hiearchy);
$addedHiearchy = explode('/', $newPath);
foreach ($addedHiearchy as $value) {
if ($value === '..') {
array_pop($hiearchy);
} else {
array_push($hiearchy, $value);
}
}
$this->currentPath = $this->generatePath($hiearchy);
}
public function generatePath(array $hiearchy)
{
$path = '';
foreach ($hiearchy as $value) {
$path .= '/' . $value;
}
return $path;
}
public function checkNameDirectory(array $hiearchy)
{
foreach ($hiearchy as $value) {
if (preg_match('[A-Z]', $value) || preg_match('[a-z]', $value)) {
throw new Exception('directory shiuld be a-z or A-Z');
}
}
}
}
$path = new Path('/a/b/c/d');
$path->cd('../x');
echo $path->currentPath;`
Much shorter and simpler version without using for/foreach loop, and it Passed! :D
class Path
{
public $currentPath;
function __construct($path)
{
$this->currentPath = $path;
}
public function cd($newPath)
{
if ($this->isAbsolutePath($newPath)) {
$this->currentPath = $newPath;
return;
}
$currentPathLevelCount = substr_count($this->currentPath, '/');
$parentLevelCount = substr_count($newPath, '..');
$currentDirs = array_filter(explode('/', $this->currentPath));
$newDirs = array_filter(explode('/', $newPath), function($value) {
return !empty($value) && $value != '..';
});
$newPaths= array_slice($currentDirs, 0, $parentLevelCount > $currentPathLevelCount ? 0 : $currentPathLevelCount - $parentLevelCount );
array_push($newPaths, ...$newDirs);
$this->currentPath = '/' . implode('/', $newPaths);
}
// For Unix
public function isAbsolutePath($path)
{
return substr($path, 0, 1) == '/';
}
}
$path = new Path('/a/b/c/d');
$path->cd('../x');
echo $path->currentPath;
`
class Path
{
public $currentPath;
function __construct($path)
{
$this->currentPath = $path;
}
public function cd($newPath)
{
$dirs = explode('/', $this->currentPath);
$new_dirs = explode('/', $newPath);
$new_path = [];
foreach ($new_dirs as $new_dir) {
if ($new_dir === '..') {
array_pop($dirs);
} else {
$new_path[] = $new_dir;
}
}
$new_path = array_merge($dirs, $new_path);
$this->currentPath = implode('/', $new_path);
}
}
$path = new Path('/a/b/c/d');
$path->cd('../x');
echo $path->currentPath;
`
I think this might be an approved answer.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is working for me kinda, taking like 4 try. What a noob