Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
August 06, 2014 Max/Min values in an associative array | ||||
---|---|---|---|---|
| ||||
I am trying to find the max and min values in an associative array. Say I have: double[char] bids; bid['A'] = 37.50; bid['B'] = 38.11; bid['C'] = 36.12; How can I find the max and min values. I am thinking that I need to use max and min functions from std.algorithm, but not sure how to. Thanks! TJB |
August 06, 2014 Re: Max/Min values in an associative array | ||||
---|---|---|---|---|
| ||||
Posted in reply to TJB | On Wed, 06 Aug 2014 17:57:54 +0000, TJB wrote: > I am trying to find the max and min values in an associative array. Say I have: > > double[char] bids; > bid['A'] = 37.50; > bid['B'] = 38.11; > bid['C'] = 36.12; > > How can I find the max and min values. I am thinking that I need to use max and min functions from std.algorithm, but not sure how to. > > Thanks! > TJB Do you just need the min and max values or do you also need the keys of those values? If the former, here's a paste: http://dpaste.dzfl.pl/0bbf31278a25 |
August 06, 2014 Re: Max/Min values in an associative array | ||||
---|---|---|---|---|
| ||||
Posted in reply to TJB | On Wednesday, 6 August 2014 at 17:57:55 UTC, TJB wrote:
> I am trying to find the max and min values in an associative array. Say I have:
>
> double[char] bids;
> bid['A'] = 37.50;
> bid['B'] = 38.11;
> bid['C'] = 36.12;
>
> How can I find the max and min values. I am thinking that I need to use max and min functions from std.algorithm, but not sure how to.
>
> Thanks!
> TJB
You can extract the values into a double[] using bid.values. Then you can simply use max and min from std.algorithm.
|
August 06, 2014 Re: Max/Min values in an associative array | ||||
---|---|---|---|---|
| ||||
Posted in reply to TJB | On Wed, Aug 06, 2014 at 05:57:54PM +0000, TJB via Digitalmars-d-learn wrote: > I am trying to find the max and min values in an associative array. Say I have: > > double[char] bids; > bid['A'] = 37.50; > bid['B'] = 38.11; > bid['C'] = 36.12; > > How can I find the max and min values. I am thinking that I need to use max and min functions from std.algorithm, but not sure how to. [...] import std.algorithm : reduce, max, min; auto highest = reduce!((a,b) => max(a,b))(-double.max, bids.byValue()); auto lowest = reduce!((a,b) => min(a,b))(double.max, bids.byValue()); T -- Designer clothes: how to cover less by paying more. |
August 06, 2014 Re: Max/Min values in an associative array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Justin Whear | Justin,
That's it! Perfect - thanks!!
TJB
> Do you just need the min and max values or do you also need the keys of
> those values? If the former, here's a paste:
> http://dpaste.dzfl.pl/0bbf31278a25
|
August 14, 2014 Re: Max/Min values in an associative array | ||||
---|---|---|---|---|
| ||||
Posted in reply to TJB | TJB:
> I am trying to find the max and min values in an associative array. Say I have:
>
> double[char] bids;
> bid['A'] = 37.50;
> bid['B'] = 38.11;
> bid['C'] = 36.12;
>
> How can I find the max and min values. I am thinking that I need to use max and min functions from std.algorithm, but not sure how to.
void main() {
import std.stdio, std.algorithm;
double[char] bids = ['A': 37.50,
'B': 38.11,
'C': 36.12];
bids.byValue.reduce!(min, max).writeln;
}
Bye,
bearophile
|
August 15, 2014 Re: Max/Min values in an associative array | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On Wednesday, 6 August 2014 at 18:07:08 UTC, H. S. Teoh via Digitalmars-d-learn wrote:
>
> import std.algorithm : reduce, max, min;
>
> auto highest = reduce!((a,b) => max(a,b))(-double.max, bids.byValue());
> auto lowest = reduce!((a,b) => min(a,b))(double.max, bids.byValue());
>
>
> T
Take a look at Justin Whear's dpaste. Dual pred reduce FTW.
|
August 15, 2014 Re: Max/Min values in an associative array | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra | On Fri, Aug 15, 2014 at 04:51:59PM +0000, monarch_dodra via Digitalmars-d-learn wrote: > On Wednesday, 6 August 2014 at 18:07:08 UTC, H. S. Teoh via Digitalmars-d-learn wrote: > > > > import std.algorithm : reduce, max, min; > > > > auto highest = reduce!((a,b) => max(a,b))(-double.max, bids.byValue()); > > auto lowest = reduce!((a,b) => min(a,b))(double.max, bids.byValue()); > > > > > >T > > Take a look at Justin Whear's dpaste. Dual pred reduce FTW. Yeah I saw that. Learned something new. :-) T -- Bare foot: (n.) A device for locating thumb tacks on the floor. |
Copyright © 1999-2021 by the D Language Foundation