'/©¡RćUηí®ýʒþd_}®«õš¤X«
Try it online or verify all test cases.
05AB1E has no regexes, so this is not the kind of challenge it will do very good, in comparison to non-verbose languages that have a strength in regexes like Retina or Pyth.
Can probably still be golfed by at least a few bytes, though.
Explanation:
'/ # Push a "/"
© # Store it in the register (without popping)
¡ # Split the (implicit) input on slashes
# i.e. "C:/Desktop3/Stuff/F0/images/new1.png"
# → ["C:","Desktop3","Stuff","F0","images","new1.png"]
R # Reverse this list
# i.e. ["C:","Desktop3","Stuff","F0","images","new1.png"]
# → ["new1.png","images","F0","Stuff","Desktop3","C:"]
ć # Head extracted
# i.e. ["new1.png","images","F0","Stuff","Desktop3","C:"]
# → ["images","F0","Stuff","Desktop3","C:"] and "new1.png"
U # Pop the extracted head, and store it in variable `X`
η # Take the prefixes of the list
# i.e. ["images","F0","Stuff","Desktop3","C:"]
# → [["images"],["images","F0"],["images","F0","Stuff"],["images","F0","Stuff","Desktop3"],["images","F0","Stuff","Desktop3","C:"]]
í # Reverse each inner list again
# i.e. [["images"],["images","F0"],["images","F0","Stuff"],["images","F0","Stuff","Desktop3"],["images","F0","Stuff","Desktop3","C:"]]
# → [["images"],["F0","images"],["Stuff","F0","images"],["Desktop3","Stuff","F0","images"],["C:","Desktop3","Stuff","F0","images"]]
®ý # And join them by slashes
# i.e. [["images"],["F0","images"],["Stuff","F0","images"],["Desktop3","Stuff","F0","images"],["C:","Desktop3","Stuff","F0","images"]]
# → ["images","F0/images","Stuff/F0/images","Desktop3/Stuff/F0/images","C:/Desktop3/Stuff/F0/images"]
ʒ } # Now filter this list by:
þ # Leave only the digits of this suffix
# i.e. "images" → ""
# i.e. "Stuff/F0/images" → 0
d # Check if its >= 0
# i.e. "" → 0 (falsey)
# i.e. 0 → 1 (truthy)
_ # Inverse the boolean (0→1; 1→0)
®« # Append a slash to each remaining suffix
# i.e. ["images"] → ["images/"]
õš # Prepend an empty string as list
# (work-around when there are no valid suffices left)
# i.e. [] → [""]
# i.e. ["images/"] → ["","images/"]
¤ # Take the last suffix
# i.e. ["","images/"] → "images/"
X« # Append variable `X` (and implicitly output the result)
# i.e. "images/" and "new1.png" → "images/new1.png"
1@Stephen "challenge proposer". – user202729 – 2018-09-14T15:52:41.667
Suggested test case:
C:/Users/User1a/Documents/foo.pdf
. (Can you confirm that the expected output isDocuments/foo.pdf
in that case?) And welcome to PPCG! – Arnauld – 2018-09-14T15:53:08.920Sorry @Stephen, I forgot to mention that. As per the example given, you should start from the last folder that contains a number in its name. I will also modify the question so that there are three examples. – Wais Kamal – 2018-09-14T15:56:44.473
3Do we have to support some weird Unicode characters like
߈
("NKO DIGIT EIGHT") and would it be considered a number? – user202729 – 2018-09-14T16:00:50.850What if the "part" after the last folder with a digit is the file name not a folder? (example
C:\a1\a.pdf
) – user202729 – 2018-09-14T16:02:00.303No, numbers are 0,1,2,3,4,5,6,7,8,9 and 0. – Wais Kamal – 2018-09-14T16:03:03.030
C:\a1\a.pdf
should returna.pdf
. – Wais Kamal – 2018-09-14T16:03:30.377Yes @arnuld, this is the required output from your path. – Wais Kamal – 2018-09-14T16:07:20.567
1@WaisKamal are backslashes considered legitimate path separators? – hidefromkgb – 2018-09-14T16:09:13.550
4I'm not clear on why the third test case is supposed to return
Animations/52.Tricky.Maneuvers/video234.png
as opposed tovideo234.png
. Why does the folder52.Tricky.Maneuvers
not count as " the last folder that contains a number"? – mypetlion – 2018-09-14T16:22:10.5972I agree with @mypetlion.
52.Tricky.Maneuvers
contains a digit, so shouldn't it outputvideo234.png
instead? And if you mean by "starting from the folder after the last folder that contains a number till the filename" that there should always be at least one folder (regardless if it contains digits) before the file-path, doesn't that mean thatC:\a1\a.pdf
should outputC:\a1\a.pdf
?.. – Kevin Cruijssen – 2018-09-14T16:52:20.5201
As per the example given, you should start from the last folder that contains a number in its name..
This is not what your third example suggests. However, this is what your second example does. So, what's the correct one? – DimChtz – 2018-09-14T17:16:20.790@mypetlion (challenge fixed) – user202729 – 2018-09-15T13:15:20.717
Um, does our code need to work with something else than the provided three examples? "Your answer should work with the above given three examples." If it should only work for the three examples, that's highly discouraged. – Erik the Outgolfer – 2018-09-18T14:45:28.790
These are just examples, so the answer must work as specified in the question. – Wais Kamal – 2018-09-18T18:13:53.953