算法练习 2:翻转数据

题目:http://exercise.acmcoder.com/online/online_judge_ques?ques_id=1656&konwledgeId=134

算法分析

1.输入n、n个数据存入list1;

2.升序排列存为list2:list2=list1.sort

3.for :

    if list[head]==list2[head]: head++

    else: l=head

    if list1[end]==list2[end]:end--

    else: r=end;

4. if list[l:r]==reverse(list2): 输出yes

    else:输出no

代码练习

def getR():
    n = input()
    list1= map(int,raw_input().split())
    list2= sorted(list1)
    head = 0
    end = len(list1) - 1
    hStop = False
    eStop = False
    if list1 == list2:
        return 'yes'
    while(head < end):
        if (not hStop) and (list1[head] == list2[head]):
            head += 1
        else:
            hStop = True
        if (not eStop) and (list1[end] == list2[end]):
            end -= 1
        else:
            eStop = True
        if hStop and eStop:
            break
    if list(reversed(list1[head: end + 1])) == list2[head: end + 1]:
        return 'yes'
    else:
        return 'no'
print getR()

基础知识备注

1.raw_input()随便输都是字符串,而input()必须按照Python的规则来;

2.map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返

3.list内置sort()方法用来排序改变原list,sorted()来对可迭代的序列排序生成新的序列

4.Python逻辑运算符,以下假设变量 a 为 10, b为 20:

运算符

逻辑表达式

描述实例
andx and y布尔"与" - 如果 x 为 False,x and y 返回 False,否则它返回 y 的计算值。(a and b) 返回 20。
orx or y布尔"或" - 如果 x 是非 0,它返回 x 的值,否则它返回 y 的计算值。(a or b) 返回 10。
notnot x布尔"非" - 如果 x 为 True,返回 False 。如果 x 为 False,它返回 True。not(a and b) 返回 False

5.break和continue

break跳出整个循环continue 语句跳出本次循环,

for letter in 'Python':

# 第一个实例

    if letter == 'h':

        break

    print '当前字母 :', letter


当前字母 : P
当前字母 : y
当前字母 : t

for letter in 'Python':

# 第一个实例

    if letter == 'h':

        continue

    print '当前字母 :', letter

当前字母 : P
当前字母 : y
当前字母 : t
当前字母 : o
当前字母 : n

-------------------------------------------------------------------------------------------------------------------

详细内容请关注公众号:目标检测和深度学习

------------------------------------------------------------------------------------------------------------------



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

女王の专属领地

您的鼓励是我最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值