Write a short program with a function called bigger

Assignment Help Python Programming
Reference no: EM133091489

Functions and Classes

Exercises

Question 1: Write a program with a function called abbathat takes two strings as input (a and b), and returns a string which consists of:
• The string a,
• Then the string b,
• Then the string b again,
• And finally the string a again.
So for example, if we passed in the strings 'a' and 'b' the function would return 'abba', andif we passed in the strings 'cat' and 'dog', the function would return 'catdogdogcat'. In your program make a few calls to the function and print out the returned value to ensure it's all working properly.

Question 2: Write a program with two functions called find_firstand find¬_all, each of which take two strings called source and target.as input.
The first_first function takes the two strings and input and returns the location of the target string within the source string as an integer - so for example, if we passed called find_firstwith the source string of "Roses are red, violets are blue" and a target string of "are", it would return the value 6 because that's the index of the first occurrence of the target string within the source string.
The find_allfunction works the same as the find_firstfunction, but it returns a list of all the locations of the target string within the source string - again, passing in "Roses are red, violets are blue" would return the list [6, 23] because the two instances of the substring start at those indices in the source string.
As before, make a few calls to the functions and print out the return values to ensure everything's working correctly.

Question 3: Write a short program with a function called bigger that takes two numbers as input parameters and returns the bigger of the two numbers. For example, if we called bigger(3, 5) it would return the value 5 because 5 is more than 3.
Once you have that working, write a function called biggest that takes three numbers and returns the biggest of the three values. An easy way to accomplish this is by using your bigger function within your biggest function - if you do it this way then it can be accomplished in just a single line of code!

Question 4: Write a program containing a function called swap that takes two parameters called first and second. You should be able to run the function passing it these two parameters and returning a 'swapped' version of these values. So, for example you could execute the function like this:
a = 123
b = 456
a, b = swap(a, b)
print(a, b) # Prints 456, 123 (i.e. the a variable now has b's value, and b now has a's value)

It's perfectly possible to do this with a single line of code within your function.

Question 5: Consider the following class definition:
class Vector:
def __init__(self,x,y):
self.x = x
self.y = y

def add1(self, v):
self.x = self.x + v.x

self.y = self.y + v.y

def add2(v1,v2):
v = Vector(0,0)
v.x = v1.x + v2.x
v.y = v1.y + v2.y
return v

def dot_product(v1,v2):
return v1.x*v2.x + v1.y*v2.y

def print(self):
print('({},{})'.format(self.x,self.y))

Save the class definition in a file and try to understand what this class is for and how it can be used. Run the code and experiment with the class definition.

Reference no: EM133091489

Questions Cloud

Global environmental problems : Why do governments have so much difficulty agreeing with each other on the measures against global environmental problems?
What is corruption : What is corruption? Is it a problem?
Organisation of a well performing information system : You have been asked to conduct a system evaluation. In doing so, you identify some system shortfalls for the payroll department in the organisation.
Prepare all the necessary journal entries on february : Assume the depreciable asset was sold February 1, 2021 for $36,000. Prepare all the necessary journal entries on February 1, 2021 to record this sale
Write a short program with a function called bigger : Write a program containing a function called swap that takes two parameters called first and how it can be used. Run the code and experiment
Calculate wacc and determine the value of the firm : Depreciation is 14.5% of EBIT, changes in NWC is 11% of EBIT and capital expenditures are 20% of EBIT. Calculate WACC and determine the value of the firm
Major public policy issues in pricing : What are the major public policy issues in pricing? Does Amazon practice predatory pricing or just good competitive marketing?
Calculate one year depreciation : An asset cost $120,000 and has a useful life of 6 years or 8,000 units of production, and no residual value. Calculate one year's depreciation
Discuss how marketing research : Select one element of the Marketing mix and discuss how marketing research could help that area.

Reviews

Write a Review

Python Programming Questions & Answers

  Write a python program to implement the diff command

Without using the system() function to call any bash commands, write a python program that will implement a simple version of the diff command.

  Write a program for checking a circle

Write a program for checking a circle program must either print "is a circle: YES" or "is a circle: NO", appropriately.

  Prepare a python program

Prepare a Python program which evaluates how many stuck numbers there are in a range of integers. The range will be input as two command-line arguments.

  Python atm program to enter account number

Write a simple Python ATM program. Ask user to enter their account number, and print their initail balance. (Just make one up). Ask them if they wish to make deposit or withdrawal.

  Python function to calculate two roots

Write a Python function main() to calculate two roots. You must input a,b and c from keyboard, and then print two roots. Suppose the discriminant D= b2-4ac is positive.

  Design program that asks user to enter amount in python

IN Python Design a program that asks the user to enter the amount that he or she has budget in a month. A loop should then prompt the user to enter his or her expenses for the month.

  Write python program which imports three dictionaries

Write a Python program called hours.py which imports three dictionaries, and uses the data in them to calculate how many hours each person has spent in the lab.

  Write python program to create factors of numbers

Write down a python program which takes two numbers and creates the factors of both numbers and displays the greatest common factor.

  Email spam filter

Analyze the emails and predict whether the mail is a spam or not a spam - Create a training file and copy the text of several mails and spams in to it And create a test set identical to the training set but with different examples.

  Improve the readability and structural design of the code

Improve the readability and structural design of the code by improving the function names, variables, and loops, as well as whitespace. Move functions close to related functions or blocks of code related to your organised code.

  Create a simple and responsive gui

Please use primarily PHP or Python to solve the exercise and create a simple and responsive GUI, using HTML, CSS and JavaScript.Do not use a database.

  The program is to print the time

The program is to print the time in seconds that the iterative version takes, the time in seconds that the recursive version takes, and the difference between the times.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd