diff --git a/sorts/sleepsort.py b/sorts/sleepsort.py new file mode 100644 index 000000000000..267b160cf3b3 --- /dev/null +++ b/sorts/sleepsort.py @@ -0,0 +1,17 @@ +from time import sleep + +import thread + +items = [] +n = int(input()) +for i in range(0, n): + a = int(input()) + items.append(a) + + +def sleep_sort(i): + sleep(i) + print(i) + + +[thread.start_new_thread(sleep_sort, (i,)) for i in items] diff --git a/strings/longest_valid_parenthesis.py b/strings/longest_valid_parenthesis.py new file mode 100644 index 000000000000..a99f75c433cc --- /dev/null +++ b/strings/longest_valid_parenthesis.py @@ -0,0 +1,34 @@ +def longest_valid_parenthesis(s: str) -> int: + """ + Returns the length of the longest valid parenthesis + >>> longest_valid_parenthesis('(()') + 2 + >>> longest_valid_parenthesis(')()())') + 4 + >>> longest_valid_parenthesis('(()()()()))') + 10 + >>> longest_valid_parenthesis(''(())))((()(()()()()) + 8 + """ + stack = [] + preceeding_matched = 0 + res = 0 + for char in s: + if char == "(": + stack.append(preceeding_matched) + preceeding_matched = 0 + else: + if stack: + preceeding_matched += 1 + stack.pop() + else: + res = max(res, preceeding_matched) + preceeding_matched = 0 + res = max(res, preceeding_matched) + while stack: + res = max(res, stack.pop()) + return res * 2 + + +if __name__ == "__main__": + s = input() + print(longest_valid_parenthesis(s))