forked from julycoding/The-Art-Of-Programming-By-July-2nd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.1:字符的移动.py
141 lines (119 loc) · 3.34 KB
/
1.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# -*- coding: utf-8 -*-
import copy
# 整体函数接口说明:
#
# Args:
# s: 给定需要移位的list
# m: 需要移动的位数
#
# Returns:
# 无。直接修改传入的list
#
# Author:
# Jasonwbw([email protected])
##########################################################################
# Tools
def left_shift_one(s, i, j):
'''s从i(包含)到j(不包含),左移一位
Author: Jasonwbw([email protected])
Editor:
Args:
s : 给定需要移位的list
i : 索引位置i
j : 索引位置j
'''
t = s[i]
for k in xrange(i, j - 1):
s[k] = s[k + 1]
s[j - 1] = t
##########################################################################
# 思路零
# python语言基础思路
def simple_shift(s, m):
'''python对于list类型的s,最适合的暴力方法是直接选取两部分的字符串直接拼接
Author:Jasonwbw([email protected])
Editor:
'''
if m < 0:
raise Exception('m is less than 0')
if len(s) <= m:
m = m % len(s)
t = copy.copy(s)
del s[:]
s += t[m:] + t[:m]
##########################################################################
# 思路一
# see https://github.com/julycoding/The-Art-Of-Programming-By-July/blob/master/ebook/zh/01.0.md#%E6%80%9D%E8%B7%AF%E4%B8%80%E6%9A%B4%E5%8A%9B%E7%A7%BB%E4%BD%8D%E6%B3%95
def left_shift(s, m):
'''循环m次左移1位
Author: Jasonwbw([email protected])
Editor:
'''
if m < 0:
raise Exception('m is less than 0')
else:
m = m % len(s)
for i in xrange(m):
left_shift_one(s, 0, len(s))
##########################################################################
# 思路二
# see https://github.com/julycoding/The-Art-Of-Programming-By-July/blob/master/ebook/zh/01.0.md#%E6%80%9D%E8%B7%AF%E4%BA%8C%E4%B8%89%E6%AD%A5%E7%BF%BB%E8%BD%AC%E6%B3%95
def invert(s, start, end):
'''Invert the list s from start to end(contained).
Author: Jasonwbw([email protected])
Editor:
Args:
s : 要翻转的list
start : 翻转的起始位置
end : 翻转的结束位置(包含)
'''
n = end - start + 1
for i in xrange(n / 2):
s[start + i], s[end - i] = s[end - i], s[start +i]
def invert_solution(s, m):
'''
Author: Jasonwbw([email protected])
Editor:
'''
n = len(s)
invert(s, 0, m - 1)
invert(s, m, n - 1)
invert(s, 0, n - 1)
##########################################################################
if __name__ == '__main__':
a1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
a2 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
a3 = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
a0_1 = copy.copy(a1)
a0_2 = copy.copy(a2)
a0_3 = copy.copy(a3)
simple_shift(a0_1, 3)
simple_shift(a0_2, 3)
simple_shift(a0_3, 3)
print 'simple_shift:'
print a0_1
print a0_2
print a0_3
print
a1_1 = copy.copy(a1)
a1_2 = copy.copy(a2)
a1_3 = copy.copy(a3)
left_shift(a1_1, 3)
left_shift(a1_2, 3)
left_shift(a1_3, 3)
print 'left_shift:'
print a1_1
print a1_2
print a1_3
print
a2_1 = copy.copy(a1)
a2_2 = copy.copy(a2)
a2_3 = copy.copy(a3)
invert_solution(a2_1, 3)
invert_solution(a2_2, 3)
invert_solution(a2_3, 3)
print 'invert_solution:'
print a2_1
print a2_2
print a2_3
print