-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfileNaming.R
executable file
·57 lines (54 loc) · 1.92 KB
/
fileNaming.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# You are given an array of desired filenames in the order of their creation.
# Since two files cannot have equal names, the one which comes later will have
# an addition to its name in a form of (k), where k is the smallest positive
# integer such that the obtained name is not used yet.
#
# Return an array of names that will be given to the files.
#
# Example
#
# For names = ["doc", "doc", "image", "doc(1)", "doc"], the output should be
# fileNaming(names) = ["doc", "doc(1)", "image", "doc(1)(1)", "doc(2)"].
#
# Input/Output
#
# [execution time limit] 5 seconds (r)
#
# [input] array.string names
#
# Guaranteed constraints: 5 ≤ names.length ≤ 1000, 1 ≤ names[i].length ≤ 15.
#
# [output] array.string
#
#
# names = list("doc", "doc", "image", "doc(1)", "doc")
# names = list("dd","dd(1)","dd(2)","dd","dd(1)","dd(1)(2)","dd(1)(1)","dd","dd(1)")
# names = list("dd","dd(1)","dd(2)","dd","dd(1)","dd(1)(2)","dd(1)(1)","dd","dd(5)")
fileNaming <- function(names) {
if (length(names) <= 1) {
return(names)
}
namessofar <- names[1]
for (ind in 2:length(names)) {
if (names[[ind]] %in% namessofar) {
suggestednewname <- paste0(names[[ind]],"(1)")
while (suggestednewname %in% namessofar) {
#get last braces and increment the int.
allparantheses <- gregexpr("[(]",suggestednewname)[[1]]
#find the last one
reqstartindex <- max(allparantheses)
reqendindex <- max(gregexpr("[)]",suggestednewname)[[1]])
currentrepindex <- substr(suggestednewname,
reqstartindex + 1,
reqendindex - 1)
suggestednewname <- paste0(substr(suggestednewname,1,reqstartindex),
as.numeric(currentrepindex) + 1,
")")
}
namessofar[[ind]] <- suggestednewname
next
}
namessofar[[ind]] <- names[[ind]]
}
return(namessofar)
}