Horse racing: Difference between revisions

m
m (→‎{{header|Raku}}: Minor cleanup)
 
(8 intermediate revisions by 6 users not shown)
Line 80:
Other things being equal (which they seldom are in actual horse racing!) what would you '''''expect''''' the '''full result''' of Race 4 to be including the '''time''' of the winner?
<br><br>
 
=={{header|FreeBASIC}}==
{{trans|Nim}}
<syntaxhighlight lang="vbnet">' Ratings on past form, assuming a rating of 100 for horse A.
Dim As Double a = 100.0
Dim As Double b = a - 8 - 2 * 2 ' carried 8 lbs less, finished 2 lengths behind.
Dim As Double c = a + 4 - 2 * 3.5
Dim As Double d = a - 4 - 10 * 0.4 ' based on relative weight and time.
Dim As Double e = d + 7 - 2 * 1
Dim As Double f = d + 11 - 2 * (4 - 2)
Dim As Double g = a - 10 + 10 * 0.2
Dim As Double h = g + 6 - 2 * 1.5
Dim As Double i = g + 15 - 2 * 2
 
' Adjustments to ratings for current race.
b += 4
c -= 4
h += 3
Dim As Double j = a - 3 + 10 * 0.2
 
' Filly's allowance to give weight adjusted weighting.
b += 3
d += 3
i += 3
j += 3
 
' Create table mapping horse to its weight adjusted rating and whether colt.
Type Pair
key As String*1
rating As Double
colt As Boolean
End Type
 
Dim As Pair list(9)
list(0).key = "A": list(0).rating = a: list(0).colt = True
list(1).key = "B": list(1).rating = b: list(1).colt = False
list(2).key = "C": list(2).rating = c: list(2).colt = True
list(3).key = "D": list(3).rating = d: list(3).colt = False
list(4).key = "E": list(4).rating = e: list(4).colt = True
list(5).key = "F": list(5).rating = f: list(5).colt = True
list(6).key = "G": list(6).rating = g: list(6).colt = True
list(7).key = "H": list(7).rating = h: list(7).colt = True
list(8).key = "I": list(8).rating = i: list(8).colt = False
list(9).key = "J": list(9).rating = j: list(9).colt = False
 
' Sort in descending order of rating.
Dim As Integer n = Ubound(list)
For x As Integer = 0 To n - 1
For y As Integer = 0 To n - x - 1
If list(y).rating < list(y + 1).rating Then Swap list(y), list(y + 1)
Next y
Next x
 
' Show expected result of race.
Print !"Race 4\n"
Print "Pos Horse Weight Dist Sex"
Dim As String posic = ""
For x As Integer = Lbound(list) To Ubound(list)
Dim As Double wt = Iif(list(x).colt, 9.00, 8.11)
Dim As Double dist = 0.0
If x > 0 Then dist = (list(x-1).rating - list(x).rating) * 0.5
posic = Iif(x = 0 Or dist > 0, Str(x + 1), Iif(Right(posic, 1) <> "=", Str(x) & "=", posic))
Dim As String sx = Iif(list(x).colt, "colt", "filly")
Print Using "\\ ! ##.## #.# \ \"; posic; list(x).key; wt; dist; sx
Next x
 
' Weight adjusted rating of winner.
Dim As Double wr = list(0).rating
 
' Expected time of winner (relative to A's time in Race 1).
Dim As Double t = 96 - (wr - 100) / 10
Dim As Integer min = Int(t / 60)
Dim As Integer sec = t Mod 60
Print
Print Using "Time ## minute ##.# seconds"; min; sec
 
Sleep</syntaxhighlight>
{{out}}
<pre>Race 4
 
Pos Horse Weight Dist Sex
1 I 8.11 0.0 filly
2 J 8.11 2.0 filly
3 A 9.00 1.0 colt
4 F 9.00 0.5 colt
5 H 9.00 0.5 colt
6 E 9.00 0.5 colt
7 B 8.11 1.0 filly
7= D 8.11 0.0 filly
9 C 9.00 1.0 colt
10 G 9.00 0.5 colt
 
Time 1 minute 35.0 seconds</pre>
 
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 177 ⟶ 271:
sec := t - float64(min)*60
fmt.Printf("\nTime %d minute %3.1f seconds\n", min, sec)
}</langsyntaxhighlight>
 
