流程控制练习题

 

使用尽可能多的方法实现list去重

方法1:

a=[1,1,2,2,3,3,4,5]
print set(a)

方法2:

a=[1,1,2,2,3,3,4,5]
b=[]
for i in a:
    if
i in b:
        continue
    else:
       
b.append(i)
print b

方法3:

a=[1,1,2,2,3,3,4,5]
d={}
for i in a:
   
d[i]=""

print d.keys()

成绩等级判断

利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示

while 1:
   
grade=raw_input("please input your grade:")
    if grade==".":
        break
    else:
       
grade=int(grade)
        if grade>=90:
            print
"your level is A"
        elif grade >=60 and grade <90:
            print
"your level is B"
        else:
            print
"your level is C"

 

实现数学中多项式求和公式的打印

a6x^6 + a5x^5 + a4x^4 + a3x^3 + a2x^2 + a1x^1 + a0

exp=""
for i in range(6,0,-1):
   
exp+=('a%s^x%s+' %(i,i))
exp+='a0'
print exp

 

统计名字列表中,各名字的首字母在名字列表中出现的次数

name_list=["jane","fosterwu","janet","jordan","flore",'michel']
d={}

for name in name_list:
    if
d.has_key(name[0]):
       
d[name[0]]+=1
    else:
       
d[name[0]] = 1

for letter,times in d.items():
    print
"%s occurs %s times" %(letter,times)

 

输入三个数,判断是否能构成三角形

能构成三角形三边关系:

         三边都大于零

         两边之和大于第三边,两边之差小于第三边

a=int(raw_input("please input side length:"))
b=int(raw_input("please input side length:"))
c=int(raw_input("please input side length:"))

if a>0 and b>0 and c>0 and a+b>c and a+c>b and b+c>a and a-b<c and a-c<b and b-c<a:
    print
"it is a triangle"
else:
    print
"it is not a triangle"

实现字典的fromkeys方法

def from_keys(lista,value):
   
d={}
    for i in lista:
       
d[i]=value
   
return d

print from_keys(['a',"b","c"],10)

键盘读入一字符串,逆序输出:

line=raw_input("input a sentence:")
print line[::-1]

读入一个整数n,输出n的阶乘

def factorial(n):
   
result=1
    for i in range(1,n+1):
       
result*=i
    return result

print factorial(5)

 

打印1/2, 1/3, 1/4,….1/10

result=[]
for i in range(2,11):
   
result.append("1/"+str(i))

print ",".join(result)

 

写一个函数实现一个数学公式

import math
def area(radius):
    return
math.pi*radius**2

print area(10)

输入数字a,n,如a,4,则打印a+aa+aaa+aaaa之和

def print_exp(letter,times):
   
result=[]
    for i in range(1,times+1):
       
result.append(letter*i)
    return result

print "+".join(print_exp("a",4))

 

求100个随机数之和,随机数要求为0—9的整数

import random
result=0
for i in range(100):
   
result+=random.randint(0,9)

print result

要求在屏幕上分别显求1到100之间奇数之和与偶数之和

odd_number_sum=0
oven_number_sum=0
for i in range(1,101):
    if
i%2==0:
       
oven_number_sum+=i
    else:
       
odd_number_sum+=i

print odd_number_sum
print oven_number_sum

 

输入10个数,并显示最大的数与最小的数

biggest_number=None
least_number=None
for i in range(5):
   
number=float(raw_input("please input a number:"))
    if i ==0:
       
biggest_number,least_number=i,i
    if number>biggest_number:
       
biggest_number=number
    if number<least_number:
       
least_number=number

print biggest_number
print least_number

 

给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。

number=raw_input("please input a number:")
print "number is %s length" %(len(number))
print str(number)[::-1]

 

求1000以内的所有水仙花数

for i in range(100,1000):
   
length=len(str(i))
    if int(str(i)[0])**length+int(str(i)[1])**length+int(str(i)[2])**length==i:
        print
i

 

编程求s=1!+2!+3!+…..+n!

def factorial(n):
   
result=1
    for i in range(1,n+1):
       
result*=i
    return result

def compute_factorial_sum(n):
   
sum=0
    for i in range(1,n+1):
       
sum+=factorial(i)
    return sum

print compute_factorial_sum(3)

 

钞票换硬币
result=[]
for i in range(0,101):
    for
j in range(0,51):
        for
m in range(0,21):
            if
i+j*2+m*5==100:
               
result.append("%s,%s,%s" %(i,j,m))

print len(result)
print result

自己实现在一句话中查找某个单词的算法,存在返回索引号,否则返回False

sentence="i am a boy!"
search_word="i"
def find_word(word,target_sentence):
   
word_length=len(word)
    for i in range(len(target_sentence)):
        if
sentence[i:i+word_length]==word:
            return
i
    return False

print find_word(search_word,sentence)

 

读入一个十进制整数,实现十进制转二进制算法将其转成二进制数要求:不能使用现成进制转换函数,自己写代码实现

请见讲义

将一个矩阵转置

a=[[1,2,3],[4,5,6]]
def covert_array(array):
   
b=[]
    for i in range(len(a[0])):
       
x=[]
        for j in range(len(a)):
           
x.append(a[j][i])
        b.append(x)
    return b

print covert_array(a)

 

posted on 2017-07-27 10:06  老样子  阅读(575)  评论(0编辑  收藏  举报

导航