Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

secret-handshake, and others: use %q to make difference between [""] and [] clear #414

Closed
Insti opened this issue Nov 8, 2016 · 3 comments

Comments

@Insti
Copy link

Insti commented Nov 8, 2016

"go path secret handshake does not work properly": exercism/exercism#3239


package secret

import (
	"regexp"
	"strconv"
)

const testVersion = 1

var cases = map[int]string{
	0: "wink",
	1: "double blink",
	2: "close your eyes",
	3: "jump",
	4:"reverse",
}

//Handshake function returns the encrypted human readable handshake
func Handshake(inp uint) []string {
	var in = strconv.Itoa(int(inp))
	if in == "0" {
		return nil
	}
	isDecimal := regexp.MustCompile("[2-9]")
	if isDecimal.MatchString(in) {
		num, _ := strconv.Atoi(in)
		in = strconv.FormatInt(int64(num), 2)
	}
	reverse := false
	out := make([]string, 0, 4)
	ln := len(in) - 1
	for num, val := range in {
		if ln-num == 4 {
			reverse = true
			continue
		}
		if string(val) == "1" {
			out = append(out, cases[ln-num])
		}
	}
	if !reverse {
		for i, j := 0, len(out)-1; i < j; i, j = i+1, j-1 {
			out[i], out[j] = out[j], out[i]
		}
	}
	return out
}
@petertseng
Copy link
Member

petertseng commented Nov 8, 2016

the posted implementation returns [""] for Handshake(32) (slice of one element, the empty string), whereas the two acceptable answers are either nil or [] (slice of zero elements)

This can be made clear by changing %v to %q in the test code, then you will see this:

--- FAIL: TestHandshake (0.00s)
	a_test.go:38: Handshake(32) = [""], want [].

I suggest the test file be updated to use %q to resolve this issue: https://play.golang.org/p/jG_SJMzJvP

@jtigger jtigger changed the title Go issue posted in exercism.io repository. go path secret handshake does not work properly Nov 15, 2016
@jtigger jtigger changed the title go path secret handshake does not work properly secret-handshake: go path does not work properly Nov 15, 2016
@petertseng petertseng changed the title secret-handshake: go path does not work properly secret-handshake: use %q to make difference between [""] and [] clear Nov 23, 2016
@petertseng
Copy link
Member

petertseng commented Nov 23, 2016

also consider using %q in the following exercises that may expect []strings:

(I just grepped for []string, but I think I did not miss any)

Anyone working on these is free to work on either just a single one of these, or all the remaining ones (just three left as of this writing)

petertseng added a commit that referenced this issue Nov 29, 2016
@petertseng petertseng changed the title secret-handshake: use %q to make difference between [""] and [] clear accumulate, kindergarten-garden, series, strain: use %q to make difference between [""] and [] clear Nov 29, 2016
robphoenix added a commit to robphoenix/exercism-go that referenced this issue Feb 21, 2017
see exercism#414, updated to make the use of strings more apparent, so [    ]
will now be ["", "", "", "", ""].

All uses of strings have also been changed to use %q rather than %s to be
consistent.
ie. `first_test.go:20: First(1, "01234") = "".  Want "0".`
rather than
ie. `first_test.go:20: First(1, 01234) = .  Want 0.`
or
ie. `first_test.go:20: First(1, 01234) = "".  Want "0".`
@petertseng petertseng changed the title accumulate, kindergarten-garden, series, strain: use %q to make difference between [""] and [] clear accumulate, kindergarten-garden, series: use %q to make difference between [""] and [] clear Feb 23, 2017
@petertseng petertseng changed the title accumulate, kindergarten-garden, series: use %q to make difference between [""] and [] clear accumulate, kindergarten-garden, strain: use %q to make difference between [""] and [] clear Feb 23, 2017
robphoenix added a commit to robphoenix/exercism-go that referenced this issue Feb 23, 2017
see exercism#414
replace `%v` with `%#v` to provide a clearer output in the case of a
failed test. `%#v` is used rather than `%q` as suggested in exercism#414 as it
provides a more accurate output, Strings[a, b, c] appears slightly
confusing.

strain.Strings{"apple", "zebra", "banana", "zombies", "cherimoya", "zelot"}.Keep()

rather than:

Strings[apple zebra banana zombies cherimoya zelot].Keep()
robphoenix added a commit to robphoenix/exercism-go that referenced this issue Feb 24, 2017
see exercism#414
replace `%v` with `%#v` to provide a clearer output in the case of a
failed test. `%#v` is used rather than `%q` as suggested in exercism#414 as it
provides a more accurate output, Strings[a, b, c] appears slightly
confusing.

strain.Strings{"apple", "zebra", "banana", "zombies", "cherimoya", "zelot"}.Keep()

rather than:

Strings[apple zebra banana zombies cherimoya zelot].Keep()
@petertseng petertseng changed the title accumulate, kindergarten-garden, strain: use %q to make difference between [""] and [] clear accumulate, kindergarten-garden: use %q to make difference between [""] and [] clear Mar 1, 2017
robphoenix added a commit to robphoenix/exercism-go that referenced this issue Mar 3, 2017
@petertseng petertseng changed the title accumulate, kindergarten-garden: use %q to make difference between [""] and [] clear accumulate: use %q to make difference between [""] and [] clear Mar 5, 2017
robphoenix added a commit to robphoenix/exercism-go that referenced this issue Mar 6, 2017
see exercism#414
Also removed unnecessary else block
robphoenix added a commit to robphoenix/exercism-go that referenced this issue Mar 6, 2017
see exercism#414
Also removed unnecessary else block
@robphoenix
Copy link
Contributor

🎉

@petertseng petertseng changed the title accumulate: use %q to make difference between [""] and [] clear secret-handshake, and others: use %q to make difference between [""] and [] clear Mar 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants