Math question regarding a Fantasy Sports (snake) draft - php

If you're familiar with any fantasy sports draft, the draft-order grid looks something like this:
EXAMPLE 1 (3-teams):
Round Team 1 Team 2 Team 3
1 1 (1.1) 2 (1.2) 3 (1.3)
2 6 (2.3) 5 (2.2) 4 (2.1)
3 7 (3.1) 8 (3.2) 9 (3.3)
The numbers 1-9 represent the overall pick number of the draft.
The items in parentheses represent the round_number and pick_number_of_that_round.
I cannot figure out a formula which converts my overall_pick_number into it's proper pick_number_of_that_round.
In the above example, the number 8 equals 2 (the 2nd pick of the 3rd round). But in a 4-team league, the number 8 equals 4 (the 4th pick of the 2nd round).
EXAMPLE 2 (4-teams):
Round Team 1 Team 2 Team 3 Team 4
1 1 (1.1) 2 (1.2) 3 (1.3) 4 (1.4)
2 8 (2.4) 7 (2.3) 6 (2.2) 5 (2.1)
3 9 (3.1) 10 (3.2) 11 (3.3) 12 (3.4)
I thought about trying to dynamically build an associative array based on the number of teams in the league containing every pick and which pick it belonged to, but it's just beyond me.

I'll modify the answer by missingno to make it twist like a 'snake'
round_number = ((overall_pick_number - 1) / number_of_teams) + 1
pick_number_of_round = ((overall_pick_number - 1) % number_of_teams) + 1
if (round_number % 2 == 0) {
pick_number_of_round = number_of_teams - pick_number_of_round + 1
}

round_number = ((overall-1) / number_of_teams) + 1
pick_number_of_round = ((overall-1) % number_of_teams) + 1

Related

Need Help ranking the poll options based on prority selection

Need some help in ranking poll options, tried doing by multidimensional arrays but nothing worked.
My data structure is as per below:
**Table** Poll
**Id** **Question** **op1** p1 **op2** p2 **op3** p3 **op4** p4 **op5** p5
1 q1 ? Excellent 5 Better 4 Good 3 Ok 2 Not OK 1
2 q2 ? Sure 5 Perfect 4 Fine 3 Never 2 No 1
**Table** Answer
**id** **poll_id** **users_id** **answer** **resultOrder**
---------------------------------------------------------------
1 1 1 Excellent 1
2 1 1 Better 2
3 1 1 Ok 3
4 1 1 Good 4
5 1 1 Not Ok 5
6 1 2 Excellent 1
7 1 2 Ok 2
8 1 2 Better 3
9 1 2 Not Ok 4
10 1 2 Good 5
Each user will submit five options priorities them as per his suggestion
Option will get ranking as per counts
e.g Excellent selected for 2 times for first preference it will get 100 points and others will get 80,60,40,20 points based on their counts for all priorities.
if count ties matches points will be given on p1, p2 column in table poll
**For Example if my group by [answer] look like below 5 people voted Excellent as first preference, 4 people voted better as second preference and so on till Not Ok preferred by 1 person for fifth preference **
Answer count(answer) it should set point like this
Excellent 5 100
Better 4 80
Good 3 60
Ok 2 40
Not Ok 1 20
Hope this is possible. I tried it many ways, but no luck.

Lucky Draw Concept : SQL Query to get random option based on percentage of appearance

I am developing a Lucky draw concept as following
There will be multiple options at database we have to get the random options according to percentage of appearance
Database table is as following
option_id | option_name | option_type | option_in_day(%) | option_status
----------------------------------------------------------------------
1 $2 Off 2 100 1
2 $3 Off 2 95 1
3 $4 Off 2 95 1
4 $5 Off 2 90 1
5 $8 Off 2 90 1
6 $60 Cashback 2 10 1
7 $100 Cashback 2 5 1
8 Umbrela 2 50 1
9 Iphone 2 2 1
10 International
Tour 2 1 1
11 Fidget
Spinner 2 70 1
12 Free
membership 2 30 1
13 Samsung S8 2 10 1
14 $20 Off 2 60 1
15 Travel Card 2 50 1
16 Soft Toys 2 70 1
Now from this table I want to get random 8 option according to percentage.
Less percentage option chance to retrieve in result will be less.
I have tried with random function in sql but can't reach the requirement.
This is how you should be able to do it:
set #percentage = 100 * rand();
select *
from table_name
order by table_name.percentage >= #percentage, rand()
limit 0, 8;
This query stores a random percentage into a variable. Then, we order the query by two criterias. The first criteria is that the percentage of the table is higher or equal to the percentage randomized. This makes sure that if there are elements both above and below the randomized percentage, then the elements above will be preferred. Elements having similar result in the first ordering criteria will be randomly ordered.

