diff --git a/Python/Arrays/largestelementinarray.py b/Python/Arrays/largestelementinarray.py new file mode 100644 index 0000000..d7f2fa8 --- /dev/null +++ b/Python/Arrays/largestelementinarray.py @@ -0,0 +1,24 @@ +# Python3 program to find maximum +# in arr[] of size n + +# python function to find maximum +# in arr[] of size n +def largest(arr,n): + + # Initialize maximum element + max = arr[0] + + # Traverse array elements from second + # and compare every element with + # current max + for i in range(1, n): + if arr[i] > max: + max = arr[i] + return max + +# Driver Code +arr = [10, 324, 45, 90, 9808] +n = len(arr) +Ans = largest(arr,n) +print ("Largest in given array is",Ans) + diff --git a/Python/Arrays/sumofarray.py b/Python/Arrays/sumofarray.py new file mode 100644 index 0000000..2650692 --- /dev/null +++ b/Python/Arrays/sumofarray.py @@ -0,0 +1,31 @@ +# Python 3 code to find sum +# of elements in given array +def _sum(arr): + + # initialize a variable + # to store the sum + # while iterating through + # the array later + sum=0 + + # iterate through the array + # and add each element to the sum variable + # one at a time + for i in arr: + sum = sum + i + + return(sum) + +# driver function +arr=[] +# input values to list +arr = [12, 3, 4, 15] + +# calculating length of array +n = len(arr) + +ans = _sum(arr) + +# display sum +print ('Sum of the array is ', ans) + diff --git a/Python/List/interchangefirstlastele.py b/Python/List/interchangefirstlastele.py new file mode 100644 index 0000000..48b5719 --- /dev/null +++ b/Python/List/interchangefirstlastele.py @@ -0,0 +1,18 @@ +# Python3 program to swap first +# and last element of a list + +# Swap function +def swapList(newList): + size = len(newList) + + # Swapping + temp = newList[0] + newList[0] = newList[size - 1] + newList[size - 1] = temp + + return newList + +# Driver code +newList = [12, 35, 9, 56, 24] + +print(swapList(newList)) diff --git a/Python/List/swap2elements.py b/Python/List/swap2elements.py new file mode 100644 index 0000000..4ca2b5f --- /dev/null +++ b/Python/List/swap2elements.py @@ -0,0 +1,14 @@ +# Python3 program to swap elements +# at given positions + +# Swap function +def swapPositions(list, pos1, pos2): + + list[pos1], list[pos2] = list[pos2], list[pos1] + return list + +# Driver function +List = [23, 65, 19, 90] +pos1, pos2 = 1, 3 + +print(swapPositions(List, pos1-1, pos2-1)) diff --git a/Python/string/checkpalindrome .py b/Python/string/checkpalindrome .py new file mode 100644 index 0000000..9a13893 --- /dev/null +++ b/Python/string/checkpalindrome .py @@ -0,0 +1,14 @@ +# function which return reverse of a string + +def isPalindrome(s): + return s == s[::-1] + + +# Driver code +s = "malayalam" +ans = isPalindrome(s) + +if ans: + print("Yes") +else: + print("No") diff --git a/Python/string/reversewords.py b/Python/string/reversewords.py new file mode 100644 index 0000000..5067ced --- /dev/null +++ b/Python/string/reversewords.py @@ -0,0 +1,16 @@ +# Function to reverse words of string + +def rev_sentence(sentence): + + # first split the string into words + words = sentence.split(' ') + + # then reverse the split string list and join using space + reverse_sentence = ' '.join(reversed(words)) + + # finally return the joined string + return reverse_sentence + +if __name__ == "__main__": + input = 'geeks quiz practice code' + print (rev_sentence(input))