Logic problem to solve with python (or any other programming language) - math

I have to solve this real problem, it seemed trivial to me at first but I am having difficulty and I don't have much time to solve it. I would like to solve it possibly with python so then I can print the result to a csv.
This is the problem:
Problem
7 employees must divide the work shifts (M,A,N, morning afternoon and night) over 7 weeks.
Each day of the 49 days must have a different employee for each shift.( e.g. day1: M:employee 1, A:employee 3, N:employee 6 )
Each employee works weekday/time slot only 1 time in the 49-day cycle (e.g. employee 1 does 1 Monday morning, 1 Monday afternoon, 1 Monday night, 1 Tuesday morning etc)
If an employee works a night shift, it cannot work shifts on the next 2 days.

Mapping shift (M, A, N) to (0, 1, 2) with variable s, day (Monday, Tuesday, ..., Sunday) to (0, 1, ..., 6) with variable d, and week in variable w, you can use the formula (w+d-s)%7+1 to get the employee assigned to shift w,d,s (% is the modulo operator).
(in some languages like C++, you might have to add 7 in the parentheses if modulo of negative numbers outputs a negative number)

Related

Jumping Frog on even number of pads using DrRacket (Scheme)

I am trying to solve this question below which first requires finding the math formula then writing code in DrRacket (Scheme dialect of Lisp):
A frog with unlimited jumping power is sitting on the first pad (pad number 0), out of N pads in a row (N is always even). The frog wants to reach the last (Nth) pad. The frog can jump any even number of pads, including N: it’s possible that it will do it in 1 jump (by jumping N pads), in N/2 jumps (by jumping 2 pads at a time) or in any other combination of even jumps. For example, if N were 4, it could jump in one jump: 0th to 4th, or in 2 jumps (from 0th to 2nd and then from 2nd to 4th). The total number of ways of jumping from the 0th to the 4th pad is 2 ways.
What is the math formula for finding the total number of ways the frog can reach the Nth pad?
Using Scheme, write a procedure that, given a number of pads N, returns the total number of ways the frog can reach the Nth pad BY USING RECURSIVE CALLS ONLY. If the number of pad N is not even, the frog can't jump and the procedure returns "x".
I got a solved question which is slightly different and here is the solution for it below, but I am not sure what the math formula is for the question above, though, so would appreciate your help.
A frog with unlimited jumping power is sitting on the first pad (pad number 1), out of N pads in a row. The frog wants to reach the last (Nth) pad. The frog can jump any number of pads, including N: it's possible that it will do it in 1 jump (by jumping N pads), in N jumps (by jumping 1 pad at a time) or in any other combination. For example, if N were 4, it could jump one jump; 1 to 4th, or in 2 jumps; the first of length 1 and then 2 (from 1st to 2nd and then from 2nd to 4th), or the first of length 2 and then 1 (from 1st to 3rd and then from 3rd to 4th), or in 3 jumps (from 1st to 2nd and from 2nd to 3rd and from 3rd to 4th). The total number of ways of jumping from the 1st to the 4th pad is 4 ways. Write a procedure that, given a number of pads N, returns the total number of ways the frog can reach the Nth pad.
Solution:
Total possible jumps = 2 ^ (N-2)
DrRacket Code: (define(pad-jumps n)(power 2 (- n 2))))

R circular "linked" list: adding +1 to last index brings you to first index

I'm trying to implement movement through four points, while recording which points I visit. Think of it as a square. I can move from corner to corner or diagonally.
If you 'unwrap' the square you get a straight line with four points, which can be thought of as 1-2-3-4- where after 4 it goes back to 1. So if I'm at point 2 I can move to 1 and 3 directly or 4 diagonally. I'd implement that as 2-1 / 2+1 for corner-to-corner or 2+/-2 for diagonally. The problem occurs when I'm at 2 and will try to subtract 2 where I'll end up outside of the list.
The thought I've had is that if I could somehow translate my "out of bounds" numbers to in bounds this would be solved. One solution is hard coding that:
0=4
-1=3
5=1
6=2
but I'm pretty sure there is a better way to do this, however I can't seem to find it.
It seems to me all you want is modular arithmetic (bless the lord for math)
magicFun <- function (x) x %% 4
Here is a simple test run
> magicFun(0:6)
[1] 0 1 2 3 0 1 2
Addendum
It's more about math but the reason it works for negatives is that in Z/nZ ("the world where n is equal to 0") n is "identified" to 0.
This means you can add n as many times as you wish to a given number without changing it's "value".
Also, by convention the numbers in Z/nZ are listed as {0, 1, ..., n-1}.
So suppose n = 4 and x = -6, by the above x = x + 2*4 = 2.

