python cyclic shifting of the characters in the string

你离开我真会死。 提交于 2019-12-24 17:52:31

问题


Subclass sstr of the standard str type that implements the "<<" and ">>" methods as a cyclic shifting of the characters in the string.What trying to do is

 >>> s1 = sstr("abcde")
 >>> s1 << 0
'abcde'
 >>> s1 >> 0
'abcde'
 >>> s1 << 2
'cdeab'
>>> s1 >> 2
'deabc'
>>> s1 >> 5
 'abcde'

# my attempt:
import string
class sstr(str):
def __new__(self, other):
    return str.__new__(self, other.upper())
def __ilshift__(self, other):
    return str.__ilshift(other)
def __rshift__(self, other):
    return str.__rshift(other)    

回答1:


This smells like homework, so I'm not going to post actual code here. But to help, I will point out flaws I see in your code and an algorithm:

My python 2.7.2 reports no __ilshift or __irshift in str. Also, if you are trying to shift a string by a certain number of characters, then you shouldn't be shifting the variable you call other. You should be shifting self by other many characters. That being said, you're probably better off naming other as n or some such.

Now, I assume you know how circular shifting is supposed to work. The examples you provide get the message across well.

As a simple algorithm (easy to read/understand), try this (pseudo-code follows):

function __ilshift(self, n) { // self is the string to be shifted. n denotes how many characters to shift it by
    answer = copy()
    for i = 1 to n {
        answer = self[1:] //answer = everything in self excluding the first character
        answer += self[0] // append the first character of self to answer
    }
    return answer
}

The above solution would work. Though, it is quite inefficient. We know that when an n-character string is shifted by n, the result of the shifting is the string itself. When you think about this a little more, you realize that you end up shifting by n % lengthOfSelf. Thus, the for i = 1 to n turns into for i = 1 to n%len(self).

Still, we can make this more efficient. To do this would require splicing self at the appropriate index, which I'll let you figure out, because I think this is homework.

Hope this gets you closer!




回答2:


s1 << 0

This calls __lshift__, not __ilshift__. The i stands for in-place; you can't change a string in-place anyway, and aren't trying to here (you're trying to create a new value).

The problem with your actual code is that you're trying to implement the shift by just calling the base str class's shift. But the base str class doesn't have that shift operation - that's the entire reason you have this exercise!

Hint: put together two slices of the string. 'foobar' << 2 is 'obar' + 'fo'. Can you see how to slice the string to get those? How do the numbers you use for the slices relate to the number specified for shifting?



来源:https://stackoverflow.com/questions/10826253/python-cyclic-shifting-of-the-characters-in-the-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!