'A'
[ ]
Note that the first index in a sequence is always zero
.
Since the first index of a sequence is zero
, the last index is going to be the length of the string minus 1
four_letter_anagram
string
, and four integers a
, b
, c
and d
a
, b
, c
and d
– concatenate the individual charactersfour_letter_anagram("balm", 2, 1, 3, 0)
should return lamb
Add the following test cases to the code below:
loin
returns lion
lugs
returns slug
reap
returns pear
Submit your four_letter_anagram
function to Gradescope for attendance.
Name your file anagram.py
in
operatorThe in
operator determines whether a given value is a constituent element of a sequence (such as a string)
Remember the string method.isnumeric()
?
is numeric
for one characterWrite a Python function that does the following:
is_numeric_one_char
string
argument of length of 1 (check if argument is valid with len()
)True
if the argument is a digit (0-9), False
otherwise (use the in
operator)is_numeric()
for any string lengthis_numeric
string
argument of any lengthTrue
if the first character of the argument is a digit (0-9), False
otherwise (remember to use [ ]
to index the string and the in
operator)Test cases:
is_numeric()
What if we want to check every character in a string of any length?
What if the string is of length 2
or 5
or 45
?
is_numeric()
is_numeric
string
argument of any lengthTrue
if every character in the argument is a digit (0-9), False
otherwise[ ]
to index the string, use a while loop with a len()
condition, and an index – something like string[index]
with index being updated inside a while
loopis_numeric()
solutiondef is_numeric(my_string):
index = 0
while index < len(my_string):
if my_string[index] not in "0123456789":
return False
index += 1
return True
def main():
print( is_numeric("234") ) # True
print( is_numeric("abc") ) # False
print( is_numeric("12c") ) # False
print( is_numeric("12.3") ) # False
main()
True
False
False
False
is_numeric()
solutionHow do we change our function so that the last test case (“12.3”) returns True
instead?
def is_numeric(my_string):
index = 0
while index < len(my_string):
if my_string[index] not in "0123456789":
return False
index += 1
return True
def main():
print( is_numeric("234") ) # True
print( is_numeric("abc") ) # False
print( is_numeric("12c") ) # False
print( is_numeric("12.3") ) # False
main()
True
False
False
False
is_numeric()
solutionHow do we change our function so that the last test case (“12.3”) returns True
instead?
def is_numeric(my_string):
index = 0
while index < len(my_string):
if my_string[index] not in "0123456789.":
return False
index += 1
return True
def main():
print( is_numeric("234") ) # True
print( is_numeric("abc") ) # False
print( is_numeric("12c") ) # False
print( is_numeric("12.3") ) # True
print( is_numeric("1.2.3") ) # True
main()
True
False
False
True
True
is_numeric()
solutiondef is_numeric(my_string):
# create control variable for one decimal point
decimal_point = False
# create index variable
index = 0
while index < len(my_string):
# first check, can character be found in a number?
if my_string[index] not in "0123456789.":
return False
# second check, if a period, has a period been found before?
if my_string[index] == ".":
if decimal_point: # a previous period was detected
return False
else: # first period detected
decimal_point = True
# increment index
index += 1
# while loop executed without returning False
# that means every character is valid, so return True
return True
def main():
print( is_numeric("234") ) # True
print( is_numeric("abc") ) # False
print( is_numeric("12c") ) # False
print( is_numeric("12.3") ) # True
print( is_numeric("1.2.3") ) # False
main()
True
False
False
True
False
You have 10 minutes to complete the quiz
main()
, no need to print test casesBuilt-in functions you can use: round()
, input()
, float()
, str()
, int()
— you don’t have to use all of these