Check if the characters in a string form a Palindrome in O(1) extra space

def firstPos(str, start, end):

firstChar = -1

for i in range(start, end + 1):

if (str[i] >= 'a' and str[i] <= 'z') :

firstChar = i

break

return firstChar

def lastPos(str, start, end):

lastChar = -1

for i in range(start, end - 1, -1) :

if (str[i] >= 'a' and str[i] <= 'z') :

lastChar = i

break

return lastChar

def isPalindrome(str):

firstChar = 0

lastChar = len(str) - 1

ch = True

for i in range(len(str)) :

firstChar = firstPos(str, firstChar, lastChar);

lastChar = lastPos(str, lastChar, firstChar);

if (lastChar < 0 or firstChar < 0):

break

if (str[firstChar] == str[lastChar]):

firstChar += 1

lastChar -= 1

continue

ch = False

break

return (ch)

if __name__ == "__main__":


str = "m a 343 la y a l am"

if (isPalindrome(str)):

print("YES")

else:

print("NO")

 

program to print all Prime numbers in an Interval

def prime(x, y):

prime_list = []

for i in range(x, y):

if i == 0 or i == 1:

continue

else:

for j in range(2, int(i/2)+1):

if i % j == 0:

break

else:

prime_list.append(i)

return prime_list

starting_range = 2

ending_range = 7

lst = prime(starting_range, ending_range)

if len(lst) == 0:

print("There are no prime numbers in this range")

else:

print("The prime numbers in this range are: ", lst)


Program for compound interest

def compound_interest(principle, rate, time):

Amount = principle * (pow((1 + rate / 100), time))

CI = Amount - principle

print("Compound interest is", CI)

compound_interest(10000, 10.25, 5)


Tkinter-Text Widget

from tkinter import *

root = Tk()

root.geometry("300x300")

root.title(" Q&A ")

def Take_input():

INPUT = inputtxt.get("1.0", "end-1c")

print(INPUT)

if(INPUT == "120"):

Output.insert(END, 'Correct')

else:

Output.insert(END, "Wrong answer")

l = Label(text = "What is 24 * 5 ? ")

inputtxt = Text(root, height = 10,

width = 25,

bg = "light yellow")

Output = Text(root, height = 5,

width = 25,

bg = "light cyan")

Display = Button(root, height = 2,

width = 20,

text ="Show",

command = lambda:Take_input())

l.pack()

inputtxt.pack()

Display.pack()

Output.pack()

mainloop()


Pattern Printing

def Pattern(line):

pat=""

for i in range(0,line):

for j in range(0,line):

if ((j == 1 and i != 0 and i != line-1) or ((i == 0 or

i == line-1) and j > 1 and j < line-2) or (i == ((line-1)/2)

and j > line-5 and j < line-1) or (j == line-2 and

i != 0 and i != line-1 and i >=((line-1)/2))):

pat=pat+"#"

else:

pat=pat+" "

pat=pat+"\n"

return pat

line = 7

print(Pattern(line))


Program for Brick Sort

def oddEvenSort(arr, n):

    isSorted = 0

    while isSorted == 0:

        isSorted = 1

        temp = 0

        for i in range(1, n-1, 2):

            if arr[i] > arr[i+1]:

                arr[i], arr[i+1] = arr[i+1], arr[i]

                isSorted = 0

                  

        for i in range(0, n-1, 2):

            if arr[i] > arr[i+1]:

                arr[i], arr[i+1] = arr[i+1], arr[i]

                isSorted = 0

    return

arr = [34, 2, 10, -9]

n = len(arr)

oddEvenSort(arr, n);

for i in range(0, n):

    print(arr[i], end =" ")