From 713f70041bb13bec2e8e07a47443f1e6b8ea455a Mon Sep 17 00:00:00 2001 From: "Lars M. Rogne" Date: Thu, 25 Apr 2024 04:41:39 +0200 Subject: [PATCH] Clean code improvements. --- collatz.go | 24 ++++++++++++++-- collatz/calculator.go | 58 ++++++++++++++++++++------------------ collatz/calculator_test.go | 5 +++- parsing/cliargs.go | 6 ---- 4 files changed, 56 insertions(+), 37 deletions(-) diff --git a/collatz.go b/collatz.go index 3c3e97b..46773c9 100644 --- a/collatz.go +++ b/collatz.go @@ -4,12 +4,32 @@ import ( "collatz/collatz" "collatz/parsing" "log" + "strings" ) func main() { - calculator := collatz.NewCalculator(new(parsing.Cliargs)) - err := calculator.CalculateCollatz() + sequence, err := runCollatz() if err != nil { log.Fatalf("Error calculating Collatz: %v", err) } + printSequence(sequence) +} + +func printSequence(sequence collatz.Sequence) { + builder := new(strings.Builder) + log.Printf("Collatz sequence with %d iterations:", sequence.Iterations) + for i, num := range sequence.Sequence { + if i != 0 { + builder.WriteString(" -> ") + } + builder.WriteString(num.String()) + } + log.Println(builder.String()) +} + +func runCollatz() (collatz.Sequence, error) { + parser := new(parsing.Cliargs) + calculator := collatz.NewCalculator(parser) + sequence, err := calculator.CalculateCollatz() + return sequence, err } diff --git a/collatz/calculator.go b/collatz/calculator.go index 3122ffe..189cda1 100644 --- a/collatz/calculator.go +++ b/collatz/calculator.go @@ -7,53 +7,55 @@ import ( ) type Calculator struct { - parser parsing.Parser - three *big.Int - two *big.Int - one *big.Int - iterations int + parser parsing.Parser + three *big.Int + two *big.Int + one *big.Int + sequence Sequence +} +type Sequence struct { + Iterations int + Sequence []*big.Int } func NewCalculator(parser parsing.Parser) *Calculator { r := &Calculator{ - parser: parser, - three: big.NewInt(3), - two: big.NewInt(2), - one: big.NewInt(1), - iterations: 0, + parser: parser, + three: big.NewInt(3), + two: big.NewInt(2), + one: big.NewInt(1), } return r } -func (o *Calculator) CalculateCollatz() error { - number, err := o.parser.Parse() +func (calc *Calculator) CalculateCollatz() (Sequence, error) { + number, err := calc.parser.Parse() if err != nil { - return fmt.Errorf("couldn't parse input: %w", err) + return Sequence{}, fmt.Errorf("couldn't parse input: %w", err) } - fmt.Print(number.Text(10)) - err = o.nextNumber(*number) + calc.sequence = Sequence{Iterations: 0} + calc.sequence.Sequence = append(calc.sequence.Sequence, new(big.Int).Set(number)) + err = calc.calculateNextCollatzNumber(*number) if err != nil { - return err + return Sequence{}, err } - fmt.Println() - fmt.Println("Iterations:", o.iterations) - return nil + return calc.sequence, nil } -func (o *Calculator) nextNumber(in big.Int) error { - if in.Cmp(o.one) < 0 { +func (calc *Calculator) calculateNextCollatzNumber(in big.Int) error { + if in.Cmp(calc.one) < 0 { return fmt.Errorf("must be greater than 0") } - if in.Cmp(o.one) == 0 { + if in.Cmp(calc.one) == 0 { return nil } var newNumber *big.Int - if new(big.Int).And(&in, o.one).Cmp(o.one) == 0 { - newNumber = new(big.Int).Add(new(big.Int).Mul(&in, o.three), o.one) + if new(big.Int).And(&in, calc.one).Cmp(calc.one) == 0 { + newNumber = new(big.Int).Add(new(big.Int).Mul(&in, calc.three), calc.one) } else { - newNumber = new(big.Int).Div(&in, o.two) + newNumber = new(big.Int).Div(&in, calc.two) } - o.iterations++ - fmt.Printf(" -> %s", newNumber.Text(10)) - return o.nextNumber(*newNumber) + calc.sequence.Iterations++ + calc.sequence.Sequence = append(calc.sequence.Sequence, newNumber) + return calc.calculateNextCollatzNumber(*newNumber) } diff --git a/collatz/calculator_test.go b/collatz/calculator_test.go index ab4f167..80cf133 100644 --- a/collatz/calculator_test.go +++ b/collatz/calculator_test.go @@ -38,8 +38,11 @@ func TestCalculateCollatz(t *testing.T) { parser := &MockParser{value: big.NewInt(6)} calculator := NewCalculator(parser) - err := calculator.CalculateCollatz() + sequence, err := calculator.CalculateCollatz() if err != nil { t.Errorf("Expected no error, got %v", err) } + if sequence.Iterations != 8 { + t.Errorf("Expected 8 iterations, got %v", sequence.Iterations) + } } diff --git a/parsing/cliargs.go b/parsing/cliargs.go index aecab34..3153e29 100644 --- a/parsing/cliargs.go +++ b/parsing/cliargs.go @@ -7,7 +7,6 @@ import ( ) type Cliargs struct { - value *big.Int } func (arg *Cliargs) Parse() (*big.Int, error) { @@ -19,10 +18,5 @@ func (arg *Cliargs) Parse() (*big.Int, error) { if !success { return nil, errors.New("argument is not an integer") } - arg.value = num return num, nil } - -func (arg *Cliargs) GetNumber() *big.Int { - return arg.value -}