Skip to content

Commit 1661130

Browse files
committed
udemy course questions
1 parent 1a252d3 commit 1661130

File tree

8 files changed

+150
-2
lines changed

8 files changed

+150
-2
lines changed

Graphs/Dijkstra.py

Whitespace-only changes.

Lists/MissingElementFinder.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'''
2+
3+
Consider an array of non-negative integers. A second array is formed by shuffling the elements of the first array and deleting a random element. Given these two arrays, find which element is missing in the second array.
4+
5+
Here is an example input, the first array is shuffled and the number 5 is removed to construct the second array.
6+
7+
Input:
8+
9+
finder([1,2,3,4,5,6,7],[3,7,2,1,4,6])
10+
Output:
11+
12+
5 is the missing number
13+
14+
'''
15+
16+
17+
def missingElement(arr1,arr2):
18+
19+
if len(arr1)==len(arr2):
20+
return "Nothing is Missing"
21+
22+
arr1.sort()
23+
arr2.sort()
24+
25+
for num1,num2 in zip(arr1,arr2):
26+
if num1!=num2:
27+
return num1
28+
29+
30+
return arr1[-1] #because zip wont form a tuple of last element which wont have a pair in arr2
31+
32+
33+
arr1 = [1,2,3,4,5,6,7]
34+
arr2 = [3,5,2,1,4,6]
35+
36+
37+
print(missingElement(arr1,arr2))

Lists/largestContinuousSum.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
'''
3+
4+
Given an array of integers (positive and negative) find the largest continuous sum.
5+
6+
'''
7+
8+
9+
def large_cont_sum(arr):
10+
11+
if len(arr)==0:
12+
return 0
13+
14+
max_sum = current_sum = arr[0]
15+
16+
for num in arr[1:]:
17+
18+
current_sum = max(current_sum + num, num)
19+
20+
max_sum = max(max_sum, current_sum)
21+
22+
return max_sum
23+
24+
arr1 = [1,2,-1,3,4,10,10,-10,-1]
25+
26+
27+
print(large_cont_sum(arr1)) #29

Misc/Valid Paranthesis - Leet Code.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ def isValid(self, s):
1616
elif char in dict.keys():
1717
if (stack == [] or dict[char] != stack.pop()): #.pop spits out last element
1818
return False
19-
19+
2020
return stack == []

Strings/ReverseSentence.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
def rev_word1(s):
2+
return " ".join(reversed(s.split()))
3+
4+
#Or
5+
6+
def rev_word2(s):
7+
return " ".join(s.split()[::-1])
8+
9+
#or
10+
11+
def rev_word3(s):
12+
"""
13+
Manually doing the splits on the spaces.
14+
"""
15+
16+
words = []
17+
length = len(s)
18+
spaces = [' ']
19+
20+
# Index Tracker
21+
i = 0
22+
23+
# While index is less than length of string
24+
while i < length:
25+
26+
# If element isn't a space
27+
if s[i] not in spaces:
28+
29+
# The word starts at this index
30+
word_start = i
31+
32+
while i < length and s[i] not in spaces:
33+
34+
# Get index where word ends
35+
i += 1
36+
# Append that word to the list
37+
words.append(s[word_start:i])
38+
# Add to index
39+
i += 1
40+
41+
# Join the reversed words
42+
return " ".join(reversed(words))
43+
44+
45+
rev_word1(' Hello John how are you ')
46+
rev_word2(' Hello John how are you ')
47+
rev_word3(' Hello John how are you ')
48+
49+
50+
#ANS: 'you are how John Hello'

Strings/StringCompression.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'''
2+
Given a string in the form 'AAAABBBBCCCCCDDEEEE' compress it to become 'A4B4C5D2E4'. For this problem, you can falsely "compress" strings of single or double letters. For instance, it is okay for 'AAB' to return 'A2B1' even though this technically takes more space.
3+
4+
The function should also be case sensitive, so that a string 'AAAaaa' returns 'A3a3'.
5+
'''
6+
7+
def string_Comp(s):
8+
9+
l = len(s)
10+
11+
if l == 0:
12+
return ""
13+
14+
if l == 1:
15+
return s+"1"
16+
17+
dic = {}
18+
19+
for char in s:
20+
21+
if char in dic:
22+
dic[char] +=1
23+
else:
24+
dic[char] = 1
25+
26+
final = ''
27+
28+
for key,value in dic.items():
29+
final +=key+str(value)
30+
31+
return final
32+
33+
34+
print(string_Comp('AAAABaa'))

Strings/amazingQuestion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
1. Create a temp string and store concatenation of str1 to
1010
str1 in temp.
1111
temp = str1.str1
12-
2. If str2 is a substring of temp then str1 and str2 are
12+
2. If str2 is a substring of temp then str1 and str2 are
1313
rotations of each other.
1414

1515
Example:

notes.pages

976 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)