forked from imtiazahmad007/PythonCourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment_07.py
More file actions
76 lines (36 loc) · 1.12 KB
/
assignment_07.py
File metadata and controls
76 lines (36 loc) · 1.12 KB
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
72
73
74
75
76
# Assignment 7
"""
Given 2 strings, a and b, return the number of the positions where
they contain the same length 2 substring. So "xxcaazz" and "xxbaaz"
yields 3, since the "xx", "aa", and "az" substrings appear in the same
place in both strings.
EXAMPLE:
string_match('xxcaazz', 'xxbaaz') → 3
string_match('abc', 'abc') → 2
string_match('abc', 'axc') → 0
"""
def string_match(a, b):
# Figure out which string is shorter
shorter = min(len(a), len(b))
count = 0
for i in range(shorter - 1):
a_sub = a[i: i+2] # gives us substring of size 2
b_sub = b[i: i+2]
if a_sub == b_sub:
count = count + 1
return count
# Solution
# def string_match(a, b):
# # Figure which string is shorter.
# shorter = min(len(a), len(b))
# count = 0
#
# # Loop i over every substring starting spot.
# # Use length-1 here, so can use char str[i+1] in the loop
# for i in range(shorter - 1):
# a_sub = a[i:i + 2]
# b_sub = b[i:i + 2]
# if a_sub == b_sub:
# count = count + 1
#
# return count