Difference between revisions of "99 questions/11 to 20"
(moved solutions to subpages of 99 questions/Solutions, reformatted code examples) 
m (unify ghci prompts) 

(4 intermediate revisions by 2 users not shown)  
Line 19:  Line 19:  
<haskell> 
<haskell> 

−  +  λ> encodeModified "aaaabccaadeeee" 

−  [Multiple 4 'a',Single 'b',Multiple 2 'c', 
+  [Multiple 4 'a',Single 'b',Multiple 2 'c', 
+  Multiple 2 'a',Single 'd',Multiple 4 'e'] 

</haskell> 
</haskell> 

[[99 questions/Solutions/11  Solutions]] 
[[99 questions/Solutions/11  Solutions]] 

−  
== Problem 12 == 
== Problem 12 == 

Line 35:  Line 34:  
<haskell> 
<haskell> 

⚫  
+  λ> decodeModified 

+  [Multiple 4 'a',Single 'b',Multiple 2 'c', 

⚫  
"aaaabccaadeeee" 
"aaaabccaadeeee" 

</haskell> 
</haskell> 

Line 41:  Line 42:  
[[99 questions/Solutions/12  Solutions]] 
[[99 questions/Solutions/12  Solutions]] 

−  
== Problem 13 == 
== Problem 13 == 

Line 58:  Line 58:  
<haskell> 
<haskell> 

−  +  λ> encodeDirect "aaaabccaadeeee" 

−  [Multiple 4 'a',Single 'b',Multiple 2 'c', 
+  [Multiple 4 'a',Single 'b',Multiple 2 'c', 
+  Multiple 2 'a',Single 'd',Multiple 4 'e'] 

</haskell> 
</haskell> 

[[99 questions/Solutions/13  Solutions]] 
[[99 questions/Solutions/13  Solutions]] 

−  
== Problem 14 == 
== Problem 14 == 

Line 79:  Line 78:  
<haskell> 
<haskell> 

−  > dupli [1, 2, 3] 
+  λ> dupli [1, 2, 3] 
[1,1,2,2,3,3] 
[1,1,2,2,3,3] 

</haskell> 
</haskell> 

Line 100:  Line 99:  
<haskell> 
<haskell> 

−  > repli "abc" 3 
+  λ> repli "abc" 3 
"aaabbbccc" 
"aaabbbccc" 

</haskell> 
</haskell> 

Line 121:  Line 120:  
<haskell> 
<haskell> 

−  +  λ> dropEvery "abcdefghik" 3 

"abdeghk" 
"abdeghk" 

</haskell> 
</haskell> 

Line 144:  Line 143:  
<haskell> 
<haskell> 

−  +  λ> split "abcdefghik" 3 

("abc", "defghik") 
("abc", "defghik") 

</haskell> 
</haskell> 

Line 167:  Line 166:  
<haskell> 
<haskell> 

−  +  λ> slice ['a','b','c','d','e','f','g','h','i','k'] 3 7 

"cdefg" 
"cdefg" 

</haskell> 
</haskell> 

Line 193:  Line 192:  
<haskell> 
<haskell> 

−  +  λ> rotate ['a','b','c','d','e','f','g','h'] 3 

"defghabc" 
"defghabc" 

−  +  λ> rotate ['a','b','c','d','e','f','g','h'] (2) 

"ghabcdef" 
"ghabcdef" 

</haskell> 
</haskell> 

Line 227:  Line 226:  
<haskell> 
<haskell> 

−  +  λ> removeAt 2 "abcd" 

('b',"acd") 
('b',"acd") 

</haskell> 
</haskell> 
Latest revision as of 08:38, 8 February 2019
This is part of NinetyNine Haskell Problems, based on NinetyNine Prolog Problems and NinetyNine Lisp Problems.
Problem 11
(*) Modified runlength encoding.
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:
* (encodemodified '(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
(**) Decode a runlength encoded list.
Given a runlength 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
(**) Runlength encoding of a list (direct solution).
Implement the socalled runlength 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:
* (encodedirect '(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
(*) Duplicate the elements of a list.
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
(**) Replicate the elements of a list a given number of times.
Example:
* (repli '(a b c) 3) (A A A B B B C C C)
Example in Haskell:
λ> repli "abc" 3
"aaabbbccc"
Problem 16
(**) Drop every N'th element from a list.
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
(*) Split a list into two parts; the length of the first part is given.
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
(**) Extract a slice from a list.
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
(**) Rotate a list N places to the left.
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
(*) Remove the K'th element from a list.
Example in Prolog:
? remove_at(X,[a,b,c,d],2,R). X = b R = [a,c,d]
Example in Lisp:
* (removeat '(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")