From 4f1bb81b4401686fa8860756a68c81a7b292008a Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sat, 27 Aug 2016 23:12:22 +0200 Subject: Solve largest --- go/largest-series-product/README.md | 31 ++++++++++++ go/largest-series-product/cases_test.go | 29 ++++++++++++ .../largest_series_product.go | 55 ++++++++++++++++++++++ .../largest_series_product_test.go | 36 ++++++++++++++ go/largest-series-product/x | 19 ++++++++ 5 files changed, 170 insertions(+) create mode 100644 go/largest-series-product/README.md create mode 100644 go/largest-series-product/cases_test.go create mode 100644 go/largest-series-product/largest_series_product.go create mode 100644 go/largest-series-product/largest_series_product_test.go create mode 100644 go/largest-series-product/x (limited to 'go') diff --git a/go/largest-series-product/README.md b/go/largest-series-product/README.md new file mode 100644 index 0000000..d9ea9be --- /dev/null +++ b/go/largest-series-product/README.md @@ -0,0 +1,31 @@ +# Largest Series Product + +Write a program that, when given a string of digits, can calculate the largest product for a contiguous substring of digits of length n. + +For example, for the input `'1027839564'`, the largest product for a +series of 3 digits is 270 (9 * 5 * 6), and the largest product for a +series of 5 digits is 7560 (7 * 8 * 3 * 9 * 5). + +Note that these series are only required to occupy *adjacent positions* +in the input; the digits need not be *numerically consecutive*. + +For the input `'73167176531330624919225119674426574742355349194934'`, +the largest product for a series of 6 digits is 23520. + +To run the tests simply run the command `go test` in the exercise directory. + +If the test suite contains benchmarks, you can run these with the `-bench` +flag: + + go test -bench . + +For more detailed info about the Go track see the [help +page](http://exercism.io/languages/go). + +## Source + +A variation on Problem 8 at Project Euler [http://projecteuler.net/problem=8](http://projecteuler.net/problem=8) + +## Submitting Incomplete Problems +It's possible to submit an incomplete solution so you can see how others have completed the exercise. + diff --git a/go/largest-series-product/cases_test.go b/go/largest-series-product/cases_test.go new file mode 100644 index 0000000..ad71012 --- /dev/null +++ b/go/largest-series-product/cases_test.go @@ -0,0 +1,29 @@ +package lsproduct + +// Source: exercism/x-common +// Commit: 3764abd Merge pull request #167 from petertseng/largest-series-product-json + +var tests = []struct { + digits string + span int + product int64 + ok bool +}{ + {"0123456789", 2, 72, true}, + {"576802143", 2, 48, true}, + {"29", 2, 18, true}, + {"0123456789", 3, 504, true}, + {"1027839564", 3, 270, true}, + {"0123456789", 5, 15120, true}, + {"73167176531330624919225119674426574742355349194934", 6, 23520, true}, + {"52677741234314237566414902593461595376319419139427", 6, 28350, true}, + {"7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450", 13, 23514624000, true}, + {"0000", 2, 0, true}, + {"99099", 3, 0, true}, + {"123", 4, -1, false}, + {"", 0, 1, true}, + {"123", 0, 1, true}, + {"", 1, -1, false}, + {"1234a5", 2, -1, false}, + {"12345", -1, -1, false}, +} diff --git a/go/largest-series-product/largest_series_product.go b/go/largest-series-product/largest_series_product.go new file mode 100644 index 0000000..df7110d --- /dev/null +++ b/go/largest-series-product/largest_series_product.go @@ -0,0 +1,55 @@ +package lsproduct + +import "errors" + +const testVersion = 3 + +func check(s string) bool { + for _, v := range s { + if v < '0' || v > '9' { + return false + } + } + return true +} + +func split(s string, n int) <-chan string { + c := make(chan string) + go func() { + for i := 0; i <= len(s)-n; i++ { + c <- s[i : i+n] + } + close(c) + }() + return c +} + +func max(c <-chan string) int { + prod := func(s string) int { + p := 1 + for _, v := range s { + p *= int(v - '0') + } + return p + } + var m int + for v := range c { + if p := prod(v); p > m { + m = p + } + } + return m +} + +func LargestSeriesProduct(s string, n int) (int, error) { + if ok := check(s); !ok { + return 0, errors.New("invalid chars") + } + if n < 0 || n > len(s) { + return 0, errors.New("invalid length") + } + if n == 0 { + return 1, nil + } + return max(split(s, n)), nil +} diff --git a/go/largest-series-product/largest_series_product_test.go b/go/largest-series-product/largest_series_product_test.go new file mode 100644 index 0000000..275c0a8 --- /dev/null +++ b/go/largest-series-product/largest_series_product_test.go @@ -0,0 +1,36 @@ +package lsproduct + +import "testing" + +const targetTestVersion = 3 + +func TestLargestSeriesProduct(t *testing.T) { + if testVersion != targetTestVersion { + t.Fatalf("Found testVersion = %v, want %v", testVersion, targetTestVersion) + } + for _, test := range tests { + p, err := LargestSeriesProduct(test.digits, test.span) + switch { + case err != nil: + if test.ok { + t.Fatalf("LargestSeriesProduct(%s, %d) returned error %q. "+ + "Error not expected.", + test.digits, test.span, err) + } + case !test.ok: + t.Fatalf("LargestSeriesProduct(%s, %d) = %d, %v. Expected error", + test.digits, test.span, p, err) + case int64(p) != test.product: + t.Fatalf("LargestSeriesProduct(%s, %d) = %d, want %d", + test.digits, test.span, p, test.product) + } + } +} + +func BenchmarkLargestSeriesProduct(b *testing.B) { + for i := 0; i < b.N; i++ { + for _, test := range tests { + LargestSeriesProduct(test.digits, test.span) + } + } +} diff --git a/go/largest-series-product/x b/go/largest-series-product/x new file mode 100644 index 0000000..ef92038 --- /dev/null +++ b/go/largest-series-product/x @@ -0,0 +1,19 @@ +1234567890 + +123 + 234 + 345 + 456 + 567 + 678 + 789 + 890 + +10 - 3 + 1 + +12345 + 23456 + 34567 + 45678 + 56789 + 67890 -- cgit v1.2.3