함수 rotateString()

1 PHP[ | ]

function rotateString($s, $offset) {
    return substr($s, $offset) . substr($s, 0, $offset);
}
echo rotateString("abcd", -2)."\n"; # cdab
echo rotateString("abcd", -1)."\n"; # dabc
echo rotateString("abcd", 0)."\n";  # abcd
echo rotateString("abcd", 1)."\n";  # bcda
echo rotateString("abcd", 2)."\n";  # cdab
echo rotateString("abcd", 3)."\n";  # dabc
echo rotateString("abcd", 4)."\n";  # abcd

2 Python[ | ]

def rotateString(s, offset):
    l = len(s)
    if s is None or l == 0: return s
    return s[offset:] + s[:offset]

print( rotateString("abcd", -2) ) # cdab
print( rotateString("abcd", -1) ) # dabc
print( rotateString("abcd", 0) )  # abcd
print( rotateString("abcd", 1) )  # bcda
print( rotateString("abcd", 2) )  # cdab
print( rotateString("abcd", 3) )  # dabc
print( rotateString("abcd", 4) )  # abcd

3 R[ | ]

rotateString <- function(s, offset) {
    n = nchar(s)
    offset = (n+offset) %% n
    paste(substring(s,offset+1),substring(s,1,offset),sep="")
}
print( rotateString("abcd", -2) ) ## [1] "cdab"
print( rotateString("abcd", -1) ) ## [1] "dabc"
print( rotateString("abcd", 0) )  ## [1] "abcd"
print( rotateString("abcd", 1) )  ## [1] "bcda"
print( rotateString("abcd", 2) )  ## [1] "cdab"
print( rotateString("abcd", 3) )  ## [1] "dabc"
print( rotateString("abcd", 4) )  ## [1] "abcd"

4 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}