0% found this document useful (0 votes)
62 views

Python Workbook 1

This document provides a list of exercises related to Python coding functions. It includes 12 exercises of varying complexity that involve writing Python functions to perform tasks like calculating the median of three values, generating lyrics to the song 'The Twelve Days of Christmas', and reducing fractions to their lowest terms. The exercises are intended to help readers practice writing reusable functions and breaking problems into smaller pieces.

Uploaded by

Tarcio Carvalho
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Python Workbook 1

This document provides a list of exercises related to Python coding functions. It includes 12 exercises of varying complexity that involve writing Python functions to perform tasks like calculating the median of three values, generating lyrics to the song 'The Twelve Days of Christmas', and reducing fractions to their lowest terms. The exercises are intended to help readers practice writing reusable functions and breaking problems into smaller pieces.

Uploaded by

Tarcio Carvalho
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

8/26/2020 python-workbook-1

python-workbook-1
Created quarta 26 agosto 2020

This is a list of exercises on Python coding.


The goal is to provide an opportunity for everyone to evolve in the concept of coding.
Each exercise must be prepared in the least amount of lines possible, above to each exercise title there is a
target for the number of lines.
You must be upload the solution in a specific repository in https://github.com/the-last-question .
Comments are necessary for you to explain each step of the code.
Each repository should have a readme with clear explanations of how to use the script.
The due date to upload your script in the repository is 04-september-2020.

Function Exercises
Functions allow a programmer to break a problem into pieces that can be reused. They can also help a
programmer focus on one part a larger problem at a time. As a result, writing functions is often an important
part of developing larger pieces of software. The exercises in this list will help you practice these skills:

Define a function for later use


Pass one or more values into a function
Perform a complex calculation within a function
Return one or more results from a function
Call a function that you have defined previously

1-Median of Three Values


(42 Lines)
Write a function that takes three numbers as parameters, and returns the median value of those parameters as
its result. Include a main program that reads three values from the user and displays their median.
Hint: The median value is the middle of the three values when they are sorted into ascending order. It can be
found using if statements, or with a little bit of mathematical creativity.

2-The Twelve Days of Christmas


(48 Lines)
The Twelve Days of Christmas is a repetitive song that describes an increasingly long list of gifts sent to
one’s true love on each of 12 days. A single gift is sent on the first day. A new gift is added to the collection
on each additional day, and then the complete collection is sent. The first three verses of the song are shown
below. The complete lyrics are available on the internet.

On the first day of Christmas


my true love sent to me:
A partridge in a pear tree.
On the second day of Christmas
my true love sent to me:
Two turtle doves,
And a partridge in a pear tree.
On the third day of Christmas
my true love sent to me:
Three French hens,
Two turtle doves,
And a partridge in a pear tree.

Your task is to write a program that displays the complete lyrics for The Twelve Days of Christmas. Write a
function that takes the verse number as its only parameter and displays the specified verse of the song. Then
call that function 12 times with integers that increase from 1 to 12.
Each item that is sent to the recipient in the song should only appear once in your program, with the possible

file:///tmp/zim-kv6t8wsq/print-to-browser.html 1/3
8/26/2020 python-workbook-1

exception of the partridge. It may appear twice if that helps you handle the difference between “A partridge
in a pear tree” in the first verse and “And a partridge in a pear tree” in the subsequent verses.

3-Center a String in the Terminal


(31 Lines)
Write a function that takes a string of characters as its first parameter, and the width of the terminal in
characters as its second parameter. Your function should return a new string that consists of the original
string and the correct number of leading spaces so that the original string will appear centered within the
provided width when it is printed. Do not add any characters to the end of the string. Include a main program
that demonstrates your function.

4-Capitalize It
(48 Lines)
Many people do not use capital letters correctly, especially when typing on small devices like smart phones.
In this exercise, you will write a function that capitalizes the appropriate characters in a string. A lowercase
“i” should be replaced with an uppercase “I” if it is both preceded and followed by a space. The first
character in the string should also be capitalized, as well as the first non-space character after a “.”, “!” or
“?”. For example, if the function is provided with the string “what time do i have to be there? what’s the
address?” then it should return the string “What time do I have to be there? What’s the address?”. Include a
main program that reads a string from the user, capitalizes it using your function, and displays the result.

5-Does a String Represent an Integer?


