Doctest: Palindromes (reverse list) --CS61A lecture9

1. Palindromes (e.g. racecar) : a word that is the same backwards and forwards

2. isPal(x): Input: list; output: True or False, depending on whether x is a palindrome

3. isPalN(n, 1st): Input: interger, list; output: print"Is a pllindrome" or "Is not a palindrome", depending on whether the first n elements of 1st form a palindrome. 

4. your task: come up with possible inputs to "isPal" and what is should output in each case; come up with possilble inputs to the function "isPalN" and what is should output in each case

def reverse_list(x):        #reverse list#
    return x[::-1]

def reverse(x):                #判断是否是Palindromes#
    temp=x
    lst = reverse_list(temp)
    print(lst)
    if lst == x:
        return True
    else:
        return False

def is_reverse(n,lt):
    new_list = []
    for i in range(n):
        new_list.append(lt[i])
    if reverse(new_list):
        print('Is a palindrome')
    else:
        print('Is not a palindrome')

"""(把x装进list)

x='abcde'
list(x)

(reverse a string表达):

x = 'abcd'          

c = x[::-1]

print(c) --- dcba

(x='1234' --- 4321  只能string不能转换int, int 要用列表)

""

int 装进list表达 列表前几位装进新列表

x = [1,2,3,4]
list = []
for i in range(2):
    print (x[i])                   # 取x前两位数1,2 #
    list.append(x[i])        # 把x[i]中的两位数装进list里面生成新list #
print (list)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值