Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: January 2023

Monday, January 30, 2023

Banking ICSE Interest Calculator

Calculation of interest on a savings bank account
Mini Python project for middle and high school students

Example




Run the code on google colaboratory
Python Code:
class Solution(object):

    def Banking(self, numberOfMonths):
        transactions = dict()
        for i in range(numberOfMonths):
            month = str(input("Enter the month:"))
            transactions_for_month = int(
                input("Enter the number of transactions for this month:"))
            tempList = []
            for j in range(transactions_for_month):
                tempList.append(int(input("Enter the date of the month:")))
                # put a negative sign before the number if the amount is withdrawn
                tempList.append(
                    float(input("Enter the amount deposited or withdrawn:")))
            transactions[month] = tempList
        #print(transactions)
        self.MinimumBalance(transactions)

    def MinimumBalance(self, transactions):
        key = list(transactions.keys())
        balance = []
        balanceFinal = 0
        date = []
        monthWiseMBList = []
        for i in range(len(transactions)):
            for j in range(len(transactions[key[i]])):
                if j % 2 == 0:
                    date.append(transactions[key[i]][j])
                else:
                    balanceFinal += (transactions[key[i]][j])
                    balance.append(balanceFinal)
            balanceFinal = balance[-1]
            #print(balance)
            #print(date)
            if i != 0:
                move_date += int((len(transactions[key[i - 1]]) / 2))
            else:
                move_date = 0
            #print(move_date)
            minimumBalanceMonthly = float('inf')
            flagTransactionLessThanTenth = False
           
            for k in range(len(date) - move_date):
                if date[k+move_date] <= 10:
                    minimumBalanceMonthly = balance[k + move_date]
                    flagTransactionLessThanTenth = True
                else:

                    if minimumBalanceMonthly > balance[k + move_date]:
                        minimumBalanceMonthly = balance[k + move_date]
            if len(transactions[key[i]]) == 0:
                monthWiseMBList.append(monthWiseMBList[i - 1])
            else:
                monthWiseMBList.append(minimumBalanceMonthly)
            if flagTransactionLessThanTenth == False:
                if i == 0:
                    print(monthWiseMBList[i], i, minimumBalanceMonthly)
                    monthWiseMBList[i] = 0
                if i!=0:
                    if monthWiseMBList[i] > monthWiseMBList[i - 1] and len(transactions[key[i]]) != 0:
                        monthWiseMBList[i] = monthWiseMBList[i - 1]
                    else:
                        monthWiseMBList[i] = balance[-1]
            if i == len(key) - 1:
                print("Press Y/y is the account was closed in the month of ", key[-1])
                choice = str(input())
                if choice == "Y" or choice == "y":
                    monthWiseMBList[-1] = 0
            print("Minimum Monthly Balance for the month of ", key[i], "= ",
                  monthWiseMBList[i])
        #calculation of interest
        qualifyingAmount = 0
        for j in (monthWiseMBList):
            qualifyingAmount += j
        print("Total of balances ", qualifyingAmount)
        rate_of_interest = float(input("Enter the rate of interest:"))
        interest = (qualifyingAmount * rate_of_interest * (1 / 12)) / 100.0
        print("Total interest earned = ", round(interest, 2))


obj = Solution()
numberOfmonths = int(input("Enter the number of months:"))
obj.Banking(numberOfmonths)






Wednesday, January 25, 2023

Leet Code Rotate Image

Leet Code Rotate Image 

You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).


Example 1:

Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[7,4,1],[8,5,2],[9,6,3]]

Example 2:

Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]

 

Constraints:

  • n == matrix.length == matrix[i].length
  • 1 <= n <= 20
  • -1000 <= matrix[i][j] <= 1000





class Solution:
    def rotate(self, matrix):
        order = len(matrix[0])
        newmatrix = []
        for i in range(order):
            newmatrix.append([])
            for j in range(order):
                newmatrix[i].append(0)
        newColoumn = []
        for i in range(order):
            newColoumn.append(order-1-i)
        for i in range(order):
            for j in range(order):
                newmatrix[j][newColoumn[i]] = matrix[i][j]
        for i in range(order):
            for j in range(order):
                matrix[i][j] = int(newmatrix[i][j])
        print(matrix)
obj = Solution()
#change the matrix here
matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
obj.rotate(matrix)

Leet Code Longest Substring Without Repeating Characters

Given a string s, find the length of the longest 

 without repeating characters.

 

Example 1:

Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.

Example 2:

Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

Example 3:

Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.

 

Constraints:

  • 0 <= s.length <= 5 * 104
  • s consists of English letters, digits, symbols and spaces.

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        maxLength = 0
        for i in range(len(s)):
            substring = []
            for j in range(i,len(s)):
                if s[j] in substring:
                    break
                else:
                    substring.append(s[j])
            if len(substring) > maxLength:
                maxLength = len(substring)
        return maxLength
obj = Solution()
s = str(input("Enter a string:"))
print(obj.lengthOfLongestSubstring(s))

Monday, January 23, 2023

Leet Code ZigZag Conversion

 





The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

P   A   H   N
A P L S I I G
Y   I   R

And then read line by line: "PAHNAPLSIIGYIR"

Write the code that will take a string and make this conversion given a number of rows:

string convert(string s, int numRows);

 

Example 1:

Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"

Example 2:

Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:
P     I    N
A   L S  I G
Y A   H R
P     I

Example 3:

Input: s = "A", numRows = 1
Output: "A"

 

Constraints:

  • 1 <= s.length <= 1000
  • s consists of English letters (lower-case and upper-case), ',' and '.'.
  • 1 <= numRows <= 1000




import numpy as np


class Solution(object):

    def convert(self, s, numRows):
        numCols = len(s)
        arr_characters = np.zeros(numRows * numCols).reshape(numRows, numCols)
        #print(arr_characters)
        if numRows == 1:
            return s
        else:

            counter = 0
            pos = 0
            vertical_shift = 0
            for i in range(len(s)):
                if counter <= numRows:
                    _zero = 0
                    for j in range(numRows):
                        if pos >= len(s):
                            break
                        else:
                            #print(s[pos],end=' ')
                            #print("(",_zero,vertical_shift,")",end= ' ') # = ord(s[pos])
                            arr_characters[_zero][vertical_shift] = ord(s[pos])
                            pos += 1
                            _zero += 1
                    counter = numRows + 1
                    vertical_shift += (numRows - 1)
                    #print(vertical_shift,"***************")

                else:
                    diagonal_shift_col = vertical_shift - (numRows - 2)
                    diagonal_shift_row = numRows - 2
                    for j in range(numRows - 2):
                        if pos >= len(s):
                            break
                        else:
                            #print(s[pos],end = ' ')
                            #print("(",diagonal_shift_row,diagonal_shift_col,")",end= ' ') # = ord(s[pos])
                            arr_characters[diagonal_shift_row][diagonal_shift_col] = ord(s[pos])
                            diagonal_shift_col += 1
                            diagonal_shift_row -= 1
                            pos += 1
                    #print("**************")
                    counter = 0
            #print(arr_characters)
            output = ""
            for i in range(numRows):
                for j in range(numCols):
                    if arr_characters[i][j] != 0:
                        output += chr(int(arr_characters[i][j]))
                    else:
                        output += ""
            #output.encode('utf-8')
            return output


obj = Solution()
s = ""
numRows = int(input("Enter the number of rows:"))
s = str(input("Enter the string:"))
print(obj.convert(s, numRows))