Skip to content

Commit bda81c7

Browse files
Stack,Queue,Reverse String,Anagram,Count Letters in String
These are programs that are basic but also necessary. lgtm
1 parent 2607be0 commit bda81c7

6 files changed

Lines changed: 118 additions & 0 deletions

AnagramString.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#a word, phrase, or name formed by rearranging the letters of another
2+
#example silent and listen
3+
#a good additionalitt to this program would be to check whether they are actuallly words or not :)
4+
5+
def mysort(s): #function that splits the letters
6+
d=sorted(s)
7+
s=''.join(d)
8+
return s
9+
10+
s1=input("enter first string")
11+
n1=mysort(s1) #function invocation /calling the function
12+
13+
s2=input("enter second string")
14+
n2=mysort(s2)
15+
16+
if n1==n2:
17+
print("anagram")
18+
else:
19+
print("not an anangram")
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
word=input("enter a word ")
2+
3+
dict={}
4+
5+
for c in word:
6+
co = dict.get(c,0)
7+
if co == 0:
8+
dict[c]=1
9+
else:
10+
dict[c]=co+1
11+
print(dict)

New Text Document.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
stack=[]
2+
try:
3+
while True:
4+
op = int(input("1 push 2 pop 3 display 4 exit"))
5+
if op==1:
6+
ele = int(input("enter elem to push "))
7+
stack.append(ele)
8+
elif op==2:
9+
if len(stack) ==0:
10+
print("empty hai stack ")
11+
else:
12+
ele=stack.pop()
13+
print("popped elements ")
14+
elif op==3:
15+
print(stack)
16+
elif op==4:
17+
break
18+
else:
19+
print("invalid option")

QueueUsingList.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
try: #try catch so that the program does not crash
2+
queue=[]
3+
4+
while True:
5+
op = int(input("Press--> 1 to insert into queue | 2 to remove from queue | 3 to display values of queue | 4 to reverse the exisiting queue| 5 to exit "))
6+
if op==1: #to insert an elelment in the queue
7+
ele = int(input("enter elem to insert "))
8+
queue.append(ele)
9+
elif op==2: #to remove an element from the queue
10+
if len(queue) ==0:
11+
print("The queue is empty, insert values if required")
12+
else:
13+
ele=queue.pop(0)
14+
print(ele)
15+
elif op==3: #to display the elements in the queue
16+
print(queue)
17+
elif op==4: #to reverse queue
18+
queue.reverse()
19+
elif op==5: #to exit
20+
break
21+
else:
22+
print("invalid option")
23+
24+
except ValueError:
25+
print("Please enter integer only") #If user inputs an alphabet or string the program should not crash
26+
except:
27+
print("There's been some issue please check the data you've entered")
28+

ReverseString.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def rev_string(s):
2+
a=s[::-1]
3+
return a
4+
5+
s1=input("enter the string ")
6+
r1=rev_string(s1)
7+
8+
print(r1)

StackUsingList.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#LIFO
2+
3+
stack=[]
4+
try: #try except so that the program does not crash[optional]
5+
while True:
6+
op = int(input("Select---> 1 to push, 2 to pop top most element, 3 to display elements in stack,4 to peek,5 to exit "))
7+
if op==1: #to push into stack
8+
ele = int(input("enter elem to push "))
9+
stack.append(ele)
10+
elif op==2: #to pop from stack. If stack is empty it will display "Stack is empty"
11+
if len(stack) ==0:
12+
print("Stack is empty ")
13+
else:
14+
ele=stack.pop()
15+
print("popped element ",ele)
16+
elif op==3: #TO display stack elements and print "Stack is empty " if there are no elements
17+
if len(stack) ==0:
18+
print("Stack is empty ")
19+
else:
20+
print(stack)
21+
elif op==4: #To peek at stack top. i.e see the topmost element
22+
if len(stack) ==0:
23+
print("Stack is empty ")
24+
else:
25+
print(stack[-1])
26+
elif op==5: #To exit from loop
27+
break
28+
else:
29+
print("invalid option")
30+
except ValueError:
31+
print("Please enter integer only") #If user inputs an alphabet or string the program should not crash
32+
except:
33+
print("There's been some issue please check the data you've entered")

0 commit comments

Comments
 (0)