From d9acf975fe1d3c38c54955bd6df6baa4040aba53 Mon Sep 17 00:00:00 2001 From: Gauri Shrimali <47269380+GauriShrimali@users.noreply.github.com> Date: Wed, 29 Apr 2020 00:50:35 +0530 Subject: [PATCH 1/4] ASSIGNMENT 6 SOLUTIONS (Q1 & Q2) --- 17EGICS030_HASH_1.py | 21 +++++++++++++++++++++ 17EGICS030_HASH_2.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 17EGICS030_HASH_1.py create mode 100644 17EGICS030_HASH_2.py diff --git a/17EGICS030_HASH_1.py b/17EGICS030_HASH_1.py new file mode 100644 index 0000000..b7beb3f --- /dev/null +++ b/17EGICS030_HASH_1.py @@ -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)) \ No newline at end of file diff --git a/17EGICS030_HASH_2.py b/17EGICS030_HASH_2.py new file mode 100644 index 0000000..d10196f --- /dev/null +++ b/17EGICS030_HASH_2.py @@ -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 Date: Wed, 29 Apr 2020 23:47:14 +0530 Subject: [PATCH 2/4] ASSIGNMENT-7 SOLUTIONS(Q3 & Q4) --- 17EGICS030_MAP_1.py | 19 +++++++++++++++++++ 17EGICS030_MAP_2.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 17EGICS030_MAP_1.py create mode 100644 17EGICS030_MAP_2.py diff --git a/17EGICS030_MAP_1.py b/17EGICS030_MAP_1.py new file mode 100644 index 0000000..24ac921 --- /dev/null +++ b/17EGICS030_MAP_1.py @@ -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 elemnts: ") + +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) \ No newline at end of file diff --git a/17EGICS030_MAP_2.py b/17EGICS030_MAP_2.py new file mode 100644 index 0000000..8976619 --- /dev/null +++ b/17EGICS030_MAP_2.py @@ -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)) \ No newline at end of file From a3806e6abfbd6f8212f62726e9537f83e4b9b77c Mon Sep 17 00:00:00 2001 From: Gauri Shrimali <47269380+GauriShrimali@users.noreply.github.com> Date: Wed, 29 Apr 2020 23:48:14 +0530 Subject: [PATCH 3/4] Update 17EGICS030_MAP_1.py --- 17EGICS030_MAP_1.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/17EGICS030_MAP_1.py b/17EGICS030_MAP_1.py index 24ac921..41f0388 100644 --- a/17EGICS030_MAP_1.py +++ b/17EGICS030_MAP_1.py @@ -3,7 +3,7 @@ # and it should return false if every element is distinct. size=int(input(("Enter the size of array: "))) -print("Enter elemnts: ") +print("Enter elements: ") d=dict() flag=False @@ -16,4 +16,4 @@ d[num]+=1 flag=True -print("RESULT OF DUPLICACY: ",flag) \ No newline at end of file +print("RESULT OF DUPLICACY: ",flag) From 9227bf22fd94a8581403a25915ed5d95bef2007b Mon Sep 17 00:00:00 2001 From: Gauri Shrimali <47269380+GauriShrimali@users.noreply.github.com> Date: Sat, 2 May 2020 23:35:18 +0530 Subject: [PATCH 4/4] ASSIGNMENT-8 STACK SOLUTIONS (Q1 & Q2) --- 17EGICS030_STACK_1.py | 27 +++++++++++++++++++++++++++ 17EGICS030_STACK_2.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 17EGICS030_STACK_1.py create mode 100644 17EGICS030_STACK_2.py diff --git a/17EGICS030_STACK_1.py b/17EGICS030_STACK_1.py new file mode 100644 index 0000000..22f569e --- /dev/null +++ b/17EGICS030_STACK_1.py @@ -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)) \ No newline at end of file diff --git a/17EGICS030_STACK_2.py b/17EGICS030_STACK_2.py new file mode 100644 index 0000000..2ba946a --- /dev/null +++ b/17EGICS030_STACK_2.py @@ -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)) \ No newline at end of file