Attachment '2012-11-09.slowo.py'
Download 1 #!/usr/bin/env python
2 # coding: utf
3 '''
4 Определить, сколько раз в строке встречается заданное слово
5 С учётом, что «слово» — это последовательность латинских букв, а все остальные символы — разделители, и например, в строке "Hubba-bubba bubba ubba ubbarah" слово "ubba" встречается один раз (оно четвёртое)
6 '''
7
8 string, word = raw_input("Введите строку: "), raw_input("Введите слово: ")
9
10 test = string+" " # В конце строки должен быть разделитель
11 alp = "abcdefghijklmnopqrstuvwxyz" # Алфавит
12 alp += alp.upper() # Большие буквы тоже есть в алфавите
13
14 count, full, width = 0, len(string), len(word)
15
16 i = 0
17 while i<full-width:
18 # Если слово встречается с позиции i и после него стоит не буква
19 if test[i:].startswith(word) and not test[i+width] in alp:
20 count += 1
21 i += width+1
22 else:
23 i += 1
24
25 print count
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.- [get | view] (2012-11-15 19:57:33, 0.5 KB) [[attachment:2012-11-09.cifry.py]]
- [get | view] (2012-11-22 19:36:51, 1.2 KB) [[attachment:2012-11-09.f2n.rec.py]]
- [get | view] (2012-11-15 19:57:57, 0.8 KB) [[attachment:2012-11-09.powtor.py]]
- [get | view] (2012-11-15 19:57:14, 1.2 KB) [[attachment:2012-11-09.slowo.py]]
You are not allowed to attach a file to this page.