Skip to content

Commit

Permalink
Merge pull request keon#146 from goswami-rahul/resolve-conflict-patch-1
Browse files Browse the repository at this point in the history
resolve conflict from refractoring modules.
  • Loading branch information
keon authored Mar 14, 2018
2 parents 5287408 + 492011a commit abe4cb1
Show file tree
Hide file tree
Showing 38 changed files with 131 additions and 142 deletions.
75 changes: 37 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,19 @@ Thanks for your interest in contributing! There are many ways to contribute to t
- [longest_common_subsequence](map/longest_common_subsequence.py)
- [randomized_set](map/randomized_set.py)
- [valid_sudoku](map/valid_sudoku.py)
- [math](math)
- [base_conversion](math/base_conversion.py)
- [extended_gcd](math/extended_gcd.py)
- [gcd/lcm](math/gcd.py)
- [generate_strobogrammtic](math/generate_strobogrammtic.py)
- [is_strobogrammatic](math/is_strobogrammatic.py)
- [nth_digit](math/nth_digit.py)
- [prime_test](math/prime_test.py)
- [primes_sieve_of_eratosthenes](math/primes_sieve_of_eratosthenes.py)
- [pythagoras](math/pythagoras.py)
- [rabin_miller](math/rabin_miller.py)
- [rsa](math/rsa.py)
- [sqrt_precision_factor](math/sqrt_precision_factor.py)
- [maths](maths)
- [base_conversion](maths/base_conversion.py)
- [extended_gcd](maths/extended_gcd.py)
- [gcd/lcm](maths/gcd.py)
- [generate_strobogrammtic](maths/generate_strobogrammtic.py)
- [is_strobogrammatic](maths/is_strobogrammatic.py)
- [nth_digit](maths/nth_digit.py)
- [prime_test](maths/prime_test.py)
- [primes_sieve_of_eratosthenes](maths/primes_sieve_of_eratosthenes.py)
- [pythagoras](maths/pythagoras.py)
- [rabin_miller](maths/rabin_miller.py)
- [rsa](maths/rsa.py)
- [sqrt_precision_factor](maths/sqrt_precision_factor.py)
- [matrix](matrix)
- [bomb_enemy](matrix/bomb_enemy.py)
- [copy_transform](matrix/copy_transform.py)
Expand All @@ -135,12 +135,12 @@ Thanks for your interest in contributing! There are many ways to contribute to t
- [sparse_dot_vector](matrix/sparse_dot_vector.py)
- [sparse_mul](matrix/sparse_mul.py)
- [spiral_traversal](matrix/spiral_traversal.py)
- [queue](queue)
- [max_sliding_window](queue/max_sliding_window.py)
- [moving_average](queue/moving_average.py)
- [queue](queue/queue.py)
- [reconstruct_queue](queue/reconstruct_queue.py)
- [zigzagiterator](queue/zigzagiterator.py)
- [queues](queues)
- [max_sliding_window](queues/max_sliding_window.py)
- [moving_average](queues/moving_average.py)
- [queue](queues/queue.py)
- [reconstruct_queue](queues/reconstruct_queue.py)
- [zigzagiterator](queues/zigzagiterator.py)
- [search](search)
- [binary_search](search/binary_search.py)
- [first_occurance](search/first_occurance.py)
Expand All @@ -166,24 +166,24 @@ Thanks for your interest in contributing! There are many ways to contribute to t
- [simplify_path](stack/simplify_path.py)
- [stack](stack/stack.py)
- [valid_parenthesis](stack/valid_parenthesis.py)
- [string](string)
- [add_binary](string/add_binary.py)
- [breaking_bad](string/breaking_bad.py)
- [decode_string](string/decode_string.py)
- [encode_decode](string/encode_decode.py)
- [group_anagrams](string/group_anagrams.py)
- [int_to_roman](string/int_to_roman.py)
- [is_palindrome](string/is_palindrome.py)
- [license_number](string/license_number.py)
- [make_sentence](string/make_sentence.py)
- [multiply_strings](string/multiply_strings.py)
- [one_edit_distance](string/one_edit_distance.py)
- [rabin_karp](string/rabin_karp.py)
- [reverse_string](string/reverse_string.py)
- [reverse_vowel](string/reverse_vowel.py)
- [reverse_words](string/reverse_words.py)
- [roman_to_int](string/roman_to_int.py)
- [word_squares](string/word_squares.py)
- [strings](strings)
- [add_binary](strings/add_binary.py)
- [breaking_bad](strings/breaking_bad.py)
- [decode_string](strings/decode_string.py)
- [encode_decode](strings/encode_decode.py)
- [group_anagrams](strings/group_anagrams.py)
- [int_to_roman](strings/int_to_roman.py)
- [is_palindrome](strings/is_palindrome.py)
- [license_number](strings/license_number.py)
- [make_sentence](strings/make_sentence.py)
- [multiply_strings](strings/multiply_strings.py)
- [one_edit_distance](strings/one_edit_distance.py)
- [rabin_karp](strings/rabin_karp.py)
- [reverse_string](strings/reverse_string.py)
- [reverse_vowel](strings/reverse_vowel.py)
- [reverse_words](strings/reverse_words.py)
- [roman_to_int](strings/roman_to_int.py)
- [word_squares](strings/word_squares.py)
- [tree](tree)
- [bst](tree/tree/bst)
- [array2bst](tree/bst/array2bst.py)
Expand Down Expand Up @@ -225,4 +225,3 @@ Thanks for your interest in contributing! There are many ways to contribute to t
- [tree](tree/tree.py)
- [union-find](union-find)
- [count_islands](union-find/count_islands.py)

