Jan-19-2018, 08:25 PM
Hello Programmers,
I am working on learning python. This isn't for school, so we don't need to be extremely rigid, I am more interested in learning how to utilize Python as opposed to Windows/DOS batch scripts.
Today I am on:
Search an Element in an array list
https://practice.geeksforgeeks.org/probl...an-array/0
I currently have:
The issue is that it is returning -1 for both valid and invalid input values. Please let me know if anyone may have some help. Thank you!
I am working on learning python. This isn't for school, so we don't need to be extremely rigid, I am more interested in learning how to utilize Python as opposed to Windows/DOS batch scripts.
Today I am on:
Search an Element in an array list
https://practice.geeksforgeeks.org/probl...an-array/0
Quote:FROM
https://practice.geeksforgeeks.org/probl...an-array/0
Search an Element in an array
Show Topic Tags
Given an integer array and an element x, find if element is present in array or not. If element is present, then print index of its first occurrence. Else print -1.
Input:
First line contains an integer, the number of test cases 'T' Each test case should contain an integer, size of array 'N' in the first line. In the second line Input the integer elements of the array in a single line separated by space. Element X should be inputted in the third line after entering the elements of array.
Output:
print the output in a separate line returning the index of the element X.If element not present then print -1.
Constraints:
1 <= T <= 100
1 <= N <= 100
1 <= Arr[i] <= 100
Example:
Input:
1
4
1 2 3 4
3
Output:
2
Explanation:
There is one test case with array as {1, 2, 3 4} and element to be searched as 3. Since 3 is present at index 2, output is 2
** For More Input/Output Examples Use 'Expected Output' option **
Author: shef5
I currently have:
#Given an integer list and an element x, find if element is present in list #or not. If element is present, then print index of its first occurrence. #Else print -1. listA = [1, 3, 99, 58, 22, 77, 8, 2] instructions = '''Please input a random integer between 1 - 100, this program will find if the element is present within the list.''' print(instructions) x = input() for i, number in enumerate(listA): y = listA[i] if y == x: print(i) break if y != x: print(-1)Which generates the output:
Quote: RESTART: C:/Users/.../SearchAnElementInList.py
Please input a random integer between 1 - 100,
this program will find if the element is present within the list.
99
-1
>>>
RESTART: C:/Users/.../SearchAnElementInList.py
Please input a random integer between 1 - 100,
this program will find if the element is present within the list.
1
-1
>>>
RESTART: C:/Users/.../SearchAnElementInList.py
Please input a random integer between 1 - 100,
this program will find if the element is present within the list.
88
-1
>>>
The issue is that it is returning -1 for both valid and invalid input values. Please let me know if anyone may have some help. Thank you!