레이블이 GO인 게시물을 표시합니다. 모든 게시물 표시
레이블이 GO인 게시물을 표시합니다. 모든 게시물 표시

3/08/2017

Hackerrank Array Left Rotation test case failing at test case 6, 8, 9

I'm learning GoLang and decided to do some algorithm quiz at Hackerrank with Go instead of reading and just follow up the tutorial code.

The one I tried yesterday was https://www.hackerrank.com/challenges/ctci-array-left-rotation

Following was my initial solution. It's kind of brute-force algorithm for shift things left, but anyway...

package main
import (
    "fmt"
    "strings"
    "bufio"
    "os"
    "bytes"
)

func main() {
    var len, nToRotate int
    var input string
    var buffer bytes.Buffer
    
    fmt.Scanln(&len, &nToRotate)
    
    scanner := bufio.NewScanner(os.Stdin)
    for scanner.Scan() {
        buffer.WriteString(scanner.Text())
    }
    input = buffer.String()
    var items []string
    items = strings.Fields(input)
    
    //fmt.Printf("%d", len(items))
    
    for i := 0; i < nToRotate; i++ {
        items = append(items[1:], items[0])
    }
    
    fmt.Println(strings.Join(items[:]," "))
}

My code passed the test when I clicked "Run Code", but it's failing unit test 6, 8, 9 when submitting the code. Those 6, 8, and 9 is pretty long one line input string and I was wondering if it's some kind of timeout or library error due to input string size.

After hours of investigation and asking questions to other GoLang experts ( than me ), I was able to pass all the tests and submitted the solution. Here is my answer. I changed algorithm a bit to be more time/space efficient as well. Check the highlighted line which was the solution of Runtime Error in Hackerrank page.

package main
import (
 "fmt"
)


func main() {
 var (
  length, nToRotate int
 )

 fmt.Scanln(&length, &nToRotate)

 items := make([]string, length)
 for i := 0; i < length; i++ {
  fmt.Scan(&items[i])
 }

 items = ShiftLeft(items, nToRotate)

 for i := 0; i < len(items); i++ {
  fmt.Printf("%s ", items[i])
 }
}

func ShiftLeft(items []string, nToRotate int) []string {
 var (
  i, j, k int
  temp string
 )
 size := len(items)
 for i = 0; i < gcd(nToRotate, size); i++ {
  temp = items[i];
  j = i
  for {
   k = j + nToRotate
   if k >= size {
    k = k - size
   }
   if k == i {
    break
   }
   items[j] = items[k]
   j = k
  }
  items[j] = temp
 }
 return items
}

func gcd(x, y int) int {
 for y != 0 {
  x, y = y, x % y
 }
 return x
}


And I just found that someone already had same issue (of course) with Go, and opened issue at GoLang's GitHub page. Check those conversation if you want to understand more about the issue.

https://github.com/golang/go/issues/17910


7/22/2014

OSCON 2014 - 월요일

작년에 이어 올해도 운좋게 OSCON에 오게 되었다. 물론 회사가 돈을 잘 벌고, 같은 미국내에 있어서 한국에서 미국으로 직접 날라오는 것보다는 경비가 적게 드는 건 사실이지만, 그래도 회사에서 개발자에게 투자하려는 의지가 없다면 가능하지 않은 일이리라. 일단 회사에 감사(아부아부)

이번 OSCON은 일요일부터 시작해서 목요일까지 진행되고 내 티켓은 화수목 3일짜리인데, 실제 주요 세션들은 화요일부터 시작되고, 빨간색 OSCON 티셔츠도 화요일부터 나눠준다고 한다.  어제(현지시간으로 지금 화요일 새벽 12시 55분이니) 잠깐 컨퍼런스 체크인도 할 겸 같이 온 회사 동료들도 만날 겸 나갔다 왔는데, 일단 엑스포 홀쪽 분위기는 작년이랑 비슷해 보였다. 오렐리 코너는 작년보다 약간 규모가 작아진거 같았고, 내 기억이 맞다면 작년에는 Riot Games의 부스가 보였는데, 일단 월요일날 오후에 확인해 봤을때는 부스가 없었다. 구글이 제법 규모가 있게 부스를 차렸고, Microsoft부스도 보였다. 그 외 클라우데라나 Pivotal, 아파치 등의 부스도 있었는데, 작년에 부스가 있었던 JFrog는 아직 없는 듯.

전체적인 컨퍼런스의 세션 내용은 OSCON 2014에서도 살펴볼 수 있겠지만 작년보다 Puppet이나 Chef에 대한 내용은 거의 없다시피 했고 대신 Docker나 가상환경, 클라우드 환경에 대한  세션이 늘었다. 구글이 부스를 크게 차리고 본격적으로 GO를 밀려고 하는건지 모르겠지만 아무튼 GO관련 세션이 꽤 보였고, 작년에 꽤 여러개의 세션을 했던걸로 기억하는 Netflix의 세션은 딱 한갠가 보이는거 같다. Erlang쪽 세션이 많이 보이고, Elasticsearch랑 ELK쪽 세션이 꽤 많이 보인다. 하둡이랑 NOSQL쪽도 한쪽으로 잔뜩 치우치지는 않고 그냥 골고루 한두개 정도씩 세션이 있다.

자세한 내용은 화수목 세션을 좀 들어보고 시간이 되면 정리해서 다시 올리든가 해야 할 듯.

아참, OSCON의 발음은 오스콘이다. 오픈 소스 컨벤션(Open Source Convention).  OS + CON이라고 생각하고 가끔씩 오에스콘이라고 발음하시는 분들은 참고하시길.

요즘 포스팅을 모두 medium쪽으로 하고 있습니다

안녕하세요? 어쩌다보니 그냥 한번 써보기로 한 medium.com 에다가 죄다 최근 포스팅을 하고 있습니다. 현재 도메인인 potatosoft.com 은 제가 대학때인가 사회 취업한지 얼마 안됐을때부터 유지해 온 도메인이고, 블로그 시스템은 그...