2020年4月7日火曜日

開発環境

入門Goプログラミング (Nathan Youngman(著)、Roger Peppé(著)、吉川 邦夫(監修, 翻訳)、翔泳社)のUNIT 1(命令型プログラミング)、LESSON 5(チャレンジ:火星行きのチケット)の解答を求めてみる。

コード

package main

import (
 "fmt"
 "math/rand"
 "strings"
 "time"
)

func main() {
 rand.Seed(time.Now().UnixNano())
 fmt.Printf("%-18s%-6s%-12s%-5s\n", "Spaceline", "Days", "Trip type", "Price")
 fmt.Println(strings.Repeat("-", 41))

 const distance = 62100000

 for i := 0; i < 10; i++ {
  var spaceline string
  switch rand.Intn(3) {
  case 0:
   spaceline = "Virgin Galactic"
  case 1:
   spaceline = "SpaceX"
  default:
   spaceline = "Space Adventures"
  }
  speed := (rand.Intn(31-16) + 16)
  days := distance / (speed * 60 * 60 * 24)
  var tripType string
  if rand.Intn(2) == 0 {
   tripType = "Round-trip"
  } else {
   tripType = "One-way"
  }
  price := speed + 20
  fmt.Printf("%-18s%-6d%-12s$ %-3d\n", spaceline, days, tripType, price)
 }
 fmt.Println("別の方法")
 fmt.Printf("%-18s%-6s%-12s%-5s\n", "Spaceline", "Days", "Trip type", "Price")
 fmt.Println(strings.Repeat("-", 41))
 spacelines := []string{
  "Virgin Galactic",
  "SpaceX",
  "Space Adventures",
 }
 triptypes := []string{"Round-trip", "One-way"}
 for i := 0; i < 10; i++ {
  speed := rand.Intn(31-16) + 16
  days := distance / (speed * 60 * 60 * 24)
  price := speed + 20
  fmt.Printf("%-18s%-6d%-12s$ %-3d\n",
   spacelines[rand.Intn(3)],
   days,
   triptypes[rand.Intn(2)],
   price)
 }

}

入出力結果(Zsh、PowerShell、Terminal)

