Python Functions and Recursion

 

S6P10-WATER CHALLENGE

Water Challenge
 
""Kids on the Move" is a challenging game show organized for kids from age 10 to 15 years. Eddy and Sarah participated at the show and progressed to the semi-finals. At the semi-finals, they were given two vessels, one of which can accommodate a litres of water and the other - b litres of water. The challenge to them was to determine the number of steps required to obtain exactly c litres of water in one of the vessels.
 
At the beginning both vessels are empty. The following operations are counted as 'steps':
  • emptying a vessel,
  • filling a vessel,
  • pouring water from one vessel to the other, without spilling, until one of the vessels is either full or empty.
Help the kids clear the challenge and progress to the finals. Write a recursive function to find the number of steps required to obtain exactly c litres of water in one of the vessels.
 
C Function Specifications:
Use the function name, return type and the argument type as:
int minSteps(int,int,int)
The Function should return the minimum number of steps required to obtain c litres, or return -1 if this is impossible.

 
Python Function Specifications:
Use the function name, return type and the argument type as:
def minSteps(int,int,int)
The Function should return the minimum number of steps required to obtain c litres, or return -1 if this is impossible.
 
Input Format:
There are three lines of the input which contains three positive integers a, b and c respectively.
 
Output Format:
Output the minimum number of steps required to obtain c litres, or -1 if this is impossible.
 
Refer sample input and output for formatting specifications.
 
Sample Input 1:
5
2
3
 
Sample Output 1:
2
 
Sample Input 2:
2
3
4
 
Sample Output 2:
-1
 


 

Solution:

def find(a, b, c):
    A, B = a, 0
    counter = 1

    while A or B:
        t = min(b - B, A)
        while t:
            B += t
            A -= t
            counter += 1

            if A == c or B == c:
                return counter

            if B == b:
                counter += 1
                B = 0

            t = min(b - B, A)

        if B:
            A = a
            counter += 1

    return -1



a = int(input())
b = int(input())
c = int(input())
q = find(a, b, c), find(b, a, c)
q = [x for x in q if x != -1]
if a == c or b == c:
    print(1)
else:
    print(min(q, default=-1))







S4P10 - JUNIOR CODERS

Junior Coders
 

Junior Coders Academy is a unique learning Centre that offers a creative and inspiring collaborative environment for building coding skills to students of Grades 1 to 12.
Williams, the proprietor of the Academy and the mentor for the students started his first session of the day with the interesting programming concept of using Functions. Post an interactive session of learning through design, Williams gave the students a small self-activity to write a program using functions to verify from two integer numbers A and B, if B corresponds to the last digit/digits of A. Williams wanted you to write the program for evaluating the students’ codes.

C Function Specifications:
Use the function name, return type and the argument type as:
int findValue(String,String)
The function must return 1 if the second number B is the last digits of A, else return 0.

Python Function Specifications:
Use the function name, return type and the argument type as:
def findValue(String,String)
The function must return 1 if the second number B is the last digits of A, else return 0.

Input Format:
First line of the input contains a string A.
Second line of the input contains a string B.

Output Format:
Print "Yes" if the second number B is the last digits of A. Print "No" otherwise.
Refer sample input and output for formatting specifications.

Sample Input 1:
1234
1234

Sample Output 1:
Yes

Sample Input 2:
5434554
543

Sample Output 2:
No



Solution:

def findValue(a,b):
    a=a[::-1]
    b=b[::-1]
    if(a.find(b)==0):
        print('Yes')
    else:
        print('No')
x=input()
y=input()
findValue(x,y)

Comments

Post a Comment

Popular posts from this blog

2-D Arrays

String

Conditional Statements