72 changes: 36 additions & 36 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,18 @@ python版数据结构和算法实现的简约版小示例
- [longest_common_subsequence:最长公共子序列](map/longest_common_subsequence.py)
- [randomized_set:随机集](map/randomized_set.py)
- [valid_sudoku:有效数独](map/valid_sudoku.py)
- [math:数学问题](math)
- [extended_gcd:扩展欧几里得算法](math/extended_gcd.py)
- [gcd/lcm:最大公约数和最小公倍数](math/gcd.py)
- [prime_test:主要测试](math/prime_test.py)
- [primes_sieve_of_eratosthenes:埃拉托色尼的质数筛](math/primes_sieve_of_eratosthenes.py)
- [generate_strobogrammtic:生成对称数](math/generate_strobogrammtic.py)
- [is_strobogrammatic:判断对称数](math/is_strobogrammatic.py)
- [nth_digit:第n位](math/nth_digit.py)
- [rabin_miller:米勒-拉宾素性检验](math/rabin_miller.py)
- [rsa:rsa加密](math/rsa.py)
- [sqrt_precision_factor:开发精度因素](math/sqrt_precision_factor.py)
- [pythagoras:毕达哥拉斯](math/pythagoras.py)
- [math:数学问题](maths)
- [extended_gcd:扩展欧几里得算法](maths/extended_gcd.py)
- [gcd/lcm:最大公约数和最小公倍数](maths/gcd.py)
- [prime_test:主要测试](maths/prime_test.py)
- [primes_sieve_of_eratosthenes:埃拉托色尼的质数筛](maths/primes_sieve_of_eratosthenes.py)
- [generate_strobogrammtic:生成对称数](maths/generate_strobogrammtic.py)
- [is_strobogrammatic:判断对称数](maths/is_strobogrammatic.py)
- [nth_digit:第n位](maths/nth_digit.py)
- [rabin_miller:米勒-拉宾素性检验](maths/rabin_miller.py)
- [rsa:rsa加密](maths/rsa.py)
- [sqrt_precision_factor:开发精度因素](maths/sqrt_precision_factor.py)
- [pythagoras:毕达哥拉斯](maths/pythagoras.py)
- [matrix:矩阵](matrix)
- [matrix_rotation:矩阵旋转](matrix/matrix_rotation.txt)
- [copy_transform:复制变换](matrix/copy_transform.py)
Expand All @@ -120,12 +120,12 @@ python版数据结构和算法实现的简约版小示例
- [sparse_mul:稀疏矩阵](matrix/sparse_mul.py)
- [spiral_traversal:循环遍历](matrix/spiral_traversal.py)
- [count_paths:计算路径](matrix/count_paths.py)
- [queue:队列](queue)
- [max_sliding_window:最大移动窗口](queue/max_sliding_window.py)
- [moving_average:移动平均](queue/moving_average.py)
- [queue:队列](queue/queue.py)
- [reconstruct_queue:重建队列](queue/reconstruct_queue.py)
- [zigzagiterator:锯齿形迭代](queue/zigzagiterator.py)
- [queue:队列](queues)
- [max_sliding_window:最大移动窗口](queues/max_sliding_window.py)
- [moving_average:移动平均](queues/moving_average.py)
- [queue:队列](queues/queue.py)
- [reconstruct_queue:重建队列](queues/reconstruct_queue.py)
- [zigzagiterator:锯齿形迭代](queues/zigzagiterator.py)
- [search:查找](search)
- [binary_search:二分查找](search/binary_search.py)
- [count_elem:元素计数](search/count_elem.py)
Expand All @@ -152,24 +152,24 @@ python版数据结构和算法实现的简约版小示例
- [simplify_path:简化路径](stack/simplify_path.py)
- [stack:栈](stack/stack.py)
- [valid_parenthesis:验证括号](stack/valid_parenthesis.py)
- [string:字符串](string)
- [add_binary:二进制数相加](string/add_binary.py)
- [breaking_bad:打破坏](string/breaking_bad.py)
- [decode_string:字符串编码](string/decode_string.py)
- [encode_decode:编解码](string/encode_decode.py)
- [group_anagrams:群组错位词](string/group_anagrams.py)
- [int_to_roman:整数转换罗马数字](string/int_to_roman.py)
- [is_palindrome:回文字符串](string/is_palindrome.py)
- [license_number:拍照号码](string/license_number.py)
- [make_sentence:造句](string/make_sentence.py)
- [multiply_strings:字符串相乘](string/multiply_strings.py)
- [one_edit_distance:一个编辑距离](string/one_edit_distance.py)
- [rabin_karp:Rabin-Karp 算法](string/rabin_karp.py)
- [reverse_string:反转字符串](string/reverse_string.py)
- [reverse_vowel:反转元音](string/reverse_vowel.py)
- [reverse_words:反转单词](string/reverse_words.py)
- [roman_to_int:罗马数转换整数](string/roman_to_int.py)
- [word_squares:单词平方](string/word_squares.py)
- [string:字符串](strings)
- [add_binary:二进制数相加](strings/add_binary.py)
- [breaking_bad:打破坏](strings/breaking_bad.py)
- [decode_string:字符串编码](strings/decode_string.py)
- [encode_decode:编解码](strings/encode_decode.py)
- [group_anagrams:群组错位词](strings/group_anagrams.py)
- [int_to_roman:整数转换罗马数字](strings/int_to_roman.py)
- [is_palindrome:回文字符串](strings/is_palindrome.py)
- [license_number:拍照号码](strings/license_number.py)
- [make_sentence:造句](strings/make_sentence.py)
- [multiply_strings:字符串相乘](strings/multiply_strings.py)
- [one_edit_distance:一个编辑距离](strings/one_edit_distance.py)
- [rabin_karp:Rabin-Karp 算法](strings/rabin_karp.py)
- [reverse_string:反转字符串](strings/reverse_string.py)
- [reverse_vowel:反转元音](strings/reverse_vowel.py)
- [reverse_words:反转单词](strings/reverse_words.py)
- [roman_to_int:罗马数转换整数](strings/roman_to_int.py)
- [word_squares:单词平方](strings/word_squares.py)
- [tree:树](tree)
- [segment-tree:线段树](tree/Segment_Tree)
- [segment_tree:线段树](tree/Segment_Tree/segment_tree.py)
Expand Down
15 changes: 9 additions & 6 deletions backtrack/palindrome_partitioning.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# It looks like you need to be looking not for all palindromic substrings, but rather for all the ways you can divide the input string up into palindromic substrings. (There's always at least one way, since one-character substrings are always palindromes.)

