python中double是什么意思_什么是此代码而不是double for循环(python)的更快版本?...

When I hand this code in on a site (from my university) that corrects it, it is too long for its standards.

Here is the code:

def pangram(String):

import string

alfabet = list(string.ascii_lowercase)

interpunctie = string.punctuation + "’" + "123456789"

String = String.lower()

string_1 = ""

for char in String:

if not char in interpunctie:

string_1 += char

string_1 = string_1.replace(" ", "")

List = list(string_1)

List.sort()

list_2 = []

for index, char in enumerate(List):

if not List[index] == 0:

if not (char == List[index - 1]):

list_2.append(char)

return list_2 == alfabet

def venster(tekst):

pangram_gevonden = False

if pangram(tekst) == False:

return None

for lengte in range(26, len(tekst)):

if pangram_gevonden == True:

break

for n in range(0, len(tekst) - lengte):

if pangram(tekst[n:lengte+n]):

kortste_pangram = tekst[n:lengte+n]

pangram_gevonden = True

break

return kortste_pangram

So the first function (pangram) is fine and it determines whether or not a given string is a pangram: it contains all the letters of the alphabet at least once.

The second function checks whether or not the string(usually a longer tekst) is a pangram or not and if it is, it returns the shortest possible pangram within that tekst (even if that's not correct English). If there are two pangrams with the same length: the most left one is returned.

For this second function I used a double for loop: The first one determines the length of the string that's being checked (26 - len(string)) and the second one uses this length to go through the string at each possible point to check if it is a pangram. Once the shortest (and most left) pangram is found, it breaks out of both of the for loops.

However this (apparantly) still takes too long. So i wonder if anyone knew a faster way of tackling this second function. It doesn't necessarily have to be with a for loop.

Thanks in advance

Lucas

解决方案

Create a map {letter; int} and activecount counter.

Make two indexes left and right, set them in 0.

Move right index.

If l=s[right] is letter, increment value for map key l.

If value becomes non-zero - increment activecount.

Continue until activecount reaches 26

Now move left index.

If l=s[left] is letter, decrement value for map key l.

If value becomes zero - decrement activecount and stop.

Start moving right index again and so on.

Minimal difference between left and right while

activecount==26 corresponds to the shortest pangram.

Algorithm is linear.

Example code for string containing only lower letters from alphabet 'abcd'. Returns length of the shortest substring that contains all letters from abcd. Does not check for valid chars, is not thoroughly tested.

import string

def findpangram(s):

alfabet = list(string.ascii_lowercase)

map = dict(zip(alfabet, [0]*len(alfabet)))

left = 0

right = 0

ac = 0

minlen = 100000

while left < len(s):

while right < len(s):

l = s[right]

c = map[l]

map[l] = c + 1

right += 1

if c==0:

ac+=1

if ac == 4:

break

if ac < 4:

break

if right - left < minlen:

minlen = right - left

while left < right:

l = s[left]

c = map[l]

map[l] = c - 1

left += 1

if c==1:

ac-=1

break

if right - left + 2 < minlen:

minlen = right - left + 1

return minlen

print(findpangram("acacdbcca"))

你可能感兴趣的:(python中double是什么意思_什么是此代码而不是double for循环(python)的更快版本?...)