Instructions
Objective
Write a program to implement function overloading in python language.
Requirements and Specifications
Complete one function at a time by changing the return statement and see the asserts pass for that function complete functions that return a string or a number value with the return statement instead of printing a value.
Source Code
import math # for math.sin() and math.pi
"""
A file with functions related to the fact that CSC 101 students are
learning to complete functions to be tested with the assert(boolean) function
Most functions can be completed with one return statement.
Programmer: Abdullah Al Dawood
"""
# -----------------------------------------------------
# -----------------------------------------------------
def wrap2(sequence, count):
"""
Return a sequence that moves the first count words to the end of sequence
Precondition: len(count) < len(sequence)
"""
# TODO: Complete this function
return sequence[count:] + sequence[:count]
# Test code for wrap2:
assert(wrap2('XY', 1) == 'YX')
assert(wrap2('12345', 2) == '34512')
assert(wrap2('abcdefg', 0) == 'abcdefg')
assert(wrap2('splash', 1) == 'plashs')
assert(wrap2('hello', 2) == 'llohe')
assert(wrap2('good bye', 3) == 'd byegoo')
assert(wrap2('XY', 2) == 'XY')
# -----------------------------------------------------
# -----------------------------------------------------
def projectile_range(degrees, velocity):
"""
Complete this function so it returns the range of a projectile rounded
to two decimal places using the free function round(float, places).
Here is the formula:
range = sin(2 * radians) * velocity**2 / gravity
Since the input is degrees, we must convert degrees to radians for use in the
math.sin() function that expects radians, not degrees. Here is the formula:
radians = degrees * math.pi / 180
"""
# TODO: complete this function
radians = degrees * math.pi / 180
meters = math.sin(2* radians) * velocity * velocity / 9.8
return round(meters, 2)
# Test Code for projectile_range
assert(projectile_range(90, 100) == 0.0) # Launch straight up into the air
assert(projectile_range(45, 100) == 1020.41)
assert(projectile_range(45, 200) == 4081.63)
assert(projectile_range(60, 200) == 3534.8)
# -----------------------------------------------------
# -----------------------------------------------------
def circle_area(radius):
"""
Return the area of a circle of the given radius to two decimal places.
You might have to look up this formula on the web.
"""
# TODO: Complete this function
area = math.pi * radius ** 2
area = round(area,2) # round to two decimal places
return area
# Test Code for circle_area(radius)
assert(circle_area(0.0) == 0.0)
assert(circle_area(1) == 3.14)
assert(circle_area(100) == 31415.93)
assert(circle_area(2) == 12.57)
# -----------------------------------------------------
# -----------------------------------------------------
def anti_matter(sequence):
"""
Everyone knows that interplanetary space travel systems
are fueled by mixing matter and anti-matter. Complete this
function that will take a string with the name of some
thing or idea. Return a string with "Anti-" prepended to it.
anti_matter("lol") returns Anti-lol
Precondition: the argument is a valid Python string (not a number or bool).
"""
# TODO: complete this function
return "Anti-" + sequence
# Test code for anti-matter:
assert(anti_matter('LOL') == 'Anti-LOL')
assert(anti_matter('splash') == 'Anti-splash')
assert(anti_matter('') == 'Anti-')
# -----------------------------------------------------
# -----------------------------------------------------
def abba(a, b):
"""
Given two strings, a and b, return the result of putting them together in the
order abba, e.g. "Hi" and "Bye" returns "HiByeByeHi".
abba("Hi", "Bye") returns "HiByeByeHi"
abba("Yo", "Alice") returns "YoAliceAliceYo"
Precondition: a and b are valid Python strings.
"""
# TODO: complete this function
return a+b+b+a
# Test Code for abba:
assert(abba("Hi", "Bye") == "HiByeByeHi")
assert(abba("O", "X") == "OXXO")
assert(abba("1", "2") == "1221")
# -----------------------------------------------------
# -----------------------------------------------------
def make_tags(tag, sequence):
"""
The web is built with HTML strings like "Yay" which draws Yay
as italic text. In this example, the "i" tag makes and which
surround the word "Yay". Given tag and word strings, create the HTML
string with tags around the word
make_tags("i", "italic") returns "italic"
Precondition: tag and word are valid Python strings
"""
# TODO: complete this function
return "<" + tag + ">" + sequence + ""
# Test Code for make_tags:
assert(make_tags("i", "italic") == "italic")
assert(make_tags("b", "bold") == "bold")
# -----------------------------------------------------
# -----------------------------------------------------
def in_lat(word):
"""
Aoccdrnig to a rscheearch at an Elingsh uinervtisy, it deosn't mttaer
in waht oredr the ltteers in a wrod are, the olny iprmoetnt tihng
is taht frist and lsat ltteer is at the rghit pclae.
Create a function that will mix up the middle characters in
a 5 letter word. The precondition states the word must have exactly
5 letters. If the argument is a different len, the function will not work.
Assuming the letters are indexed as 0-1-2-3-4, they should end up in
the order 0-3-2-1-4. Your function will accept a 5-character string as an
argument and return a 5-character string reordered as described.
in_lat ("apple") returns "alppe"
in_lat ("gears") returns "gares"
in_lat ("prior") returns "piorr"
in_lat ("scone") returns "sonce"
Precondition len(word) == 5 exactly
"""
# TODO: complete this function
# Note that, for a 5-characters word with index 0-1-2-3-4, the resulting index are 0-3-2-1-4
# Which means that we only have to switch the characters at index 1 and 3
# First, convert the word to a list of characters
word_lst = list(word)
temp = word_lst[1] # store the character 1 in a temporary variable
word_lst[1] = word_lst[3] # switch character at index 3 to the index 1
word_lst[3] = temp # Set the character stored in 'temp' at the index 3 in word
# Now, convert the list of characters to a str again
newWord = ''.join(word_lst)
return newWord # bogus return that does not work
# Test Code for in_lat
assert(in_lat("apple") == "alppe")
assert(in_lat("first") == "fsrit")
assert(in_lat("habit") == "hibat")
assert(in_lat("loose") == "lsooe")
assert(in_lat("thing") == "tnihg")
assert (in_lat('Quick') + " " + in_lat('brown') + " " + in_lat('foxes')
+ " " + in_lat('jumps') == 'Qciuk bworn fexos jpmus')
Related Samples
At ProgrammingHomeworkHelp.com, we offer comprehensive support for Python assignments, including access to related sample code. Our expertly crafted samples are designed to guide students through complex concepts and enhance their understanding. Whether you need help with syntax, algorithms, or debugging, our Python samples provide practical examples to support your learning journey. Get the assistance you need to excel in your Python coursework with our tailored resources.
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python