[go: up one dir, main page]

0% found this document useful (0 votes)
3 views11 pages

assignment function solutions

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 11

COMPUTER SCIENCE

ASSIGNMENT-

FUNCTIONS
# Write a Python function to multiply all the numbers
in a list.SAMple List : (8, 2, 3, -1, 7)
Expected Output : -336
def multiply(numbers):
total = 1
for x in numbers:
total *= x
return total
print(multiply((8, 2, 3, -1, 7)))

# Write a Python program to reverse a string.


Sample String : "1234abcd"
ExpectedOutput"dcba4321"
def string_reverse(str1):
rstr1 = ''
index = len(str1)
while index > 0:
rstr1 += str1[ index - 1 ]
index = index - 1
return rstr1
print(string_reverse('1234abcd'))
# Write a Python function to calculate the factorial
of a number (a non-negative integer). The function
accepts the number as an argument.
def factorial(n):

if n == 0:

return 1

else:

return n * factorial(n-1)

n=int(input("Input a number to compute the factiorial : "))

print(factorial(n))
# Write a Python function to check whether a
number is in a given range.
def testingrange(n):

if n in range(3,9):

print( " %s is in the range"%str(n))

else :

print("The number is outside the given range.")

testingrange(5)
# Write a Python function that accepts a string and
calculate the number of upper case letters and lower case
letters. Sample String : 'The quick Brow Fox'
Expected Output :
No. of Upper case characters : 3
No. of Lower case Characters : 12
def stringtesting(s):
k={"UPPER_CASE":0, "LOWER_CASE":0}
for c in s:
if c.isupper():
k["UPPER_CASE"]+=1
elif c.islower():
k["LOWER_CASE"]+=1
else:
pass
print ("Original String : ", s)
print ("No. of Upper case characters : ", k["UPPER_CASE"])
print ("No. of Lower case Characters : ", k["LOWER_CASE"])

stringtesting('The quick Brown Fox')


# Write a Python function that takes a list and returns a new
list with unique elements of the first list. Sample List :
[1,2,3,3,3,3,4,5]
Unique List : [1, 2, 3, 4, 5]
def unique_list(l):
x = []
for a in l:
if a not in x:
x.append(a)
return x

print(unique_list([1,2,3,3,3,3,4,5]))
# Write a Python function that takes a number as a
parameter and check the number is prime or not.
def testprimenumber(n):
if (n==1):
return False
elif (n==2):
return True;
else:
for x in range(2,n):
if(n % x==0):
return False
return True
print(testprimenumber(11))
# Write a Python function that checks whether a passed
string is palindrome or not. Note: A palindrome is a word,
phrase, or sequence that reads the same backward as
forward, e.g., madam or nurses run.
def Palindrome(string):
left= 0
right= len(string) - 1

while right>=left:
if not string[left] == string[right]:
return False
left+=1
right-=1
return True
print(Palindrome('madam'))
# Write a Python function that prints out the first n rows
of Pascal's triangle. Note : Pascal's triangle is an
arithmetic and geometric figure first imagined by Blaise
Pascal.
def pascaltriangle(n):
row = [1]
y = [0]
for x in range(max(n,0)):
print(row)
row=[l+r for l,r in zip(row+y, y+row)]
return n>=1
pascaltriangle(6)
DIFFERENCE BETWEEN FORMAL PARAMETERS AND ACTUAL
PARAMETERS=
formal parameter — the identifier used in a method to stand for
the value that is passed into the method by a caller.
actual parameter — the actual value that is passed into the method
by a caller.
def addition(x, y) #X AND Y ARE FORMAL PARAMETERS
addition = x+y
print(f”{addition}”)
addition(2, 3) #2 AND 3 ARE ACTUAL PARAMETERS
addition(4, 5) #4 AND 5 ARE ALSO ACTUAL PARAMETRS

You might also like