From d88da48bc64092e3417cd48e8d8628317e4555af Mon Sep 17 00:00:00 2001 From: Inanc Gumus Date: Wed, 30 Jan 2019 17:01:50 +0300 Subject: [PATCH] fix: slicing-housing-prices exercise bug --- .../exercises/15-slicing-housing-prices/main.go | 15 +++++++++++++++ .../15-slicing-housing-prices/solution/main.go | 7 ++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/16-slices/exercises/15-slicing-housing-prices/main.go b/16-slices/exercises/15-slicing-housing-prices/main.go index e05b359..607d33d 100644 --- a/16-slices/exercises/15-slicing-housing-prices/main.go +++ b/16-slices/exercises/15-slicing-housing-prices/main.go @@ -94,6 +94,21 @@ package main // Istanbul 500 10 5 1000000 // // +// Note : It works even if the Price's index > Size's index +// +// In that case, it resets the invalid starting position to 0. +// +// But it still uses the Size column. +// +// go run main.go Price Size +// Location Size +// +// New York 100 +// New York 150 +// Paris 200 +// Istanbul 500 +// +// // HINTS // // + strings.Split function can separate a string into a []string diff --git a/16-slices/exercises/15-slicing-housing-prices/solution/main.go b/16-slices/exercises/15-slicing-housing-prices/solution/main.go index 34b20f7..808024c 100644 --- a/16-slices/exercises/15-slicing-housing-prices/solution/main.go +++ b/16-slices/exercises/15-slicing-housing-prices/solution/main.go @@ -45,10 +45,15 @@ Istanbul,500,10,5,1000000` } } + // from cannot be greater than to: reset invalid arg to 0 + if from > to { + from = 0 + } + for i, row := range rows { cols := strings.Split(row, separator) - // print the only the requested columns + // print only the requested columns for _, h := range cols[from:to] { fmt.Printf("%-15s", h) }