Reference no: EM133465203
Each of the below listed codes should converted to Scala Code .
Python code 1
def HistogramArea(arr):
stack = list()
max_area = 0
index = 0
while index < len(arr):
if (not stack) or (arr[stack[-1]] <= arr[index]):
stack.append(index)
index +=1
else:
top_of_stack = stack.pop()
area = (arr[top_of_stack] *
((index - stack[-1] -1 )
if stack else index))
max_area = max(max_area,area)
while stack:
top_of_stack = stack.pop()
area = (arr[top_of_stack] *
((index - stack[-1] -1 )
if stack else index))
max_area = max(max_area,area)
return max_area
input = [int(x) for x in input()]
print(HistogramArea(input))
#sample for testing [6, 3, 1, 4, 12, 4]
Python code 2
global maximum
def LongestIncreasingSequence(arr, n):
global maximum
if n == 1:
return 1
meh = 1
for i in range(1, n):
res = LongestIncreasingSequence(arr, i)
if arr[i - 1] < arr[n -1] and res + 1 > meh:
meh = res + 1
maximum = max(maximum, meh)
return meh
def lis(arr):
global maximum
n = len(arr)
maximum = 1
LongestIncreasingSequence(arr, n)
return maximum
input = [int(x) for x in input()]
n = len(input)
# keep this function call here
print(lis(input))
#sample for testing [9, 9, 4, 2]
Python code 3
no_of_chars = 256
def MinWindowSubstring(string, pat):
len1 = len(string)
len2 = len(pat)
if len1 < len2:
print("No such window exists")
return ""
hash_pat = [0] * no_of_chars
hash_str = [0] * no_of_chars
# Store occurrence ofs characters of pattern
for i in range(0, len2):
hash_pat[ord(pat[i])] += 1
start, start_index, min_len = 0, -1, float('inf')
# Start traversing the string
count = 0 # count of characters
for j in range(0, len1):
# count occurrence of characters of string
hash_str[ord(string[j])] += 1
# If string's char matches with
# pattern's char then increment count
if (hash_str[ord(string[j])] <=
hash_pat[ord(string[j])]):
count += 1
# if all the characters are matched
if count == len2:
# Try to minimize the window
while (hash_str[ord(string[start])] >
hash_pat[ord(string[start])] or
hash_pat[ord(string[start])] == 0):
if (hash_str[ord(string[start])] >
hash_pat[ord(string[start])]):
hash_str[ord(string[start])] -= 1
start += 1
# update window size
len_window = j - start + 1
if min_len > len_window:
min_len = len_window
start_index = start
# If no window found
if start_index == -1:
print("No such window exists")
return ""
# Return substring starting from
# start_index and length min_len
return string[start_index: start_index + min_len]
#sample for test data.
#string = "ahffaksfajeeubsne"
#pat = "jefaa"
Type = input()
x = Type[0]
y = Type[1]
print(MinWindowSubstring(x,y))