Created
March 20, 2019 13:02
-
-
Save panyan928/7509497a4c821268dc66afdfb1700ca2 to your computer and use it in GitHub Desktop.
snake.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding:utf-8 -*- | |
import collections | |
line = 'xxsrR^Aas' | |
dic = collections.Counter(line) | |
print(dic) | |
start = 0 | |
for i in range(65, 91): | |
if chr(i) in dic.keys() and chr(i+32) in dic.keys(): | |
start = i | |
break | |
if start == 0: | |
print("Not Found") | |
exit(0) | |
while True: | |
end1 = start # now longest | |
end2, start2 = 0, 0 | |
for i in range(start + 1, 91): | |
if chr(i) in dic.keys() and chr(i+32) in dic.keys(): | |
if i == end1 + 1: | |
end1 += 1 | |
elif start2 == 0: | |
end2, start2 = i, i | |
elif i == end2 + 1: | |
end2 += 1 | |
else: | |
if end2 - start2 > end1 - start: | |
start, end1 = start2, end2 | |
if end2 - start2 == end1 - start: | |
if min(line.index( chr(start2) ), line.index( chr(start2+32) ) ) < \ | |
min(line.index( chr(start) ), line.index( chr(start+32) ) ): | |
start, end1 = start2, end2 | |
start2, end2 = i, i | |
if end2 - start2 > end1 - start: | |
start, end1 = start2, end2 | |
if start2 != 0 and end2 - start2 == end1 - start: | |
if min(line.index(chr(start2)), line.index(chr(start2 + 32))) < \ | |
min(line.index(chr(start)), line.index(chr(start + 32))): | |
start, end1 = start2, end2 | |
res = '' | |
for i in range(start, end1+1): | |
line.strip(chr(i)) | |
line.strip(chr(i + 32)) | |
dic[chr(i)] -= 1 | |
if dic[chr(i)] == 0: | |
dic.pop(chr(i)) | |
dic[chr(i+32)] -= 1 | |
if dic[chr(i+32)] == 0: | |
dic.pop(chr(i+32)) | |
res += (chr(i) + chr(i+32)) | |
print(res) | |
print(dic) | |
start = 0 | |
for i in range(65, 91): | |
if chr(i) in dic.keys() and chr(i+32) in dic.keys(): | |
start = i | |
break | |
if start == 0: | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment