-
Notifications
You must be signed in to change notification settings - Fork 0
homeWork20 done #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package homeWork_20; | ||
|
|
||
| import org.assertj.core.api.Assertions; | ||
|
|
||
| import java.util.stream.IntStream; | ||
| import java.util.stream.Stream; | ||
|
|
||
| public class Calculator { | ||
|
|
||
| public static int sumOfNaturalNumbers(int limit) { | ||
| return IntStream.range(1,limit) | ||
| .filter(number -> number % 3 == 0 || number % 5 == 0) | ||
| .sum(); | ||
| } | ||
|
|
||
| public static int fibonacciSequence(int limit) { | ||
| return Stream.iterate(new int[]{1, 1}, n -> new int[]{n[1], n[0] + n[1]}) | ||
| .limit(limit) | ||
| .map(n -> n[0]) | ||
| .filter(n -> n % 2 == 0) | ||
| .mapToInt(n -> n) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Taka funkcja nazywa się identity i można jej implementację znaleźć w |
||
| .sum(); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package homeWork_20_Tests; | ||
|
|
||
|
|
||
| import org.assertj.core.api.Assertions; | ||
| import org.junit.Test; | ||
|
|
||
| import static homeWork_20.Calculator.fibonacciSequence; | ||
| import static org.assertj.core.api.Assertions.*; | ||
|
|
||
| public class SumOfEvenFibonacciNumbersTest { | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. O jeden enter za dużo |
||
| @Test | ||
| public void shouldReturn10WhenAddingSumOfEvenFibonacciAndNumbers() { | ||
| //given | ||
| int numberOfLimit = 7; | ||
|
|
||
| //when | ||
| int sumOfEvenNumbers = fibonacciSequence(numberOfLimit); | ||
|
|
||
| //then | ||
| int expectedNumber = 10; | ||
| assertThat(sumOfEvenNumbers).isEqualTo(expectedNumber); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturn44WhenAddingSumOfEvenFibonacciAndNumbers() { | ||
| //given | ||
| int numberOfLimit = 10; | ||
|
|
||
| //when | ||
| int sumOfEvenNumbers = fibonacciSequence(numberOfLimit); | ||
|
|
||
| //then | ||
| int expectedNumber = 44; | ||
| assertThat(sumOfEvenNumbers).isEqualTo(expectedNumber); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturn188WhenAddingSumOfEvenFibonacciAndNumbers() { | ||
| //given | ||
| int numberOfLimit = 12; | ||
|
|
||
| //when | ||
| int sumOfEvenNumbers = fibonacciSequence(numberOfLimit); | ||
|
|
||
| //then | ||
| int expectedNumber = 188; | ||
| assertThat(sumOfEvenNumbers).isEqualTo(expectedNumber); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package homeWork_20_Tests; | ||
|
|
||
| import org.assertj.core.api.Assertions; | ||
| import org.junit.Test; | ||
|
|
||
| import static homeWork_20.Calculator.*; | ||
| import static org.assertj.core.api.Assertions.*; | ||
|
|
||
| public class SumOfMutlipliers5or3Test { | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. za dużo o jeden enter |
||
| @Test | ||
| public void shouldReturn23WhenAddingSumOfMutliplies5or3ofNumber10() { | ||
| //given | ||
| int initialNumber = 10; | ||
|
|
||
| //when | ||
| int sumOfMultipliers = sumOfNaturalNumbers(initialNumber); | ||
|
|
||
| //then | ||
| int expectedNumber = 23; | ||
| assertThat(sumOfMultipliers).isEqualTo(expectedNumber); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturn33WhenAddingSumOfMutliplies5or3ofNumber11() { | ||
| //given | ||
| int initialNumber = 11; | ||
|
|
||
| //when | ||
| int sumOfMultipliers = sumOfNaturalNumbers(initialNumber); | ||
|
|
||
| //then | ||
| int expectedNumber = 33; | ||
| assertThat(sumOfMultipliers).isEqualTo(expectedNumber); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturn33WhenAddingSumOfMutliplies5or3ofNumber12() { | ||
| //given | ||
| int initialNumber = 12; | ||
|
|
||
| //when | ||
| int sumOfMultipliers = sumOfNaturalNumbers(initialNumber); | ||
|
|
||
| //then | ||
| int expectedNumber = 33; | ||
| assertThat(sumOfMultipliers).isEqualTo(expectedNumber); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturn45WhenAddingSumOfMutliplies5or3ofNumber13() { | ||
| //given | ||
| int initialNumber = 13; | ||
|
|
||
| //when | ||
| int sumOfMultipliers = sumOfNaturalNumbers(initialNumber); | ||
|
|
||
| //then | ||
| int expectedNumber = 45; | ||
| assertThat(sumOfMultipliers).isEqualTo(expectedNumber); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldReturn60WhenAddingSumOfMutliplies5or3ofNumber16() { | ||
| //given | ||
| int initialNumber = 16; | ||
|
|
||
| //when | ||
| int sumOfMultipliers = sumOfNaturalNumbers(initialNumber); | ||
|
|
||
| //then | ||
| int expectedNumber = 60; | ||
| assertThat(sumOfMultipliers).isEqualTo(expectedNumber); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Liczby fibonacciego jest to jedna z rzeczy które lepiej wyglądają jak są zaimplementowane na
forwhileniż na streamach :)