Write a program containing a function called swap

Assignment Help Python Programming
Reference no: EM133087563

Functions, Methods and Objects

Introduction

In each lab you'll work through a series of exercises that help you to understand and be able to apply and use the knowledge and techniques learnt in the associated weeks' lecture.Remember - you aren't expected to do everything from memory - sometimes it's best to do a little research / reading / re-reading of materials and come up with a great, and correct, answer rather than just 'taking a stab' at a question or exercise!

Exercises

Question 1. Write a program with a function called abba that 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.

Attachment:- Functions, Methods.rar

Reference no: EM133087563

Questions Cloud

Law enforcement officer used physical force : Describe one example from real-life news within the past three years to illustrate a time when a law enforcement officer used physical force
Safe policing for safe communities : Also research the Executive Action implemented by President Trump before he left office in June 2020. It is titled Safe Policing for Safe Communities.
Historical and current status of police community relations : What is historical and current status of police community relations (particularly concerns minorities) and what are some underlying reasons for that situation.
Emergence of concept of community-oriented-policing : Using CSU library resources look for literature on the emergence of the concept of Community-Oriented-Policing.
Write a program containing a function called swap : Write a short program with a function called bigger that takes two numbers as input parameters and returns the bigger of the two numbers
Drawbacks of selecting judges through popular elections : What are the benefits and drawbacks of selecting judges through popular elections? What are the arguments for and against three-strikes laws?
Supreme court placed upon the practice of religion : What limitations has the Supreme Court placed upon the practice of religion?
Effectiveness of the workforce : Bloomfield Designers is a UK fashion retailer which designs and sells its own brand of women's clothes through its chain of shops in Wales. However, it is facin
Write a small program that uses pythons logging module : Write a small program that uses Python's logging module to log five different log messages, each with different levels of importance

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