#!/usr/bin/env python3 # -*- coding: utf-8 -*- def odd_even_counter(in_file, count_letter): '''Function: Get a number of symbols if it even or odd in any text file''' odd_counter = 0 even_counter = 0 with open(in_file, 'r') as f0: phrase_to_check = ''.join([x for x in f0.read() if x.isalpha()]) for position, item in enumerate(phrase_to_check): if item == count_letter: if (position + 1) % 2 == 1: odd_counter += 1 else: even_counter += 1 print('Π‘ΠΈΠΌΠ²ΠΎΠ» [', count_letter, '] Π½Π° Π½Π΅ΡΠ΅ΡΠ½ΠΎΠΉ ΠΏΠΎΠ·ΠΈΡΠΈΠΈ:', odd_counter, 'ΡΠ°Π·') print('Π‘ΠΈΠΌΠ²ΠΎΠ» [', count_letter, '] Π½Π° ΡΠ΅ΡΠ½ΠΎΠΉ ΠΏΠΎΠ·ΠΈΡΠΈΠΈ:', even_counter, 'ΡΠ°Π·') if __name__ == '__main__': test_file = 'lorem_ipsum.txt' # Π»ΡΠ±ΠΎΠΉ ΡΠ΅ΠΊΡΡΠΎΠ²ΡΠΉ ΡΠ°ΠΉΠ» test_letter = 'o' # Π»ΡΠ±ΠΎΠΉ ΡΠΈΠΌΠ²ΠΎΠ» odd_even_counter(test_file, test_letter) - oneit would be useful to add a verbal description of what the code should do to make sure that it works correctly. - jfs
|
1 answer
It is necessary to separate calculations and input / output - such code is simpler, it is easier to test and reuse.
Calculations:
from collections import namedtuple Counters = namedtuple('Counters', 'even odd') def odd_even_counter(text, char): """How many times *char* is in even/odd positions in *text*. Indexing starts with one. """ counters = [0, 0] # even, odd for position, current_char in enumerate(text, start=1): if current_char == char: counters[position % 2] += 1 return Counters(*counters) Example:
>>> odd_even_counter('abbb', 'b') Counters(even=2, odd=1) Input Output:
with open('lorem_ipsum.txt') as file: letters = (c for line in file for c in line if c.isalpha()) char = 'o' count = odd_even_counter(letters, char) print('Π‘ΠΈΠΌΠ²ΠΎΠ» [ {char} ] Π½Π° Π½Π΅ΡΡΡΠ½ΠΎΠΉ ΠΏΠΎΠ·ΠΈΡΠΈΠΈ: {count.odd} ΡΠ°Π·'.format(**vars())) print('Π‘ΠΈΠΌΠ²ΠΎΠ» [ {char} ] Π½Π° ΡΡΡΠ½ΠΎΠΉ ΠΏΠΎΠ·ΠΈΡΠΈΠΈ: {count.even} ΡΠ°Π·'.format(**vars())) If necessary, the definition and calculation of letters (casefold (), NFKD, grapheme clusters) can also be put into a separate function.
The code reads one line at a time, instead of the entire file.
Result
Π‘ΠΈΠΌΠ²ΠΎΠ» [ o ] Π½Π° Π½Π΅ΡΡΡΠ½ΠΎΠΉ ΠΏΠΎΠ·ΠΈΡΠΈΠΈ: 20 ΡΠ°Π· Π‘ΠΈΠΌΠ²ΠΎΠ» [ o ] Π½Π° ΡΡΡΠ½ΠΎΠΉ ΠΏΠΎΠ·ΠΈΡΠΈΠΈ: 18 ΡΠ°Π· |