{{out}}
Line 200 ⟶ 294:
=={{header|Julia}}==
{{trans|Go}}
<langsyntaxhighlight lang="julia">function main()
# ratings on past form, assuming a rating of 100 for horse A
a = 100
Line 262 ⟶ 356:
 
main()
</langsyntaxhighlight>{{out}}
<pre>
Race 4
Line 283 ⟶ 377:
=={{header|Nim}}==
{{trans|Wren}}
<langsyntaxhighlight Nimlang="nim">import algorithm, math, strformat, strutils
 
# Ratings on past form, assuming a rating of 100 for horse A.
Line 341 ⟶ 435:
var min = int(t / 60)
var sec = t mod 60
echo &"\nTime {min} minute {sec:.1f} seconds"</langsyntaxhighlight>
 
{{out}}
Line 359 ⟶ 453:
 
Time 1 minute 35.4 seconds</pre>
 
=={{header|Perl}}==
{{trans|Raku}}
<syntaxhighlight lang="perl">use strict;
use warnings;
 
my %card;
$card{a} = { name => 'Alberta Clipper', sex => 'M', rating => 100 };
$card{b} = { name => 'Beetlebaum', sex => 'F', rating => $card{a}{rating} - 8 - 2*2 };
$card{c} = { name => 'Canyonero', sex => 'M', rating => $card{a}{rating} + 4 - 2*3.5 };
$card{d} = { name => 'Donnatello', sex => 'F', rating => $card{a}{rating} - 4 - 10*0.4 };
$card{e} = { name => 'Exterminator', sex => 'M', rating => $card{d}{rating} + 7 - 2*1 };
$card{f} = { name => 'Frequent Flyer', sex => 'M', rating => $card{d}{rating} + 11 - 2*(4-2) };
$card{g} = { name => 'Grindstone', sex => 'M', rating => $card{a}{rating} - 10 + 10*0.2 };
$card{h} = { name => 'His Honor', sex => 'M', rating => $card{g}{rating} + 6 - 2*1.5 };
$card{i} = { name => 'Iphigenia in Brooklyn', sex => 'F', rating => $card{g}{rating} + 15 - 2*2 };
$card{j} = { name => 'Josephine', sex => 'F' };
 
# adjustments to ratings for current race
$card{b}{rating} += 4;
$card{c}{rating} -= 4;
$card{h}{rating} += 3;
$card{j}{rating} = $card{a}{rating} - 3 + 10*0.2;
 
# initialize carry weights
for (keys %card) {
$card{$_}{rating} += 3 if $card{$_}{sex} eq 'F';
$card{$_}{weight} = $card{$_}{sex} eq 'M' ? 9.00 : 8.11
}
 
print "Pos Horse Name Weight Back Sex Time\n";
 
my($previous, $position, $leader, @predictions) = 0;
for (sort { $card{$b}{rating} <=> $card{$a}{rating} } keys %card) {
$leader = $_ unless $leader;
++$position if $previous != $card{$_}{rating};
$previous = $card{$_}{rating};
$card{$_}{back} = ($card{$leader}{rating} - $card{$_}{rating}) / 2;
$card{$_}{time} = 96 - ($card{$_}{rating} - 100) / 10;
push @predictions, sprintf "%2d %s %-22s %.2f %.1f %-6s %s\n", $position, $_, $card{$_}{name}, $card{$_}{weight},
$card{$_}{back}, $card{$_}{sex} eq 'M' ? 'colt' : 'filly', sprintf '%1d:%.1f', int($card{$_}{time}/60), $card{$_}{time}-60;
}
 
print for sort @predictions;</syntaxhighlight>
{{out}}
<pre> 1 i Iphigenia in Brooklyn 8.11 0.0 filly 1:35.4
2 j Josephine 8.11 2.0 filly 1:35.8
3 a Alberta Clipper 9.00 3.0 colt 1:36.0
4 f Frequent Flyer 9.00 3.5 colt 1:36.1
5 h His Honor 9.00 4.0 colt 1:36.2
6 e Exterminator 9.00 4.5 colt 1:36.3
7 b Beetlebaum 8.11 5.5 filly 1:36.5
7 d Donnatello 8.11 5.5 filly 1:36.5
8 c Canyonero 9.00 6.5 colt 1:36.7
9 g Grindstone 9.00 7.0 colt 1:36.8</pre>
 
