99 questions/11 to 20: Difference between revisions
No edit summary |
mNo edit summary |
||
(69 intermediate revisions by 32 users not shown) | |||
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
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 11 == | == Problem 11 == | ||
<div style="border-bottom:1px solid #eee">(*) Modified run-length encoding. <span style="float:right"><small>[[99 questions/Solutions/11|Solutions]]</small></span> | |||
</div> | |||
<br> | |||
Modify the result of problem 10 in such a way that if an element has no duplicates it is simply copied into the result list. Only elements with duplicates are transferred as (N E) lists. | |||
Example: | |||
<pre> | <pre> | ||
* (encode-modified '(a a a a b c c a a d e e e e)) | |||
< | ((4 A) B (2 C) (2 A) D (4 E)) | ||
</pre> | |||
Example in Haskell: | Example in Haskell: | ||
<haskell> | <haskell> | ||
λ> encodeModified "aaaabccaadeeee" | |||
[Multiple 4 'a',Single 'b',Multiple 2 'c', | |||
Multiple 2 'a',Single 'd',Multiple 4 'e'] | |||
</haskell> | </haskell> | ||
== Problem 12 == | == Problem 12 == | ||
<div style="border-bottom:1px solid #eee">(**) Decode a run-length encoded list. <span style="float:right"><small>[[99 questions/Solutions/12|Solutions]]</small></span> | |||
</div> | |||
<br> | |||
Given a run-length code list generated as specified in problem 11. Construct its uncompressed version. | |||
Example in Haskell: | Example in Haskell: | ||
<haskell> | <haskell> | ||
λ> decodeModified | |||
[Multiple 4 'a',Single 'b',Multiple 2 'c', | |||
Multiple 2 'a',Single 'd',Multiple 4 'e'] | |||
"aaaabccaadeeee" | |||
</haskell> | </haskell> | ||
== Problem 13 == | == Problem 13 == | ||
<div style="border-bottom:1px solid #eee">(**) Run-length encoding of a list (direct solution). <span style="float:right"><small>[[99 questions/Solutions/13|Solutions]]</small></span> | |||
</div> | |||
<br> | |||
Implement the so-called run-length encoding data compression method directly. I.e. don't explicitly create the sublists containing the duplicates, as in problem 9, but only count them. As in problem P11, simplify the result list by replacing the singleton lists (1 X) by X. | |||
Example: | |||
<pre> | <pre> | ||
* (encode-direct '(a a a a b c c a a d e e e e)) | |||
< | ((4 A) B (2 C) (2 A) D (4 E)) | ||
</pre> | |||
Example in Haskell: | Example in Haskell: | ||
<haskell> | <haskell> | ||
λ> encodeDirect "aaaabccaadeeee" | |||
[Multiple 4 'a',Single 'b',Multiple 2 'c', | |||
Multiple 2 'a',Single 'd',Multiple 4 'e'] | |||
</haskell> | </haskell> | ||
== Problem 14 == | == Problem 14 == | ||
<div style="border-bottom:1px solid #eee">(*) Duplicate the elements of a list. <span style="float:right"><small>[[99 questions/Solutions/14|Solutions]]</small></span> | |||
</div> | |||
<br> | |||
Example: | |||
<pre> | <pre> | ||
* (dupli '(a b c c d)) | |||
< | (A A B B C C C C D D) | ||
</pre> | |||
Example in Haskell: | Example in Haskell: | ||
<haskell> | <haskell> | ||
λ> dupli [1, 2, 3] | |||
[1,1,2,2,3,3] | |||
</haskell> | </haskell> | ||
== Problem 15 == | == Problem 15 == | ||
<div style="border-bottom:1px solid #eee">(**) Replicate the elements of a list a given number of times. <span style="float:right"><small>[[99 questions/Solutions/15|Solutions]]</small></span> | |||
</div> | |||
Example: | |||
<pre> | <pre> | ||
* (repli '(a b c) 3) | |||
< | (A A A B B B C C C) | ||
</pre> | |||
Example in Haskell: | Example in Haskell: | ||
<haskell> | <haskell> | ||
λ> repli "abc" 3 | |||
"aaabbbccc" | |||
</haskell> | </haskell> | ||
== Problem 16 == | == Problem 16 == | ||
<div style="border-bottom:1px solid #eee">(**) Drop every N'th element from a list. <span style="float:right"><small>[[99 questions/Solutions/16|Solutions]]</small></span> | |||
</div> | |||
<br> | |||
Example: | |||
<pre> | <pre> | ||
* (drop '(a b c d e f g h i k) 3) | |||
< | (A B D E G H K) | ||
</pre> | |||
Example in Haskell: | Example in Haskell: | ||
<haskell> | <haskell> | ||
λ> dropEvery "abcdefghik" 3 | |||
"abdeghk" | |||
</haskell> | </haskell> | ||
== Problem 17 == | == Problem 17 == | ||
<div style="border-bottom:1px solid #eee">(*) Split a list into two parts; the length of the first part is given. <span style="float:right"><small>[[99 questions/Solutions/17|Solutions]]</small></span> | |||
</div> | |||
<br> | |||
Do not use any predefined predicates. | |||
Example: | |||
<pre> | <pre> | ||
* (split '(a b c d e f g h i k) 3) | |||
< | ( (A B C) (D E F G H I K)) | ||
</pre> | |||
Example in Haskell: | Example in Haskell: | ||
<haskell> | <haskell> | ||
λ> split "abcdefghik" 3 | |||
("abc", "defghik") | |||
</haskell> | </haskell> | ||
== Problem 18 == | == Problem 18 == | ||
<div style="border-bottom:1px solid #eee">(**) Extract a slice from a list. <span style="float:right"><small>[[99 questions/Solutions/18|Solutions]]</small></span> | |||
</div> | |||
<br> | |||
Given two indices, i and k, the slice is the list containing the elements between the i'th and k'th element of the original list (both limits included). Start counting the elements with 1. | |||
Example: | |||
<pre> | <pre> | ||
* (slice '(a b c d e f g h i k) 3 7) | |||
< | (C D E F G) | ||
</pre> | |||
Example in Haskell: | Example in Haskell: | ||
<haskell> | <haskell> | ||
λ> slice ['a','b','c','d','e','f','g','h','i','k'] 3 7 | |||
"cdefg" | |||
</haskell> | </haskell> | ||
== Problem 19 == | == Problem 19 == | ||
<div style="border-bottom:1px solid #eee">(**) Rotate a list N places to the left. <span style="float:right"><small>[[99 questions/Solutions/19|Solutions]]</small></span> | |||
</div> | |||
<br> | |||
( | Hint: Use the predefined functions length and (++). | ||
Examples: | |||
<pre> | <pre> | ||
* (rotate '(a b c d e f g h) 3) | * (rotate '(a b c d e f g h) 3) | ||
(D E F G H A B C) | (D E F G H A B C) | ||
Line 171: | Line 189: | ||
* (rotate '(a b c d e f g h) -2) | * (rotate '(a b c d e f g h) -2) | ||
(G H A B C D E F) | (G H A B C D E F) | ||
</pre> | |||
Examples in Haskell: | Examples in Haskell: | ||
<haskell> | |||
λ> rotate ['a','b','c','d','e','f','g','h'] 3 | |||
"defghabc" | |||
λ> rotate ['a','b','c','d','e','f','g','h'] (-2) | |||
"ghabcdef" | |||
rotate [] | |||
</haskell> | </haskell> | ||
== Problem 20 == | == Problem 20 == | ||
<div style="border-bottom:1px solid #eee">(*) Remove the K'th element from a list. <span style="float:right"><small>[[99 questions/Solutions/20|Solutions]]</small></span> | |||
</div> | |||
<br> | |||
Example in Prolog: | |||
<pre> | |||
?- remove_at(X,[a,b,c,d],2,R). | |||
X = b | |||
R = [a,c,d] | |||
</pre> | |||
Example in Lisp: | |||
<pre> | <pre> | ||
* (remove-at '(a b c d) 2) | |||
< | (A C D) | ||
</pre> | |||
(Note that this only returns the residue list, while the Prolog version also returns the deleted element.) | |||
Example in Haskell: | Example in Haskell: | ||
<haskell> | <haskell> | ||
λ> removeAt 2 "abcd" | |||
('b',"acd") | |||
</haskell> | </haskell> | ||
[[Category:Tutorials]] | [[Category:Tutorials]] |
Latest revision as of 04:58, 10 June 2023
This is part of Ninety-Nine Haskell Problems, based on Ninety-Nine Prolog Problems and Ninety-Nine Lisp Problems.
Problem 11
Modify the result of problem 10 in such a way that if an element has no duplicates it is simply copied into the result list. Only elements with duplicates are transferred as (N E) lists.
Example:
* (encode-modified '(a a a a b c c a a d e e e e)) ((4 A) B (2 C) (2 A) D (4 E))
Example in Haskell:
λ> encodeModified "aaaabccaadeeee"
[Multiple 4 'a',Single 'b',Multiple 2 'c',
Multiple 2 'a',Single 'd',Multiple 4 'e']
Problem 12
Given a run-length code list generated as specified in problem 11. Construct its uncompressed version.
Example in Haskell:
λ> decodeModified
[Multiple 4 'a',Single 'b',Multiple 2 'c',
Multiple 2 'a',Single 'd',Multiple 4 'e']
"aaaabccaadeeee"
Problem 13
Implement the so-called run-length encoding data compression method directly. I.e. don't explicitly create the sublists containing the duplicates, as in problem 9, but only count them. As in problem P11, simplify the result list by replacing the singleton lists (1 X) by X.
Example:
* (encode-direct '(a a a a b c c a a d e e e e)) ((4 A) B (2 C) (2 A) D (4 E))
Example in Haskell:
λ> encodeDirect "aaaabccaadeeee"
[Multiple 4 'a',Single 'b',Multiple 2 'c',
Multiple 2 'a',Single 'd',Multiple 4 'e']
Problem 14
Example:
* (dupli '(a b c c d)) (A A B B C C C C D D)
Example in Haskell:
λ> dupli [1, 2, 3]
[1,1,2,2,3,3]
Problem 15
Example:
* (repli '(a b c) 3) (A A A B B B C C C)
Example in Haskell:
λ> repli "abc" 3
"aaabbbccc"
Problem 16
Example:
* (drop '(a b c d e f g h i k) 3) (A B D E G H K)
Example in Haskell:
λ> dropEvery "abcdefghik" 3
"abdeghk"
Problem 17
Do not use any predefined predicates.
Example:
* (split '(a b c d e f g h i k) 3) ( (A B C) (D E F G H I K))
Example in Haskell:
λ> split "abcdefghik" 3
("abc", "defghik")
Problem 18
Given two indices, i and k, the slice is the list containing the elements between the i'th and k'th element of the original list (both limits included). Start counting the elements with 1.
Example:
* (slice '(a b c d e f g h i k) 3 7) (C D E F G)
Example in Haskell:
λ> slice ['a','b','c','d','e','f','g','h','i','k'] 3 7
"cdefg"
Problem 19
Hint: Use the predefined functions length and (++).
Examples:
* (rotate '(a b c d e f g h) 3) (D E F G H A B C) * (rotate '(a b c d e f g h) -2) (G H A B C D E F)
Examples in Haskell:
λ> rotate ['a','b','c','d','e','f','g','h'] 3
"defghabc"
λ> rotate ['a','b','c','d','e','f','g','h'] (-2)
"ghabcdef"
Problem 20
Example in Prolog:
?- remove_at(X,[a,b,c,d],2,R). X = b R = [a,c,d]
Example in Lisp:
* (remove-at '(a b c d) 2) (A C D)
(Note that this only returns the residue list, while the Prolog version also returns the deleted element.)
Example in Haskell:
λ> removeAt 2 "abcd"
('b',"acd")