Any idea? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

24th May 2021, 1:31 PM
Ceno Python
Ceno Python - avatar
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!
24th May 2021, 1:55 PM
Lothar
Lothar - avatar
+ 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?
24th May 2021, 1:42 PM
Lisa
Lisa - avatar
+ 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?
24th May 2021, 2:39 PM
Ipang