=={{header|Phix}}==
Disclaimer: I know nothing about horse racing, all calculations were reverse-engineered from those in the Go entry.<br>
Made table-based so you can plug in different races and adjustments, but any horse appearing in more than one race input would too in the output.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #7060A8;">requires</span> <span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.1"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">// [else sort_columns() needs a deep_copy()]</span>
Line 427 ⟶ 576:
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">60</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nTime %s %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">m</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 449 ⟶ 598:
=={{header|Raku}}==
I know nothing about horse racing and/or handicapping. This is mostly a straightforward translation of the logic from Go though I did take certain liberties with formatting and layout.
<syntaxhighlight lang="raku" perl6line>my %card;
%card<a> = { name => 'Alberta Clipper', :sex<M>, rating => 100 };
%card<b> = { name => 'Beetlebaum', :sex<F>, rating => %card<a><rating> - 8 - 2*2 };
Line 485 ⟶ 634:
printf "%2d %s %-22s %.2f %.1f %-6s %s\n", $position, .key, .value<name>, .value<weight>,
.value<back>, .value<sex> eq 'M' ?? 'colt' !! 'filly', .value.<time>.polymod(60 xx*).reverse.join: ':';
}</langsyntaxhighlight>
{{out}}
<pre>Pos Horse Name Weight Back Sex Time
Line 498 ⟶ 647:
8 c Canyonero 9.00 6.5 colt 1:36.7
9 g Grindstone 9.00 7.0 colt 1:36.8</pre>
 
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">struct Kv {
key string
value []f64
}
fn main() {
// ratings on past form, assuming a rating of 100 for horse A
a := 100.0
mut b := a - 8 - 2*2 // carried 8 lbs less, finished 2 lengths behind
mut c := a + 4 - 2*3.5
mut d := a - 4 - 10*0.4 // based on relative weight and time
e := d + 7 - 2*1
f := d + 11 - 2*(4-2)
g := a - 10 + 10*0.2
mut h := g + 6 - 2*1.5
mut i := g + 15 - 2*2
// adjustments to ratings for current race
b += 4
c -= 4
h += 3
mut j := a - 3 + 10*0.2
// filly's allowance to give weight adjusted weighting
b += 3
d += 3
i += 3
j += 3
// create map of horse to its weight adjusted rating and whether colt (1 == yes, 0 == no)
m := {
"A": [a, 1],
"B": [b, 0],
"C": [c, 1],
"D": [d, 0],
"E": [e, 1],
"F": [f, 1],
"G": [g, 1],
"H": [h, 1],
"I": [i, 0],
"J": [j, 0],
}
// convert to slice of Kv
mut l := []Kv{ len: m.len}
mut x := 0
for k, v in m {
l[x] = Kv{k, v}
x++
}
// sort in descending order of rating
//sort.Slice(l, func(i, j int) bool { return l[i].value[0] > l[j].value[0] })
l.sort_with_compare(fn(a &Kv, b &Kv) int {
if a.value[0] < b.value[0] {
return 1
}else if a.value[0] > b.value[0] {
return -1
}
return 0
})
// show expected result of race
println("Race 4\n")
println("Pos Horse Weight Dist Sex")
mut pos := ""
for v in 0..l.len {
mut wt := "9.00"
if l[v].value[1] == 0 {
wt = "8.11"
}
mut dist := 0.0
if v > 0 {
dist = (l[v-1].value[0]-l[v].value[0]) * 0.5
}
if v == 0 || dist > 0.0 {
pos = "${v+1}"
} else {
if pos.index('=') or {-1} < 0{
pos = "${v}="
}
}
mut sx := "colt"
if l[v].value[1] == 0 {
sx = "filly"
}
println("${pos:-2} ${l[v].key} $wt ${dist:3.1f} $sx")
}
// weight adjusted rating of winner
wr := l[0].value[0]
// expected time of winner (relative to A's time in Race 1)
t := 96.0 - (wr-100)/10
min := int(t / 60)
sec := t - f64(min)*60
println("\nTime $min minute ${sec:3.1f} seconds")
}</syntaxhighlight>
{{out}}
<pre>Same as go entry</pre>
 
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
 
// ratings on past form, assuming a rating of 100 for horse A
Line 565 ⟶ 815:
var min = (t/60).floor
var sec = t % 60
System.print("\nTime %(min) minute %(sec) seconds")</langsyntaxhighlight>
 
{{out}}
2,169

edits