'X'
[ ]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
Function four_letter_anagram takes a string, and four integers a, b, c and d. It returns the anagram of the string according to the indices a, b, c and d – concatenate the individual characters.
Test cases:
in operatorThe in operator determines whether a given value is a constituent element of a sequence (such as a string)
How to check if a string can be converted to an integer or float?
"BladeRunner2049" will return False
"2049" will return True
"12.5" will return True
How to check if a string can be converted to an integer or float?
Can we use the string method.isnumeric()?
index variablePrint characters at even indices.
Function name is_numeric that takes one string argument of any length (assume length > 0).
It returns True if every character in the argument is a digit (0-9) or a period (.), False otherwise.
Use in operator instead of built-in function isnumeric().
is_numeric() solutionHow do we change our function so that the last test case (“1.2.3”) returns False 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():
assert is_numeric("BladeRunner2049") == False
assert is_numeric("2049") == True
assert is_numeric("12.5") == True
assert is_numeric("1.2.3") == True # how to return False?
main()is_numeric() – solution 1def is_numeric(my_string):
index = 0
count_period = 0
while index < len(my_string):
if my_string[index] == ".":
count_period += 1
if my_string[index] not in "0123456789." or count_period > 1:
return False
index += 1
return True
def main():
assert is_numeric("234") == True
assert is_numeric("abc") == False
assert is_numeric("12c") == False
assert is_numeric("12.3") == True
assert is_numeric("1.2.3") == False
main()is_numeric() – solution 2def 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():
assert is_numeric("234") == True
assert is_numeric("abc") == False
assert is_numeric("12c") == False
assert is_numeric("12.3") == True
assert is_numeric("1.2.3") == False
main()