+ 1
Any idea?
Task 1: Write a function that receives two portions of a path and joins them. The portions will be joined with the "/"separator. There could be only one separator and if it is not present it should be added. Examples joinPath("portion1", "portion2") â "portion1/portion2" joinPath("portion1/", "portion2") â "portion1/portion2" joinPath("portion1", "/portion2") â "portion1/portion2" joinPath("portion1/", "/portion2") â "portion1/portion2" Bonus Try not to solve this challenge using only if-else conditions.
3 Answers
+ 8
Arsen Lufo ,
when getting the strings in your function, you could use the string method replace(), so you are able to "remove" the existing separators. replace them with an empty string: "".
after that you can concatenate the portion1 + separator + portion2.
for the bonus (only if - else allowed) you can use an other string fuctions:
startswith() or endswith() methods allow you to detect if and where the existing separators are located.
then use a slice to get string information and concatenate the new string
happy coding and good success!
+ 3
Maybe try to strip the separator from portion1 and portion2 (regardless if it is present at all) and then join the stripped strings with the separator?
+ 3
What output was expected for
joinPath( "home/docs/", "public.txt" )
Or
joinPath( "/home/docs/", "private/secrets.txt" )
What happens when either path contain space?