forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
53 changed files
with
2,720 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
""" | ||
There are people sitting in a circular fashion, | ||
print every third member while removing them, | ||
the next counter starts immediately after the member is removed. | ||
Print till all the members are exhausted. | ||
For example: | ||
Input: consider 123456789 members sitting in a circular fashion, | ||
Output: 369485271 | ||
""" | ||
|
||
a = ['1','2','3','4','5','6','7','8','9'] | ||
|
||
def josepheus(int_list, skip): | ||
skip = skip - 1 #list starts with 0 index | ||
idx = 0 | ||
while len(int_list)>0: | ||
idx = (skip+idx)%len(int_list) #hashing to keep changing the index to every 3rd | ||
print int_list.pop(idx) | ||
|
||
|
||
josepheus(a,3) | ||
|
||
""" | ||
the reason for hashing is that we have to find the index of the item which needs to be removed. | ||
So for e.g. if you iterate with the initial list of folks with every 3rd item eliminated: | ||
INPUT | ||
int_list = 123456789 | ||
skip = 3 | ||
While Iteration: | ||
int_list = 123456789 | ||
len(int_list) = 9 | ||
skip = 2 # as int_list starts from 0 | ||
idx = (0 + 2) % 9 #here previous index was 0 | ||
so 3rd element which is 3 in this case eliminated | ||
int_list = 12456789 | ||
len(int_list) = 8 | ||
idx = (2 + 2) % 8 #here previous index was 2 | ||
so 3rd element starting from 4th person which is 6 would be deleted. | ||
and so on | ||
The reason why we have to do this way is I am not putting the people who escape at the back of list so ideally in 2 while iteration the list should have been | ||
45678912 and then no hashing needed to be done, which means you can directly remove the third element | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
""" | ||
Implement Flatten Arrays. | ||
Given an array that may contain nested arrays, | ||
give a single resultant array. | ||
function flatten(input){ | ||
} | ||
Example: | ||
Input: var input = [2, 1, [3, [4, 5], 6], 7, [8]]; | ||
flatten(input); | ||
Output: [2, 1, 3, 4, 5, 6, 7, 8] | ||
""" | ||
|
||
|
||
def list_flatten(l, a=None): | ||
# check a | ||
if a is None: | ||
# initialize with empty list | ||
a = [] | ||
|
||
for i in l: | ||
if isinstance(i, list): | ||
list_flatten(i, a) | ||
else: | ||
a.append(i) | ||
return a | ||
|
||
|
||
# stack version | ||
# public static List<Integer> flatten(List<NestedList> l) { | ||
# List<Integer> main = new ArrayList<Integer>(); | ||
# Stack<List<NestedList>> stack = new Stack<List<NestedList>>(); | ||
# Stack<Integer> indexes = new Stack<Integer>(); | ||
# stack.add(l); | ||
# indexes.add(0); | ||
# while (true) { | ||
# if (stack.isEmpty()) | ||
# break; | ||
# int index1 = indexes.pop(); | ||
# l = stack.pop(); | ||
# for (int i = index1; i < l.size(); i++) { | ||
# NestedList n = l.get(i); | ||
# if (n.isInteger()) { | ||
# main.add(n.value); | ||
# } else { | ||
# stack.add(l); | ||
# indexes.add(i+1); | ||
# l = n.list; | ||
# stack.add(l); | ||
# indexes.add(0); | ||
# break; | ||
|
||
# } | ||
# } | ||
# } | ||
|
||
# return main; | ||
# } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
""" | ||
Given a sorted integer array without duplicates, | ||
return the summary of its ranges. | ||
For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. | ||
""" | ||
|
||
|
||
def summary_ranges(nums): | ||
""" | ||
:type nums: List[int] | ||
:rtype: List[str] | ||
""" | ||
res = [] | ||
if len(nums) == 1: | ||
return [str(nums[0])] | ||
i = 0 | ||
while i < len(nums): | ||
num = nums[i] | ||
while i+1 < len(nums) and nums[i+1] - nums[i] == 1: | ||
i += 1 | ||
if nums[i] != num: | ||
res.append(str(num) + "->" + str(nums[i])) | ||
else: | ||
res.append(str(num)) | ||
i += 1 | ||
return res |
Oops, something went wrong.