-
Notifications
You must be signed in to change notification settings - Fork 0
/
max_seq_debug.py
149 lines (116 loc) · 4.39 KB
/
max_seq_debug.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
142
143
144
145
146
147
148
149
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# max_seq.py
#
# Copyright 2019 Diserere <diserere@cooler>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
"""
DOCSTRING
"""
__version__ = '1.0.5'
#~ import argparse
def is_in_seq(charset, char, charset_len):
if char in charset:
print("seq: found")
found = True
elif len(charset) < charset_len:
charset += char
print("seq: added")
found = True
else:
print("seq: not found")
found = False
return charset, found
def save_max_substr(cur_substr, max_substr):
if len(cur_substr) >= len(max_substr): # Use ">" here to save first max substr of the same length instead of last one
print("-- Replace max [" + max_substr + "] to new [" + cur_substr + "]")
max_substr = cur_substr
return max_substr
def get_max_seq(string, charset_len):
import time
"""
Function to find max substring containing not more than 'charset_len'
symbols in 'string' string.
Returns last found substring of max length
"""
# if string is zero-length
if not len(string):
return ""
# start from beginning of string
p = 0
max_substr = ""
cur_substr = ""
charset = ""
while p <= len(string)-1 :
time.sleep(0.1)
print("...sleep ...")
while True:
print(" ...")
print("Curr p is: " + str(p))
cur_char = string[p]
print("Curr char is: " + cur_char)
print("Curr seq is: " + charset)
print("Curr substr is: " + cur_substr)
print("Max substr is: " + max_substr)
charset, found = is_in_seq(charset, cur_char, charset_len)
if found:
cur_substr += cur_char
print("Extend substr: " + cur_substr)
p += 1
break
else:
max_substr = save_max_substr(cur_substr, max_substr)
# flush charset
charset = ""
# flush current substr
cur_substr = ""
# rewind ptr to start of new sequence
while True:
cur_char = string[p]
print("Rewind: p is: %s; char is: %s; charset is: %s" % (str(p), cur_char, charset))
charset, found = is_in_seq(charset, cur_char, charset_len)
if not found:
# shift ptr fwd, clear registers, exit
p += 1
charset = ""
break
else:
p -= 1
break
max_substr = save_max_substr(cur_substr, max_substr)
print ("Ptr is in %s" % (str(p)))
return max_substr
def main(args):
import argparse
d_MAX_SEQ_LEN = 2
parser = argparse.ArgumentParser(
description='Python script for print max substring from given string containing not more than '+ str(d_MAX_SEQ_LEN) + ' symbols',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('str', type=str, metavar='STRING', default="", help='String to find a sequence')
parser.add_argument('-n', '--num', type=int, metavar='NUM', default=d_MAX_SEQ_LEN, help='Max. number of different symbols in substring')
aargs = parser.parse_args()
string = aargs.str
max_charset = aargs.num
max_substr = get_max_seq(string, max_charset)
print ("Last substring of max length of " + str(len(max_substr)) + " is:")
print ("[" + max_substr + "]")
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))