简体   繁体   English

根据用户输入旋转给定列表中的索引,而旋转顺序则来自另一个列表中的索引

[英]Rotating indexes in a given list according to a user input, while the sequence of rotation is derived from indexes from another list

This problem is from the ACM ICPC MidCentral 2014 problem set. 此问题来自ACM ICPC MidCentral 2014问题集。 Specifically, it is problem G, ReverseRot. 具体来说,是问题G,ReverseRot。

I have to write a program which takes a user inputted string, reverses it, and then rotates it according to a list containing the uppercase alphabet from A to Z, underscore, and period. 我必须编写一个程序,该程序接受用户输入的字符串,将其反转,然后根据包含从A到Z,下划线和句点的大写字母的列表旋转它。 So far, I've been able to do everything but rotate it correctly. 到目前为止,除了正确旋转外,我已经可以执行所有操作。 This is the code I have thus far: 这是我到目前为止的代码:

#character list which is referenced during the rotation
rotationList = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "_", "."] 
reverseRotList = [] #list which stores the completed rotation, later to be joined into a string

def reverse(string) : #reverses the string and stores each character in indexes within a list
    string = string[::-1]
    string = list(string)
    return string

string = input("What is the string? ").upper()
print(reverse(string)) #to show string was reversed and converted into a list correctly

rotAmount = int(input("Amount to be rotated? ")) #user decides how much rotation there will be

for i in string:

How would I rotate the string list according to the user input while rotating in accordance to the rotation list? 在按照旋转列表旋转时,如何根据用户输入旋转字符串列表?

Building the rotated list 建立轮换清单

Method one 方法一

Say you have a sequence of numbers such as: 假设您有一系列数字,例如:

[0, 1, 2, 3, 4, 5, 6, 7]

and you want to rotate them by 3 to the left like this: 并且您想像这样将它们向左旋转3

[3, 4, 5, 6, 7, 0, 1, 2]

Notice that the first three numbers are shifted by +3 from the original while the remaining two numbers are shifted by -5 from the original. 请注意,前三个数字与原始数字相比偏移了+3 ,而其余​​两个数字与原始数字相比偏移了-5 Do you notice a pattern here? 您在这里注意到一种模式吗?

The key thing to notice is that -5 is just +3 - 8 , where 8 is the length of the list. 要注意的关键是-5只是+3 - 8 ,其中8是列表的长度。 In other words, the numbers undergo an additive shift followed by a modulo operation : 换句话说,这些数字经过加法运算后进行模运算

new_i = (old_i - shift) % length

Now, what you have is not a list of numbers, but a list of characters, but the difference is superficial: applying the same transformation to the index of the strings leads to the same effect of a rotation: 现在,您拥有的不是一个数字列表,而是一个字符列表,但是区别是肤浅的:对字符串的索引应用相同的转换会产生相同的旋转效果:

new_list[old_i] = old_list[new_i]

Then it's just a matter of repeating this operation for every element in the list. 然后,只需对列表中的每个元素重复此操作即可。

(There is a slight subtlety with regards to the sign of shift ; here, I use the convention that a positive shift is equivalent to rotating the values right.) (关于shift的符号有些微妙;在这里,我习惯使用正移位等效于将值向右旋转。)

Method two 方法二

Another way is to think of a list rotation as a two step operation: 另一种方法是将列表轮换视为两步操作:

  1. First you break up the list old_list into two parts: left_half and right_half . 首先,将清单old_list分为两部分: left_halfright_half
  2. Then you join them back together as right_half + left_half . 然后将它们重新合并为right_half + left_half

The main question here is: where do you break the list? 这里的主要问题是:您在哪里打破名单? It is again related to the amount you want to shift in a simple way: 它又以一种简单的方式与您要转移的金额有关:

break_index = shift % length

The modulo is needed because a rotation by an integer multiple of length has no real effect on your list. 需要取模,因为旋转length的整数倍对列表没有实际影响。

Translating characters 翻译字符

To use the rotated list to translate a character (or a string of characters), you will need to find the location of the character in the original ordering: 使用旋转列表翻译字符(或字符串),您将需要按原始顺序查找字符的位置:

index = old_list.index(character)

Then find the corresponding character in the rotated list: 然后在轮换列表中找到相应的字符:

translated_character = new_list[index]

Optimizations 最佳化

Building the new_list that contains the rotated alphabet is actually unnecessary for performing translations. new_list ,构建包含旋转字母的new_list对于执行翻译是不必要的。 By performing some algebraic transformations, one see that the translated characters can be directly computed from the original alphabet: 通过执行一些代数转换,可以看到可以直接从原始字母计算出翻译的字符:

translated_character = old_list[(index - shift) % length]

Additionally, using the index method is inefficient: it's better to construct a dictionary to speed up the lookups. 此外,使用index方法效率低下:最好构造一个字典以加快查找速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM