In: Computer Science
Using Python
1.
#Now, let's improve our steps() function to take one
parameter
#that represents the number of 'steps' to print. It should
#then return a string that, when printed, shows output like
#the following:
#
#print(steps(3))
#111
# 222
# 333
#
#print(steps(6))
#111
# 222
# 333
# 444
#
555
#
666
#
#Specifically, it should start with 1, and show three of each
#number from 1 to the inputted value, each on a separate
#line. The first line should have no tabs in front, but each
#subsequent line should have one more tab than the line
#before it. You may assume that we will not call steps() with
#a value greater than 9.
#
#Hint: You'll want to use a loop, and you'll want to create
#the string you're building before the loop starts, then add
#to it with every iteration.
#Write your function here!
#The following two lines will test your code, but are not
#required for grading, so feel free to modify them.
print(steps(3))
print(steps(6))
2.
#Write a function called mock. mock should take one
#parameter, a string. You may assume that the string will
#have only lowercase letters and spaces.
#
#Your function should return the same string, but any letter
#at an even index should be converted to uppercase.
#
#For example: mock("abcd efgh ijkl") would return
#"AbCd eFgH IjKl".
#
#Remember, you can use the ordinal function ord() to get the
#number associated with a single letter. For example,
#ord("a") returns 97. The number associated with lowercase
#letters is always 32 larger than the number associated with
#the equivalent uppercase letter. ord("a") is 97, and
#ord("A") is 65. ord("z") is 122, and ord("Z") is 90.
#
#Remember, you can use the character function chr() to
#convert a number back to a letter. For example, chr(65) will
#return "A".
#
#HINT: Treat all characters the same initially, then worry
#about taking care of spaces afterwards.
#Write your function here!
def mock(a_string):
#Below are some lines of code that will test your function.
#You can change the value of the variable(s) to test your
#function with different inputs.
#
#If your function works correctly, this will originally
#print: "AbCd eFgH IjKl".
print(mock("abcd efgh ijkl"))
PART 1 -
CODE -
def steps(n):
# Creating an empty string
str_text = ""
# Loop to build our string
for i in range(n):
# Adding a line of string to the previous string in every
iteration.
# Remember I have used 2 spaces for a tab. You can change it if you
need to.
str_text += i*" " + 3*str(i+1) + "\n"
# Returning the string
return str_text
SCREENSHOT -
PART 2 -
CODE -
def mock(a_string):
# Creating an empty string
result = ""
# Iterating over the original string
for i in range(len(a_string)):
# Converting the even indexed character to uppercase if it is not a
space and appending it to the string result
if i%2 == 0 and a_string[i] != " ":
result += chr(ord(a_string[i]) - 32)
# Appending the character to the string result without any
modification if it is not even indexed character or if it is a
space
else:
result += a_string[i]
# Returning the result string
return result
SCREENSHOT -
If you have any doubt regarding the solution, then do
comment.
Do upvote.