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

Sumofsquares (M) That Takes An Integer M Returns True If M Is A Sum of Squares

This document contains instructions for writing 4 Python functions: 1. A function called wellbracketed that takes a string and returns True if it contains properly matched parentheses, and False otherwise. 2. A function called sumofsquares that takes an integer and returns True if it can be written as the sum of two perfect squares, and False otherwise. 3. A function that returns a new string containing the first 2 and last 2 characters of a given string, or an empty string if the original is less than 2 characters. 4. A function that takes a string, adds 'ing' to the end if it's at least 3 characters long, or adds 'ly' if it already ends

Uploaded by

Mansi A
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Sumofsquares (M) That Takes An Integer M Returns True If M Is A Sum of Squares

This document contains instructions for writing 4 Python functions: 1. A function called wellbracketed that takes a string and returns True if it contains properly matched parentheses, and False otherwise. 2. A function called sumofsquares that takes an integer and returns True if it can be written as the sum of two perfect squares, and False otherwise. 3. A function that returns a new string containing the first 2 and last 2 characters of a given string, or an empty string if the original is less than 2 characters. 4. A function that takes a string, adds 'ing' to the end if it's at least 3 characters long, or adds 'ly' if it already ends

Uploaded by

Mansi A
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Module 1 and 2

1. A string with parentheses is well bracketed if all parentheses are matched:


every
opening bracket has a matching closing bracket and vice versa.

Write a Python function wellbracketed(s) that takes a string s containing parentheses


and returns True if s is well bracketed and False otherwise. Here are some examples
to show how your function
should work.
>>> wellbracketed("22)") should return False
>>> wellbracketed("(x+y)(x-y)") should return True
>>> wellbracketed("(x(y+z)-a)((b+c)") should return False

2. A positive integer m is a sum of squares if it can be written as k + x where k >


0, x > 0 and both k and x are perfect squares. Write a Python function
sumofsquares(m) that takes an integer m returns True if m is a sum of squares
and False otherwise. Here are some examples to show how your function
should work.
>>> sumofsquares(41) should return True
>>> sumofsquares(30) should return False
>>> sumofsquares(17) should return True

3. Write a Python function to get a string made of the first 2 and the last 2 chars
from a given string. If the string length is less than 2, return empty string,
otherwise return new string created.

4. Write a Python program to add 'ing' at the end of a given string (length should
be at least 3). If the given string already ends with 'ing' then add 'ly' instead. If
the string length of the given string is less than 3, leave it unchanged Sample
String : 'abc' Expected Result : 'abcing'
Sample String : 'string' Expected Result : 'stringly‘

You might also like