<h5>1.给定一个字符串a,如a='12345',对a进行逆序输出a</h5>
<pre>
-- coding:utf-8 --
def fun1(a):
return a[::-1]
</br>
def fun11(a):
b = list(a)
b.reverse()
return ''.join(b)
</pre>
<h5>2.给你一字典a,如a={1:1,2:2,3:3},输出字典a的key,以','链接,如‘1,2,3'。</h5>
<pre>
-- coding:utf-8 --
def fun2(a):
b = []
for i in a.keys():
if type(i) == str:
b.append(i)
elif type(i) == int or type(i) == tuple:
b.append(str(i))
else:
print u'函数参数有误'
return ','.join(b)
</br>
x = {'a':1,'b':2,'c':3}
y = {1:1,2:2,3:3}
z = {'a':1,2:2,(1,2,3):999}
fun2(x)
'a,c,b'
fun2(y)
'1,2,3'
fun2(z)
'a,2,(1, 2, 3)'
fun2({1:1,2:2,3:3})
'1,2,3'
fun2({[1,2]:1,'b':2})
</pre>
<h5>3.给你一个字符串 a, 输出字符奇数位置的字符串。如a=‘12345’,则输出135。</h5>
<pre>
-- coding:utf-8 --
def fun3(a):
return a[::2]
</br>
def fun33(a):
b = []
for i in range(len(a)):
if i % 2 == 0:
b.append(a[i])
return ''.join(b)
</br>
a='12345'
fun3(a)
'135'
fun33(a)
'135'
</pre>
<h5>4.输出n以内的所有素数</h5>
素数之间以一个空格区分,质数(prime number)又称素数,有无限个。质数定义为在大于1的自然数中,除了1和它本身以外不再有其他因数的数称为质数
<pre>
-- coding:utf-8 --
def sushu1(n):
a = set()
b = []
for i in range(2,n+1):
for j in range(2,i):
if i % j == 0:
a.add(i)
s = set(range(2,n+1)) - a
s = list(s)
for k in s:
b.append(str(k))
return ' '.join(b)
print sushu1(100)
</br>
def sushu2(n):
a = ''
for i in range(2,n+1):
flag = 1
for j in range(2,i):
if i % j == 0:
flag = 0
if flag == 1:
a += str(i) + ' '
return a
print sushu2(100)
</br>
def sushu3(n):
a = ''
for i in range(2,n):
for j in range(2,i):
if i % j == 0:
break
else:
a += str(i) + ' '
return a
print sushu3(100)
</br>
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
</pre>
<h5>5.给你一个整数a,数出a在二进制表示下1的个数,并输出</h5>
<pre>
-- coding:utf-8 --
def fun(a):
b = bin(a)
return b.count('1')
fun(8)
1
fun(16)
1
fun(7)
3
fun(255)
8
fun(254)
7
</pre>
<h5>6.给你一个整数组成的列表L,按照下列条件输出:</h5>
若L是升序排列的,则输出"UP";
若L是降序排列的,则输出"DOWN";
若L无序,则输出"WRONG"。
<pre>
-- coding:utf-8 --
def fun6(l):
result = ''
isUp = 1
isDown = 1
isSame = 1
for i in range(0,len(l)-1):
if l[i+1] < l[i]:
isUp = 0
isSame = 0
elif l[i+1] > l[i]:
isDown = 0
isSame = 0
if isSame == 1:
result = 'Same'
elif isUp == 1:
result = 'Up'
elif isDown == 1:
result = 'Down'
else:
result = 'Wrong'
return result
</br>
def fun66(l):
a = l[:]
a.sort()
b = a[:]
b.reverse()
if a == l:
print 'UP'
elif b == l :
print 'DOWN'
else:
print 'WRONG'
</br>
def fun666(l):
if sorted(l) == l:
print 'UP'
elif list(reversed(sorted(l))) == l :
print 'DOWN'
else:
print 'WROGR'
</br>
a = [2,3,3,4,8,9]
b = [8,7,6,6,5,4]
c = [2,6,3,4,5,8]
d = [2,2,2,2,2,2]
fun6(a)
'Up'
fun66(a)
UP
fun666(a)
UP
fun6(b)
'Down'
fun66(b)
DOWN
fun666(b)
DOWN
fun6(c)
'Wrong'
fun66(c)
WRONG
fun666(c)
WROGR
fun6(d)
'Same'
fun66(d)
UP
fun666(d)
UP
</pre>
<h5>7.给你一个整数列表L,判断L中是否存在相同的数字,
若存在,输出YES,否则输出NO。</h5>
<pre>
-- coding:utf-8 --
def fun7(l):
a = ''
for i in l:
if l.count(i) > 1:
a = 'YES'
else:
a = 'NO'
return a
</br>
def fun77(l):
if len(l) == len(set(l)):
print 'NO'
else:
print 'YES'
</br>
l = [2,3,6,8,5,4]
l1 = [5,3,6,2,1,3]
fun7(l)
'NO'
fun77(l)
NO
fun7(l1)
'YES'
fun77(l1)
YES
</pre>
<h5>8.利用递归写斐波那契数列的函数f(n)。</h5>
注:(斐波那契数列:1,1,2,3,5,8,13,21,34,55,·······)
解析:
f(1) = 1
f(2) = 1
f(3) = f(1) + f(2)
.....
f(n) = f(n-2) + f(n-1)
<pre>
-- coding:utf-8 --
def fun8(n):
if n == 1 or n == 2:
return 1
else:
return fun9(n-2)+fun9(n-1)
fun8(1)
1
fun8(2)
1
fun8(3)
2
fun8(4)
3
fun8(5)
5
fun8(6)
8
fun8(7)
13
fun8(8)
21
fun8(5)
5
</pre>
<h5>9.编写一个签名函数:</h5>
valid(text,chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
这个函数应当返回一个(或者是空的)字符串,它是只含有char字符的text的副本。
例如
valid('Barking!') #Returns 'B'
valid('KL754','0123456789') #Returns '754'
valid('BEAN','abcdefghijklmnopqrstuvwxyz') #Returns ''
该函数应该在6行左右完成,可以使用一个for循环和一个if语句。
<pre>
-- coding:utf-8 --
def valid(text,char='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'):
a = []
for i in text:
if i in char:
a.append(i)
return ''.join(a)
valid('Barking!')
'B'
valid('KL754','0123456789')
'754'
valid('BEAN','abcdefghijklmnopqrstuvwxyz')
''
</pre>
<h5>10.编写一个签名函数:</h5>
charcount(text)
这个函数应当返回一个带有28键的字典,28键是'a','b',·······,'z',
外加'whitespace'和'others'。对于text中每一个小写字符,如果字符是字母
,增加其对应的键的数量;如果该字符是空格,增加'whitespace'键的数量;
否则,增加'other'键的数量。
例如,如下调用:
stats= charcount('Exceedingly Edible')
将意味着,stats是一个带有如下内容的字典:
{'whitespase': 1, 'other': 0, 'a': 0,
'c': 1, 'b': 1, 'e': 5, 'd': 2, 'g': 1, 'f': 0, 'i': 2, 'h': 0, 'k': 0, 'j': 0,
'm': 0, 'l': 2, 'o': 0, 'n': 1, 'q': 0, 'p': 0, 's': 0, 'r': 0, 'u': 0, 't': 0,
'w': 0, 'v': 0, 'y': 1, 'x': 1, 'z': 0}
使用一个字典和一个for循环,应该可以用12行左右的代码完成
<pre>
-- coding:utf-8 --
def charcount(text):
a = {'whitespase': 0, 'others': 0,'a': 0,
'c': 0, 'b': 0, 'e': 0, 'd': 0, 'g': 0, 'f': 0, 'i': 0, 'h': 0, 'k': 0, 'j': 0,
'm': 0, 'l': 0, 'o': 0, 'n': 0, 'q': 0, 'p': 0, 's': 0, 'r': 0, 'u': 0, 't': 0,
'w': 0, 'v': 0, 'y': 0, 'x': 0, 'z': 0}
for i in text.lower():
if i in a.keys():
a[i] += 1
elif i == ' ':
a['whitespase'] += 1
else:
a['others'] += 1
return a
stats= charcount('Exceedingly Edible')
stats
{'whitespase': 1, 'others': 0, 'a': 0, 'c': 1, 'b': 1, 'e': 5, 'd': 2, 'g': 1, 'f': 0, 'i': 2, 'h': 0, 'k': 0, 'j': 0, 'm': 0, 'l': 2, 'o': 0, 'n': 1, 'q': 0, 'p': 0, 's': 0, 'r': 0, 'u': 0, 't': 0, 'w': 0, 'v': 0, 'y': 1, 'x': 1, 'z': 0}
</pre>