114 lines
3.5 KiB
Go
114 lines
3.5 KiB
Go
![]() |
package main
|
||
|
|
||
|
// ---------------------------------------------------------
|
||
|
// EXERCISE: Slicing the Housing Prices
|
||
|
//
|
||
|
// We have received housing prices. Your task is to print only the requested
|
||
|
// columns of data (see the expected output).
|
||
|
//
|
||
|
// 1. Separate the data by the newline ("\n") -> rows
|
||
|
//
|
||
|
// 2. Separate each row of the data by the separator (",") -> columns
|
||
|
//
|
||
|
// 3. Find the from and to positions inside the columns depending
|
||
|
// on the command-line arguments.
|
||
|
//
|
||
|
// 4. Print only the requested column headers and their data
|
||
|
//
|
||
|
//
|
||
|
// RESTRICTIONS
|
||
|
//
|
||
|
// + You should use slicing when printing the columns.
|
||
|
//
|
||
|
//
|
||
|
// EXPECTED OUTPUT
|
||
|
//
|
||
|
// go run main.go
|
||
|
// Location Size Beds Baths Price
|
||
|
//
|
||
|
// New York 100 2 1 100000
|
||
|
// New York 150 3 2 200000
|
||
|
// Paris 200 4 3 400000
|
||
|
// Istanbul 500 10 5 1000000
|
||
|
//
|
||
|
//
|
||
|
// go run main.go Location
|
||
|
// Location Size Beds Baths Price
|
||
|
//
|
||
|
// New York 100 2 1 100000
|
||
|
// New York 150 3 2 200000
|
||
|
// Paris 200 4 3 400000
|
||
|
// Istanbul 500 10 5 1000000
|
||
|
//
|
||
|
//
|
||
|
// NOTE : Finds the position of the Size column and slices the columns
|
||
|
// appropriately.
|
||
|
//
|
||
|
// go run main.go Size
|
||
|
// Size Beds Baths Price
|
||
|
//
|
||
|
// 100 2 1 100000
|
||
|
// 150 3 2 200000
|
||
|
// 200 4 3 400000
|
||
|
// 500 10 5 1000000
|
||
|
//
|
||
|
//
|
||
|
// NOTE : Finds the positions of the Size and Baths columns and
|
||
|
// slices the columns appropriately.
|
||
|
//
|
||
|
// go run main.go Size Baths
|
||
|
// Size Beds Baths
|
||
|
//
|
||
|
// 100 2 1
|
||
|
// 150 3 2
|
||
|
// 200 4 3
|
||
|
// 500 10 5
|
||
|
//
|
||
|
//
|
||
|
// go run main.go Beds Price
|
||
|
// Beds Baths Price
|
||
|
//
|
||
|
// 2 1 100000
|
||
|
// 3 2 200000
|
||
|
// 4 3 400000
|
||
|
// 10 5 1000000
|
||
|
//
|
||
|
//
|
||
|
// Note : It works even if the given column name does not exist.
|
||
|
//
|
||
|
// go run main.go Beds NotExist
|
||
|
// Beds Baths Price
|
||
|
//
|
||
|
// 2 1 100000
|
||
|
// 3 2 200000
|
||
|
// 4 3 400000
|
||
|
// 10 5 1000000
|
||
|
//
|
||
|
//
|
||
|
// go run main.go NotExist NotExist
|
||
|
// Location Size Beds Baths Price
|
||
|
//
|
||
|
// New York 100 2 1 100000
|
||
|
// New York 150 3 2 200000
|
||
|
// Paris 200 4 3 400000
|
||
|
// Istanbul 500 10 5 1000000
|
||
|
//
|
||
|
//
|
||
|
// HINTS
|
||
|
//
|
||
|
// + strings.Split function can separate a string into a []string
|
||
|
//
|
||
|
// ---------------------------------------------------------
|
||
|
|
||
|
func main() {
|
||
|
const (
|
||
|
data = `Location,Size,Beds,Baths,Price
|
||
|
New York,100,2,1,100000
|
||
|
New York,150,3,2,200000
|
||
|
Paris,200,4,3,400000
|
||
|
Istanbul,500,10,5,1000000`
|
||
|
|
||
|
separator = ","
|
||
|
)
|
||
|
}
|