-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdo_reduce.py
More file actions
83 lines (67 loc) · 1.46 KB
/
do_reduce.py
File metadata and controls
83 lines (67 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from functools import reduce
CHAR_TO_INT ={
"0":0,
"1":1,
"2":2,
"3":3,
"4":4,
"5":5,
"6":6,
"7":7,
"8":8,
"9":9
}
def str2int(s):
ints=map(lambda ch: CHAR_TO_INT[ch],s)
return reduce(lambda x,y:x*10+y,ints)
print(str2int("0"))
print(str2int("12300"))
print(str2int("0012345"))
CHAR_TO_FLOAT ={
"0":0,
"1":1,
"2":2,
"3":3,
"4":4,
"5":5,
"6":6,
"7":7,
"8":8,
"9":9,
".":-1
}
def str2float(s):
nums=map(lambda ch: CHAR_TO_FLOAT[ch],s)
point=0
def to_float(f,n):
nonlocal point
if n==-1:
point=1
return f
if point == 0:
return f*10+n
else:
point = point*10
return f+n/point
return reduce(to_float,nums,0.0)
print(str2float("0"))
print(str2float("123.456"))
print(str2float("123.45600"))
print(str2float("0.1234"))
print(str2float(".1234"))
print(str2float("120.0034"))
def normalize(name):
return name[0:1].upper() + name[1:].lower()
L1=["adam", "LISA", "barT"]
print(list(map(normalize,L1)))
def prod(L):
return reduce(lambda x,y: x*y, L)
print("3*5*7*9= ", prod([3,5,7,9]))
def str22float(s):
def str2list(s):
return {"0":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9,".":"."}[s]
ss=list(map(str2list,s))
l=len(ss)-1-ss.index(".")
ss.remove(".")
return reduce(lambda x,y: x*10+y,ss)/10.0**l
print(str22float("123.4567"))