0% found this document useful (0 votes)
18 views12 pages

Competative Practical

Uploaded by

A.k Unity world
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views12 pages

Competative Practical

Uploaded by

A.k Unity world
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

COMPETITIVE PROGRAMMING

Skill Based Mini Project Report

Submitted for the partial fulfilment of the degree of

Bachelor of Technology
In
Artificial Intelligence and Machine Learning
Submitted By

ADITYA KUMAR SINGH


0901AM231004
UNDER THE SUPERVISION AND GUIDANCE OF

Mr. Khemchand Shakywar


(Assistant Professor)
Ms. Aditi Samadhiya
(Assistant Professor)

Centre for Artificial Intelligence

Session 2024-25
DECLARATION BY THE CANDIDATE

We hereby declare that the work entitled “Console Application and Python Scripts” is our
work, conducted under the supervision of Mr. Khemchand Shakywar (Assistant Professor)
& Ms. Aditi Samadhiya (Assistant Professor), during the session Aug-Dec 2024. The report
submitted by us is a record of bonafide work carried out by me.

We further declare that the work reported in this report has not been submitted and will not be
submitted, either in part or in full, for the award of any other degree or diploma in this institute
or any other institute or university.

--------------------------------

Aditya Kumar Singh (0901AM231004)

Date: 22.11.2024
Place: Gwalior

This is to certify that the above statement made by the candidates is correct to the best of our
knowledge and belief.
ACKNOWLEDGEMENT
We would like to express our greatest appreciation to all the individuals who have helped and
supported us throughout this report. We are thankful to the whole Centre for Artificial
Intelligence for their ongoing support during the experiments, from initial advice and provision
of contact in the first stages through ongoing advice and encouragement, which led to the final
report.

A special acknowledgement goes to our colleagues who help us in completing the file and by
exchanging interesting ideas to deal with problems and sharing the experience.

We wish to thank the faculty and supporting staff for their undivided support and interest which
inspired us and encouraged us to go my own way without whom we would be unable to
complete my project.

In the end, We want to thank our friends who displayed appreciation for our work and
motivated us to continue our work.

Aditya Kumar Singh


0901AM231004
CERTIFICATE

This is certified that Aditya Kumar Singh (0901AM231004) has submitted


the skill based mini project report titles “Console Application and Python
Scripts” under the mentorship of Mr. Khemchand Shakywar (Assistant
Professor) & Ms. Aditi Samadhiya (Assistant Professor), in partial
fulfilment of the requirement for the award of degree of Bachelor of
Technology in Artificial Intelligence and Machine learning From Madhav
Institute of Technology and Science, Gwalior.

-----------------------------------------------
Prof. Khemchand Shakywar
Faculty Mentor Assistant Professor
Centre for Artificial Intelligence

-----------------------------------------------
Prof. Aditi Samadhiya
Faculty Mentor Assistant Professor
Centre for Artificial Intelligence
Develop a console application that solves classic dynamic programming
problems like the Fibonacci sequence, coin change problem, and longest
common subsequence. Users can input problem parameters, and the
program outputs the solution.

CODE:

def fibonacci(n):
if n <= 0:
return "Input must be a positive integer."
dp = [0] * (n + 1)
dp[1] = 1
for i in range(2, n + 1):
dp[i] = dp[i - 1] + dp[i - 2]
return dp[n]

def coin_change(coins, amount):


dp = [float('inf')] * (amount + 1)
dp[0] = 0
for coin in coins:
for i in range(coin, amount + 1):
dp[i] = min(dp[i], dp[i - coin] + 1)
return dp[amount] if dp[amount] != float('inf') else -1

def longest_common_subsequence(s1, s2):


m, n = len(s1), len(s2)
dp = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(1, m + 1):
for j in range(1, n + 1):
if s1[i - 1] == s2[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
return dp[m][n]

def main():
while True:
print("\nDynamic Programming Problems:")
print("1. Fibonacci Sequence")
print("2. Coin Change Problem")
print("3. Longest Common Subsequence")
print("4. Exit")
choice = input("Select an option (1-4): ")

if choice == '1':
n = int(input("Enter the Fibonacci index (n): "))
result = fibonacci(n)
print(f"Fibonacci({n}): {result}")

elif choice == '2':


coins = list(map(int, input("Enter coin denominations (space-separated):
").split()))
amount = int(input("Enter the amount: "))
result = coin_change(coins, amount)
print(f"Minimum coins required: {result}")

elif choice == '3':


s1 = input("Enter the first string: ")
s2 = input("Enter the second string: ")
result = longest_common_subsequence(s1, s2)
print(f"Length of Longest Common Subsequence: {result}")

elif choice == '4':


print("Exiting program. Goodbye!")
break

else:
print("Invalid choice. Please try again.")

if __name__ == "__main__":
main()

OUTPUT
Implement a Python script that solves divide and conquer problems like
finding the maximum subarray sum or computing the nth Fibonacci
number efficiently using the divide and conquer approach.

CODE:
def max_subarray_sum(arr, low, high):
"""Divide and Conquer: Find the maximum subarray sum"""
if low == high:
return arr[low]

mid = (low + high) // 2

# Max subarray sum in left half


left_sum = max_subarray_sum(arr, low, mid)

# Max subarray sum in right half


right_sum = max_subarray_sum(arr, mid + 1, high)

# Max subarray sum crossing the middle


cross_sum = max_crossing_sum(arr, low, mid, high)

return max(left_sum, right_sum, cross_sum)

def max_crossing_sum(arr, low, mid, high):


"""Helper function to find max crossing sum."""
left_sum = float('-inf')
sum_ = 0
for i in range(mid, low - 1, -1):
sum_ += arr[i]
if sum_ > left_sum:
left_sum = sum_

right_sum = float('-inf')
sum_ = 0
for i in range(mid + 1, high + 1):
sum_ += arr[i]
if sum_ > right_sum:
right_sum = sum_

return left_sum + right_sum

def fibonacci_divide_and_conquer(n):
"""Divide and Conquer: Compute nth Fibonacci number."""
def fib_helper(k):
if k == 0:
return (0, 1) # Base case: (F(0), F(1))

a, b = fib_helper(k // 2)
c = a * ((b << 1) - a)
d=a*a+b*b

if k % 2 == 0:
return (c, d)
else:
return (d, c + d)

if n < 0:
return "Input must be a non-negative integer."
return fib_helper(n)[0]

def main():
while True:
print("\nDivide and Conquer Problems:")
print("1. Maximum Subarray Sum")
print("2. Fibonacci Number")
print("3. Exit")
choice = input("Select an option (1-3): ")

if choice == '1':
arr = list(map(int, input("Enter the array (space-separated): ").split()))
result = max_subarray_sum(arr, 0, len(arr) - 1)
print(f"Maximum Subarray Sum: {result}")

elif choice == '2':


n = int(input("Enter the Fibonacci index (n): "))
result = fibonacci_divide_and_conquer(n)
print(f"Fibonacci({n}): {result}")

elif choice == '3':


print("Exiting program. Goodbye!")
break

else:
print("Invalid choice. Please try again.")

if __name__ == "__main__":
main()
OUTPUT

You might also like