From da705aafa5a1890f640c06176bd7576dcdc60b05 Mon Sep 17 00:00:00 2001 From: Jonata Weber Date: Thu, 25 Oct 2018 11:57:52 -0200 Subject: [PATCH 1/3] implement find first recurrence function --- .../jonataweber/FirstRecurrence.php | 31 +++++++++++++++++++ .../jonataweber/FirstRecurrenceTest.php | 10 ++++++ 2 files changed, 41 insertions(+) create mode 100644 src/primeira_recorrencia/jonataweber/FirstRecurrence.php create mode 100644 src/primeira_recorrencia/jonataweber/FirstRecurrenceTest.php diff --git a/src/primeira_recorrencia/jonataweber/FirstRecurrence.php b/src/primeira_recorrencia/jonataweber/FirstRecurrence.php new file mode 100644 index 0000000..7e75479 --- /dev/null +++ b/src/primeira_recorrencia/jonataweber/FirstRecurrence.php @@ -0,0 +1,31 @@ += (strlen($haystack) - 1); + $isRecurrence = $carry[$char] > 1; + $notFoundRecurrence = $isLastCharacter && false === $isRecurrence; + + if ($notFoundRecurrence) return null; + if ($isLastCharacter || $isRecurrence) return $char; + + return first($haystack, $offset + 1, $carry); +} \ No newline at end of file diff --git a/src/primeira_recorrencia/jonataweber/FirstRecurrenceTest.php b/src/primeira_recorrencia/jonataweber/FirstRecurrenceTest.php new file mode 100644 index 0000000..ee4462d --- /dev/null +++ b/src/primeira_recorrencia/jonataweber/FirstRecurrenceTest.php @@ -0,0 +1,10 @@ + Date: Thu, 25 Oct 2018 12:04:43 -0200 Subject: [PATCH 2/3] add end of line --- src/primeira_recorrencia/jonataweber/FirstRecurrence.php | 2 +- src/primeira_recorrencia/jonataweber/FirstRecurrenceTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/primeira_recorrencia/jonataweber/FirstRecurrence.php b/src/primeira_recorrencia/jonataweber/FirstRecurrence.php index 7e75479..862c1f3 100644 --- a/src/primeira_recorrencia/jonataweber/FirstRecurrence.php +++ b/src/primeira_recorrencia/jonataweber/FirstRecurrence.php @@ -28,4 +28,4 @@ function first(string $haystack, int $offset = 0, Array $carry = []) if ($isLastCharacter || $isRecurrence) return $char; return first($haystack, $offset + 1, $carry); -} \ No newline at end of file +} diff --git a/src/primeira_recorrencia/jonataweber/FirstRecurrenceTest.php b/src/primeira_recorrencia/jonataweber/FirstRecurrenceTest.php index ee4462d..9d4f4c0 100644 --- a/src/primeira_recorrencia/jonataweber/FirstRecurrenceTest.php +++ b/src/primeira_recorrencia/jonataweber/FirstRecurrenceTest.php @@ -7,4 +7,4 @@ assert(first('ABCDA') === 'A'); assert(first('ABBCDA') === 'B'); assert(first('ABBCDDA') === 'B'); -assert(first('ABC') === null); \ No newline at end of file +assert(first('ABC') === null); From 4597893f052cc95ebb1df04712d5aa49aa3b1e58 Mon Sep 17 00:00:00 2001 From: Jonata Weber Date: Thu, 25 Oct 2018 19:07:52 -0200 Subject: [PATCH 3/3] bit of refactorig --- src/primeira_recorrencia/jonataweber/FirstRecurrence.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/primeira_recorrencia/jonataweber/FirstRecurrence.php b/src/primeira_recorrencia/jonataweber/FirstRecurrence.php index 862c1f3..b43d663 100644 --- a/src/primeira_recorrencia/jonataweber/FirstRecurrence.php +++ b/src/primeira_recorrencia/jonataweber/FirstRecurrence.php @@ -24,8 +24,8 @@ function first(string $haystack, int $offset = 0, Array $carry = []) $isRecurrence = $carry[$char] > 1; $notFoundRecurrence = $isLastCharacter && false === $isRecurrence; + if ($isRecurrence) return $char; if ($notFoundRecurrence) return null; - if ($isLastCharacter || $isRecurrence) return $char; return first($haystack, $offset + 1, $carry); }