加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python 第二式

发布时间:2020-12-20 12:52:46 所属栏目: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.

@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)

  • 还是太菜了
  • 熟悉列表切片用法

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读