/
ch-1.py
99 lines (86 loc) · 2.62 KB
/
ch-1.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# https://theweeklychallenge.org/blog/perl-weekly-challenge-230/
"""
Task 1: Separate Digits
Submitted by: Mohammad S Anwar
You are given an array of positive integers.
Write a script to separate the given array into single digits.
Example 1
Input: @ints = (1, 34, 5, 6)
Output: (1, 3, 4, 5, 6)
Example 2
Input: @ints = (1, 24, 51, 60)
Output: (1, 2, 4, 5, 1, 6, 0)
"""
### function solution
def intToDigLst(i):
lst = []
while i // 10 > 0:
lst.insert(0, i % 10)
i //= 10
lst.insert(0,i)
return lst
"""
for i in (480,8080,8089):
print(i,intToDigLst(i))
"""
def intLstToDigLst(lst):
dlst = []
for i in lst:
dlst += intToDigLst(i)
return dlst
for ipt,opt in (
([1, 34, 5, 6], [1, 3, 4, 5, 6]),
([1, 24, 51, 60], [1, 2, 4, 5, 1, 6, 0])
):
print(intLstToDigLst(ipt)==opt)
#print(ipt,intLstToDigLst(ipt))
### generator solution
def digFromInt(i): # output need to be reversed
while i // 10 > 0:
yield i % 10
i //= 10
yield i
"""
for i in (480,8080,8089):
print(i,[ d for d in digFromInt(i)])
"""
def digFromIntLst(lst):
for i in lst:
for d in reversed([ d for d in digFromInt(i)]): # reserved() function work with iterable but not generator, need list comprehension
yield d
for ipt,opt in (
([1, 34, 5, 6], [1, 3, 4, 5, 6]),
([1, 24, 51, 60], [1, 2, 4, 5, 1, 6, 0])
):
print([ d for d in digFromIntLst(ipt)]==opt)
### class solution, __iter__ to call generator method, basically put generator solution into a class
class DigFromIntLst():
def __init__(self,lst):
self.lst = lst
self.i = 0
def generator(self,i):
while i // 10 > 0:
yield i % 10
i //= 10
yield i
def __iter__(self):
for i in self.lst:
for d in reversed([ d for d in self.generator(i)]):
yield d
for ipt,opt in (
([1, 34, 5, 6], [1, 3, 4, 5, 6]),
([1, 24, 51, 60], [1, 2, 4, 5, 1, 6, 0])
):
print([ d for d in DigFromIntLst(ipt)]==opt)
### lazy solution - use converting type to/from int/str/bytes
def intTupTodigTup(itup):
dlst = []
for i in itup:
for cp in bytes(str(i),"ascii"): # convert int to str, then convert str to list of codepoint
dlst.append(int(chr(cp))) # convert codepoint to str, then str to int, and append it to array
return tuple(dlst)
for ipt,opt in ( # use tuple instead of list so we can simply copy and past sample data
((1, 34, 5, 6), (1, 3, 4, 5, 6)),
((1, 24, 51, 60), (1, 2, 4, 5, 1, 6, 0))
):
print(intTupTodigTup(ipt)==opt)