Skip to content

Instantly share code, notes, and snippets.

@AlexDel
Last active May 11, 2018 02:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexDel/ebe2db3aa2e709fadad0e33de43e292b to your computer and use it in GitHub Desktop.
Save AlexDel/ebe2db3aa2e709fadad0e33de43e292b to your computer and use it in GitHub Desktop.
Примерный код программы для зачета
# Открываем файл
fileWithText = open('someFile.txt', 'r', encoding='utf-8')
# Если появляется ошибка с текстов "Unicode error, can't decode..." используйте это - "fileWithText = open('someFile.txt', 'r', encoding='cp1251')"
# Читаем содержимое файла. Записываем строку в переменную textFromFile
textFromFile = fileWithText.read()
# Функция, которая ищет слова, которые начинаются с 'un' - unbreak, undone, etc.
def countWordsWithUn(textString):
# Разделяем строку по пробелам, чтобы получить список слова
words = textString.split()
# Инициализируем пустой список куда будем складвать слова
wordsWithUn = []
# Проходим циклом по каждому слову
for word in words:
# Если первые 2 символа в слове - un, то добавляем слово в массив
if word[0:2] == 'un':
wordsWithUn.append(word)
# Возращаем список слов
return wordsWithUn
# Получаем слова с un из текста из нашего файла
print(countWordsWithUn(textFromFile))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment