-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrings_6.7.a.py
58 lines (44 loc) · 1.46 KB
/
strings_6.7.a.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Dadas dos cadenas... escribir una funcion que indique si la segunda cadena es
subcadena de la primera
"""
def subcadena1(string1, string2):
# if string2 in string1:
# return True
# return False
return string2 in string1 # retorno directamente la condicion
def subcadena2(string1, string2):
# usar index para simplificar esta version
for i1 in range(len(string1)):
if string1[i1] == string2[0]:
largo2 = len(string2)
sub1 = string1[i1:i1+largo2]
print('sub1', sub1)
if sub1 == string2:
return True
return False
def subcadena3(string1, string2):
for i1 in range(len(string1)):
if string1[i1] == string2[0]:
largo2 = len(string2)
sub1 = string1[i1:i1+largo2]
print('sub1', sub1)
if sub1 == string2:
return True
return False
def subcadena4(string1, string2):
for i1 in range(len(string1)):
if string1[i1] == string2[0]:
coincide = True
for i2 in range(len(string2)):
j = i1 + i2
if j == len(string1) or string2[i2] != string1[j]:
coincide = False
break
if coincide:
print('la coincidencia arranca en el indice:', i1)
return True
return False
print(subcadena('la barca era una barcaza', 'caza'))