Skip to content
Open
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
41 changes: 16 additions & 25 deletions problems/easy/easy_q16.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,23 @@
# Print the Sum of First and Last Array Element
def sum_first_last(arr):

for i in range(len(arr)):
return arr[0] + arr[-1]


if len(arr) < 2:
return "Array must have at least two characters."
return "Array must have at least two elements."
return arr[0] + arr[-1]


if __name__ == "__main__":
user = [int(x) for x in (input("Enter a collection number:")).split(",")]

print(sum_first_last(user))


arr=int()
sum_first_last()

arr=[]
n=int(input("Enter the Total number of Elements:"))
# Input 1: user enters comma-separated numbers
user_input = input("Enter a collection of numbers separated by commas: ")
user = [int(x.strip()) for x in user_input.split(",")]
print("Sum of first and last element:", sum_first_last(user))

# Input 2: user enters numbers one by one
arr = []
n = int(input("Enter the total number of elements: "))
for i in range(n):
x=int(input("Enter the element :"))
x = int(input(f"Enter element {i+1}: "))
arr.append(x)

arr=[2,5,8,7,6]

print(sum_first_last(arr))


print("Sum of first and last element:", sum_first_last(arr))

# Test with a predefined list
arr = [2, 5, 8, 7, 6]
print("Sum of first and last element (predefined list):", sum_first_last(arr))