questions cisco

mail@pastecode.io avatar
unknown
python
a year ago
2.9 kB
8
Indexable
# Online Python compiler (interpreter) to run Python online.
# Write Python 3 code in this online editor and run it.

import string
print(string.ascii_lowercase)
from collections import defaultdict


order = {}

for char in string.ascii_lowercase:
    order[char] = ord(char)
    
def in_order(s):
    prev = ord(s[0])
    
    for i in range(1, len(s)):
        if order[s[i]] < prev:
            return i
        prev = order[s[i]]
    return 0


def max_diff(nums):
    
    best = 0
    
    left = 0
    for right in range(1,len(nums)):
        
        if nums[right] > nums[left]:
            best = max(best, nums[right] - nums[left])
        
        if nums[right] < nums[left]:
            left = right
    return best

nums = [2,3,10,6,4,8,1]
print('max diff test1: ', max_diff(nums))
print('max diff test2: ', max_diff([4,3,1]))

def x_add_y(x,y):
    x = int(x)
    y = int(y)
    
    def digit_sum(num):
        total = 0
        while num > 0:
            total += num % 10
            num = num // 10
        return total
        
    count = 0
    for num in range(x+1):
        if digit_sum(num) == y:
            count += 1
       
    return count

print('x sum y test1: ',x_add_y(20,5))

def pilot_drop(x_coords, y_coords):
    
    coords = []
    row_map = defaultdict(int)
    col_map = defaultdict(int)
    
    for i in range(len(x_coords)):
        coords.append([x_coords[i], y_coords[i]])
    
    for x,y in coords:
        row_map[x] += 1
        col_map[y] += 1
    
    row_vals = row_map.values()
    col_vals = col_map.values()
    
    max_r = max(row_vals)
    max_c = max(col_vals)
    return max(max_r, max_c)

pilot_test1 = pilot_drop([2,3,2,4,2], [2,2,6,5,8])
print('pilot test1: ', pilot_test1)

def palindrome_sub(s):
    if len(s) == 1:
        print("None")
        return
    
    buckets = [set() for i in range(len(s) + 1)]
    # even palindromes
    for i in range(len(s)):
        l,r = i, i + 1
        while l >= 0 and r < len(s) and s[l] == s[r]:
            sub = s[l:r + 1]
            buckets[len(sub)].add(sub)
            l -= 1
            r += 1
    # odd palindromes
    for i in range(len(s)):
        l = r = i
        while l >= 0 and r < len(s) and s[l] == s[r]:
            sub = s[l:r+1]
            buckets[len(sub)].add(sub)
            l -=1
            r += 1
    # palindromes all accounted for, grab largest one
    print(buckets)
    for i in range(len(buckets)-1,1,-1):
    
        if  not buckets[i]:
            continue
        # we have some big palindromes
        results = sorted(list(buckets[i]))
        print(results[0])
        return results[0]
    print('None')
    return None

pal_test1 = palindrome_sub('YABCCBAZ')
pal_test2 = palindrome_sub('ABC')

print('paltest1: ',pal_test1)
print('paltest2: ', pal_test2)
print(palindrome_sub('AABBB'))