cory
June 7, 2022, 2:26pm
1
I'm writing Caesar's code. The code works, but the space doesn't apply. For example, "AB" can be transformed "A B" is not converted. Help me.
Caesar = function(input){
answer = ""
solution = NULL
Low = "abcdefghijklmnopqrstuvwxyz"
Up = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Lower = strsplit(Low,"")
Lower = Lower[[1]]
Upper = strsplit(Up, "")
Upper = Upper[[1]]
cutI = strsplit(input,"")
cutI = unlist(cutI)
df_Result = data.frame(n.Kan = 1:26)
for(n in 1:26){
answer = ""
for (i in cutI){
if (i %in% ""){
answer = paste0(collapse = "")
} else if (i %in% Lower){
ind = which(Lower == i)
indN = (as.numeric(ind)+n)%%26
if(indN %% 26==0){
indN = 26
}
answer = paste0(c(answer, Lower[indN]),collapse="")
} else{
ind = which(Upper == i)
indN = (as.numeric(ind)+n)%%26
if(indN%%26==0){
indN = 26
}
answer = paste0(c(answer, Upper[indN]), collapse = "")
}
answer=paste0(answer, collapse="")
}
solution = c(solution,answer)
}
df_Result = cbind(df_Result , "Result" = solution)
return(df_Result)
}
```
if you want "A B" to be processible, then you need a solution that can scramble at a minimum 262+1 characters, not just 26 2 characters I think.
The easiest way to address is to add space to your list of letters and process 27 instead of 26.
or you could come up with a solution where you record the position of every space in the input and put them back in after scrambling. but this extra code solution is so easy ...
Caesar = function(input){
answer = ""
solution = NULL
Low = "abcdefghijklmnopqrstuvwxyz "
Up = "ABCDEFGHIJKLMNOPQRSTUVWXYZ "
Lower = strsplit(Low,"")
Lower = Lower[[1]]
Upper = strsplit(Up, "")
Upper = Upper[[1]]
cutI = strsplit(input,"")
cutI = unlist(cutI)
df_Result = data.frame(n.Kan = 1:27)
for(n in 1:27){
answer = ""
for (i in cutI){
if (i %in% ""){
answer = paste0(collapse = "")
} else if (i %in% Lower){
ind = which(Lower == i)
indN = (as.numeric(ind)+n)%%27
if(indN %% 27==0){
indN = 27
}
answer = paste0(c(answer, Lower[indN]),collapse="")
} else{
ind = which(Upper == i)
indN = (as.numeric(ind)+n)%%27
if(indN%%27==0){
indN = 27
}
answer = paste0(c(answer, Upper[indN]), collapse = "")
}
answer=paste0(answer, collapse="")
}
solution = c(solution,answer)
}
df_Result = cbind(df_Result , "Result" = solution)
return(df_Result)
}
system
Closed
June 28, 2022, 4:32pm
3
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.