(30 Lines)
In this exercise you will write a function named isInteger that determines whether or not the characters in a
string represent a valid integer. When determining if a string represents an integer you should ignore any
leading or trailing white space. Once this white space is ignored, a string represents an integer if its length is
at least 1 and it only contains digits, or if its first character is either + or - and the first character is followed
by one or more characters, all of which are digits.
Write a main program that reads a string from the user and reports whether or not it represents an integer.
Ensure that the main program will not run if the file containing your solution is imported into another
program.
Hint: You may find the lstrip, rstrip and/or strip methods for strings helpful when completing this
exercise. Documentation for these methods is available online.

6-Is a Number Prime?


(28 Lines)
A prime number is an integer greater than 1 that is only divisible by one and itself. Write a function that
determines whether or not its parameter is prime, returning True if it is, and False otherwise. Write a main
program that reads an integer from the user and displays a message indicating whether or not it is prime.
Ensure that the main program will not run if the file containing your solution is imported into another
program.

7-Random Password
(33 Lines)
Write a function that generates a random password. The password should have a random length of between 7
and 10 characters. Each character should be randomly selected from positions 33 to 126 in the ASCII table.
Your function will not take any parameters. It will return the randomly generated password as its only result.
Display the randomly generated password in your file’s main program. Your main program should only run
when your solution has not been imported into another file.
Hint: You will probably find the chr function helpful when completing this exercise. Detailed
information about this function is available online.

8-Check a Password
(40 Lines)
In this exercise you will write a function that determines whether or not a password is good. We will define a
good password to be a one that is at least 8 characters long and contains at least one uppercase letter, at least
one lowercase letter, and at least one number. Your function should return true if the password passed to it as
file:///tmp/zim-kv6t8wsq/print-to-browser.html 2/3
8/26/2020 python-workbook-1

its only parameter is good. Otherwise it should return false. Include a main program that reads a password
from the user and reports whether or not it is good. Ensure that your main program only runs when your
solution has not been imported into another file.

9-Arbitrary Base Conversions


(61 Lines)
Write a program that allows the user to convert a number from one base to another. Your program should
support bases between 2 and 16 for both the input number and the result number. If the user chooses a base
outside of this range then an appropriate error message should be displayed and the program should exit.
Divide your program into several functions, including a function that converts from an arbitrary base to base
10, a function that converts from base 10 to an arbitrary base, and a main program that reads the bases and
input number from the user.

10-Reduce a Fraction to Lowest Terms


(47 Lines)
Write a function that takes two positive integers that represent the numerator and denominator of a fraction
as its only two parameters. The body of the function should reduce the fraction to lowest terms and then
return both the numerator and denominator of the reduced fraction as its result. For example, if the
parameters passed to the function are 6 and 63 then the function should return 2 and 21. Include a main
program that allows the user to enter a numerator and denominator. Then your program should display the
reduced fraction.

11-Reduce Measures
(83 Lines)
Many recipe books still use cups, tablespoons and teaspoons to describe the volumes of ingredients used
when cooking or baking. While such recipes are easy enough to follow if you have the appropriate
measuring cups and spoons, they can be difficult to double, triple or quadruple when cooking Christmas
dinner for the entire extended family. For example, a recipe that calls for 4 tablespoons of an ingredient
requires 16 tablespoons when quadrupled. However, 16 tablespoons would be better expressed (and easier to
measure) as 1 cup.
Write a function that expresses an imperial volume using the largest units possible. The function will take the
number of units as its first parameter, and the unit of measure (cup, tablespoon or teaspoon) as its second
parameter. Return a string representing the measure using the largest possible units as the function’s only
result. For example, if the function is provided with parameters representing 59 teaspoons then it should
return the string “1 cup, 3 tablespoons, 2 teaspoons”.
Hint: One cup is equivalent to 16 tablespoons. One tablespoon is equivalent to 3 teaspoons.

12-Magic Dates
(26 Lines)
A magic date is a date where the day multiplied by the month is equal to the two digit
year. For example, June 10, 1960 is a magic date because June is the sixth month, and
6 times 10 is 60, which is equal to the two digit year. Write a function that determines
whether or not a date is a magic date. Use your function to create a main program
that finds and displays all of the magic dates in the 20th century.

==============================================================================

file:///tmp/zim-kv6t8wsq/print-to-browser.html 3/3

You might also like