99 questions/21 to 28: Difference between revisions
m (unify ghci prompts) |
mNo edit summary |
||
Line 2: | Line 2: | ||
This is part of [[H-99:_Ninety-Nine_Haskell_Problems|Ninety-Nine Haskell Problems]], based on [https://prof.ti.bfh.ch/hew1/informatik3/prolog/p-99/ Ninety-Nine Prolog Problems] and [http://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L-99_Ninety-Nine_Lisp_Problems.html Ninety-Nine Lisp Problems]. | This is part of [[H-99:_Ninety-Nine_Haskell_Problems|Ninety-Nine Haskell Problems]], based on [https://prof.ti.bfh.ch/hew1/informatik3/prolog/p-99/ Ninety-Nine Prolog Problems] and [http://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L-99_Ninety-Nine_Lisp_Problems.html Ninety-Nine Lisp Problems]. | ||
== Problem 21 == | == Problem 21 == | ||
<div style="border-bottom:1px solid #eee">Insert an element at a given position into a list. <span style="float:right"><small>[[99 questions/Solutions/21|Solutions]]</small></span> | |||
Insert an element at a given position into a list. | </div> | ||
<br> | |||
Example: | Example: | ||
Line 19: | Line 21: | ||
"aXbcd" | "aXbcd" | ||
</haskell> | </haskell> | ||
== Problem 22 == | == Problem 22 == | ||
<div style="border-bottom:1px solid #eee">Create a list containing all integers within a given range. <span style="float:right"><small>[[99 questions/Solutions/22|Solutions]]</small></span> | |||
Create a list containing all integers within a given range. | </div> | ||
<br> | |||
Example: | Example: | ||
Line 41: | Line 42: | ||
</haskell> | </haskell> | ||
== Problem 23 == | == Problem 23 == | ||
<div style="border-bottom:1px solid #eee">Extract a given number of randomly selected elements from a list. <span style="float:right"><small>[[99 questions/Solutions/23|Solutions]]</small></span> | |||
Extract a given number of randomly selected elements from a list. | </div> | ||
<br> | |||
Example: | Example: | ||
Line 60: | Line 61: | ||
eda | eda | ||
</haskell> | </haskell> | ||
== Problem 24 == | == Problem 24 == | ||
<div style="border-bottom:1px solid #eee">Lotto: Draw N different random numbers from the set 1..M. <span style="float:right"><small>[[99 questions/Solutions/24|Solutions]]</small></span> | |||
Lotto: Draw N different random numbers from the set 1..M. | </div> | ||
<br> | |||
Example: | Example: | ||
Line 81: | Line 81: | ||
[23,1,17,33,21,37] | [23,1,17,33,21,37] | ||
</haskell> | </haskell> | ||
== Problem 25 == | == Problem 25 == | ||
<div style="border-bottom:1px solid #eee">Generate a random permutation of the elements of a list. <span style="float:right"><small>[[99 questions/Solutions/25|Solutions]]</small></span> | |||
Generate a random permutation of the elements of a list. | </div> | ||
<br> | |||
Example: | Example: | ||
Line 102: | Line 101: | ||
"badcef" | "badcef" | ||
</haskell> | </haskell> | ||
== Problem 26 == | == Problem 26 == | ||
<div style="border-bottom:1px solid #eee">(**) Generate combinations of K distinct objects chosen from the N elements of a list. <span style="float:right"><small>[[99 questions/Solutions/26|Solutions]]</small></span> | |||
(**) Generate | </div> | ||
<br> | |||
In how many ways can a committee of 3 be chosen from a group of 12 people? We all know that there are C(12,3) = 220 possibilities (C(N,K) denotes the | In how many ways can a committee of 3 be chosen from a group of 12 people? We all know that there are C(12,3) = 220 possibilities (C(N,K) denotes the | ||
Line 126: | Line 124: | ||
["abc","abd","abe",...] | ["abc","abd","abe",...] | ||
</haskell> | </haskell> | ||
== Problem 27 == | == Problem 27 == | ||
<div style="border-bottom:1px solid #eee">Group the elements of a set into disjoint subsets. <span style="float:right"><small>[[99 questions/Solutions/27|Solutions]]</small></span> | |||
Group the elements of a set into disjoint subsets. | </div> | ||
<br> | |||
a) In how many ways can a group of 9 people work in 3 disjoint subgroups of 2, 3 and 4 persons? Write a function that generates all the possibilities and returns them in a list. | a) In how many ways can a group of 9 people work in 3 disjoint subgroups of 2, 3 and 4 persons? Write a function that generates all the possibilities and returns them in a list. | ||
Line 169: | Line 166: | ||
(altogether 756 solutions) | (altogether 756 solutions) | ||
</haskell> | </haskell> | ||
== Problem 28 == | == Problem 28 == | ||
<div style="border-bottom:1px solid #eee">Sorting a list of lists according to length of sublists. <span style="float:right"><small>[[99 questions/Solutions/28|Solutions]]</small></span> | |||
Sorting a list of lists according to length of sublists | </div> | ||
<br> | |||
a) We suppose that a list contains elements that are lists themselves. The objective is to sort the elements of this list according to their length. E.g. short lists first, longer lists later, or vice versa. | a) We suppose that a list contains elements that are lists themselves. The objective is to sort the elements of this list according to their length. E.g. short lists first, longer lists later, or vice versa. | ||
Line 209: | Line 204: | ||
["ijkl","o","abc","fgh","de","de","mn"] | ["ijkl","o","abc","fgh","de","de","mn"] | ||
</haskell> | </haskell> | ||
[[Category:Tutorials]] | [[Category:Tutorials]] |
Latest revision as of 05:51, 10 June 2023
This is part of Ninety-Nine Haskell Problems, based on Ninety-Nine Prolog Problems and Ninety-Nine Lisp Problems.
Problem 21
Example:
* (insert-at 'alfa '(a b c d) 2) (A ALFA B C D)
Example in Haskell:
λ> insertAt 'X' "abcd" 2
"aXbcd"
Problem 22
Example:
* (range 4 9) (4 5 6 7 8 9)
Example in Haskell:
λ> range 4 9
[4,5,6,7,8,9]
Problem 23
Example:
* (rnd-select '(a b c d e f g h) 3) (E D A)
Example in Haskell:
λ> rnd_select "abcdefgh" 3 >>= putStrLn
eda
Problem 24
Example:
* (rnd-select 6 49) (23 1 17 33 21 37)
Example in Haskell:
λ> diff_select 6 49
[23,1,17,33,21,37]
Problem 25
Example:
* (rnd-permu '(a b c d e f)) (B A D C E F)
Example in Haskell:
λ> rnd_permu "abcdef"
"badcef"
Problem 26
In how many ways can a committee of 3 be chosen from a group of 12 people? We all know that there are C(12,3) = 220 possibilities (C(N,K) denotes the well-known binomial coefficients). For pure mathematicians, this result may be great. But we want to really generate all the possibilities in a list.
Example:
* (combinations 3 '(a b c d e f)) ((A B C) (A B D) (A B E) ... )
Example in Haskell:
λ> combinations 3 "abcdef"
["abc","abd","abe",...]
Problem 27
a) In how many ways can a group of 9 people work in 3 disjoint subgroups of 2, 3 and 4 persons? Write a function that generates all the possibilities and returns them in a list.
Example:
* (group3 '(aldo beat carla david evi flip gary hugo ida)) ( ( (ALDO BEAT) (CARLA DAVID EVI) (FLIP GARY HUGO IDA) ) ... )
b) Generalize the above predicate in a way that we can specify a list of group sizes and the predicate will return a list of groups.
Example:
* (group '(aldo beat carla david evi flip gary hugo ida) '(2 2 5)) ( ( (ALDO BEAT) (CARLA DAVID) (EVI FLIP GARY HUGO IDA) ) ... )
Note that we do not want permutations of the group members; i.e. ((ALDO BEAT) ...) is the same solution as ((BEAT ALDO) ...). However, we make a difference between ((ALDO BEAT) (CARLA DAVID) ...) and ((CARLA DAVID) (ALDO BEAT) ...).
You may find more about this combinatorial problem in a good book on discrete mathematics under the term "multinomial coefficients".
Example in Haskell:
λ> group [2,3,4] ["aldo","beat","carla","david","evi","flip","gary","hugo","ida"]
[[["aldo","beat"],["carla","david","evi"],["flip","gary","hugo","ida"]],...]
(altogether 1260 solutions)
λ> group [2,2,5] ["aldo","beat","carla","david","evi","flip","gary","hugo","ida"]
[[["aldo","beat"],["carla","david"],["evi","flip","gary","hugo","ida"]],...]
(altogether 756 solutions)
Problem 28
a) We suppose that a list contains elements that are lists themselves. The objective is to sort the elements of this list according to their length. E.g. short lists first, longer lists later, or vice versa.
Example:
* (lsort '((a b c) (d e) (f g h) (d e) (i j k l) (m n) (o))) ((O) (D E) (D E) (M N) (A B C) (F G H) (I J K L))
Example in Haskell:
λ> lsort ["abc","de","fgh","de","ijkl","mn","o"]
["o","de","de","mn","abc","fgh","ijkl"]
b) Again, we suppose that a list contains elements that are lists themselves. But this time the objective is to sort the elements of this list according to their length frequency; i.e., in the default, where sorting is done ascendingly, lists with rare lengths are placed first, others with a more frequent length come later.
Example:
* (lfsort '((a b c) (d e) (f g h) (d e) (i j k l) (m n) (o))) ((i j k l) (o) (a b c) (f g h) (d e) (d e) (m n))
Example in Haskell:
λ> lfsort ["abc", "de", "fgh", "de", "ijkl", "mn", "o"]
["ijkl","o","abc","fgh","de","de","mn"]