示例#1
0
	def __readGrid(self, textLines):
		"""Parse the iterable textLines into a grid"""
		colsIndex = None
		for line in textLines:
			line = line.split("#",1)[0].rstrip() # We don't take in account the comments and whitespaces at the end
			if len(line) == 0: continue          # If the line is empty, we can skip it

			"""Parse the first line"""
			if colsIndex == None:
				colsIndex = [(0,len(line.split("_",1)[0])-1)] # give the width of the first column of the lines
				if line[0] != " " : 
					raise ValueError("The first line should start with white spaces.")
				for char, nb in ((label, sum(1 for _ in group)) for label, group in gb(line)):
					if not char in " _":
						raise ValueError("The first line should only contain white spaces and underscores.")
					if char == " " and nb > 1 and len(colsIndex) > 1:
						raise ValueError("The column separator between col "+str(len(colsIndex)-1)+" and col "+str(len(colsIndex))+" is too wide.")
					if char == "_":
						colsIndex.append(((colsIndex[-1][1]+1), (nb+colsIndex[-1][1]+1)))
				self.__l = len(colsIndex)-1
				self.__values["v"] = [-1]*self.__l
				continue

			"""Prepare the parsing of other lines"""
			"""try:
				splitted_line = [line[x:y] for x,y in colsIndex]
			except Exception as e:
				raise e"""

			"""Parse the last line"""
			if line[colsIndex[0][1]] != "|": 
				self.__values["v"] = [self.__strToVal(line[x:y],len(self.__barrier["v"])) for x,y in colsIndex[1:]]

				"""Parse all the other lines"""
			else : 
				barrier = {"v":[], "h":[]}
				self.__values["h"].append(self.__strToVal(line[0:colsIndex[0][1]], len(colsIndex)-1))
				for x,y in colsIndex[1:] :
					s = line[x:y]
					if not (s[0] in " _") or len(list(gb(s))) > 1 :
						raise ValueError("La grille a une erreur ligne "+str(len(self.__values["h"])))

					if s[0] == '_':
						barrier["h"].append(True)
					else :
						barrier["h"].append(False)

					if line[y] == '|':
						barrier["v"].append(True)
					else :
						barrier["v"].append(False)

				self.__barrier["h"].append(barrier["h"])
				barrier["v"].pop()
				self.__barrier["v"].append(barrier["v"])

		self.__barrier["h"].pop()
		self.__h = len(self.__barrier["v"])
def split_sent(t):
    sent_list = []
    for sent in t:
        k = [list(sent) for i, sent in gb(sent, lambda item: item == '.')]
        for i in k:
            if len(i) > 1:
                sent_list.append(i)
    return (sent_list)
示例#3
0
def split_char(string_list):
    for i,j in gb(string_list,key=ig(0)):
        print(i)
        for k in j:
            print(k)
示例#4
0
#! /usr/bin/env python3
#-*- coding:utf-8 -*-

from itertools import groupby as gb
for key, group in gb('AAAABBBBCCCCDDDD'):
    print(key, list(group))
from itertools import groupby as gb
import re

S = """==Heading1==
<test>
some text here
</test> 

==Heading2==
<test>
even more text
</test>"""

GROUP = gb(S.splitlines(), lambda x: re.match("^==\w+==$$", x))
for k, g in GROUP:
    header = list(g)[0][2:-2]
    text = "\n".join(list(GROUP.next()[1])[1:-1])
   
    print "header".center(20, '-')
    print header
    print "text".center(20, '-')
    print text
示例#6
0
import sys
from fractions import gcd
from itertools import groupby as gb
from itertools import permutations as perm
from collections import Counter as C
from collections import defaultdict as dd
sys.setrecursionlimit(10**5)

s = input()
s = sorted(s)
g = gb(s)
l = len(s)

res = l * (l - 1) // 2 + 1
for k, v in g:
    p = len(list(v))
    res -= p * (p - 1) // 2
print(res)
示例#7
0
Constraints

All the characters of  denote integers between  and .


Sample Input

1222311
Sample Output

(1, 1) (3, 2) (1, 3) (2, 1)
Explanation

First, the character  occurs only once. It is replaced by . Then the character  occurs three times, and it is replaced by  and so on.

Also, note the single space within each compression and between the compressions.
"""
from itertools import groupby as gb
s = input()
if len(s) not in range(1,(10**4)+1):
    pass
else:
    groups = []
    uniquekeys = []
    for k, g in gb(s, lambda x:x):
        groups.append(list(g))    # Store group iterator as a list
        uniquekeys.append(k)

    for i in range(0,len(uniquekeys)):
        print(tuple([len(groups[i]),int(uniquekeys[i])]),end=" ")
示例#8
0
import sys
from fractions import gcd
from itertools import groupby as gb
from itertools import permutations as perm
from collections import Counter as C
from collections import defaultdict as dd
sys.setrecursionlimit(10**5)

n = int(input())
a = list(map(int, input().split()))
if a[0] != 0 or a.count(0) > 1:
    print(0)
    exit()
a = sorted(a)
g = gb(a)
l = []
cnt = []
for k, v in g:
    l.append(k)
    ln = len(list(v))
    cnt.append(ln)
if l != list(range(len(l))):
    print(0)
    exit()
res = 1
MOD = 10**9 + 7
for i in range(1, len(cnt)):
    res *= pow((pow(2, cnt[i - 1], MOD) - 1) % MOD, cnt[i], MOD)
    res *= pow(2, cnt[i] * (cnt[i] - 1) // 2, MOD)
    res %= MOD
print(res)