Python-nowcoder Zero-complexity Transposition

题目描述

You are given a sequence of integer numbers. Zero-complexity transposition of the sequence is the reverse of this sequence. Your task is to write a program that prints zero-complexity transposition of the given sequence.

输入描述:
For each case, the first line of the input file contains one integer n-length of the sequence (0 < n ≤ 10 000). The second line contains n integers numbers-a1, a2, …, an (-1 000 000 000 000 000 ≤ ai ≤ 1 000 000 000 000 000).
输出描述:
For each case, on the first line of the output file print the sequence in the reverse order.

输入
5
-3 4 6 -8 9
输出
9 -8 6 4 -3

原思路:将首部和尾部元素进行调换,直至到中间元素为止(使用了列表输出,不符合题意)
在这里插入图片描述

错误之处有:
1、replace函数使用错误,会导致做元素替换事把11当作“1”,“1”来进行处理
2、使用列表对输出进行处理,即最后一行不应该为list类型,这样输出就有中括号,来瞅瞅这样就对了?
在这里插入图片描述

  • replace 和 split的区别
  • replace()是替换函数,两个参数,第一个参数是被替换内容,第二个参数是替换内容。即第二个替换第一个。
  • split() 是分割函数,操作:a=a.split(’,’),a变成了列表[‘1’,‘2’,‘3’]

他人解答,看别人的解决办法也是一种学习的途径
如:直接利用Python的语法特性

try:
    while 1:
        n = input()
        print(' '.join(input().split()[::-1]))
except:
    pass

?但是输入n根本没有任何意义

如:使用一些内置函数

while True:
    try:
        n = int(input())
        num = [int(i) for i in input().split(" ")]
        num.reverse()
        for i in range(0, n):
            print(num[i], end=" ")
    except:
        break
知识点:

Python字符串与列表的相互转换
1、字符串转列表 split
2、列表转字符串 join

>>> str1 = "hi hello world"
>>> print(str1.split(" "))
['hi', 'hello', 'world']
>>> list1 = ['hi', 'hello', 'world']
>>> print(" ".join(list1))
hi hello world
>>> 

Python实现字符串反转的几种方法
1、使用字符串切片
result = s[::-1]

s = 1, 2, 3, 4, 5
result = s[::-1]
print(result)

2、使用列表的reverse方法
l = list(s)
result = “”.join(l.reverse)

同理
l = list(s)
result = “”.join(l[::-1])

s = 1, 2, 3, 4, 5
l = list(s)
print(l)
l.reverse()
print(l)
result = " ".join('%s' %id for id in l)
print(result)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值