有这么一道题,要求用python实现:
Question 4 (6 marks): Deja Vu
Your company is creating the next WinZip!
WinZip is a popular program that makes your files smaller to save storage space and download times. You are a new hire on the data compression team, and you are asked to create the first processing stage, in which repeated data is identified for further checking.
Your task is to obtain from the user a text string to be compressed, and then replace each repeated character in this text string with a relative index to the previous occurrence of the character, thus creating a list that is linked (so that recent substrings beginning with a given character can be located easily).
You must output each repeated character as an open parenthesis, a negative number, and then a close parenthesis. This counts as only one position in the resulting string. You may assume that there are no parentheses in the input, or that if there are, they need not be distinguishable from any parentheses you add.
For example, given the text string ‘hello’, you will output the string ‘hel(-1)o’, indicating that after ‘hel’ there is a repeated character which can be found by going backwards one character in the string. Multiple repeats point to the most recent, for instance ‘aardvark’ gives ‘a(-1)rdv(-4)(-4)k’.
First sample session with the program:
Text to compress?
hello hel(-1)o
Second sample session with the program:
Text to compress? aardvark
a(-1)rdv(-4)(-4)k
Third sample session with the program:
Text to compress?
The cat in the hat
The cat(-4)in(-3)(-5)(-11)(-11)(-4)(-3)(-11)(-6)
如下图,可以更清晰:
image.png实现方法:
# 20180826 By Galory
# Email:996377370a@gmail.com
# coding:utf-8
string_input = input("Text to compress?")
# 新建一个列表,保存当前字符串
str_list = []
for i in string_input:
if i not in str_list:
# 把当前字符串添加进str_list列表中
str_list.append(i)
print(i, end="")
elif i in str_list:
# 把列表逆序输出
current_str = str_list[::-1]
# 把列表转换为字符串
str_current = "".join(current_str)
# 字母重复出现时输出其上一次出现时的位置
print("(-" + str(str_current.find(i) + 1) + ")", end="")
str_list.append(i)
写这个程序get到的知识:
- 字符串与列表的转换
列表转字符串:
命令:"".join(list)
其中,引号中是字符之间的分割符,如“,”,“;”,“\t”等等
-----------------------------------------------
列表转字符串:
命令:
str_name = "Ths is a string."
list_new = list(str_name)
- python中字符串查找的 find和index方法
# 这里直接引自链接:http://outofmemory.cn/code-snippet/6682/python-string-find-or-index
python字符串查找有4个方法,1.find,2.index方法,3.rfind方法,4.rindex方法。
1.find()方法:查找子字符串,若找到返回从0开始的下标值,若找不到返回-1
info = 'abca'
print info.find('a')##从下标0开始,查找在字符串里第一个出现的子串,返回结果:0
info = 'abca'
print info.find('a',1)##从下标1开始,查找在字符串里第一个出现的子串:返回结果3
info = 'abca'
print info.find('333')##返回-1,查找不到返回-1
2.index()方法:
python的index方法是在字符串里查找子串第一次出现的位置,类似字符串的find方法,不过比find方法更好的是,如果查找不到子串,会抛出异常,而不是返回-1
info = 'abca'
print info.index('a')
print info.index('33')
rfind和rindex方法用法和上面一样,只是从字符串的末尾开始查找。
- python中reverse一个字符串
>>> 'hello world'[::-1]
'dlrow olleh'
It works by doing [begin:end:step] - by leaving begin and end off and specifying a step of -1, it reverses a string.
- python中reverse与reversed函数
foo.reverse()
actually reverses the elements in the container.
reversed()
doesn't actually reverse anything, it merely returns an object that can be used to iterate over the container's elements in reverse order. If that's what you need, it's often faster than actually reversing the elements.
总之有什么问题,Google和stackoverflow都会给你很大的帮助。
20180826
网友评论