0% found this document useful (0 votes)
2 views

assignment 2AI

The document contains Python code for an assignment with multiple questions, including functions for calculating factorials, generating Fibonacci series, finding the maximum of three numbers, determining the minimum steps to measure a specific volume using two jugs, and calculating the sum of two numbers. Each question includes user input prompts and outputs the results. The code demonstrates basic programming concepts such as recursion, loops, and mathematical operations.

Uploaded by

rudraksheez
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

assignment 2AI

The document contains Python code for an assignment with multiple questions, including functions for calculating factorials, generating Fibonacci series, finding the maximum of three numbers, determining the minimum steps to measure a specific volume using two jugs, and calculating the sum of two numbers. Each question includes user input prompts and outputs the results. The code demonstrates basic programming concepts such as recursion, loops, and mathematical operations.

Uploaded by

rudraksheez
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

15/01/2025, 17:32 assignment_2

In [ ]: #QUESTION 1

def factorial(num):
if (num == 0 or num == 1) :
return 1;
else :
return num * factorial(num - 1);

num = int(input("Enter a number: "));


print(factorial(num));

120

In [ ]: #QUESTION 2

def fibonacci(num):
vals = [];
for i in range(0, num):
if (i == 0 or i == 1):
val = i;
else:
val = vals[i - 1] + vals[i - 2];
vals.insert(i, val);
return vals;

len = int(input("Enter a number: "));


series = fibonacci(len);
for val in series:
print(val);

0
1
1
2
3
5
8
13
21
34

In [ ]: #QUESTION 3 & 6

num1 = float(input("Enter number 1: "));


num2 = float(input("Enter number 2: "));
num3 = float(input("Enter number 3: "));
print(max(num1, num2, num3), " is maximum");

123.54 is maximum

In [ ]: #QUESTION 4

from math import gcd;

def Pour(toJugMax, fromJugMax, d):


fromJug = fromJugMax;
toJug = 0;
steps = 1;
while (fromJug != d and toJug != d):

file:///home/administrator/Documents/assignment_2.html 1/2
15/01/2025, 17:32 assignment_2

temp = min(fromJug, toJugMax - toJug);


toJug = toJug + temp;
fromJug = fromJug - temp;
steps += 1;
if (fromJug == d or toJug == d):
break;
if fromJug == 0:
fromJug = fromJugMax;
steps += 1;
if toJug == toJugMax:
toJug = 0;
steps += 1;
return steps;

def minSteps(n, m, d):


if (d % gcd(n,m) != 0):
return -1;
return(min(Pour(n,m,d), Pour(m,n,d)));

m = int(input("Enter capacity of jug 1: "));


n = int(input("Enter capacity of jug 2: "));
d = int(input("Enter target capacity: "));
print('Minimum number of steps required is', minSteps(n, m, d));

Minimum number of steps required is 6

In [ ]: #QUESTION 5

num1 = float(input("Enter number 1: "));


num2 = float(input("Enter number 2: "));
print("sum is: ", num1 + num2);

sum is: 44.0

file:///home/administrator/Documents/assignment_2.html 2/2

You might also like