Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #43647 > unrolled thread

Find combinations with the right sum

Started by"WJ" <w_a_x_man@yahoo.com>
First post2015-11-22 19:21 +0000
Last post2015-11-24 06:22 +0000
Articles 7 — 5 participants

Back to article view | Back to comp.lang.forth


Contents

  Find combinations with the right sum "WJ" <w_a_x_man@yahoo.com> - 2015-11-22 19:21 +0000
    Re: Find combinations with the right sum foxaudioresearch@gmail.com - 2015-11-22 14:46 -0800
      Re: Find combinations with the right sum Chris Curl <ccurl609@gmail.com> - 2015-11-22 16:48 -0800
      Re: Find combinations with the right sum Chris Curl <ccurl609@gmail.com> - 2015-11-22 16:49 -0800
      Re: Find combinations with the right sum Ron Aaron <rambamist@gmail.com> - 2015-11-23 07:27 +0200
        Re: Find combinations with the right sum Alfred Singlestone <vitonis@gmail.com> - 2015-11-23 15:45 -0800
    Re: Find combinations with the right sum "WJ" <w_a_x_man@yahoo.com> - 2015-11-24 06:22 +0000

#43647 — Find combinations with the right sum

From"WJ" <w_a_x_man@yahoo.com>
Date2015-11-22 19:21 +0000
SubjectFind combinations with the right sum
Message-ID<n2t4ju$ald$1@dont-email.me>
Ruby:

def combinations_summing_to sum,len,numbers
  numbers.to_a.combination(len).select{|xs| xs.reduce(:+) == sum}
end

combinations_summing_to 45,4,(9..22)
    ===>[[9, 10, 11, 15], [9, 10, 12, 14], [9, 11, 12, 13]]

combinations_summing_to 20,3,[2,3,5,7,8,9]
    ===>[[3, 8, 9], [5, 7, 8]]

One would imagine that this would be painfully difficult in
ANS Forth, wouldn't he?

-- 
The report card by the American Society of Civil Engineers showed the national
infrastructure a single grade above failure, a step from declining to the point
where everyday things simply stop working the way people expect them to. ---
washingtonpost.com/local/trafficandcommuting/us-infrastructure-gets-d-in-annual-report/2013/03/19/c48cb010-900b-11e2-9cfd-36d6c9b5d7ad_story.html

[toc] | [next] | [standalone]


#43656

Fromfoxaudioresearch@gmail.com
Date2015-11-22 14:46 -0800
Message-ID<6659594e-814c-4dbc-a035-6bfb34274999@googlegroups.com>
In reply to#43647
On Sunday, November 22, 2015 at 2:23:58 PM UTC-5, WJ wrote:
> Ruby:
> 
> def combinations_summing_to sum,len,numbers
>   numbers.to_a.combination(len).select{|xs| xs.reduce(:+) == sum}
> end
> 
> combinations_summing_to 45,4,(9..22)
>     ===>[[9, 10, 11, 15], [9, 10, 12, 14], [9, 11, 12, 13]]
> 
> combinations_summing_to 20,3,[2,3,5,7,8,9]
>     ===>[[3, 8, 9], [5, 7, 8]]
> 
> One would imagine that this would be painfully difficult in
> ANS Forth, wouldn't he?
> 
> -- 

Intelligent people don't actually program in Forth after working on a problem for a few minutes.

They program in the language that they write in Forth, to solve the problem at
 hand.

So after writing the language to solve the problem, it's much simpler.

Your question indicates to me that you have failed to grasp that Forth
 is a primitive meta language system while most other languages take a general
 purpose solution approach.

Nothing is "easy" in raw Forth... except extending the language in the
 direction required by the problem.  

I know this won't change anything, but I had to say it.

BF 

[toc] | [prev] | [next] | [standalone]


#43659

FromChris Curl <ccurl609@gmail.com>
Date2015-11-22 16:48 -0800
Message-ID<4bbaf8cb-4e80-4520-8527-a2779fbbe574@googlegroups.com>
In reply to#43656
On Sunday, November 22, 2015 at 5:46:13 PM UTC-5, foxaudio...@gmail.com wrote:
> On Sunday, November 22, 2015 at 2:23:58 PM UTC-5, WJ wrote:
> > Ruby:
> > 
> > def combinations_summing_to sum,len,numbers
> >   numbers.to_a.combination(len).select{|xs| xs.reduce(:+) == sum}
> > end
> > 
> > combinations_summing_to 45,4,(9..22)
> >     ===>[[9, 10, 11, 15], [9, 10, 12, 14], [9, 11, 12, 13]]
> > 
> > combinations_summing_to 20,3,[2,3,5,7,8,9]
> >     ===>[[3, 8, 9], [5, 7, 8]]
> > 
> > One would imagine that this would be painfully difficult in
> > ANS Forth, wouldn't he?
> > 
> > -- 
> 
> Intelligent people don't actually program in Forth after working on a problem for a few minutes.
> 
> They program in the language that they write in Forth, to solve the problem at
>  hand.
> 
> So after writing the language to solve the problem, it's much simpler.
> 
> Your question indicates to me that you have failed to grasp that Forth
>  is a primitive meta language system while most other languages take a general
>  purpose solution approach.
> 
> Nothing is "easy" in raw Forth... except extending the language in the
>  direction required by the problem.  
> 
> I know this won't change anything, but I had to say it.
> 
> BF
Well said.

