Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions 17EGICS030_HASH_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#Given an integer array arr, count element x such that x + 1 is also in arr.
#If there are duplicates in arr, count them separately.

#LOGIC
def elements(ele,arr):
count=0
for e in ele:
count+=arr.count(e)
return count

#INPUT
size=int(input(("Enter the size of array: ")))
arr=[]

ele=set()
for i in range(size):
num=int(input())
arr.append(num)
if num-1 in arr:
ele.add(num-1)
print("Number of elements: ",elements(ele,arr))
34 changes: 34 additions & 0 deletions 17EGICS030_HASH_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#Given an array of integers, return the number of pairs such that they add up to a specific target.
#You may assume that each input would have exactly one solution, and you may not use the same element twice.

#LOGIC
def pair(arr,t):
pairs=set()
flag=True
while flag:
for ele in arr:
if ele<t and (t-ele in arr):
a1=ele
a2=t-ele
pairs.add((a1,a2))
arr.remove(a1)
arr.remove(a2)
break
else:
arr.remove(ele)
break
if len(arr)<2:
flag=False
print("PAIRS ARE: ",pairs)
return len(pairs)

#INPUT
size=int(input(("Enter the size of array: ")))
t=int(input("Enter target value: "))
arr=[]

for i in range(size):
num=int(input())
arr.append(num)

print("Number of pairs: ",pair(arr,t))
19 changes: 19 additions & 0 deletions 17EGICS030_MAP_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#Given an array of integers, find if the array contains any duplicates.
#Your function should return true if any value appears at least twice in the array,
# and it should return false if every element is distinct.

size=int(input(("Enter the size of array: ")))
print("Enter elements: ")

d=dict()
flag=False

for i in range(size):
num=int(input())
if num not in d:
d[num]=1
else:
d[num]+=1
flag=True

print("RESULT OF DUPLICACY: ",flag)
30 changes: 30 additions & 0 deletions 17EGICS030_MAP_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#Given a pattern and a string str, find if str follows the same pattern.
#Here follow means a full match, such that there is a bijection between a
# letter in pattern and a non-empty word in str.

def compare(pattern,strng):
p=[ch for ch in pattern]
s=list(strng.split(' '))
if len(p)!=len(s):
return False

com=dict()
f=True

for i,j in zip(p,s):
if i not in com.keys():
com[i]=j
elif i in com.keys() and j != com[i]:
f=False
return False
elif p.index(i)==len(p)-1 or s.index(j)==len(p)-1:
return False
else:
continue
if f:
return True

pattern=input("Enter pattern: ")
strng=input("Enter string: ")

print(compare(pattern,strng))
27 changes: 27 additions & 0 deletions 17EGICS030_STACK_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Given a string containing just the characters '(', ')' determine if the input string is valid or not.
# An input string is valid if:
# Open brackets must be closed by the same type of brackets.
# Open brackets must be closed in the correct order.

def stack(inp):
if len(inp)==0:
return True
stack=[]
for i in inp:
if i == '(':
stack.append('x')
elif i == ')' and len(stack)==0:
return False
elif i == ')':
stack.pop()
else:
continue

if len(stack) != 0:
return False
else:
return True


inp=input("Enter the input string: ")
print(stack(inp))
33 changes: 33 additions & 0 deletions 17EGICS030_STACK_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#Given two strings S and T, return if they are equal when both are typed into empty text editors.
# means a backspace character.Note that after backspacing an empty text, the text will continue empty.

def strings(inp1,inp2):
s1=[]
s2=[]
f1=True
f2=True
for i in inp1:
if i == '#' and len(s1)==0:
f1=False
elif i == '#':
s1.pop()
else:
s1.append(i)

for i in inp2:
if i == '#' and len(s2)==0:
f2=False
elif i == '#':
s2.pop()
else:
s2.append(i)

if (not(f1 and f2)) or s1==s2:
return True
else:
return False


inp1=input("Enter first input string: ")
inp2=input("Enter second input string: ")
print(strings(inp1,inp2))