Showing posts with label 99 Haskell problems in Julia. Show all posts
Showing posts with label 99 Haskell problems in Julia. Show all posts

Tuesday, May 20, 2014

99 problems in Julia (11-20)

In this article i am proudly presenting solutions to 11-20 problems from classical 99 Haskell problem set.


 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:
Haskell 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))

Solution:
julia>function modifiedEncoding(x)
        lest=[]
        for c in unique(x)
                if counter(x,c)>1
                        lest=[lest...,(counter(x,c),c)]
                else
                        lest=[lest...,c]
                end
        end
   lest

end

julia>function counter(l,el)
        temp=0
        for i=1:endof(l)
                if el==l[i]
                        temp+=1
                end
        end
     temp
end

Problem 12

(**) Decode a run-length encoded list.
Given a run-length code list generated as specified in problem 11. Construct its uncompressed version.
Example in Haskell:
P12> decodeModified 
       [Multiple 4 'a',Single 'b',Multiple 2 'c',
        Multiple 2 'a',Single 'd',Multiple 4 'e']

Solution:
julia>function de_encode(x)
        lest=[]
        for c in x
                if typeof(c)==(Int32,Int32)
                        for i=1:c[1]
                                lest=[lest...,c[2]]
                        end
                else
                        lest=[lest...,c]
                end
        end
    lest
end

Problem 13

(**) Run-length encoding of a list (direct solution).
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))

Solution:
Same as problem 11.We used shortcut solution already in our early 11th problem.

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)

Solution:
julia>function duplicate(x)
        lest=[]
        for c in x
                lest=[lest...,c,c]
        end
    lest
end

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) 
 
Solution: 
julia>function replicate(x,k)
        lest=[]
        for c in x
                for i=1:k
                        lest=[lest...,c]
                end
        end
    lest
end

 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)
 Solution:
julia>function dropN(x,n)
        lest=[]
        for i=1:endof(x)
                if i%n!=0
                        lest=[lest...,x[i]]
                end
        end
    lest
end

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))
 
Solution: 
julia>split(x,n)=x[1:n],x[n+1,end]

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)

Solution:
julia>slice(x,i,j)=x[i:j]

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)
 
Solution: 
 julia>rotLeft(x,n)=[x[n+1:end],x[1:n]]

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]
 
Solution: 
julia>removeAt(x,n)=delete!(x,n)

This delete!() method will go to jail because it changes original list which 
is anti philosophy of functional programming.Here is functional version.

julia>function removeAt(x,n)
        lest=[]
        for i=1:endof(x)
                if i!=n
                        lest=[lest...,x[i]]
                end
        end
   lest
end

Thanks for your patience.see you next time.



Monday, May 19, 2014

99 problems in Julia programming Language (1-10)

Prolog and Haskell got 99 problems to understand basic logic constructs .This is an attempt to create Julia versions of solutions to those problems.Here first 10 problems are solved by me.Remaining will be looked in further articles.If you like to contribute to my work mail me at narenarya@live.com.

Problem 1:

Q) Find the last element of a list
Ex: julia>myLast([1,2,3,4,5])
5
Solution:
julia>myLast(l)= l[end]

Prblem 2:

Q) Find last but one element from list
Ex: julia>myButOne([1,2,3,4,5])
4
Solution:
julia>myButOne(l)=l[end-1]

Problem 3:

Q) Find the kth element of list
Ex: julia>kthElement([12,3,4,16,43,9],4)
16
since 16 is 4th element in the list.
Solution: 
julia>kthElement(l,k)=l[k]
Note:expression is so simple because Julia indexes from 1 not 0.

Problem 4:
Q)Find the no of elements in list
Ex:julia>ElementNo([1,2,3,4])
4
Solution:
julia>ElementNo(l)=length(l)

Problem 5:
Q)Reverse a list
Ex:julia>myReversedList([1,2,3,4])
[4,3,2,1]
Solution:
julia>myReversedList(l)=reverse(l)

Problem 6:
Q)Find out whether a list is a palindrome. A palindrome can be read forward or backward; e.g. (x a m a x).
Ex:julia>Palindrome([1,2,3,2,1])
true
Solution:
julia>Palindrome(l)= return l==reverse(l)

Problem 7:
Q)Flatten a nested list structure.
Ex:julia>myFlatten([1,[2,3,],16,[45,76,84],0])
1
2
3
16
45
76
84
0
Flattening means removing recursive lists i.e lists inside lists.So [1,[2,3]] list is broken into [1,2,3]
Solution:
julia>myFlatten(l)=l
This simple,yes it is utterly simple Julia automatically flats your nested structure.

Problem 8:
Q) Eliminate consecutive duplicates of list elements.
Ex:julia>deleteConsDuplicates([1,1,1,2,2,3,3,3,3,4,4,5])
1
2
3
4
5
Solution:
julia> function deleteConsDuplicates(l)
            myset=Set()
            for i=1:endof(l)
               push!(myset,l[i])
            end
           sort([x for x in myset])
        end
we can simply cheat using below oneliner
julia>deleteConsDuplicates(l)=unique(l)

Problem 9:
Q)Pack consecutive duplicates of list elements into sublists. If a list contains repeated elements they should be placed in separate sublists.
Ex: julia>packDuplicates([1,1,1,2,2,3,3,3,4,4,4,4,4])
[(1,1,1),(2,2),(3,3,3),(4,4,4,4)]
Solution:
julia>function packDuplicates(x)
               lest=[]
               for c in deleteConsDuplicates(x)
                     tup=()
                     for i=1:counter(x,c)
                             tup=tuple(tup...,(c))
                     end
                    lest=[lest,tup]
              end
              lest
       end
julia> function counter(l,x)
              temp=0
              for i=1:endof(l)
                   if l[i]==x
                   temp=temp+1
                   end
             end
             temp
        end

Problem 10:
Q)Run-length encoding of a list. Use the result of problem P09 to implement the so-called run-length encoding data compression method. Consecutive duplicates of elements are encoded as lists (N E) where N is the number of duplicates of the element E.
Ex: julia>encodeList([a,a,a,b,b,c,c,c,d,d,d,d])
[(3,a),(2,b),(3,c),(4,d)]
Solution:
 julia> function encodeList(x)
               finlist=[]
               for c in deleteConsDuplicates(x)
                       finlist=[finlist,(counter(x,c),c)]
               end
              finlist
         end

We cleverly used comprehensions for list and tuple to generate new lists,tuples respectively.From now each week new 10 problems will be solved until 99 project is completed,so please be updated with learnjulia.