Choco solver shift scheduling

i m a total beginner in Choco Solver. I want to make a simple shift scheduler.
i have set integer variables like this
IntVar day1 = model.intVar("day1", new int[] {0,1,2,3,4,5});
where 0 , 1,...5 is a reference ID to an employee.
I have a total of 30 variables,(one for every day of the month) since this a monthly based shift schedule.
I have set up constraints, that do not allow e.g. not be on shift for two days in a row.
My question is,
how can i set up a constraint, such that each employer has a minimum of 5 shifts ie. each value in the domain appears at least 5 times in all 30 variables ?
Thank you!
There are several ways of doing this. Give a look at model.globalCardinality and model.count, these constraints enable to count the number of times a value is used by a set of variables.
http://choco-solver.org/apidocs/org/chocosolver/solver/constraints/IConstraintFactory.html
For instance, model.count(3, vars, model.intVar(5,10)).post(); means that between 5 and 10 variables in vars should be equal to 3, so employee 3 should do between 5 and 10 shifts.

How to calculate the expected cost?

I am not good at probability and I know it's not a coding problem directly. But I wish you would help me with this. While I was solving a computation problem I found this difficulty:
Problem definition:
The Little Elephant from the Zoo of Lviv is going to the Birthday
Party of the Big Hippo tomorrow. Now he wants to prepare a gift for
the Big Hippo. He has N balloons, numbered from 1 to N. The i-th
balloon has the color Ci and it costs Pi dollars. The gift for the Big
Hippo will be any subset (chosen randomly, possibly empty) of the
balloons such that the number of different colors in that subset is at
least M. Help Little Elephant to find the expected cost of the gift.
Input
The first line of the input contains a single integer T - the number
of test cases. T test cases follow. The first line of each test case
contains a pair of integers N and M. The next N lines contain N pairs
of integers Ci and Pi, one pair per line.
Output
In T lines print T real numbers - the answers for the corresponding test cases. Your answer will considered correct if it has at most 10^-6 absolute or relative error.
Example
Input:
2
2 2
1 4
2 7
2 1
1 4
2 7
Output:
11.000000000
7.333333333
So, Here I don't understand why the expected cost of the gift for the second case is 7.333333333, because the expected cost equals Summation[xP(x)] and according to this formula it should be 33/2?
Yes, it is a codechef question. But, I am not asking for the solution or the algorithm( because if I take the algo from other than it would not increase my coding potentiality). I just don't understand their example. And hence, I am not being able to start thinking about the algo.
Please help. Thanks in advance!
There are three possible choices, 1, 2, 1+2, with costs 4, 7 and 11. Each is equally likely, so the expected cost is (4 + 7 + 11) / 3 = 22 / 3 = 7.33333.

Make-change: Beginner Trouble

I'm trying to create "make-change" that will return a ls of coins whose sum = the input, and it needs to contain the least number of coins possible.
Ex: (make-change 99)
=> (quarter quarter quarter dime dime penny penny penny penny)
Here's the lines along which make-change should operate:
If the remaining amount is exactly equal to 1, 5, 10, or 25 then return the appropriate coin.
Otherwise, cons the largest coin you can use onto the result of (make-change (- x value)) where value is the amount of the coin that you just used.
You can tell this procedure will terminate, since the amount will become smaller and smaller via step 2 until it is finally amenable to concluding with step 1.

Resources