问题
Functions and Methods Homework
Complete the following questions:
Write a function that computes the volume of a sphere given its radius.
The volume of a sphere is given as
In [1]:
def vol(rad): pass
In [2]:
# Check vol(2)
Out[2]:
33.49333333333333
Write a function that checks whether a number is in a given range (inclusive of high and low)
In [3]:
def ran_check(num,low,high): pass
In [4]:
# Check ran_check(5,2,7)
5 is in the range between 2 and 7
If you only wanted to return a boolean:
In [5]:
def ran_bool(num,low,high): pass
In [6]:
ran_bool(3,1,10)
Out[6]:
True
Write a Python function that accepts a string and calculates the number of upper case letters and lower case letters.
Sample String : 'Hello Mr. Rogers, how are you this fine Tuesday?'
Expected Output :
No. of Upper case characters : 4
No. of Lower case Characters : 33
HINT: Two string methods that might prove useful: .isupper() and .islower()
If you feel ambitious, explore the Collections module to solve this problem!
In [7]:
def up_low(s): pass
In [8]:
s = 'Hello Mr. Rogers, how are you this fine Tuesday?' up_low(s)
Original String : Hello Mr. Rogers, how are you this fine Tuesday? No. of Upper case characters : 4 No. of Lower case Characters : 33
Write a Python function that takes a list and returns a new list with unique elements of the first list.
Sample List : [1,1,1,1,2,2,3,3,3,3,4,5]
Unique List : [1, 2, 3, 4, 5]
In [9]:
def unique_list(lst): pass
In [10]:
unique_list([1,1,1,1,2,2,3,3,3,3,4,5])
Out[10]:
[1, 2, 3, 4, 5]
Write a Python function to multiply all the numbers in a list.
Sample List : [1, 2, 3, -4]
Expected Output : -24
In [11]:
def multiply(numbers): pass
In [12]:
multiply([1,2,3,-4])
Out[12]:
-24
Write a Python function that checks whether a passed in string is palindrome or not.
Note: A palindrome is word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run.
In [13]:
def palindrome(s): pass
In [14]:
palindrome('helleh')
Out[14]:
True
Hard:
Write a Python function to check whether a string is pangram or not.
Note : Pangrams are words or sentences containing every letter of the alphabet at least once.
For example : "The quick brown fox jumps over the lazy dog"
Hint: Look at the string module
In [15]:
import string def ispangram(str1, alphabet=string.ascii_lowercase): pass
In [16]:
ispangram("The quick brown fox jumps over the lazy dog")
Out[16]:
True
In [17]:
string.ascii_lowercase
Out[17]:
'abcdefghijklmnopqrstuvwxyz'
答案
def ran_check(num,low,high):
return "num: %d is in the range between %d and %d"%(num,low,high) if int(num) <= int(high) and int(num) >= int(low) else print("no")
ran_check(5,2,6)
def up_low(s):
s=re.split(' |,|?',s)
s1,s2=0,0
for i in s:
for t in i:
if t.isupper():
s1+=1
if t.islower():
s2+=1
return s1,s2
up_low('Hello Mr. Rogers, how are you this fine Tuesday?')
def unique_test(x):
return np.unique(x)
unique_test([1,1,1,1,2,2,3,3,3,3,4,5])
def multipy(x):
s=0
t=x[s]
while s<=(len(x)-2):
t=t*x[s+1]
s+=1
return t
multipy([1,2,3,-4])
def parlindrom(x):
x=x.replace(' ','')
t=len(x)/2
if str(t).isdigit():
x1=x[t:]
return x[0:t] == x1[::-1]
else:
x2=x[round(t)+1:]
return x[0:round(t)] == x2[::-1]
parlindrom('helleh')
parlindrom('madam')
parlindrom('nurses run')
import string
def ispangram(x):
alphat=set('abcdefghijklmnopqrstuvwxyz')
x=x.replace(' ','')
x=x.lower()
x=set(x)
return x == alphat
ispangram("The quick brown fox jumps over the lazy dog")