Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: Turning a Sequence into a List?
Replies: 4   Last Post: Apr 25, 2013 2:51 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Alexei Boulbitch

Posts: 478
Registered: 2/28/08
Re: Turning a Sequence into a List?
Posted: Apr 18, 2013 5:33 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Hello, I'm playing with a problem with minimum coins to make change.
Here's a problem spot where I look at ways to make up 52 and 53 cents
(later I'll use v = Range[1,99].

v=Range[52,53];
sol=(Reduce[25q+10d+5n+p==# &&0<=p<5 &&0<=n<2 &&0<=d<3
&&0<=q<4,{q,d,n,p},Integers])& /@v

(* which gives {(q == 1 && d == 2 && n == 1 && p == 2) || (=
q == 2 && d
== 0 &&
n == 0 && p == 2), (q == 1 && d == 2 && n == 1 &&
p == 3) || (q == 2 && d == 0 && n == 0 && p == 3)}=
*)

There are two cases for each coin and I need to pick the one with the
smallest number of coins. But the only way I know to begin that is with
a list of rules. I can get a Sequence for the first element of the List
sol but I can't figure how to get the Sequence to a list.

x = (sol[[1]]) // ToRules
(* Sequence[{q -> 1, d -> 2, n -> 1, p -> 2}, {q -> 2, d -> 0, n -> 0,
p -> 2}] *)

I've always had problems figuring out what a Sequence is and the HELP
doesn't really help me. Can someone please suggest how I can use this
sequence to select the {q,d,n,p} set that has the minimum coin count?
It's got me stumped. Thanks.

--

Hi,

To turn this

sq=Sequence[{q -> 1, d -> 2, n -> 1, p -> 2}, {q -> 2, d -> 0, n -> 0,
p -> 2}];

into the list you may plainly write:

lst1=List[sq]

It returns
{{q -> 1, d -> 2, n -> 1, p -> 2}, {q -> 2, d -> 0, n -> 0, p -> 2}}

Now, if I understand you right, you would like to sort out a combination with the minimum sum q+d+n+p, would you?
If yes, one way to do this is as follows:

This removes the rules, but leaves instead the right part of each rule in the list:

lst2 = lst1 /. Rule[x_, y_] -> y

(* {{1, 2, 1, 2}, {2, 0, 0, 2}} *)

This finds the position of the sublist with the minimal sum:

pos = Position[Total[#] & /@ lst2, Min[Total[#] & /@ lst2]][[1, 1]]

(* 2 *)

And this returns the sublist of rules with the minimal sum:

lst1[[pos]]

(* {q -> 2, d -> 0, n -> 0, p -> 2} *)

Have fun, Alexei



Alexei BOULBITCH, Dr., habil.
IEE S.A.
ZAE Weiergewan,
11, rue Edmond Reuter,
L-5326 Contern, LUXEMBOURG

Office phone : +352-2454-2566
Office fax: +352-2454-3566
mobile phone: +49 151 52 40 66 44

e-mail: alexei.boulbitch@iee.lu







Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.