[toc] | [prev] | [next] | [standalone]


#43660

FromChris Curl <ccurl609@gmail.com>
Date2015-11-22 16:49 -0800
Message-ID<d717ff4c-ecc9-41af-8114-b10f6060c4f8@googlegroups.com>
In reply to#43656
On Sunday, November 22, 2015 at 5:46:13 PM UTC-5, foxaudio...@gmail.com wrote:
> Intelligent people don't actually program in Forth after working on a problem for a few minutes.
> 
> They program in the language that they write in Forth, to solve the problem at
>  hand.
> 
> So after writing the language to solve the problem, it's much simpler.
> 
> Your question indicates to me that you have failed to grasp that Forth
>  is a primitive meta language system while most other languages take a general
>  purpose solution approach.
> 
> Nothing is "easy" in raw Forth... except extending the language in the
>  direction required by the problem.  
> 
> I know this won't change anything, but I had to say it.
> 
> BF
Well said.

[toc] | [prev] | [next] | [standalone]


#43664

FromRon Aaron <rambamist@gmail.com>
Date2015-11-23 07:27 +0200
Message-ID<n2u7vk$smd$1@dont-email.me>
In reply to#43656
On 11/23/2015 00:46, foxaudioresearch@gmail.com wrote:

> Intelligent people don't actually program in Forth after working on a problem for a few minutes.
>
> They program in the language that they write in Forth, to solve the problem at
>   hand.
>
> So after writing the language to solve the problem, it's much simpler.
>
> Your question indicates to me that you have failed to grasp that Forth
>   is a primitive meta language system while most other languages take a general
>   purpose solution approach.


I would expand on that: intelligent people don't waste their time on 
problems which are not relevant.

Intelligent people research various approaches to solving actually 
relevant problems before spending any time writing their own solutions.

Intelligent people realize that not every problem is best solved with a 
hammer; sometimes a screwdriver or a pliers is a better fit -- and an 
experienced and intelligent coder knows how to find the best tool for 
the job and use it.

[toc] | [prev] | [next] | [standalone]


#43690

FromAlfred Singlestone <vitonis@gmail.com>
Date2015-11-23 15:45 -0800
Message-ID<eed54d4e-3ed8-4745-9126-2699467d9d2f@googlegroups.com>
In reply to#43664
On Monday, November 23, 2015 at 12:27:31 AM UTC-5, Ron Aaron wrote:
> I would expand on that: intelligent people don't waste their time on 
> problems which are not relevant.

Relevant to what? Is it better to waste your time on problems that *are* relevant?

[toc] | [prev] | [next] | [standalone]


#43697

From"WJ" <w_a_x_man@yahoo.com>
Date2015-11-24 06:22 +0000
Message-ID<n30vmf$er5$1@dont-email.me>
In reply to#43647
WJ wrote:

> Ruby:
> 
> def combinations_summing_to sum,len,numbers
>   numbers.to_a.combination(len).select{|xs| xs.reduce(:+) == sum}
> end
> 
> combinations_summing_to 45,4,(9..22)
>     ===>[[9, 10, 11, 15], [9, 10, 12, 14], [9, 11, 12, 13]]
> 
> combinations_summing_to 20,3,[2,3,5,7,8,9]
>     ===>[[3, 8, 9], [5, 7, 8]]
> 
> One would imagine that this would be painfully difficult in
> ANS Forth, wouldn't he?

Ocaml:

(* All combinations of size m from a list. *)
let rec  combins m list =
  if 1 = m then
    List.map (fun x -> [x]) list
  else
    let rec  go = function
        [x] -> []
      | x::xs -> (List.map ((@) [x]) (combins (m-1) xs)) @ (go xs)
    in go list ;;

let combinations_summing_to sum len integers =
  integers |> combins len |>
    List.filter (fun list -> sum = List.fold_left (+) 0 list) ;;

combinations_summing_to 20 3 [2;3;5;7;8;9];;
 ===>
[[3; 8; 9]; [5; 7; 8]]

combinations_summing_to 20 3 [2;3;4;5;6;7;8;9;10];;
 ===>
[[2; 8; 10]; [3; 7; 10]; [3; 8; 9]; [4; 6; 10]; [4; 7; 9]; [5; 6; 9];
 [5; 7; 8]]

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.forth


csiph-web