diff --git a/LinearSearch.py b/LinearSearch.py new file mode 100644 index 0000000..85bc369 --- /dev/null +++ b/LinearSearch.py @@ -0,0 +1,25 @@ +# Function for linear search operation +def linear_search(array, element): + for i in range(len(array)): + + # Check if element is in specific index of the array + if array[i] == element: + print (f"{element} is in {i+1} position") + + # If the element is not present in the array, it prints not found + if element not in array: + print ("Not found") + +# Get the size of the array from the user +length = int(input('Enter the number of elements: ')) +my_list = [] + +# Get the input for the list +for i in range(length): + element = int(input(f"Enter {i+1} element: ")) + my_list.append(element) + +# Get the element whose position is to be searched for +position = int(input('Enter the element whose position you want to find: ')) + +linear_search(my_list,position) diff --git a/README.md b/README.md index e052e20..c90bfcd 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ Thanks! |[FileComparison.py](https://github.com/accakks/Simple-Programs-in-Python/blob/master/FileComparison.py)| Downloads and unzip zip files from a webpage, compares them and prints out duplicate sentences|[Aakanksha](https://github.com/accakks) | | [TicTacToe.py](https://github.com/accakks/Simple-Programs-in-Python/blob/master/TicTacToe.py) | 2 Player Tic Tac Toe | [Aakanksha](https://github.com/accakks) | | [RockPaperScissors.py](https://github.com/accakks/Simple-Programs-in-Python/blob/master/RockPaperScissors.py) | Rock Paper Scissors with Computer | [Aakanksha](https://github.com/accakks) | +| [LinearSearch.py](https://github.com/accakks/Simple-Programs-in-Python/blob/master/LinearSearch.py) | Linear Search | [Prathima Kadari](https://github.com/prathimacode-hub) | | [countCharacter.py](https://github.com/accakks/Simple-Programs-in-Python/blob/master/countCharacter.py) | Counts number of times each character occurs in the input string. | [Aakanksha](https://github.com/accakks) | | temperature_table.py | Prints a table of fahrenheit and Celsius values | [Eventhisone](https://github.com/eventhisone) | [calculatePi.py](https://github.com/accakks/Simple-Programs-in-Python/blob/master/calculatePi.py) | Calculates PI to a specified number of digits of accuracy. | [Joseph-Acevedo](https://github.com/joseph-acevedo) |