Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Week-3 Python Lab Programs

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

Week-3 Python Lab Programs

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

WEEK-3

1.
I) Write a program to convert a list and tuple into arrays
import numpy as np
#Declare List Elements
my_list = []
list_length = int(input("Enter No of List Elements:" ))
for i in range(list_length):
lst = int(input('Enter Element: '))
my_list.append(lst)
#Display List Elements
print("\n List Elements are: ",end=' ')
for i in my_list:
print(i,end=' ')
#Convert and Display Elements from List to Array
array1=np.asarray(my_list)
print("\n Convert After list to array is : ",array1)

#Declare Tuple Elements


my_tuple = []
tuple_length = int(input("Enter No of List Elements:" ))
for i in range(tuple_length):
tpl = int(input('Enter Element: '))
my_tuple.append(tpl)
#Display List Elements
print("\n Tuple Elements are: ",end=' ')
for i in my_tuple:
print(i,end=' ')
#Convert and Display Elements from List to Array
array2=np.asarray(my_tuple)
print("\n Convert After list to array is : ",array2)
1.

II)Write a program to find common values between two arrays.

from numpy import *


#Declare Array1 Elements
arr1 = array([],int)
length_arr1 = int(input("Enter the number of values you want: "))
for i in range(length_arr1):
values1 =int(input("Element: "))
arr1 = append(arr1, values1)
#Display Array1 Elements
print(arr1)
#Declare Array 2 elements
arr2 = array([],int)
length_arr2 = int(input("Enter the number of values you want: "))
for i in range(length_arr2):
values2 =int(input("Element: "))
arr2 = append(arr2, values2)
#Display Array2 Elements
print(arr2)
#Display Common values between two arrays
common=intersect1d(arr1, arr2)
print("\n Common values between two arrays",common)
2. Write a function called gcd that takes parameters a and b and returns their greatest common divisor

Program-1
# Python code to demonstrate naive
# method to compute gcd ( recursion )
def gcd(a, b):
if(b == 0):
return a
else:
return gcd(b, a % b)
# Driver program to test above function
m = int(input("Enter a Number: "))
n = int(input("Enter b Number: "))
print("The gcd of {0} and {1} is : ".format(m,n),end=' ')
print(gcd(m, n))

Program-2
# Recursive function to return gcd of a and b
def gcd(a, b):
# Everything divides 0
if (a == 0):
return b
if (b == 0):
return a
# base case
if (a == b):
return a
# a is greater
if (a > b):
return gcd(a-b, b)
return gcd(a, b-a)

# Driver program to test above function


m = int(input ("Please, Enter the First Value: "))
n = int(input ("Please, Enter the Second Value: "))
if(gcd(m, n)):
print('GCD of', m, 'and', n, 'is', gcd(m, n))
else:
print('not found')
3. Write a function called palindrome that takes a string argument and returns True if it is a palindrome
and False otherwise.
Remember that you can use the built-in function len to check the length of a string.

#Declare a Function with palindrome with argument str


def palindrome(str):
#use built-in function len to check the length of a string
for i in range(0, int(len(str)/2)):
if str[i] != str[len(str)-i-1]:
return False
return True

# main function
s = input("Enter a String: ")
#call the function to check the string is palindrome or not
ans =palindrome(s)
print(s," is Palindrome:",end='')
if (ans):
print("TRUE ")
else:
print("FALSE")

You might also like