Python 第二式

@Codewars Python练习

question

** Simple transposition **

Simple transposition is a basic and simple cryptography technique. We make 2 rows and put first a letter in the Row 1, the second in the Row 2, third in Row 1 and so on until the end. Then we put the text from Row 2 next to the Row 1 text and thats it.

Complete the function that recieves a string and encrypt it with this simple transposition.

Ex:

For example if the text to encrypt is: "Simple text", the 2 rows will be:

  1. Row 1 S m l e t
  2. Row 2 i p e t x

So the result string will be: "Sml etipetx"

solutions

my solutions

def simple_transposition(text):
    l1 = [] 
    l2 = []
    for i in range(len(text)) :
        if i % 2 == 0:
            l1.append(text[i])
        elif i % 2 != 0:
            l2.append(text[i])
    return "".join(l1)+"".join(l2)
    

other solutions

def simple_transposition(text):
    return text[::2] + text[1::2]

def simple_transposition(text):
    rowOne = True
    one = ""
    two = ""
    for x in text:
        if(rowOne):
            one += x
        else:
            two += x
        rowOne = not rowOne
    return str(one) + str(two)

  • 还是太菜了
  • 熟悉列表切片用法
posted @ 2019-07-31 15:23  gg12138  阅读(117)  评论(0编辑  收藏  举报