# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3.8.6 64-bit
#     metadata:
#       interpreter:
#         hash: 604522f1918a4f70ab804f3225c4c25389e7cf7d629ed4133598ebf924cdb86e
#     name: python3
# ---



print('hello world')


# +
def hello():
    print('world!')

print(hello)
# -

x = 4
print(hex(id(x)))


# Create a function taking a positive integer as its parameter and returning a string containing the Roman Numeral representation of that integer.
#
# Modern Roman numerals are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero. In Roman numerals 1990 is rendered: 1000=M, 900=CM, 90=XC; resulting in MCMXC. 2008 is written as 2000=MM, 8=VIII; or MMVIII. 1666 uses each Roman symbol in descending order: MDCLXVI.
#
# Symbol    Value
#
# ---
#
# I     |     1
#
# ---
# V     |     5
#
# ---
# X     |     10
#
# ---
# L     |     50
#
# ---
# C     |     100
#
# ---
# D     |     500
#
# ---
# M     |     1,000
#
# ---
#
# solution(1000) # should return 'M'

# + tags=[]
def solution(n):
    values = [1000, 900, 500, 400,
            100, 90, 50, 40,
            10, 9, 5, 4,
            1]

    literal = ["M", "CM", "D", "CD",
            "C", "XC", "L", "XL",
            "X", "IX", "V", "IV",
            "I"]

    roman = ''
    
    for i in range(len(values)):
        cDig = n // values[i]
        if (cDig):
            roman += cDig * literal[i]
            n = n % values[i]

    return roman

# solution(56)
print(solution(516))


# -

# Using recursion(best practice)

def solution(x):
    table = [
        (1000,"M"),
        (900,"CM"),
        (500,"D"),
        (400,"CD"),
        (100,"C"),
        (90,"XC"),
        (50,"L"),
        (40,"XL"),
        (10,"X"),
        (9,"IX"),
        (5,"V"),
        (4,"IV"),
        (1,"I")
    ]
    for num, rep in table:
        if x >= num:
            return rep + solution(x-num)
    return str()


# +
# Find the number is prime or not
def is_prime(num):
    # print(list(range(2, num)))
    for n in range(2, num):
        if (num % n == 0):
            return False
    return True

is_prime(9)


# +
# Efficient method, less number to check for
def is_prime(num):
    # Eliminates all even numbers
    if num % 2 == 0 and num > 2:
        return False
    
    # check for all numbers starting from 3, in 
    # step of 2 like 3, 5, 7...
    # no need to check the full range only upto
    # the square root of the number, so the number of checks reduced
    for n in range(3, int(num**(1/2))+1, 2):
        if (num % n == 0):
            return False
    return True

is_prime()


# +
# need to consider cases like , -ve values, 0 and 1

def is_prime(num):
    # Eliminates all numbers less tham 2
    if num < 2:
        return False

    # Eliminates all even numbers
    if num % 2 == 0 and num > 2:
        return False
    
    # check for all numbers starting from 3, in 
    # step of 2 up to the sq. root of given number.
    for n in range(3, int(num**(1/2))+1, 2):
        if (num % n == 0):
            return False
    return True
# -



print ('testing')


# Given two arrays of strings a1 and a2 return a sorted array r in lexicographical order of the strings of a1 which are substrings of strings of a2.
#
# #Example 1: a1 = \["arp", "live", "strong"\]
#
# a2 = \["lively", "alive", "harp", "sharp", "armstrong"\]
#
# returns \["arp", "live", "strong"\]
#
# #Example 2: a1 = \["tarp", "mice", "bull"\]
#
# a2 = \["lively", "alive", "harp", "sharp", "armstrong"\]
#
# returns \[\]

# +
def in_array(array1, array2):
    r = []
    for w2 in array2:
        for w1 in array1:
            if (w1 in w2) and (w1 not in r) :
                r.append(w1)
    return sorted(r)

a1 = ["live", "arp", "strong"] 
a2 = ["lively", "alive", "harp", "sharp", "armstrong"]
# r = ['arp', 'live', 'strong']
print(in_array(a1, a2))
in_array(a1, a2)


# +
# Good solution
def in_array(a1, a2):
    return sorted(set(s1 for s1 in a1 if any(s1 in s2 for s2 in a2)))

a1 = ["live", "arp", "strong"] 
a2 = ["lively", "alive", "harp", "sharp", "armstrong"]
# r = ['arp', 'live', 'strong']
print(in_array(a1, a2))
in_array(a1, a2)
# -



# +
# encryption

def encrypt(text, n):
    str1 = ''
    str2 = ''
    for i in range(0, len(text), 2):
        str1 += text[i]
        if i < len(text) - 1:
            str2 += text[i + 1]
    text = str2 + str1
    n -= 1

    if n != 0:
        return encrypt(text, n)

    return text


# encrypt("This is a test!", 0)
# "This is a test!"
encrypt("This is a test!", 10)
# "hsi  etTi sats!"

# +
def decrypt(text, n):
    print(n % 4)
    return (encrypt(text, 4 - (n % 4)))

decrypt("s eT ashi tist!", 10)


# +
# decription

def decrypt(text, n):
    eIStr = text[int(len(text)/2):]
    oIStr = text[:int(len(text)/2)]

    orStr = "".join([a + b for a,b in zip(eIStr, oIStr)])

    if len(eIStr) > len(oIStr):
        orStr = orStr + eIStr[-1]

    n -= 1
    if n != 0:
        return decrypt(orStr, n)
    return orStr
    

decrypt("hskt svr neetn!Ti aai eyitrsig", 1)

# +
d = "patti"
s = 'Thendi'
f = zip(d, s)
# print(list(f))
print([a + b for a,b in f])

# print the one with max length
print(max(d,s, key=len)[min(len(d),len(s)):])
# [min(len(d),len(s)):]

# +
def encrypt(text, n):
    if text and n > 0:
        str1, str2 = '',''
        for i in range(0, len(text), 2):
            str1 += text[i]
            if i < len(text) - 1:
                str2 += text[i + 1]
        text = str2 + str1
        n -= 1
        return encrypt(text, n)

    return text

# encrypt("This", 1)
encrypt("hskt svr neetn!Ti aai eyitrsig", 4)


# +
def decrypt(encrypted_text, n):
    if len(encrypted_text)%2 == 0:
        c = 4
    else:
        c = 5
    return (encrypt(encrypted_text, c - (n % c)))

# decrypt("hsi  etTi sats!", 1)
# encrypt("This is a test!", -1)
# hsi  etTi sats!
# decrypt("hsi  etTi sats!", 1)
# decrypt("hsi  etTi sats!", 1)
decrypt("This is a test!", 1)


# +
def encrypt(text, n):
    if text and n > 0:
        str1, str2 = '',''
        for i in range(0, len(text), 2):
            str1 += text[i]
            if i < len(text) - 1:
                str2 += text[i + 1]
        text = str2 + str1
        n -= 1
        return encrypt(text, n)

    return text


def decrypt(encrypted_text, n):
    if encrypted_text and n > 0:
        eIStr = encrypted_text[int(len(encrypted_text)/2):]
        oIStr = encrypted_text[:int(len(encrypted_text)/2)]

        orStr = "".join([a + b for a,b in zip(eIStr, oIStr)])

        if len(eIStr) > len(oIStr):
            orStr = orStr + eIStr[-1]
        n -= 1
        return decrypt(orStr, n)
    return encrypted_text


# +
def add(a, b):
    return a + b

add(1, 2)
# -



StarcoderdataJupyterScriptsDedupFiltered