# Here's the way I've done it:
""" It looks like you need to be looking not for all palindromic substrings,
but rather for all the ways you can divide the input string up into palindromic substrings.
(There's always at least one way, since one-character substrings are always palindromes.)
"""

def palindromic_substrings(s):
if not s:
Expand All @@ -13,9 +14,11 @@ def palindromic_substrings(s):
results.append([sub] + rest)
return results

# There's two loops.
# The outer loop checks each length of initial substring (in descending length order) to see if it is a palindrome. If so, it recurses on the rest of the string and loops over the returned values, adding the initial substring to each item before adding it to the results.

""" There's two loops.
The outer loop checks each length of initial substring (in descending length order) to see
if it is a palindrome. If so, it recurses on the rest of the string and loops over the returned
values, adding the initial substring to each item before adding it to the results.
"""
# A slightly more Pythonic approach would be to make a recursive generator:

def palindromic_substrings(s):
Expand Down
35 changes: 20 additions & 15 deletions heap/merge_sorted_k_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
"""


from heapq import heappop, heapreplace, heapify
from queue import PriorityQueue


# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None

from heapq import heappush, heappop, heapreplace, heapify

def mergeKLists(lists):
dummy = node = ListNode(0)
Expand All @@ -17,40 +21,41 @@ def mergeKLists(lists):
while h:
v, n = h[0]
if n.next is None:
heappop(h) #only change heap size when necessary
heappop(h) # only change heap size when necessary
else:
heapreplace(h, (n.next.val, n.next))
node.next = n
node = node.next

return dummy.next

from Queue import PriorityQueue

def merge_k_lists(lists):
dummy = ListNode(None)
curr = dummy
q = PriorityQueue()
for node in lists:
if node: q.put((node.val,node))
while q.qsize()>0:
curr.next = q.get()[1]
curr=curr.next
if curr.next: q.put((curr.next.val, curr.next))
if node:
q.put((node.val, node))
while not q.empty():
curr.next = q.get()[1] # These two lines seem to
curr = curr.next # be equivalent to :- curr = q.get()[1]
if curr.next:
q.put((curr.next.val, curr.next))
return dummy.next


"""
I think my code's complexity is also O(nlogk) and not using heap or priority queue,
n means the total elements and k means the size of list.
The mergeTwoLists functiony in my code comes from the problem Merge Two Sorted Lists
The mergeTwoLists function in my code comes from the problem Merge Two Sorted Lists
whose complexity obviously is O(n), n is the sum of length of l1 and l2.
To put it simpler, assume the k is 2^x, So the progress of combination is like a full binary tree,
from bottom to top. So on every level of tree, the combination complexity is n,
beacause every level have all n numbers without repetition.
The level of tree is x, ie logk. So the complexity is O(nlogk).
because every level have all n numbers without repetition.
The level of tree is x, ie log k. So the complexity is O(n log k).
for example, 8 ListNode, and the length of every ListNode is x1, x2,
x3, x4, x5, x6, x7, x8, total is n.
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion math/is_strobogrammatic.py → maths/is_strobogrammatic.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def is_strobogrammatic(num):
j -= 1
return True

