# Problem 16

## The problem

P16 (**) Drop every Nth element from a list.

Example:

```scala> drop(3, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, ... more

```

## Python decorators: metaprogramming with style

This post is the result of a lot of personal research on Python decorators, meta- and functional programming. I want however to thank Bruce Eckel and the people behind the open source book "Python 3 Patterns, Recipes and Idioms" for a lot of precious information on the subject. See the Resources section at the end of the post to check their work.

## Is Python functional?

Well, no. Python is a strong object-oriented programming language and is not really going to mix OOP and functional like, for example, Scala (which is a very good language, by the way).

However, Python provides some features taken from functional programming. Generators and iterators are one of them, and Python is not the only non pure functional programming language to have them in their toolbox.

Perhaps the most distinguishing feature of functional ... more

## The problem

P15 (**) Duplicate the elements of a list a given number of times.

Example:

```scala> duplicateN(3, List('a, 'b, 'c, 'c, 'd))
res0: List[Symbol] = List('a, ... more

```

## The problem

P14 (*) Duplicate the elements of a list.

Example:

```scala> duplicate(List('a, 'b, 'c, 'c, 'd))
res0: List[Symbol] = List('a, 'a, 'b... more

```

## The problem

P13 (**) Run-length encoding of a list (direct solution). Implement the so-called run-length encoding data compression method directly. I.e. don't use other methods you've written (like P09's pack); do all the work directly.

Example:

```scala> encodeDirect(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a... more

```

## The problem

P12 (**) Decode a run-length encoded list. Given a run-length code list generated as specified in problem P10, construct its uncompressed version.

Example:

```scala> decode(List((4, 'a), (1, 'b), (2, 'c), (2, 'a), (1, ... more

```

## The problem

P11 (*) Modified run-length encoding. Modify the result of problem P10 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) terms.

Example:

```scala> encodeModified(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, ... more

```

## The problem

P10 (*) 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 tuples (N, E) where N is the number of duplicates of the element E.

Example:

```scala> encode(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, ... more

```

## 99 Scala Problems Index

I decided to learn Scala. I looked for some good exercises for Scala programmers and found S-99: Ninety-Nine Scala Problems, and since writing about things helps me learning them I am going to write a post for each problem I manage to solve.

The official site provides solutions for the problems, so my solutions will obviously be influenced by those, when not the same.

CAVEAT: I am a beginner so what I state in the following posts may be inaccurate and sometimes wrong. Please submit an issue on the blog GitHub issues page.

This is the index of the problems I already discussed. I kept the original title and difficulty ranking (asterisks)

## The problem

P09 (**) Pack consecutive duplicates of list elements into sublists. If a list contains repeated elements they should be placed in separate sublists.

Example:

```scala> pack(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd,

... more

```