def count_words(sentence): n = len(sentence) # parsing s = sentence[0:n].lower() i = 0 while i < n: if not isalnum(s[i]) and not isspace(s[i]): if i == 0: s = s[i + 1:] elif i == n - 1: s = s[0:i] elif s[i] == '\'' and isalpha(s[i - 1]) and isalpha(s[i + 1]): i += 1 else: s = s[:i] + " " + s[i + 1:] elif isspace(s[i]): if isspace(s[i - 1]): # there can only be one space between words s = s[:i] + s[i + 1:] elif isalnum(s[i - 1]) and s[i] != ' ': # this will make it easier to split the lines s = s[:i] + " " + s[i + 1:] else: i += 1 else: i += 1 n = len(s) parsed_l = s.strip().split(' ') # searching counts = dict() for i in parsed_l: counts[i] = counts.get(i, 0) + 1 return counts
def brailleify(rel): '''Turn 1.3.45 or AKPR54633-1PHI into UEB''' # FIXME: if we do this at all it should be in braille.py, and we # probably shouldn't be trying to do liblouis-level translation at # all. ret = u'' digits = False for c in rel: if ASCII.isdigit(c): if not digits: ret += u'⠼' digits = True c = ueb_number_mapping[int(c)] ret += alpha_to_unicode(c) elif c == '.': ret += u'⠲' elif ASCII.isalpha(c): if digits: # UEB 'guidelines for technical material' suggests capital # letter marker, not letter sign ret += u'⠠' digits = False ret += alpha_to_unicode(c) else: # e.g. dash in serial digits = False ret += alpha_to_unicode(c) return ret
def test_board_shortname_in_correct_format(self): """ Board shortnames should only represent themselves in lowercase, alphanumeric characters. """ board = create_board("Bacon", "b") self.assertTrue(islower(board.shortname)) self.assertTrue(isalpha(board.shortname))
def brute_force(msg, iv): scores = {} for k in range(0, 10000): key = string_to_hex("{}{:04d}".format("0" * 12, k)) plaintext = decrypt(key, msg, iv) tmp_score = 0 for i in range(0, len(plaintext)): if chr(plaintext[i]).isdigit(): tmp_score += 10 elif isalpha(plaintext[i]): tmp_score += 2 scores[key] = tmp_score best_score = 0 best_key = "" for k in scores: if scores[k] > best_score: best_score = scores[k] best_key = k logging.debug("Best key score: {}".format(best_score)) pin = binascii.unhexlify(best_key[24:32]) logging.info("(Probably) found the correct ...") logging.info(" encryption key: {}".format(best_key)) logging.info(" pin: {}".format(int((pin))))
def compute_score_en(self, s): x = DictWithDefault(lambda: 0) nspace = 0 nalpha = 0 nbad = 0 n = len(s) for c in s: if isspace(c) or c == '_': nspace += 1 elif isalpha(c): nalpha += 1 x[c | 32] += 1 elif not isgoodchar(c): nbad += 1 tb = sorted(x.values()) tb.reverse() score = 0 if nalpha == 0: score += 1000 else: for c, v in self.freq_en.items(): score += (v - x[c] / nalpha)**2 #score += (nspace / n - self.freq_sorted[0])**2 score += (300 * nbad / n)**2 return Attributize(score=score, nbad=nbad)
def prefix_tab(string): if string.strip() == '': return 0 i = 0 s = string[i] while not isalpha(s) and i < len(string): s = string[i] i += 1 return i
def 分開音標(self, 連做伙音標): 頂一个音 = None 音標結果 = '' for 音 in 連做伙音標: if 頂一个音 != None and isalpha(音) and isdigit(頂一个音): 音標結果 += ' ' 音標結果 += 音 頂一个音 = 音 return 音標結果
def prefix_tab(string): if string.strip() == "": return 0 i = 0 s = string[i] while not isalpha(s) and i < len(string): s = string[i] i += 1 return i
def ShowAscii(char, code): if ascii.isalpha(code): print char, 'is an ascii alphabeta' elif ascii.isdigit(code): print char, 'is an ascii digital' elif ascii.ispunct(code): print char, 'is an ascii punctuation' else: print char, 'is an ascii code(not alphabeta, number or punctuation)'
def solution(str1, str2): dict1 = dict() dict2 = dict() # 합집합 uni = 0 # 교집합 inter = 0 for i in range(len(str1) - 1): word1 = str1[i] word2 = str1[i + 1] if isalpha(word1) and isalpha(word2): word = word1.upper() + word2.upper() if word in dict1: dict1[word] += 1 else: dict1[word] = 1 for i in range(len(str2) - 1): word1 = str2[i] word2 = str2[i + 1] if isalpha(word1) and isalpha(word2): word = word1.upper() + word2.upper() if word in dict2: dict2[word] += 1 else: dict2[word] = 1 for i in dict1: if i in dict2: inter += min(dict1[i], dict2[i]) uni += max(dict1[i], dict2[i]) else: uni += dict1[i] for i in dict2: if i in dict1: continue else: uni += dict2[i] if uni == 0 and inter == 0: return 65536 return int(float(inter / uni * 65536))
def parse(source, dest): with open(source, 'rb') as f: res = bytes([c if isalpha(c) or c == 46 else 32 for c in f.read()]).decode('utf-8') word_list = [s.split(' ') for s in res.lower().split('.')] text = '\n'.join( [' '.join(c for c in x if c != '') for x in word_list]) with open(dest, 'w') as f: f.write(text)
def splitStringByLen(text, Len): # 其中使用\n分割,因此原来的\n将会被替换,一个中文大致顶三个英文的宽度 text = text.replace('\n', '.') (myText, nLen) = ('', 0) for s in text: myText += s nLen += 3 if isalpha(s) else 1 if nLen >= (Len - 1): myText += '\n' nLen = 0 return myText
def reverse_upper_low(the_str): new_str='' for d in the_str: if isalpha(d): if isupper(d): new_str+=d.lower() else: new_str+=d.upper() else: new_str+=d print new_str
def validate_collection_name(collection_name): '''Return true if the given name is a valid collection name :param collection_name: :return: bool ''' if not collection_name: raise InvalidCollectionName('Collection name must not be empty.') if re.search('\W+', collection_name): raise InvalidCollectionName( 'Invalid collection name %s. Allowed characters: "%s"' % (collection_name, collection_allowed_chars)) if not isalpha(collection_name[0]): raise InvalidCollectionName('The first character must be a letter.')
def create_from_user_cmd(cls, card_cmd_str): '''Creates a card instance based on user input string''' # Determine rank if rank is double digit if len(card_cmd_str) == 3: rank = card_cmd_str[0:2] suit = card_cmd_str[2] else: if isalpha(card_cmd_str[0]): rank = FACE_VALS[card_cmd_str[0]] print(rank) else: rank = card_cmd_str[0] suit = card_cmd_str[1] return cls(int(rank), suit)
def validate_collection_name(collection_name, collections={}, new=True): '''Return true if the given name is a valid collection name :param collection_name: :return: bool ''' if not collection_name: raise InvalidCollectionName('Collection name must not be empty.') if not re.search('^[a-zA-Z0-9]+$', collection_name): raise InvalidCollectionName('Invalid collection name %s. Allowed characters: "%s"' % (collection_name, collection_allowed_chars)) if not isalpha(collection_name[0]): raise InvalidCollectionName('The first character must be a letter.') if new: for collection in collections: if collection.lower() == collection_name.lower(): raise InvalidCollectionName('Collection %s already exists' % collection_name) elif collection_name not in collections: raise InvalidCollectionName('Collection %s does not exist' % collection_name)
def shorten(sentence, n): dic = {} for c in sentence: if isalpha(c): dic[c] = dic.get(c, 0) + 1 chars_to_replace = set() tuples_should_be_replace = sorted(dic.items(), key=lambda x: (x[1], x[0]))[-n:] for (k, v) in (tuples_should_be_replace): chars_to_replace.add(k) print(chars_to_replace) res = "" for c in sentence: if not c in chars_to_replace: res += c return res
def toColumnLine(c, l): if ascii.isalpha(c) == False: print("invalid column") return -1, -1 if ascii.isupper(c): cColumn = c.lower() else: cColumn = c column = ord(cColumn) - ord('a') if column < 0 or column > 7: print("invalid column") return -1, -1 if ascii.isdigit(l) == False: print("invalid line") return -1, -1 line = int(l) - 1 line = 7 - line if line < 0 or line > 7: print("invalid line") return -1, -1 return column, line
def toColumnLine(c, l): if ascii.isalpha(c) == False: print "invalid column" return -1,-1 if ascii.isupper(c): cColumn = string.lower(c) else: cColumn = c column = ord(cColumn) - ord('a') if column < 0 or column > 7 : print "invalid column" return -1,-1 if ascii.isdigit(l) == False: print "invalid line" return -1,-1 line = int(l) - 1 line = 7 - line if line < 0 or line > 7: print "invalid line" return -1,-1 return column, line
def get_bulletin_base_name(bulletin_id): bulletins = cavedb.models.Bulletin.objects.filter(id=bulletin_id) if bulletins.count() == 0: raise Http404 base = '' for char in bulletins[0].short_name.lower().encode('ascii'): if isalpha(char): base += chr(char) else: base += '_' if base == '': base = 'bulletin_%s' % (bulletin_id) mtime = bulletins[0].get_bulletin_mod_date() if mtime != None: disp_date = strftime("%Y%m%d-%H%M%S", mtime) else: disp_date = "UNKNOWN" return '%s_%s' % (base, disp_date)
def get_bulletin_base_name(bulletin_id): bulletins = Bulletin.objects.filter(id=bulletin_id) if (bulletins.count() == 0): raise Http404 base = '' for c in bulletins[0].short_name.lower().encode('ascii'): if (isalpha(c)): base += c else: base += '_' if (base == ''): base = 'bulletin_%s' % (bulletin_id) mtime = bulletins[0].get_bulletin_mod_date() if (mtime != None): disp_date = strftime("%Y%m%d-%H%M%S", mtime) else: disp_date = "UNKNOWN" return '%s_%s' % (base, disp_date)
# Descent # if i % skip == 0: # mixable.append(fullMixable.pop()) parts = [] if len(line) > 0: for token in doc: if mixable.count(token.pos_) > 0: parts.append([token.pos_, token.text[0].isupper()]) addWordToPool(token.text.lower(), token.pos_) else: parts.append([token.text, None]) structure.append(parts) # Word pools are shuffled to mix up mixable words for part in wordPool: random.shuffle(wordPool[part]) # Words are re-inserted into the poem structure, with non-mixable words retaining their order and mixable # words being taken out of their respective word pools for line in structure: newLine = "" for part in line: if not part[1] is None: word = wordPool[part[0]].pop() if part[1]: word = word.capitalize() newLine += " " + word else: newLine += (" " if isalpha(part[0][0]) else "") + part[0] print(newLine.strip())
def enter_name(self, withName=True): enter_name = True SPACEHEIGHT = 80 # indice para los espacios de texto SPACEWIDTH = 150 name = '' scores = db.get_all_score() while enter_name: events = pg.event.get() for event in events: if event.type == pg.KEYDOWN and withName: print(pg.key.name(event.key)) # condicional para limitar 3 caracteres if len(name) <= 2 and isalpha(pg.key.name(event.key)): name += (pg.key.name(event.key)) if event.type == pg.QUIT: pg.quit() sys.exit() self.pantalla.blit(self.bg4, (0, 0)) # self.pantalla.fill((11, 44, 94)) # condicional score if withName: titulo_tabla = create_font( "Ingresa 3 iniciales y pulsa (Enter)", 25, (255, 255, 255)) else: titulo_tabla = create_font("Pulsa (ESC) para salir", 20, (255, 255, 255)) SURF.blit(titulo_tabla, ((GAME_DIMENSIONS[0] - titulo_tabla.get_width()) / 2, titulo_tabla.get_rect().centery)) text_name = create_font(name, 30, (255, 255, 255)) # rectangulo para el ingreso de las iniciales # text_name_rect = text_name.get_rect() if withName: SURF.blit(text_name, ((GAME_DIMENSIONS[0] - text_name.get_width()) / 2, SPACEHEIGHT)) pg.draw.rect(self.pantalla, (255, 255, 0), ((GAME_DIMENSIONS[0] - text_name.get_width()) / 2, SPACEHEIGHT + 3, 49, 30), width=1) # witdth ancho de linea # tabla de puntajes spaceproduct = 1.2 min = len(scores) if len(scores) < 10 else 10 titulo_puesto = create_font("Puesto", 30, (255, 255, 255)) SURF.blit(titulo_puesto, ((GAME_DIMENSIONS[0] - titulo_puesto.get_width()) / 2 - SPACEWIDTH, titulo_tabla.get_rect().centery + SPACEHEIGHT * spaceproduct)) titulo_nombre = create_font("Nombre", 30, (255, 255, 255)) SURF.blit( titulo_nombre, ((GAME_DIMENSIONS[0] - titulo_nombre.get_width()) / 2, titulo_tabla.get_rect().centery + SPACEHEIGHT * spaceproduct)) titulo_score = create_font("Score", 30, (255, 255, 255)) SURF.blit(titulo_score, ((GAME_DIMENSIONS[0] - titulo_score.get_width()) / 2 + SPACEWIDTH, titulo_tabla.get_rect().centery + SPACEHEIGHT * spaceproduct)) # tabla cenrada spaceproduct = 1.5 # indice de arranque for i in range(min): spaceproduct += 0.5 #interlineado de la tabla puntaje_puesto = create_font(str(i + 1), 26, (255, 255, 255)) SURF.blit( puntaje_puesto, ((GAME_DIMENSIONS[0] - titulo_puesto.get_width()) / 2 - SPACEWIDTH + (titulo_puesto.get_width() - puntaje_puesto.get_width()) / 2, text_name.get_rect().centery + SPACEHEIGHT * spaceproduct)) puntaje_nombre = create_font(scores[i][1], 26, (255, 255, 255)) SURF.blit( puntaje_nombre, ((GAME_DIMENSIONS[0] - titulo_nombre.get_width()) / 2 + (titulo_nombre.get_width() - puntaje_nombre.get_width()) / 2, text_name.get_rect().centery + SPACEHEIGHT * spaceproduct)) puntaje_score = create_font(str(scores[i][2]), 26, (255, 255, 255)) SURF.blit(puntaje_score, ( (GAME_DIMENSIONS[0] - titulo_score.get_width()) / 2 + SPACEWIDTH + (titulo_score.get_width() - puntaje_score.get_width()) / 2, text_name.get_rect().centery + SPACEHEIGHT * spaceproduct)) tecla_pulsada = pg.key.get_pressed() if tecla_pulsada[pg.K_RETURN] and withName: db.insert_score(name, self.puntosAcumulados) enter_name = False if tecla_pulsada[pg.K_ESCAPE] and not withName: enter_name = False pg.display.flip()
def validate(self, attrs): fname = CustomUser.objects.filter(first_name=attrs.get('first_name')) lname = CustomUser.objects.filter(last_name=attrs.get('last_name')) if not isalpha(fname) and not isalpha(lname): raise ValidationError('Name Fields can not have digits') return attrs
import sys from collections import deque from curses.ascii import isalpha string = list(sys.stdin.readline().rstrip()) stack = deque() res = [] high = ["*", "/"] low = ["+", "-"] for i in range(len(string)): if isalpha(string[i]): res.append(string[i]) else: if string[i] in high: while stack and stack[-1] in high: res.append(stack.pop()) stack.append(string[i]) elif string[i] in low: while stack and (stack[-1] in high or stack[-1] in low): res.append(stack.pop()) stack.append(string[i]) elif string[i] == "(": stack.append(string[i]) elif string[i] == ")": while stack and stack[-1] != "(": res.append(stack.pop()) stack.pop() while stack: temp = stack.pop()
def isword(ch: int): return isalpha(ch)
def main(): fp = open("Data_pointers") fo = open("Data_pointers_out", "w") current_t = 0 prev_t = 0 lines = fp.readlines() ii = 0 while ii < len(lines): line = lines[ii] if line.strip() == "" or "#" in line or "/*" in line or "*/" in line: ii += 1 continue if line[0] != "\t": parts = line.split() current = parts[-1].lstrip("*").rstrip(";") prev_t = 0 ii += 1 fo.write('fprintf(fp,"%ld\\n",ctx->' + current + ");\n") continue current_t - 0 i = 0 s = line[i] while not isalpha(s): s = line[i] i += 1 current_t = i tmp = current.replace(".", "->") parts = tmp.split("->") if parts[-1] == "": parts = parts[: len(parts) - 1] # print current, line.strip(),parts if not (current_t > prev_t) and len(parts) > current_t: # print current_t diff = parts[(len(parts) - current_t) * -1] # print diff pos = current.rfind(diff) if current[pos - 1] == ">": current = current[: pos - 2] else: current = current[:pos] parts = line.split() if "," in parts[-1]: parts1 = parts[-1].split(",") pcurrent = current for p in parts1: if pcurrent[-1] != ".": current = pcurrent + "->" + p.rstrip(";").lstrip("*") else: current = pcurrent + p.rstrip(";").lstrip("*") if current[-1] != ".": # fo.write(current+'\n') fo.write('fprintf(fp,"%ld\\n",ctx->' + current + ");\n") # print current else: if current[-1] != ".": current = current + "->" + parts[-1].rstrip(";").lstrip("*") else: current = current + parts[-1].rstrip(";").lstrip("*") if current[-1] != ".": if ii < len(lines) - 1 and prefix_tab(lines[ii]) < prefix_tab(lines[ii + 1]): # fo.write(current+'\n') fo.write('fprintf(fp,"%ld\\n",ctx->' + current + ");\n") # print current elif ii > len(lines) - 1: # fo.write(current+'\n') fo.write('fprintf(fp,"%ld\\n",ctx->' + current + ");\n") # print current prev_t = current_t ii += 1
def makeTarget(wordFile="words.txt"): STATIC_ROOT = settings.STATIC_ROOT + "target/words/" offensive1 = open(STATIC_ROOT + "offensive.1").readlines() offensive2 = open(STATIC_ROOT + "offensive.2").readlines() profane1 = open(STATIC_ROOT + "profane.1").readlines() profane3 = open(STATIC_ROOT + "profane.3").readlines() banned = offensive1 + offensive2 + profane1 + profane3 with open(STATIC_ROOT + wordFile, 'r', errors='replace') as f: words = [ w[:-1] for w in f.readlines() if all(isalpha(c) for c in w[:-1]) and w == w.lower() and len(w) > 4 and len(w) < 11 and w not in banned ] words_9 = [w for w in words if len(w) == 9] sorted_words_9 = sorted([''.join(sorted(w)) for w in words_9]) sorted_words_9_nodups = [] previous_word = sorted_words_9[0] duplicate = False for s in sorted_words_9: if s != previous_word: previous_word = s if duplicate == False: sorted_words_9_nodups.append(previous_word) duplicate = False else: duplicate = True index = random.randint(0, len(sorted_words_9_nodups) - 1) letters = sorted(sorted_words_9_nodups[index]) target_letters = letters.copy() random.shuffle(target_letters) bullseye = target_letters[0] target = "" target_words = [] for word in words: if bullseye not in word: continue sorted_word = sorted(word) i = 0 num_matches = 0 for c in sorted_word: matched = False while (i < 9) and (matched == False): if c == letters[i]: matched = True num_matches = num_matches + 1 i = i + 1 if i == 9: break l = len(word) if num_matches == l: target_words.append(word) if (l == 9): target = word return { "letters": target_letters, "bullseye": bullseye, "target": target, "words": target_words, "hashed_words": [hashCode(w) for w in target_words] }
def main(): fp = open("Data_pointers") fo = open("Data_pointers_out", "w") current_t = 0 prev_t = 0 lines = fp.readlines() ii = 0 while ii < len(lines): line = lines[ii] if line.strip() == '' or '#' in line or '/*' in line or '*/' in line: ii += 1 continue if line[0] != '\t': parts = line.split() current = parts[-1].lstrip('*').rstrip(';') prev_t = 0 ii += 1 fo.write('fprintf(fp,"%ld\\n",ctx->' + current + ');\n') continue current_t - 0 i = 0 s = line[i] while not isalpha(s): s = line[i] i += 1 current_t = i tmp = current.replace('.', '->') parts = tmp.split('->') if parts[-1] == '': parts = parts[:len(parts) - 1] #print current, line.strip(),parts if not (current_t > prev_t) and len(parts) > current_t: #print current_t diff = parts[(len(parts) - current_t) * -1] #print diff pos = current.rfind(diff) if current[pos - 1] == '>': current = current[:pos - 2] else: current = current[:pos] parts = line.split() if ',' in parts[-1]: parts1 = parts[-1].split(',') pcurrent = current for p in parts1: if pcurrent[-1] != '.': current = pcurrent + '->' + p.rstrip(';').lstrip('*') else: current = pcurrent + p.rstrip(';').lstrip('*') if current[-1] != '.': #fo.write(current+'\n') fo.write('fprintf(fp,"%ld\\n",ctx->' + current + ');\n') #print current else: if current[-1] != '.': current = current + '->' + parts[-1].rstrip(';').lstrip('*') else: current = current + parts[-1].rstrip(';').lstrip('*') if current[-1] != '.': if ii < len(lines) - 1 and prefix_tab(lines[ii]) < prefix_tab( lines[ii + 1]): #fo.write(current+'\n') fo.write('fprintf(fp,"%ld\\n",ctx->' + current + ');\n') #print current elif ii > len(lines) - 1: #fo.write(current+'\n') fo.write('fprintf(fp,"%ld\\n",ctx->' + current + ');\n') #print current prev_t = current_t ii += 1
number_labels = labels letter_images = np.loadtxt(path_letter_images, np.uint8) letter_labels = np.loadtxt(path_letter_labels, np.float32) letter_labels = letter_labels.reshape((letter_labels.size, 1)) converted_images_l = [] labels_l = [] for index in range(len(letter_images)): image = letter_images[index] reshaped = Image(image=image.reshape((character_original_height, character_original_width))) reshaped.binarize(adaptative=True) mean_value = np.mean(reshaped.data) if mean_value < 220: if isalpha(chr(int(letter_labels[index]))): converted_images_l.append(reshaped) labels_l.append(letter_labels[index]) letter_images = converted_images_l letter_labels = labels_l # Train classifiers. logger.log(Logger.INFO, "Training letter classifier.") letter_classifier = CharacterRecognizer(character_width, character_height, character_classifier_type) letter_classifier.train(letter_images, letter_labels) if letter_classifier.classifier_type != "knn": letter_classifier.save(path_letter_classifier) else: letter_classifier.save("", path_letter_knn_images_classifier, path_letter_knn_labels_classifier)
line = line.replace('$', '\$') for p in patterns: if len(re.findall(p, line)) > 0: #and line.find('null') == -1: cur.append((line, id)) # print(cur) res_g = [[cur[0][0]]] for i in range(1, len(cur)): if cur[i][1] == cur[i - 1][1] + 1: res_g[-1].append(cur[i][0]) else: res_g.append([cur[i][0]]) for g in res_g: s = g[0] while not isalpha(s[0]): s = s[1:] g[0] = s explain_pattern = ''' explainStr -> explainStr.contains(''' explain_pattern1 = ''') && explainStr.contains(''' chkstr = '' for g in res_g: chkstr = chkstr + '\t\t' + 'explainStr.contains("' + g[0] if len(g) == 1: chkstr = chkstr + '") && \n'