Skip to main content

Persistent Coding Questions and Answer in 2022 - Question 4

Persistent Coding Questions and Answer in 2022

Problem Statement :

Sum is given a rectangular paper having dimension h x w, where h is the height and w is the width. Sam wants to fold the paper so its dimension are h1 x w1 in the minimum number of moves. The paper can only be folded parallel to its edges, and after folding the dimension should be integers.

For example, given h =8 and w =4, fold the paper until it is h1,w1 =6,1. Find fold along the long edge 6 units from a side, resulting in a paper that is 6 x 4. Next, fold along the width 2 units from the 4 unit edge to have 6 x 2. Fold along the center of the 2 units edge to achieve a 6 x 111 page in three folds.

Function Description

Write a function minMoves, which will return an integer value that denotes the minimum number of moves in which the task can be achieved

minMoves has the following parameter:

  • h: an integer that denotes the initial height
  • w: an integer that denotes the initial width
  • h1: an integer that denotes the final height
  • w1: an integer that denotes the initial width

Sample Input

2
3
2
2

Sample Output 

1

Explanation

The initial dimension of the paper is 2 x 3. This should be connected to 2 x 2. Fold along a line 1 unit in from the edge of the 7 unit side.

Sample Input

6
3
3
1

Sample Output

3

Explanation

Initial dimension of the paper are 6 x 3. This should be connected to 3 x 3. This can be done in the following way. There can be many other ways to solve, this is one of the ways.

6 x 3 -> 3 x 3 -> 3 x 2 -> 3 x 1.

 

 

 

Program :

h = float(input("Initial Height : "))

w = float(input("Initial Width : "))

h1 = float(input("Final Height : "))

w1 = float(input("Final Width : "))

count = 0

while h != h1:

    if h1 <= h/2:

          count+=1

          h = h/2

    elif h1 > h/2:

        count+=1

        h = abs(h-(h-h1))

while w != w1:

    if w1 <= w/2:

          count+=1

          w = w/2

    elif w1 > w/2:

        count+=1

        w = abs(w-(w-w1))

print(count)

 

for execution and output so check the video :




Comments