Skip to content
Merged

List #14

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Python/Arrays/largestelementinarray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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)

# This code is contributed by Smitha Dinesh Semwal
32 changes: 32 additions & 0 deletions Python/Arrays/sumofarray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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)

# This code is contributed by Himanshu Ranjan
18 changes: 18 additions & 0 deletions Python/List/interchangefirstlastele.py
Original file line number Diff line number Diff line change
@@ -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))
14 changes: 14 additions & 0 deletions Python/List/swap2elements.py
Original file line number Diff line number Diff line change
@@ -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))