def is_strobogrammatic(num: str):

def is_strobogrammatic2(num: str):
"""Another implementation."""
return num == num[::-1].replace('6', '#').replace('9', '6').replace('#', '9')
File renamed without changes.
27 changes: 13 additions & 14 deletions math/prime_test.py → maths/prime_test.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
'''
"""
prime_test(n) returns a True if n is a prime number else it returns False
'''
"""


def prime_test(n):
if n <= 1:
return False
if n==2 or n==3:
if n == 2 or n == 3:
return True
if n%2==0 or n%3==0:
if n % 2 == 0 or n % 3 == 0:
return False
j = 5
while(j*j <= n):
if n%(j)==0 or n%(j+2)==0:
while j * j <= n:
if n % j == 0 or n % (j + 2) == 0:
return False
j += 6
return True


def prime_test(n):
def prime_test2(n):
# prime numbers are greater than 1
if n > 1:
# check for factors
# check for factors
for i in range(2, int(n ** 0.5) + 1):
if (n % i) == 0:
#print(num,"is not a prime number")
#print(i,"times",num//i,"is",num)
if (n % i) == 0:
# print(num, "is not a prime number")
# print(i, "times", num//i, "is", num)
return False
break
else:
#print(num,"is a prime number")
# print(num, "is a prime number")
return True

# if input number is less than
# or equal to 1, it is not prime
else:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 3 additions & 2 deletions queue/max_sliding_window.py → queues/max_sliding_window.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import collections


# Keep indexes of good candidates in deque d.
# The indexes in d are from the current window, they're increasing,
# and their corresponding nums are decreasing.
Expand Down Expand Up @@ -29,6 +30,6 @@ def max_sliding_window(nums, k):
return out


array = [1,3,-1,-3,5,3,6,7]
array = [1, 3, -1, -3, 5, 3, 6, 7]

print(max_sliding_window(array))
print(max_sliding_window(array, 5))
File renamed without changes.
Loading

0 comments on commit abe4cb1

Please sign in to comment.