Question

In: Computer Science

Using Python 1. #Now, let's improve our steps() function to take one parameter #that represents the...

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"))


Solutions

Expert Solution

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.


Related Solutions

Python 3 Write the definition of a function that take one number, that represents a temperature...
Python 3 Write the definition of a function that take one number, that represents a temperature in Fahrenheit and prints the equivalent temperature in degrees Celsius. Write the definition of another function that takes one number, that represents speed in miles/hour and prints the equivalent speed in meters/second. Write the definition of a function named main. It takes no input, hence empty parenthesis, and does the following: - prints Enter 1 to convert Fahrenheit temperature to Celsius - prints on...
using python 1. #Write a function called multiply_by_index. multiply_by_index #should have one parameter, a list; you...
using python 1. #Write a function called multiply_by_index. multiply_by_index #should have one parameter, a list; you may assume every item #in the list will be an integer. multiply_by_index should #return a list where each number in the original list is #multipled by the index at which it appeared. # #For example: # #multiply_by_index([1, 2, 3, 4, 5]) -> [0, 2, 6, 12, 20] # #In the example above, the numbers 1, 2, 3, 4, and 5 appear #at indices 0,...
Create a function named ‘dividing’. This function will take one parameter value, which will be an...
Create a function named ‘dividing’. This function will take one parameter value, which will be an integer. For the function you will use ‘Exception Handling’ with the use of the try and except statements to try and return the results of a number divided by the parameter value given to the function. If the parameter passed to the function is the integer value of zero then your function should return ‘You tried to divide by zero!’ through the use of...
solve with python 3.8 please 1,Write a function called make_squares_coordinate_pair that has one parameter - an...
solve with python 3.8 please 1,Write a function called make_squares_coordinate_pair that has one parameter - an int. The function should return a tuple containing that int and its square. Suppose you were to call your function like this: print(make_squares_coordinate_pair(5)) This should output: (5, 25) Your function MUST be called make_squares_coordinate_pair. You can write code in the main part of your program to test your function, but when you submit, your code must ONLY have the function definition! 2, Write a...
Python Programming 1. Write a function name split_name which takes one parameter called name. If the...
Python Programming 1. Write a function name split_name which takes one parameter called name. If the name parameter value is a name in the form LastName, FirstName(e.g., ”Grounds, Nic”) then the function should return a list of two elements where FirstName is the first element and LastName is the second element. If the name parameter value is a name in the form FirstName LastName(e.g., ”Nic Grounds”) then the function should return a list of two elements where FirstName is the...
using python 1. #Write a function called multiply_file_by_index. This function #should take two parameters, both strings....
using python 1. #Write a function called multiply_file_by_index. This function #should take two parameters, both strings. The first string is #the filename of a file to which to write (output_file), and #the second string is the filename of a file from which to read #(input_file). # #In the input file, there will be an integer on every line. #To the output file, you should write the integer from the #original file multiplied by the line number on which it #appeared....
ALL IN PYTHON PLEASE Problem 3: Define a VALUE-RETURNING function that accepts one parameter - an...
ALL IN PYTHON PLEASE Problem 3: Define a VALUE-RETURNING function that accepts one parameter - an integer number. The function, which must be a value-returning function, returns 1 if the number is even or 0 if the number is odd. In the “main” function (i.e. def main()), capture the return value and print an appropriate message on screen (i.e. number is even or odd). Rubric: Correctly defined a value-returning function: 5 pts Correctly capture and use return value from a...
Python Programcomplete theprocessString(string)function. This function takesin a string as a parameter and prints the...
Python Program complete theprocessString(string)function. This function takes in a string as a parameter and prints the average number of characters per word in each sentence in the string. Print the average character count per word for each sentence with 1 decimal precision(see test cases below).-Assume a sentence always ends with a period (.)or when the string ends. -Assume there is always a blank space character(" ")between each word. -Do not count the blank spaces between words or the periods as...
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT