Given a program name and a list of paths, return the first path whose filename matches the name exactly. For example, map matches /bin/map, not /app/maple. Search only the supplied list, not your computer.
Input: One line: the program name, followed by candidate paths separated by spaces. Paths do not contain spaces.
Output: Print the first matching full path. If no filename matches, print not found.
Input:
cat /bin/cat /tools/cat /app/readme
Output:
/bin/cat
Explanation: Both /bin/cat and /tools/cat have filename cat. The first candidate wins, so return /bin/cat.
Input:
edit /tools/edit /app/edit
Output:
/tools/edit
Explanation: The first candidate's filename is edit, so return /tools/edit without choosing the later match.
Input:
zip /bin/zap /tools/zip
Output:
/tools/zip
Explanation: zap does not match zip. The second candidate has the exact filename zip, so return /tools/zip.
Input:
map /app/maple /bin/map
Output:
/bin/map
Explanation: maple is not an exact match for map. Skip it and return /bin/map.
Input:
nope /bin/cat /tools/edit
Output:
not found
Explanation: Neither candidate has filename nope, so print not found.
What part of a path is its filename? Can you stop as soon as that part matches?
A shell searches possible locations in order when you enter a program name. This exercise practices that ordered choice; it does not check whether a file is executable.
System 1: Build a Command-Line Shell
Optional application of the idea. You write the System solution separately.
Keep track of tasks as they start, finish, and get cleared.
/bin/cat