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 b33d9aa

Browse files
Added More Practice Programs.
1 parent 3c9d62a commit b33d9aa

5 files changed

+122
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'''
2+
List = [5, 10, 15, 20, 25]
3+
4+
After shifting elements to the Left:
5+
List = [10, 15, 20, 25, 5]
6+
7+
After shifting elements to the Right:
8+
List = [25, 5, 10, 15, 20]
9+
'''
10+
11+
12+
emptyList = []
13+
sizeOfList = int(input('Enter size of the list: '))
14+
for i in range(sizeOfList):
15+
values = int(input('Enter values to store in a list: '))
16+
emptyList.append(values)
17+
print(emptyList)
18+
19+
# For Left Shift:
20+
# key = emptyList[0]
21+
# for i in range(1, sizeOfList):
22+
# emptyList[i - 1] = emptyList[i]
23+
# emptyList[sizeOfList - 1] = key
24+
# print('List after shifting all the elements to one step Left =', emptyList)
25+
26+
27+
28+
# For Right Shift:
29+
# key = emptyList[sizeOfList - 1]
30+
# for i in range(sizeOfList - 2, -1, -1):
31+
# emptyList[i + 1] = emptyList[i]
32+
# emptyList[0] = key
33+
# print('List after shifting all the elements to one step Right =', emptyList)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
emptyList = []
2+
sizeOfList = int(input('Enter size of the List: '))
3+
for i in range(sizeOfList):
4+
values = int(input('Enter values to store in a List: '))
5+
emptyList.append(values)
6+
print(emptyList)
7+
insertValue = int(input('Enter value to insert in a List: '))
8+
position = int(input('Enter Position at which you want to insert an element in a List: '))
9+
emptyList.append(None)
10+
for i in range(sizeOfList - 1, position - 2, -1):
11+
emptyList[i + 1] = emptyList[i]
12+
emptyList[position - 1] = insertValue
13+
print('List after inserting', insertValue, 'to the', position, 'position is =', emptyList)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'''
2+
lst = [5, 10, 15, 20, 25]
3+
4+
List after deleting 10 is:
5+
lst = [5, 15, 20, 25]
6+
'''
7+
8+
# This program is used to delete first occurence of a deleting element.
9+
10+
emptyList = []
11+
sizeOfList = int(input('Enter size of the List: '))
12+
for i in range(sizeOfList):
13+
values = int(input('Enter values to store in a List: '))
14+
emptyList.append(values)
15+
print('Original List =',emptyList)
16+
deleteValue = int(input('Enter value which you want to delete from a list: '))
17+
flag = 0
18+
for i in range(sizeOfList):
19+
if(emptyList[i] == deleteValue):
20+
flag = 1
21+
position = i
22+
break
23+
if(flag == 0):
24+
print('The element which you want to delete from a list is not in the list!')
25+
else:
26+
for i in range(position, sizeOfList - 1):
27+
emptyList[i] = emptyList[i + 1]
28+
# emptyList.pop() # It deletes last element of a list.
29+
emptyList.pop(sizeOfList - 1) # you can use this logic as well.
30+
print('List after deleting', deleteValue, 'from a list =', emptyList)
31+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
emptyList = []
2+
sizeOfList = int(input('Enter size of the List: '))
3+
for i in range(sizeOfList):
4+
values = int(input('Enter values to store in a List: '))
5+
emptyList.append(values)
6+
print('Original List =',emptyList)
7+
shiftValue = int(input("Enter No of position you want to shift in Left/Right: "))
8+
9+
# For N steps right shift:
10+
for count in range(shiftValue):
11+
storeValue = emptyList[sizeOfList - 1]
12+
for i in range(sizeOfList - 2, -1, -1):
13+
emptyList[i + 1] = emptyList[i]
14+
emptyList[0] = storeValue
15+
print(f"List after shifting elements to {shiftValue} steps right is {emptyList}")
16+
17+
18+
# For N steps left shift
19+
'''
20+
for count in range(shiftValue):
21+
storeValue = emptyList[0]
22+
for i in range(1, sizeOfList):
23+
emptyList[i - 1] = emptyList[i]
24+
emptyList[sizeOfList - 1] = storeValue
25+
print(f"List after shifting elements to {shiftValue} steps left is {emptyList}")
26+
'''
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'''
2+
If n = 4 then 1 +γŸγ™ 2 +γŸγ™ 3 +γŸγ™ 4 =わ 10
3+
we start our program from 4 to 1
4+
4 => Recursion start from 4
5+
3
6+
2
7+
1 => This is the base case of this program so recursion stops here.
8+
'''
9+
10+
11+
def Sum(n):
12+
if n == 1:
13+
return 1
14+
else:
15+
return (n + Sum(n - 1))
16+
17+
n = int(input("Enter number upto which you want to sum: "))
18+
sum = Sum(n)
19+
print(f"Sum of numbers from {1} to {n} is {sum}")

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /