Computer Science, asked by ashutoshsingh5452, 1 month ago

python question class 12th computer science

note - without using inbuilt functions

WAP to accept a List of numbers and display the following menu:

a) Reverse of List
b) Maximum of List
c) Search in List
d) Sum of all the elements of the List
e) Copy all the even elements in another list​

Answers

Answered by anindyaadhikari13
7

\textsf{\large{\underline{Solution}:}}

The given problem is solved using language - Python.

Note: No inbuilt functions are used.

n=int(input('How many elements for the list? '))

arr=list()

print('Enter the elements..')

for i in range(n):

   arr.append(int(input('Enter: ')))

print('1. Reverse The List.')

print('2. Maximum of List.')

print('3. Search in list.')

print('4. Sum of all elements in list.')

print('5. Copy all the even elements in another list.')

ch=int(input('Enter your choice: '))

if ch==1:

   print('Given List:',arr)

   arr=arr[::-1]

   print('Reversed List:',arr)

elif ch==2:

   max=arr[0]

   for i in range(1,n):

       if arr[i]>max:

           max=arr[i]

   print('Given list:',arr)

   print('Largest Number:',max)

elif ch==3:

   print('Given list:',arr)

   y=int(input('Enter the element to be searched for: '))

   found=False

   for i in range(n):

       if arr[i]==y:

           Found=True

           print('Element found in list at index',i)

   if not Found:

       print('Element not found in list.')

elif ch==4:

   print('Given List:',arr)

   s=0

   for i in arr:

       s+=i

   print('Sum of the elements in the list is:',s)

elif ch==5:

   even_items=[i for i in arr if i%2==0]

   print('Given List:',arr)

   print('Even items from the list:',even_items)

else:

   print('Invalid Choice.')

\textsf{\large{\underline{Logic Used}:}}

  1. To reverse the list: Use list slicing.
  2. To find the maximum of list: Assume that the largest number is present at the first index. Then transverse through array elements. If any number is found greater than the max variable, store that number in 'max'. Finally display it.
  3. To search for an element in list: Transverse through array elements. If the array element is equal to the number, display its location and a message that the number is found. If not found, display an error message.
  4. Sum of all elements: Initialize sum = 0. Use for each loop to transverse through array elements. Add each elements one by one and display it.
  5. Copy all even elements in another list: Use list comprehension for that.

anindyaadhikari13: Thanks for the brainliest :)
Similar questions