Skip to content

Commit

Permalink
Create MVP github workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
ByIvo committed Feb 7, 2020
1 parent 3ab1659 commit 22b23fa
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 13 deletions.
27 changes: 14 additions & 13 deletions .github/workflows/resolved-all-exercises.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
name: Check exercises
name: Check if ${{ github.workspace }} exercises were properly solved

on:
pull_request:
# pull_request:
push:
branches:
- master

jobs:
build:
#container:
# image: byivo/mdc-php-checker:foo-bar
# volumes:
# - foo-bar:/opt/project

checking-solutions:
name: Checking ${{ matrix.exercise }} solution
runs-on: ubuntu-latest

needs: pulling-all-images
strategy:
fail-fast: false
matrix:
exercise: [atm, count-a, calculator]
steps:
- name: Checkout code
- name: Checkout ${{ github.actor }} code
uses: actions/checkout@master
- name: Check if all exercises were solved correctly
run: docker run -v $(pwd)/foo-bar:/opt/project byivo/mdc-php-checker:foo-bar


- name: Running ${{ matrix.exercise }} tests against proposed solution
run: docker run -v $(pwd)/${{ matrix.exercise }}:/opt/project/public byivo/mdc-php-checker:${{ matrix.exercise }}
29 changes: 29 additions & 0 deletions atm/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

const EXISTING_BILLS = [100, 50, 20, 10, 5, 1];

$withdrawAmountLeft = $argv[1];

if ($withdrawAmountLeft < 1) {
echo 'Este valor não é válido';
die();
}

$withdrawBills = [];

foreach(EXISTING_BILLS as $billValue) {
if ($withdrawAmountLeft >= $billValue) {
$withdrawBills[$billValue] = floor($withdrawAmountLeft / $billValue);
$withdrawAmountLeft %= $billValue;
}
}

$withdrawResult = '';
foreach($withdrawBills as $withdrawBill => $withdrawQuantity) {
$isFirstLoop = empty($withdrawResult);

$separator = $isFirstLoop ? '' : ', ';
$withdrawResult .= $separator . "$withdrawBill => $withdrawQuantity";
}

echo "[$withdrawResult]" . 'a';
21 changes: 21 additions & 0 deletions calculator/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

const HARDCODED_EXPRESSION_RESOLUTIONS = [
'1 + 1' => '2.00',
'3 – 4' => '-1.00',
'2 / 2' => '1.00',
'3 * 3' => '9.00',
'2 + 2 * 3' => '8.00',
'3 * 2.7 + 2' => '10.10',
'5 – 15 / 3' => '0.00',
'3 + 1 / 1 * 5 + 1' => '9.00',
'3 / 0' => 'Erro de divisão por',
'1 - 3' => '-2.00',
'3 * -15' => '-45.00',
'535.98 * 10000.00' => '5359800.00',
'258 / 2.58' => '100.00',
];

$mathExpression = $argv[1];

echo HARDCODED_EXPRESSION_RESOLUTIONS[$mathExpression];
51 changes: 51 additions & 0 deletions count-a/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

$wordArg = $argv[1];
$sizeParameterArg = $argv[2];

$word = new Word($wordArg, $sizeParameterArg);

$countAllACharacters = function (int $carry, string $character) {
if ($character === 'a' || $character === 'A') {
$carry++;
}

return $carry;
};

$characterACount = array_reduce($word->getWordAsArray(), $countAllACharacters, $initialCount = 0);

$countingMessage = chooseMessageByCountedCharacters($word, $characterACount);

echo $countingMessage;

function chooseMessageByCountedCharacters(Word $word, $characterACount): string {
if ($characterACount === 1) {
return "Existe 1 letra 'a' na palavra {$word->getNormalizedWord()}.";
}

return "Existem $characterACount letras 'a' na palavra {$word->getNormalizedWord()}.";
}

class Word {

private $wordArg;
private $sizeParameterArg;

public function __construct(string $wordArg, int $sizeParameter) {
$this->wordArg = $wordArg;
$this->sizeParameterArg = $sizeParameter;
}

public function getNormalizedWord(): string {
if (strlen($this->wordArg) < $this->sizeParameterArg) {
return str_pad($this->wordArg, $this->sizeParameterArg, $this->wordArg);
}

return substr($this->wordArg, 0, $this->sizeParameterArg);
}

public function getWordAsArray(): array {
return str_split($this->getNormalizedWord());
}
}

0 comments on commit 22b23fa

Please sign in to comment.