Price list behind API: Difference between revisions
Content added Content deleted
(→{{header|Wren}}: Didn't satisfy the task requirements previously.) |
(Added Go) |
||
Line 14: | Line 14: | ||
# Show ascending price ranges and the number of items covered by each range. |
# Show ascending price ranges and the number of items covered by each range. |
||
# Show output from a sample run here. |
# Show output from a sample run here. |
||
=={{header|Go}}== |
|||
{{trans|Wren}} |
|||
<lang go>package main |
|||
import ( |
|||
"fmt" |
|||
"math" |
|||
"math/rand" |
|||
"time" |
|||
) |
|||
var minDelta = 1.0 |
|||
func getMaxPrice(prices []float64) float64 { |
|||
max := prices[0] |
|||
for i := 1; i < len(prices); i++ { |
|||
if prices[i] > max { |
|||
max = prices[i] |
|||
} |
|||
} |
|||
return max |
|||
} |
|||
func getPRangeCount(prices []float64, min, max float64) int { |
|||
count := 0 |
|||
for _, price := range prices { |
|||
if price >= min && price <= max { |
|||
count++ |
|||
} |
|||
} |
|||
return count |
|||
} |
|||
func get5000(prices []float64, min, max float64, n int) (float64, int) { |
|||
count := getPRangeCount(prices, min, max) |
|||
delta := (max - min) / 2 |
|||
for count != n && delta >= minDelta/2 { |
|||
if count > n { |
|||
max -= delta |
|||
} else { |
|||
max += delta |
|||
} |
|||
max = math.Floor(max) |
|||
count = getPRangeCount(prices, min, max) |
|||
delta /= 2 |
|||
} |
|||
return max, count |
|||
} |
|||
func getAll5000(prices []float64, min, max float64, n int) [][3]float64 { |
|||
pmax, pcount := get5000(prices, min, max, n) |
|||
res := [][3]float64{{min, pmax, float64(pcount)}} |
|||
for pmax < max { |
|||
pmin := pmax + 1 |
|||
pmax, pcount = get5000(prices, pmin, max, n) |
|||
res = append(res, [3]float64{pmin, pmax, float64(pcount)}) |
|||
} |
|||
return res |
|||
} |
|||
func main() { |
|||
rand.Seed(time.Now().UnixNano()) |
|||
numPrices := 99000 + rand.Intn(2001) |
|||
maxPrice := 1e5 |
|||
prices := make([]float64, numPrices) // list of prices |
|||
for i := 0; i < numPrices; i++ { |
|||
prices[i] = float64(rand.Intn(int(maxPrice) + 1)) |
|||
} |
|||
actualMax := getMaxPrice(prices) |
|||
fmt.Println("Using", numPrices, "items with prices from 0 to", actualMax, "\b:") |
|||
res := getAll5000(prices, 0, actualMax, 5000) |
|||
fmt.Println("Split into", len(res), "bins of approx 5000 elements:") |
|||
total := 0 |
|||
for _, r := range res { |
|||
min := int(r[0]) |
|||
tmx := r[1] |
|||
if tmx > actualMax { |
|||
tmx = actualMax |
|||
} |
|||
max := int(tmx) |
|||
cnt := int(r[2]) |
|||
total += cnt |
|||
fmt.Printf(" From %6d to %6d with %4d items\n", min, max, cnt) |
|||
} |
|||
if total != numPrices { |
|||
fmt.Println("Something went wrong - grand total of", total, "doesn't equal", numPrices, "\b!") |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
Using 99784 items with prices from 0 to 99999 : |
|||
Split into 20 bins of approx 5000 elements: |
|||
From 0 to 5061 with 4997 items |
|||
From 5062 to 10031 with 5000 items |
|||
From 10032 to 15091 with 5000 items |
|||
From 15092 to 20114 with 5000 items |
|||
From 20115 to 25141 with 5000 items |
|||
From 25142 to 30206 with 4997 items |
|||
From 30207 to 35291 with 5000 items |
|||
From 35292 to 40333 with 4999 items |
|||
From 40334 to 45451 with 4999 items |
|||
From 45452 to 50422 with 4998 items |
|||
From 50423 to 55355 with 4997 items |
|||
From 55356 to 60268 with 4997 items |
|||
From 60269 to 65240 with 5000 items |
|||
From 65241 to 70193 with 4999 items |
|||
From 70194 to 75272 with 4998 items |
|||
From 75273 to 80154 with 5000 items |
|||
From 80155 to 85218 with 5000 items |
|||
From 85219 to 90120 with 4996 items |
|||
From 90121 to 95102 with 4998 items |
|||
From 95103 to 99999 with 4809 items |
|||
</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |