Skip to content

Commit 102fc45

Browse files
committed
new ques
1 parent a8d7e2c commit 102fc45

22 files changed

+100
-75
lines changed

Lists/Circular_counter.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
a = ['1','2','3','4','5','6','7','8','9']
1313

1414
def harish(int_list, skip):
15-
skip = skip - 1 #we are doing this because list starts with 0 index
16-
idx = 0
17-
while len(int_list)>0:
18-
idx = (skip+idx)%len(int_list) #hashing to keep changing the index to every 3rd, idx should be smaller than len(list)-so using %
19-
print(int_list.pop(idx))
15+
skip = skip - 1 #we are doing this because list starts with 0 index
16+
idx = 0
17+
while len(int_list)>0:
18+
idx = (skip+idx)%len(int_list) #hashing to keep changing the index to every 3rd, idx should be smaller than len(list)-so using %
19+
print(int_list.pop(idx))
2020

2121
harish(a,3)
2222

Lists/LeastIntegerNotInAList.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
import itertools
77
sum_list = []
88
GivenArr = [1, 2, 5, 7]
9+
910
for L in range(0, len(GivenArr)+1): #for lengths of 0,1,2,3,4
1011
for subset in itertools.combinations(GivenArr, L): #find different subsets for each length in above line
1112
sum_list.append(sum(subset)) #write all the sums in to this newlist
1213

1314
new_list = list(set(sum_list)) #finding unique sums
1415
new_list.sort()
1516
print(new_list)
17+
1618
for each in range(0,new_list[-1]+2): #cuz if all possible sums are already possible, then "highest+1" is our ans
1719
if each not in new_list:
1820
print(each)

Lists/List - allPermutationsToAGivenSum.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1+
"""Given a list of integers and a target, print subset of list which add up to target.
2+
Note: Can be more than one subset """
3+
4+
5+
16
def subset_sum(numbers, target, partial=[]):
2-
print(partial)
7+
#print(partial)
38
s = sum(partial)
49

510
if(s == target):
611
print("sum of ",partial,"=",target)
712

8-
if s>= target:
13+
if s >= target:
914
return
1015

1116
for i in range(len(numbers)):
12-
print("iteration",i)
17+
#print("iteration",i)
1318
n = numbers[i]
1419
rest = numbers[i+1:]
1520
subset_sum(rest,target,partial + [n])

Lists/Queue-UsingTwoStacks.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,20 @@
1010
class Queue2Stacks(object):
1111

1212
def __init__(self):
13-
# Two Stacks
14-
self.in_stack = []
13+
14+
self.in_stack = [] # Two Stacks
1515
self.out_stack = []
1616

17-
def enqueue(self, element):
1817

18+
def enqueue(self, element):
1919
self.in_stack.append(element)
20-
pass
2120

22-
def dequeue(self):
2321

22+
def dequeue(self):
2423
if not self.out_stack:
2524
while self.in_stack:
2625
self.out_stack.append(self.in_stack.pop())
2726
return self.out_stack.pop()
28-
pass
29-
3027

3128

3229
q = Queue2Stacks()

Lists/Queues.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
"""Make a Queue class using a list!
2-
Hint: You can use any Python list method
3-
you'd like! Try to write each one in as
4-
few lines as possible.
5-
Make sure you pass the test cases too!"""
1+
"""Make a Queue class using a list!"""
62

73

84
class Queue:
95
def __init__(self, head=None):
106
self.storage = [head]
117

128
def enqueue(self, new_element):
13-
self.storage.append(new_element). #append generally appends at the tail
9+
self.storage.append(new_element) #append generally appends at the tail
1410

1511
def peek(self):
1612
return self.storage[0] # element at head . so storage[0]

Lists/SummaryOfRanges.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77

88
def summaryRanges(arr):
99
summary = []
10+
1011
if len(arr) == 1: # base condition
1112
return [str(arr[0])]
1213

1314
i = 0
15+
1416
while (i < len(arr)):
1517
temp = ""
1618
start = str(arr[i])

Lists/Top K Frequent elements.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ def topKFrequent(self, nums, k):
1515
:type k: int
1616
:rtype: List[int]
1717
"""
18-
c = collections.Counter(nums)
19-
return [x[0] for x in c.most_common(k)] #Here x is a tuple. only tuples can form keys not lists.
18+
c = collections.Counter(nums) #returns a dict with {num : how many times this num appeared in the list}
19+
return [x[0] for x in c.most_common(k)] # Here x is a tuple. only tuples can form keys not lists.

Lists/allSubsetsOfASet.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
total = len(numbers)
33

44
def subset_sum(numbers, partial=[]):
5-
global total
5+
global total # this is how you use global variables inside a function in python
6+
7+
if(len(partial)>= total): # base case - when to stop recursion
8+
return
69

7-
if (len(partial)<= total):
10+
if (len(partial)<= total):
811
print(partial)
912

10-
if(len(partial)>= total):
11-
return
12-
13-
for i in range(len(numbers)):
13+
for i in range(len(numbers)):
1414
n = numbers[i]
1515
rest = numbers[i+1:]
1616
subset_sum(rest,partial + [n])

Lists/massSuicide.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
# code to find the order of people how they are going to suicide.
2-
# In "n" people who are going to suicide, here every "mth" person does it.
2+
# List 'a' people who are going to suicide. Eevery "mth" person jumps one after another.
33
# you have to find out the order in which people commit suicide.
44

5-
n=10
5+
6+
a = range(0,10)
67
m=3
7-
a = []
8-
for i in range(n):
9-
a.append(i)
8+
109
s = ""
1110
i = 0
11+
1212
while(len(a)>0):
13-
i = (i + m - 1)%len(a)
14-
s += str(a[i]) + " "
15-
a.remove(a[i])
16-
print (s)
13+
i = (i + m - 1)%len(a)
14+
s += str(a[i]) + " "
15+
a.remove(a[i])
16+
print (s)

Lists/max 3 num prod.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
""" Given a list of integers, find max 3 numbered product"""
2+
3+
14
import sys
25

36
def solution(lis):

0 commit comments

Comments
 (0)