Last active
March 9, 2016 06:02
-
-
Save louy2/3fcc616f61e8e44f3fa4 to your computer and use it in GitHub Desktop.
「眼帯」ゲットチャレンジ! https://paiza.jp/poh/ando (go) | 恋愛SLG: プログラミングで彼女をつくる|paizaオンラインハッカソン7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"log" | |
"strings" | |
"bufio" | |
"strconv" | |
) | |
func parseBookList (bookListStr string) []int { | |
bookListScanner := bufio.NewScanner(strings.NewReader(bookListStr)) | |
bookListScanner.Split(bufio.ScanWords) | |
var bookList []int | |
for bookListScanner.Scan() { | |
book, err := strconv.Atoi(bookListScanner.Text()) | |
if err != nil { | |
log.Fatal("fail to convert book in list") | |
} | |
bookList = append(bookList, book) | |
} | |
return bookList | |
} | |
func parseStdin() (int, BookOwner, BookOwner) { | |
stdin := bufio.NewScanner(os.Stdin) | |
stdinContent := make([]string, 5) | |
for i := 0; i < 5; i++ { | |
stdin.Scan() | |
stdinContent[i] = stdin.Text() | |
} | |
numberOfBooks, err := strconv.Atoi(stdinContent[0]) | |
if err != nil { | |
log.Fatal("fail to convert numberOfBooks") | |
} | |
// fmt.Println(numberOfBooks) | |
// numberOfBooksFromBuyer, err := strconv.Atoi(stdinContent[1]) | |
// if err != nil { | |
// log.Fatal("fail to convert numberOfBooksFromBuyer") | |
// } | |
// fmt.Println(numberOfBooksFromBuyer) | |
booksFromBuyer := parseBookList(stdinContent[2]) | |
// fmt.Println(booksFromBuyer) | |
// numberOfBooksFromShop, err := strconv.Atoi(stdinContent[3]) | |
// if err != nil { | |
// log.Fatal("fail to convert numberOfBooksFromShop") | |
// } | |
// fmt.Println(numberOfBooksFromShop) | |
booksFromShop := parseBookList(stdinContent[4]) | |
// fmt.Println(booksFromShop) | |
return numberOfBooks, | |
BookOwner{ | |
books: booksFromBuyer, | |
}, | |
BookOwner{ | |
books: booksFromShop, | |
} | |
} | |
type BookOwner struct { | |
books []int | |
} | |
// func (aBookOwner BookOwner) String() string { | |
// bookListStr := "[" | |
// for _, book := range aBookOwner.books { | |
// bookListStr = bookListStr + strconv.Itoa(book) + " " | |
// } | |
// return strings.Trim(bookListStr, " ") + "]" | |
// } | |
func (aBookOwner BookOwner) hasBook(theBook int) bool { | |
for _, book := range aBookOwner.books { | |
if book == theBook { | |
return true | |
} | |
} | |
return false | |
} | |
func (aBookBuyer BookOwner) findMissingBooksFrom(aBookShop BookOwner, numberOfBooks int) []int { | |
var missingBooks []int | |
for i := 1; i <= numberOfBooks; i++ { | |
if aBookBuyer.hasBook(i) { | |
} else { | |
if aBookShop.hasBook(i) { | |
missingBooks = append(missingBooks, i) | |
} | |
} | |
} | |
return missingBooks | |
} | |
func printBookList(books []int) { | |
if len(books) == 0 { | |
fmt.Println("None") | |
return | |
} | |
var bookListStr string | |
for _, book := range books { | |
bookListStr = bookListStr + strconv.Itoa(book) + " " | |
} | |
fmt.Println(strings.Trim(bookListStr, " ")) | |
} | |
func printBookOwner(aBookOwner BookOwner) { | |
printBookList(aBookOwner.books) | |
} | |
func main(){ | |
numberOfBooks, testBuyer1, testShop1 := parseStdin() | |
// fmt.Println(numberOfBooks) | |
// printBookOwner(testBuyer1) | |
// printBookOwner(testShop1) | |
printBookList(testBuyer1.findMissingBooksFrom(testShop1, numberOfBooks)) | |
// fmt.Println("--------------------------") | |
// testBuyer2 := BookOwner{ | |
// books: []int{1, 3, 4, 5, 6}, | |
// } | |
// testShop2 := BookOwner{ | |
// books: []int{1, 5, 6}, | |
// } | |
// fmt.Println(8) | |
// printBookOwner(testBuyer2) | |
// printBookOwner(testShop2) | |
// printBookList(testBuyer2.findMissingBooksFrom(testShop2, 8)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment