Practice problems¶
Project Euler and 99 Scala Problems or 99 Python Problems contain collections of small coding problems suitable for functional programming - many of them are also good exercises for nuts-flow.
99 Problems¶
A few 99 Scala Problems with solutions using nuts-flow:
>>> from nutsflow import *
>>> from nutsflow import _
P01 : Find the last element of a list.
>>> [1, 1, 2, 3, 5, 8] >> Tail(1)
[8]
P02 : Find the last but one element of a list.
>>> [1, 1, 2, 3, 5, 8] >> Tail(2) >> Head(1)
[5]
P03 : Find the Kth element of a list.
>>> [1, 1, 2, 3, 5, 8] >> Nth(2)
2
>>> [1, 1, 2, 3, 5, 8] >> Drop(2) >> Head(1)
[2]
P04 : Find the number of elements of a list.
>>> [1, 1, 2, 3, 5, 8] >> Count()
6
P14 : Duplicate the elements of a list.
>>> Range(5) >> Clone(2) >> Collect()
[0, 0, 1, 1, 2, 2, 3, 3, 4, 4]
P15 : Duplicate the elements of a list a given number of times.
>>> Range(5) >> Clone(n) >> Collect()
[0, 0, 1, 1, 2, 2, 3, 3, 4, 4]
P16 : Drop every Nth element from a list.
>>> n==3
>>> lst = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']
>>> lst >> Chunk(n, list) >> Map(Take(n-1)) >> Flatten() >> Collect()
['a', 'b', 'd', 'e', 'g', 'h', 'j', 'k']
Euler¶
Some Project Euler with solutions using nuts-flow.
P1: Multiples of 3 and 5
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
>>> IsMultiple = nut_filter(lambda x: x % 3 == 0 or x % 5 == 0)
>>> Range(1, 1000) >> IsMultiple() >> Sum()
233168
P2: Even Fibonacci numbers
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
>>> @nut_source
... def Fib():
... a, b = 1, 1
... while True:
... a, b = b, a + b
... yield a
>>> IsEven = nut_filter(lambda x: x%2 == 0)
>>> Fib() >> TakeWhile(_ < 4000000) >> IsEven() >> Sum()
4613732
P4: Largest palindrome product
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
>>> n1, n2 = 100, 1000
>>> IsPalindrom = nut_filter(lambda p: p == p[::-1])
>>> BuildString = nut_function(lambda (a, b): str(a * b))
>>> product = Product(Range(n1, n2), repeat=2)
>>> product >> BuildString() >> IsPalindrom() >> Max(int)
'906609'
P6: Sum square difference
The sum of the squares of the first ten natural numbers is,
1^2 + 2^2 + … + 10^2 = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + … + 10)^2 = 55^2 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
>>> sum_sqr = Range(1, 11) >> Square() >> Sum()
>>> sqr_sum = (Range(1, 11) >> Sum())**2
>>> sqr_sum - sum_sqr
2640