mysql advanced query select based on params

As first please be nice to me im a beginner with SQL language and english language.
So i have problem with this query.
I've created sqlfiddle.
My query look like it's working properly but I find it is not working
I would like to write a query that returns the product ID variant based on the parameters that will send
Correct query result would look like the following
PARAM = 6
id product_id productvariant_id attributevalue_id
1 3 1 6
2 3 2 6
---- BAD RESULTS -----
3 3 3 6
4 3 3 9
6 3 5 6
7 3 6 6
8 3 6 11
PARAM = 6,9
id product_id productvariant_id attributevalue_id
3 3 3 6
4 3 3 9
---- BAD RESULTS -----
3 3 3 6
4 3 3 9
6 3 5 6
7 3 6 6
8 3 6 11
What i really need is return productvariant_id which contains combination of inserted params, if I send only one attributevalue_id, i need to find productvariant which contain only ONE attributevalue_id. IF i send two params i finding combination of two.. not more or less
Unfortunately I do not have enough reputation for a comment, thus I have to write this as answer.
Could you clarify your goal?
Do you want to retrieve the productvariant_id of all datasets where the attributevalue_id matches your parameter?
SELECT productvariant_id
FROM productvariantattribute
WHERE attributevalue_id IN ($YOUR_PARAMETER_LIST);
Or do you want to retrieve the productvariant_id of all datasets where the productvariant_id has only these attributevalue_ids you specified as parameter?
SELECT `productvariant_id`
FROM `productvariantattribute`
WHERE `attributevalue_id` IN ($YOUR_PARAMETER_LIST)
AND `productvariant_id` NOT IN (
SELECT `productvariant_id`
FROM `productvariantattribute`
WHERE `attributevalue_id` NOT IN ($YOUR_PARAMETER_LIST)
)

SQL - Filtering search results

OK – need some help here – might have bitten off more than I can chew here – but I’m looking to write a SQL query that always returns a minimum of 10 results. If the first condition is applied and the result set exceeds 10 results then move on to the next condition etc..
Example
Find all the small red plastic toy cars that cost less than £5.00.
I always want to show a minimum of 10 items on the screen. Want to search on “small” then on “red” and then on “< £5.00”. If the query returns more than 10 items then continue to filter on as many tags as possible (i.e plastic, toy and car) – the more matches it can make the higher it should be ranked (i.e if a products has all 3 tags associated with it – it will be at the top of the list, if a product only matches 1 tag – then this will be lower down the list.
Price Table
ID Price
1 £1.50
2 £2.50
3 £6.00
.
Colour Table
ID Colour
1 Red
2 Blue
3 Yellow
.
Size Table
ID Shape
1 Small
2 Medium
3 Large
.
Products Table
ID Description price_id colour_id size_id
1 Item 1 1 2 2
2 Item 2 2 2 1
3 Item 3 1 1 1
4 Item 4 3 2 3
5 Item 5 3 1 2
6 Item 6 1 1 2
7 Item 7 1 1 3
.
Tags Table
ID Description
1 Shiny
2 Plastic
3 Wood
4 Toy
5 Disney
6 Animal
7 Car
.
Items_Tags Table
ID tag_id product_id
1 1 1
2 4 1
3 7 1
4 2 2
5 3 3
6 4 3
7 7 6
8 7 7
9 7 2
Quite a long example - but I hope you get the point. I have wondered whether there would be any benefit to putting all the filters within the tags table - i.e the price, colour and size and then would only have to search against the tags table.
Any ideas anyone?
Thanks
It depends on your usage: filtering integers is faster than string (by the way, are they indexed?) but JOIN is also time consuming. So if the code outside the sql works with the integers, keep them (and don't join if not required to show the text)

Hilbert Curve in PHP GD

I have a large amount of data that I'd like to be able to read out, pixel by pixel, into a Hilbert Curve using PHP's GD library.
The aim is to create a lookup table of an arbitrary size mapping an address to a point on a pixel grid. eg.
0 1 2 3
+-------------
0 | 0 1 14 15 ->
1 | 3 2 13 12
2 | 4 7 8 11
3 | 5 6 9 10
The eighth sequential address in this example would be 2,2. The end-result lookup table will just consist of points that can be referenced from.
1 - 0,0
2 - 0,1
3 - 1,1
4 - 0,2
I realise there is most certainly an effective way to generate this, I just haven't thought of it yet.

Categories