These are some answers to the Week 112 of the Perl Weekly Challenge organized by Mohammad S. Anwar.
Spoiler Alert: This weekly challenge deadline is due in a few days (May 16, 2021). This blog post offers some solutions to this challenge, please don’t read on if you intend to complete the challenge on your own.
Task 1: Canonical Path
You are given a string path, starting with a slash ‘/’.
Write a script to convert the given absolute path to the simplified canonical path.
In a Unix-style file system:
- A period ’.’ refers to the current directory
- A double period ’..’ refers to the directory up a level
- Multiple consecutive slashes (’//’) are treated as a single slash ’/’
The canonical path format:
- The path starts with a single slash ’/’.
- Any two directories are separated by a single slash ’/’.
- The path does not end with a trailing ’/’.
- The path only contains the directories on the path from the root directory to the target file or directory
Example: