forked from imtiazahmad007/PythonCourse
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathassignment_06.py
71 lines (33 loc) · 1.04 KB
/
assignment_06.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Assignment 6
"""
Create a function called last2 that accepts a string argument.
The function should return the count of the number of times that the last
2 characters appear in the rest of the string. You should not count
the last 2 characters as an occurrence. The last 2 characters is just the
sequence your function should look for in the remaining string.
So "hixxxhi" yields 1 (we won't count the end substring).
last2('hixxhi') → 1
last2('xaxxaxaxx') → 1
last2('axxxaaxx') → 2
"""
# Your Code Below:
print(last2('hixxhi')) #→ 1
print(last2('xaxxaxaxx')) #→ 1
print(last2('axxxxaaxx')) #→ 3
# Solution
# def last2(str):
# # Screen out too-short string case.
# if len(str) < 2:
# return 0
#
# # last 2 chars, can be written as str[-2:]
# last2 = str[len(str) - 2:]
# count = 0
#
# # Check each substring length 2 starting at i
# for i in range(len(str) - 2):
# sub = str[i:i + 2]
# if sub == last2:
# count = count + 1
#
# return count