-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeek4-StringsQuiz.py
119 lines (91 loc) · 5.43 KB
/
Week4-StringsQuiz.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#############################################################################
#Question 1
#The is_palindrome function checks if a string is a palindrome. A palindrome is a string that can be equally read
#from left to right or right to left, omitting blank spaces, and ignoring capitalization. Examples of palindromes
##are words like kayak and radar, and phrases like "Never Odd or Even". Fill in the blanks in this function to
#return True if the passed string is a palindrome, False if not.
def is_palindrome(input_string):
# We'll create two strings, to compare them
new_string = ""
reverse_string = ""
# Traverse through each letter of the input string
for word in input_string.lower():
# Add any non-blank letters to the
# end of one string, and to the front
# of the other string.
if word != ' ':
new_string = new_string + word
reverse_string = word + reverse_string
# Compare the strings
if new_string == reverse_string:
return True
return False
print(is_palindrome("Never Odd or Even")) # Should be True
print(is_palindrome("abc")) # Should be False
print(is_palindrome("kayak")) # Should be True
##########################################################################################################################################################
##########################################################################################################################################################
def palindrome(input_string):
#print (input_string)
new_string = input_string.lower().replace(" ","")
reverse_string = input_string.lower().replace(" ","")[::-1]
#print (new_string)
#print (reverse_string)
if new_string == reverse_string:
return True
return False
print(palindrome("Never Odd or Even")) # Should be True
print(palindrome("abc")) # Should be False
print(palindrome("kayak")) # Should be True
##########################################################################################################################################################
##########################################################################################################################################################
##Using the format method, fill in the gaps in the convert_distance function so that it returns the phrase "X
# miles equals Y km", with Y having only 1 decimal place. For example, convert_distance(12) should return "12 miles equals 19.2 km".
def convert_distance(miles):
km = miles * 1.6
result = "{miles} miles equals {km:.1f} km".format(miles=miles,km=km)
return result
print(convert_distance(12)) # Should be: 12 miles equals 19.2 km
print(convert_distance(5.5)) # Should be: 5.5 miles equals 8.8 km
print(convert_distance(11)) # Should be: 11 miles equals 17.6 km
##########################################################################################################################################################
##########################################################################################################################################################
###Fill in the gaps in the nametag function so that it uses the format method to return first_name and the first initial of last_
# name followed by a period. For example, nametag("Jane", "Smith") should return "Jane S."
def nametag(first_name, last_name):
return("{first_name} {last_name}.".format(first_name=first_name,last_name=last_name[0]))
print(nametag("Jane", "Smith"))
# Should display "Jane S."
print(nametag("Francesco", "Rinaldi"))
# Should display "Francesco R."
print(nametag("Jean-Luc", "Grand-Pierre"))
# Should display "Jean-Luc G."
##########################################################################################################################################################
##########################################################################################################################################################
###The replace_ending function replaces the old string in a sentence with the new string, but only if the sentence ends
# with the old string. If there is more than one occurrence of the old string in the sentence, only the one at the end
# is replaced, not all of them. For example, replace_ending("abcabc", "abc", "xyz") should return abcxyz, not xyzxyz
# or xyzabc. The string comparison is case-sensitive, so replace_ending("abcabc", "ABC", "xyz") should return abcabc
# (no changes made).
def replace_ending(sentence, old, new):
#Check if the old string is at the end of the sentence
if sentence.endswith(old):
# Using i as the slicing index, combine the part
# of the sentence up to the matched string at the
# end with the new string
i = sentence.rindex(old)
# print(i)
# i = ''.join(sentence).rindex(old)
# print(i)
new_sentence = sentence[:i] + new
return new_sentence
# Return the original sentence if there is no match
return sentence
print(replace_ending("It's raining cats and cats", "cats", "dogs"))
# Should display "It's raining cats and dogs"
print(replace_ending("She sells seashells by the seashore", "seashells", "donuts"))
# Should display "She sells seashells by the seashore"
print(replace_ending("The weather is nice in May", "may", "april"))
# Should display "The weather is nice in May"
print(replace_ending("The weather is nice in May", "May", "April"))
# Should display "The weather is nice in April"