% go build ./sample1.go 
% ./sample1 
Spaceline         Days  Trip type   Price
-----------------------------------------
Virgin Galactic   39    One-way     $ 38 
Space Adventures  44    One-way     $ 36 
Space Adventures  24    One-way     $ 49 
Virgin Galactic   42    One-way     $ 37 
Virgin Galactic   32    One-way     $ 42 
Virgin Galactic   25    One-way     $ 48 
Virgin Galactic   34    One-way     $ 41 
SpaceX            32    One-way     $ 42 
Virgin Galactic   25    One-way     $ 48 
Virgin Galactic   25    Round-trip  $ 48 
別の方法
Spaceline         Days  Trip type   Price
-----------------------------------------
SpaceX            42    One-way     $ 37 
Virgin Galactic   37    Round-trip  $ 39 
Space Adventures  31    Round-trip  $ 43 
SpaceX            34    One-way     $ 41 
Virgin Galactic   39    Round-trip  $ 38 
Virgin Galactic   24    Round-trip  $ 49 
SpaceX            31    One-way     $ 43 
SpaceX            39    One-way     $ 38 
Space Adventures  28    Round-trip  $ 45 
SpaceX            39    Round-trip  $ 38 
% ./sample1 
Spaceline         Days  Trip type   Price
-----------------------------------------
Virgin Galactic   32    Round-trip  $ 42 
Virgin Galactic   42    One-way     $ 37 
SpaceX            24    Round-trip  $ 49 
Space Adventures  42    One-way     $ 37 
Space Adventures  25    One-way     $ 48 
SpaceX            39    Round-trip  $ 38 
Space Adventures  31    Round-trip  $ 43 
SpaceX            44    One-way     $ 36 
Virgin Galactic   28    Round-trip  $ 45 
SpaceX            28    One-way     $ 45 
別の方法
Spaceline         Days  Trip type   Price
-----------------------------------------
Virgin Galactic   29    Round-trip  $ 44 
SpaceX            23    One-way     $ 50 
Virgin Galactic   26    Round-trip  $ 47 
Virgin Galactic   42    One-way     $ 37 
Virgin Galactic   28    Round-trip  $ 45 
Space Adventures  32    One-way     $ 42 
Space Adventures  34    Round-trip  $ 41 
Space Adventures  31    One-way     $ 43 
Space Adventures  39    One-way     $ 38 
Virgin Galactic   25    Round-trip  $ 48 
% ./sample1 
Spaceline         Days  Trip type   Price
-----------------------------------------
Virgin Galactic   44    Round-trip  $ 36 
SpaceX            39    Round-trip  $ 38 
Virgin Galactic   39    One-way     $ 38 
SpaceX            42    One-way     $ 37 
SpaceX            24    Round-trip  $ 49 
Virgin Galactic   34    One-way     $ 41 
SpaceX            23    Round-trip  $ 50 
Virgin Galactic   34    One-way     $ 41 
Space Adventures  34    Round-trip  $ 41 
Space Adventures  31    One-way     $ 43 
別の方法
Spaceline         Days  Trip type   Price
-----------------------------------------
SpaceX            31    Round-trip  $ 43 
Space Adventures  39    Round-trip  $ 38 
Space Adventures  34    One-way     $ 41 
Virgin Galactic   25    One-way     $ 48 
Virgin Galactic   29    One-way     $ 44 
Virgin Galactic   23    One-way     $ 50 
Virgin Galactic   35    Round-trip  $ 40 
Virgin Galactic   26    One-way     $ 47 
SpaceX            34    Round-trip  $ 41 
Virgin Galactic   25    Round-trip  $ 48 
% ./sample1
Spaceline         Days  Trip type   Price
-----------------------------------------
Virgin Galactic   44    Round-trip  $ 36 
SpaceX            35    Round-trip  $ 40 
Virgin Galactic   31    Round-trip  $ 43 
Space Adventures  44    One-way     $ 36 
Space Adventures  37    One-way     $ 39 
Virgin Galactic   31    Round-trip  $ 43 
Space Adventures  44    One-way     $ 36 
Virgin Galactic   39    One-way     $ 38 
Virgin Galactic   26    One-way     $ 47 
SpaceX            42    One-way     $ 37 
別の方法
Spaceline         Days  Trip type   Price
-----------------------------------------
Space Adventures  44    Round-trip  $ 36 
Space Adventures  28    Round-trip  $ 45 
Space Adventures  37    Round-trip  $ 39 
Virgin Galactic   32    One-way     $ 42 
Space Adventures  25    Round-trip  $ 48 
Virgin Galactic   39    One-way     $ 38 
Space Adventures  37    One-way     $ 39 
Virgin Galactic   39    Round-trip  $ 38 
Virgin Galactic   28    Round-trip  $ 45 
SpaceX            28    Round-trip  $ 45 
% ./sample1
Spaceline         Days  Trip type   Price
-----------------------------------------
Space Adventures  26    Round-trip  $ 47 
Virgin Galactic   26    One-way     $ 47 
SpaceX            42    Round-trip  $ 37 
SpaceX            34    One-way     $ 41 
SpaceX            35    One-way     $ 40 
SpaceX            42    One-way     $ 37 
SpaceX            35    Round-trip  $ 40 
SpaceX            29    Round-trip  $ 44 
SpaceX            39    One-way     $ 38 
Space Adventures  24    Round-trip  $ 49 
別の方法
Spaceline         Days  Trip type   Price
-----------------------------------------
Space Adventures  28    One-way     $ 45 
Space Adventures  39    One-way     $ 38 
Virgin Galactic   35    One-way     $ 40 
Virgin Galactic   42    One-way     $ 37 
Space Adventures  23    Round-trip  $ 50 
Space Adventures  23    One-way     $ 50 
SpaceX            23    Round-trip  $ 50 
SpaceX            28    One-way     $ 45 
Space Adventures  35    One-way     $ 40 
SpaceX            28    Round-trip  $ 45 
%

0 コメント:

コメントを投稿