Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b959281

Browse files
Added more programs.
1 parent 174e0ff commit b959281

5 files changed

+118
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'''
2+
Frequency means how many times the element repeated in list.
3+
'''
4+
5+
emptyList = []
6+
for i in range(5):
7+
no = int(input('Enter number to store in a list: '))
8+
emptyList.append(no)
9+
findFrequency = int(input('Enter an element to find its frequency: '))
10+
print('Frequency of',findFrequency,'is',emptyList.count(findFrequency))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def LinearSearch(emptyList, sizeOfList, searchValue):
2+
flag = 0
3+
for i in range(sizeOfList):
4+
if emptyList[i] == searchValue:
5+
flag = 1
6+
position = i
7+
if flag == 1:
8+
print(f'The value you want to search in a list is at {position + 1} position.')
9+
else:
10+
print('Value not found!')
11+
12+
13+
# Driver Code/main section
14+
emptyList = []
15+
sizeOfList = int(input('Enter size of the list: '))
16+
for i in range(sizeOfList):
17+
value = int(input('Enter values to store in a list: '))
18+
emptyList.append(value)
19+
print(emptyList)
20+
searchValue = int(input('Enter value you want to search in a list: '))
21+
LinearSearch(emptyList, sizeOfList, searchValue)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
def BinarySearch(emptyList, sizeOfList, searchValue):
2+
i = 0
3+
j = (len(emptyList)) - 1
4+
flag = 0
5+
while (i <= j and flag == 0):
6+
mid = (i + j) // 2
7+
if (emptyList[mid] == searchValue):
8+
print('Value found!')
9+
position = mid
10+
flag = 1
11+
elif (emptyList[mid] > searchValue):
12+
j = mid - 1
13+
elif (emptyList[mid] < searchValue):
14+
i = mid + 1
15+
if flag == 1:
16+
print(f'The value you want to search in a list is at {position + 1} position.')
17+
else:
18+
print('Value not found!')
19+
20+
21+
# Driver Code/main section
22+
emptyList = []
23+
sizeOfList = int(input('Enter size of the list: '))
24+
for i in range(sizeOfList):
25+
value = int(input('Enter values to store in a list in ascending order: '))
26+
emptyList.append(value)
27+
print(emptyList)
28+
searchValue = int(input('Enter value you want to search in a list: '))
29+
BinarySearch(emptyList, sizeOfList, searchValue)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def InsertValue(emptyList, sizeOfList, insertValue):
2+
# if in case list is not sorted then first .sort() method apply on a list and then insert value in it.
3+
i = (len(emptyList)) - 1
4+
emptyList[i + 1] = emptyList.append(None)
5+
while (emptyList[i] > insertValue and i >= 0):
6+
emptyList[i + 1] = emptyList[i]
7+
i -= 1
8+
emptyList[i + 1] = insertValue
9+
print(f'List after inserting {insertValue} to its suitable index is {emptyList}')
10+
11+
12+
# Driver Code/main section
13+
emptyList = []
14+
sizeOfList = int(input('Enter size of the list: '))
15+
for i in range(sizeOfList):
16+
value = int(input('Enter values to store in a list in ascending order: '))
17+
emptyList.append(value)
18+
print(f'List before inserting a value is {emptyList}')
19+
insertValue = int(input('Enter value you want to insert in a list: '))
20+
InsertValue(emptyList, sizeOfList, insertValue)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
def DeleteValue(emptyList, sizeOfList, deleteValue):
2+
# if in case list is not sorted then first .sort() method apply on a list and then insert value in it.
3+
# when array is sorted so implementing binary search is a better option to find the position of
4+
# a value.
5+
i = 0
6+
j = (len(emptyList)) - 1
7+
flag = 0
8+
while (i <= j and flag == 0):
9+
mid = (i + j) // 2
10+
# for descending order list both conditions are reversed.
11+
if emptyList[mid] == deleteValue:
12+
position = mid
13+
flag = 1
14+
elif emptyList[mid] > deleteValue:
15+
j = mid - 1
16+
elif emptyList[mid] < deleteValue:
17+
i = mid + 1
18+
if flag == 1:
19+
return position
20+
else:
21+
return -1 # -1 means value not found.
22+
23+
24+
25+
# Driver code/main section
26+
emptyList = []
27+
sizeOfList = int(input('Enter size of the list: '))
28+
for i in range(sizeOfList):
29+
value = int(input('Enter values to store in a list in ascending order: '))
30+
emptyList.append(value)
31+
print(f'List before deletion of a desired element is:', emptyList)
32+
deleteValue = int(input('Enter value which you want to delete from a list: '))
33+
deletionFunction = DeleteValue(emptyList, sizeOfList, deleteValue)
34+
if deletionFunction == -1:
35+
print('The value you want to delete from a list is not present in the list.')
36+
else:
37+
del(emptyList[deletionFunction])
38+
print('List after deletion is', emptyList)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /