An open API service indexing awesome lists of open source software.

https://github.com/gabfr/linian-number-analyzer

A simple code challenge with tricky requirements :) (more on readme)
https://github.com/gabfr/linian-number-analyzer

php72 phpunit

Last synced: 4 months ago
JSON representation

A simple code challenge with tricky requirements :) (more on readme)

Awesome Lists containing this project

README

        

# Linian Number Analyzer Code Challenge

We have to write a program that prints all the numbers from 1 to 100. However, for
multiples of 3, instead of the number, print "Linio". For multiples of 5 print
"IT". For numbers which are multiples of both 3 and 5, print "Linianos".

But here's the catch: you can use only one `if`. No multiple branches, ternary
operators or `else`.

## Requirements

- 1 if
- You can't use `else`, `else if` or ternary
- Unit tests
- Feel free to apply your SOLID knowledge
- You can write the challenge in any language you want. Here at Linio we are big fans of PHP, Kotlin and TypeScript

# Solution

The solution purpose is to implement the described rules above with pre-defined analyzers.

This way we can concentrate each analysis in a self-contained class with a common contract with other analyzers.

This contract is made to receive an integer and return a simple string with the analysis result.

In case the analyzer is not the right one for the number, the Analyzer will return `null` and the AnalysisManager will try to run the next Analyzers until he finds one that returns the analysis result. For that reason we will have a Fallback analyzer called DecimalFallbackAnalyzer.

## Getting started

Before running the analyzer, you will need to install the project dependencies with `composer install`.

### Running the Analyzer

By default the analyzer will run in the range from 1 to 100, you can run it with this simple command:

```
bin/linian-number-analyzer
```

If you receive the permission error, try adding execution permission for this binary file:

```
chmod +x bin/linian-number-analyzer
```

## Running the tests

To run the test cases within this project just execute:

```
vendor/bin/phpunit tests/AnalyzersTests.php
```