Save the prisoner hackerrank solution in python

A jail has a number of prisoners and a number of treats to pass out to them. Their jailer decides the fairest way to divide the treats is to seat the prisoners around a circular table in sequentially numbered chairs. A chair number will be drawn from a hat. Beginning with the prisoner in that chair, one candy will be handed to each prisoner sequentially around the table until all have been distributed.

The jailer is playing a little joke, though. The last piece of candy looks like all the others, but it tastes awful. Determine the chair number occupied by the prisoner who will receive that candy.

For example, there are 4 prisoners and 6 pieces of candy. The prisoners arrange themselves in seats numbered 1 to 4. Let’s suppose two is drawn from the hat. Prisoners receive candy at positions 2, 3, 4, 1, 2, 3. The prisoner to be warned sits in chair number 3.

Function Description

Complete the saveThePrisoner function in the editor below. It should return an integer representing the chair number of the prisoner to warn.

saveThePrisoner has the following parameter[s]:

  • n: an integer, the number of prisoners
  • m: an integer, the number of sweets
  • s: an integer, the chair number to begin passing out sweets from

My Solution

I’m providing the solution for Python and JS, please leave on the comments if you found a better way.

A jail has a number of prisoners and a number of treats to pass out to them. Their jailer decides the fairest way to divide the treats is to seat the prisoners around a circular table in sequentially numbered chairs. A chair number will be drawn from a hat. Beginning with the prisoner in that chair, one candy will be handed to each prisoner sequentially around the table until all have been distributed.

The jailer is playing a little joke, though. The last piece of candy looks like all the others, but it tastes awful. Determine the chair number occupied by the prisoner who will receive that candy.

Example

n=4

m=6

s=2

There are 4 prisoners,6  pieces of candy and distribution starts at chair 2. The prisoners arrange themselves in seats numbered 1 to 4. Prisoners receive candy at positions 2,3,4,1,3 The prisoner to be warned sits in chair number 3.

Function Description

Complete the saveThePrisoner function in the editor below. It should return an integer representing the chair number of the prisoner to warn.

saveThePrisoner has the following parameter[s]:

  • int n: the number of prisoners
  • int m: the number of sweets
  • int s: the chair number to begin passing out sweets from

Returns

  • int: the chair number of the prisoner to warn

Input Format

The first line contains an integer,l , the number of test cases.

The next t lines each contain 3 space-separated integers:

  • n: the number of prisoners
  • m: the number of sweets
  • s: the chair number to start passing out treats at

Constraints

  • 1 s; --s; --m; s += m; s %= n; s++; cout n >> m >> s; cout tc; forn[it, tc] solve[]; return 0; }

    Problem solution in C programming.

    #include #include #include #include int main[] { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ long int t,n,m,s,tot; scanf["%ld",&t]; while[t--] { scanf["%ld %ld %ld",&n,&m,&s]; tot=m+s-1; if[tot%n==0] printf["%ld\n",n]; else printf["%ld\n",tot%n]; } return 0; }

    Problem solution in JavaScript programming.

    function processData[input] { //Enter your code here var a = input.split["\n"]; var n = a.shift[]; for[var i = 0; i < n; i++] { var b = a[i].split[" "]; var prisoners = parseInt[b[0]]; var sweets = parseInt[b[1]]; var id = parseInt[b[2]]; var last = 0; last = [sweets+id-1]%prisoners; if[last === 0] { last = prisoners; } console.log[last]; } } process.stdin.resume[]; process.stdin.setEncoding["ascii"]; _input = ""; process.stdin.on["data", function [input] { _input += input; }]; process.stdin.on["end", function [] { processData[_input]; }];

    Video liên quan

Chủ Đề