Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/Base' into Base
Browse files Browse the repository at this point in the history
# Conflicts:
#	Filter.py
#	map and reduce function.py
  • Loading branch information
a625687551 committed Jun 17, 2016
2 parents 61c135a + 345f861 commit cb0d8dc
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
48 changes: 48 additions & 0 deletions Filter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<<<<<<< HEAD
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

Expand Down Expand Up @@ -43,3 +44,50 @@ def is_palindrome(n):
return str(n)[::-1]==str(n)
output=filter(is_palindrome,range(1,10000))
print(list(output))
=======
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

' filter '

_author_='wangjianfeng'

def is_odd(n):
return n%2==0
print(list(filter(is_odd,[1,2,3,4,5,6,7,8,9,10])))

def not_empty(s):
return s and s.strip()
print(list(filter(not_empty,['a','b',None,'c',' '])))

#filter prime number
#define a odd number sequence
def _odd_iter():
n=1
while True:
n=n+2
yield n
#define a filter
def _not_divisible(n):
return lambda x:x%n>0
#define a generator
def primes():
yield 2
it=_odd_iter()#初始化序列
while True:
n=next(it)#返回序列的第一个数
yield n
it=filter(_not_divisible(n),it)#构造新数列
#打印1000以内的素数
for n in primes():
if n<100:
print(n)
else:
break

#过滤掉非回数
def is_palindrome(n):
return str(n)[::-1]==str(n)
output=filter(is_palindrome,range(1,10000))
print(list(output))
>>>>>>> origin/Base
45 changes: 45 additions & 0 deletions map and reduce function.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<<<<<<< HEAD
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

Expand Down Expand Up @@ -40,3 +41,47 @@ def str2float(s):
num=reduce(lambda x,y:x*10+y,map(char2num,(a+b)))
return num/(10**len(b))
print(str2float('123.456'))
=======
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

' map/reduce function'

_author_='wangjianfeng'

#map function can do list's element
print(list(map(str,[1,2,3,4,5,6,7,8,9])))

#reduce function
#reduce函数可以用传给reduce的func()(必须是一个二元操作函数)先对集合中的第1,2数据进行操作,
#得到的结果在与第三个数据用func()函数运算,最后得到一个结果
from functools import reduce
def add(x,y):
return x+y
print(reduce(add,[1,2,3,4,5,6]))
#[1,3,5,7,9]become 13579
def fn(x,y):
return x*10+y
print(reduce(fn,[1,3,5,7,9]))

#endlish name normalize
def normalize(name):
return name.capitalize()
L1=['adam','LISA','barT','WANGJIANFENG']
print(list(map(normalize,L1)))

#接收一个list并且求积

def prod(L):
return reduce(lambda x,y:x*y,L)
print('3*5*7*9=',prod([3,5,7,9]))
#using str2float function make string '123.456'to 123.456
#sn=s.split('.')[0]+s.split('.')[-1]
def char2num(s):
return {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}[s]
def str2float(s):
a,b=s.split('.')
num=reduce(lambda x,y:x*10+y,map(char2num,(a+b)))
return num/(10**len(b))
print(str2float('123.456'))
>>>>>>> origin/Base

0 comments on commit cb0d8dc

Please sign in to comment.