Hmm I should try that IRL, how much does a deck of cards Go for nowadays?
shuffle.go
packageshuffleimport("errors")// Faro interweaves two halves of a slice, leaving the first and last elements unchangedfuncFaro(deck[]string)([]string,error){size:=len(deck)ifsize%2!=0{returnnil,errors.New("deck must be of an even size")}// if the size is empty or just two, simply return the deckifsize<=2{returndeck,nil}left:=deck[:(size/2)]right:=deck[(size/2):]shuffled:=[]string{}fori:=rangeleft{shuffled=append(shuffled,left[i],right[i])}returnshuffled,nil}
shuffle_test.go
packageshuffleimport"testing"funcequal(result[]string,expected[]string)bool{iflen(result)!=len(expected){returnfalse}fori:=rangeresult{ifresult[i]!=expected[i]{returnfalse}}returntrue}funcTestFaro(t*testing.T){testCases:=[]struct{descriptionstringinput[]stringexpected[]stringexpectErrbool}{{"empty slice",[]string{},[]string{},false,},{"returns error on odd sized slice",[]string{"ace","one","two"},nil,true,},{"only two entries",[]string{"ace","king"},[]string{"ace","king"},false,},{"small deck",[]string{"ace","two","three","four"},[]string{"ace","three","two","four"},false,},{"post example",[]string{"ace","two","three","four","five","six"},[]string{"ace","four","two","five","three","six"},false,},}for_,test:=rangetestCases{result,err:=Faro(test.input)iferr==nil&&test.expectErr{t.Fatalf("FAIL: %s - Faro(%+v) - expected error to be thrown",test.description,test.input)}if!equal(result,test.expected){t.Fatalf("FAIL: %s - Faro(%+v): %+v, %+v - expected '%+v'",test.description,test.input,result,err,test.expected)}t.Logf("PASS: %s",test.description)}}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hmm I should try that IRL, how much does a deck of cards Go for nowadays?
shuffle.go
shuffle_test.go