I'm making a simple hangman game in Go, but I have come across an error, the unknow_string string (will be shown in the code) has _ characters so the other player guessing the letters can see the length of the word, whenever the player enters a correct letter, I want it to replace the nth (n depending on which index the letter is found in the word) _ character with the letter, which has been successful, but with one problem. If the word has two duplicate letters, only one of them is replaced.
I created a separate function called iterate (function because I want to avoid nested code), to iterate over all indexes. but it didn't work, here's the code:
package main
import (
"fmt"
"strings"
)
var input string
var word string
var unknown_word []string
var unknown_word_string string
var index int
var guesses int
var change_unknown_word_string []byte
func main() {
fmt.Println("Welcome to hangman")
fmt.Println("Player 1, choose a word!")
fmt.Scan(&word)
for i := 1; i <= len(word); i++ {
unknown_word = append(unknown_word, "_")
}
unknown_word_string = strings.Join(unknown_word, "")
for {
fmt.Println("Player 2, guess a letter or a word")
fmt.Println(unknown_word_string)
fmt.Scan(&input)
if guesses == 6 {
fmt.Println("Player 2 lost! Player 1's word was: ", word)
break
} else if unknown_word_string == input {
fmt.Println("Player 1 lost! Player 2 guessed the word by guessing the letter!")
}
if strings.Contains(word, input) && word != input {
index = strings.Index(word, input)
iterate()
fmt.Println("You guessed a letter!")
} else if word == input {
fmt.Println("Player 1 lost! Player 2 guessed the word by guessing the whole word!")
break
} else {
fmt.Println("Nothing found")
guesses++
}
}
}
func iterate() {
change_unknown_word_string = []byte(unknown_word_string)
for i := 0; i < len(change_unknown_word_string); i++ {
if change_unknown_word_string[i] == change_unknown_word_string[index] {
change_unknown_word_string[i] = []byte(input)[0]
}
}
unknown_word_string = string(change_unknown_word_string)
}
The comparison if change_unknown_word_string[i] == change_unknown_word_string[index] makes no sense, as unknown_word_string obviously contains _ at those positions.
You should be comparing word[i] == input[0] in the loop instead.
But note that converting a string to a byte array is not Unicode-friendly. It's better to work with runes (Unicode code points) instead, this way you're not limited to latin1 characters.
func iterate() {
needle := []rune(input)[0]
haystack := []rune(word)
buf := []rune(unknown_word_string)
for i := range haystack {
if haystack[i] == needle {
buf[i] = needle
}
}
unknown_word_string = string(buf)
}
Bonus note: this comparison is wrong
if unknown_word_string == input {
fmt.Println("Player 1 lost! Player 2 guessed the word by guessing the letter!")
}
It should be if unknown_word_string == word and be located immediately after the call to iterate().
Related
Trying to find the longest word using Go from a sentence.
At the moment I am using this method:
func longestWord(s string) string {
newArr := strings.Split(s, " ")
l := len(newArr[0])
long := newArr[0]
var result string
// fmt.Println(long)
for _, lenString := range newArr {
if len(lenString) > l {
// ll := len(lenString)
// l := len(lenString)
d := &l
p := &long
c := &result
*d = len(lenString)
*p = lenString
*c = lenString
// fmt.Println(lenString)
} else {
c := &result
*c = newArr[0]
}
}
return result
}
func main() {
args := "Monday Tuesday Friday Sunday Wednesday"
fmt.Println(longestWord(args))
}
But I'm not sure that this is the best method to achieve that. Is there any other elegant way to do that? I know that there is one more method by using sort, but I would prefer more using the way with iteration between words.
"Best" solution
We can even write it more compact than the other answers by taking advantage of the following:
using tuple assignments
initializing the best and its length with the zero values ("" and 0) and omitting the check for 0 words as the for range handles that properly
no need to store words as a local variable as it is only used in the loop
We lose nothing from readability:
func longestWord(s string) string {
best, length := "", 0
for _, word := range strings.Split(s, " ") {
if len(word) > length {
best, length = word, len(word)
}
}
return best
}
Testing it:
fmt.Printf("%q\n", longestWord(""))
args := "Monday Tuesday Friday Sunday Wednesday"
fmt.Printf("%q\n", longestWord(args))
Output (try it on the Go Playground):
""
"Wednesday"
Most compact solution
Note that storing the length of the best is optional and is purely for optimization purposes, since if we have best, its length is always len(best).
Taking advantage of this, and that we can use named result parameters (and that all variables are initialized to the zero value of their types unless an initial value is provided–which for string is ""), we can even write it more compact, again losing nothing from readability:
func longestWord(s string) (best string) {
for _, word := range strings.Split(s, " ") {
if len(word) > len(best) {
best = word
}
}
return
}
Testing and output is the same, try it on the Go Playground. Again, in most cases this is probably slightly slower compared to when we stored the length too.
That totally works! You could make it a bit shorter, while also using longer variable names that explain a bit more about your intention.
func longestWord(s string) string {
words := strings.Split(s, " ")
if len(words) == 0 {
return ""
}
best := words[0]
best_length := 0
for _, word := range words {
if len(word) > best_length {
best = word
best_length = len(word)
}
}
return best
}
You could change this to track a pointer instead of the word itself if you like.
I would do it like this:
func longestWord(s string) string {
newArr := strings.Split(s, " ")
longestWord := ""
longestLength := 0
// loop through the array
for _, word := range newArr {
// save length of word in the actual iteration
length := len(word)
// if length is larger, than longest
if length > longestLength {
// save the new longest word
longestWord = word
longestLength = length
}
}
// return the longest word
return longestWord
}
Implementation can be found on the go playground
So I got stuck on a coding challenge that I almost knew the answer too. And I think I have to use the subString call in Swift 4 to get it 100%. I want to reverse every OTHER word in a string, but ignore or keep the punctuation in its original place( index ).
var sample = "lets start. And not worry about proper sentences."
func reverseString(inputString: String) -> String {
let oldSentence = sample.components(separatedBy: " ")
var newSentence = ""
for index in 0...oldSentence.count - 1 {
let word = oldSentence[index]
if newSentence != "" {
newSentence += " "
}
if index % 2 == 1 {
let reverseWord = String(word.reversed())
newSentence += reverseWord
} else {
newSentence += word
}
}
return newSentence
}
reverseString(inputString: sample)
And this would be the expected output.
"lets trats. And ton worry tuoba proper secnetnes."
Notice the punctuation is not reversed.
You shouldn't use components(separatedBy: ) to split a string in words. See this article for the reason. Use enumerateSubstrings and pass in the appropriate option:
func reverseString(inputString: String) -> String {
var index = 1
var newSentence = inputString
inputString.enumerateSubstrings(in: inputString.startIndex..., options: .byWords) { substr, range, _, stop in
guard let substr = substr else { return }
if index % 2 == 0 {
newSentence = newSentence.replacingCharacters(in: range, with: String(substr.reversed()))
}
index += 1
}
return newSentence
}
print(reverseString(inputString: "lets start. And not worry about proper sentences."))
// lets trats. And ton worry tuoba proper secnetnes.
print(reverseString(inputString: "I think, therefore I'm"))
// I kniht, therefore m'I
InputOutput
abc abc___
a a___
abcdeabcde_
Attempt
package main
import "fmt"
import "unicode/utf8"
func main() {
input := "abc"
if utf8.RuneCountInString(input) == 1 {
fmt.Println(input + "_____")
} else if utf8.RuneCountInString(input) == 2 {
fmt.Println(input + "____")
} else if utf8.RuneCountInString(input) == 3 {
fmt.Println(input + "___")
} else if utf8.RuneCountInString(input) == 4 {
fmt.Println(input + "__")
} else if utf8.RuneCountInString(input) == 5 {
fmt.Println(input + "_")
} else {
fmt.Println(input)
}
}
returns
abc___
Discussion
Although the code is creating the expected output, it looks very verbose and devious.
Question
Is there a concise way?
The strings package has a Repeat function, so something like
input += strings.Repeat("_", desiredLen - utf8.RuneCountInString(input))
would be simpler. You should probably check that desiredLen is smaller than inpult length first.
You can also do this efficiently without loops and "external" function calls, by slicing a prepared "max padding" (slice out the required padding and simply add it to the input):
const max = "______"
func pad(s string) string {
if i := utf8.RuneCountInString(s); i < len(max) {
s += max[i:]
}
return s
}
Using it:
fmt.Println(pad("abc"))
fmt.Println(pad("a"))
fmt.Println(pad("abcde"))
Output (try it on the Go Playground):
abc___
a_____
abcde_
Notes:
len(max) is a constant (because max is a constant): Spec: Length and capacity:
The expression len(s) is constant if s is a string constant.
Slicing a string is efficient:
An important consequence of this slice-like design for strings is that creating a substring is very efficient. All that needs to happen is the creation of a two-word string header. Since the string is read-only, the original string and the string resulting from the slice operation can share the same array safely.
You could just do input += "_" in a cycle, but that would allocate unnecessary strings. Here is a version that doesn't allocate more than it needs:
const limit = 6
func f(s string) string {
if len(s) >= limit {
return s
}
b := make([]byte, limit)
copy(b, s)
for i := len(s); i < limit; i++ {
b[i] = '_'
}
return string(b)
}
Playground: http://play.golang.org/p/B_Wx1449QM.
Let's say for example that I have one string, like this:
<h1>Hello World!</h1>
What Go code would be able to extract Hello World! from that string? I'm still relatively new to Go. Any help is greatly appreciated!
If the string looks like whatever;START;extract;END;whatever you can use this which will get the string in between:
// GetStringInBetween Returns empty string if no start string found
func GetStringInBetween(str string, start string, end string) (result string) {
s := strings.Index(str, start)
if s == -1 {
return
}
s += len(start)
e := strings.Index(str[s:], end)
if e == -1 {
return
}
e += s + e - 1
return str[s:e]
}
What happens here is it will find first index of START, adds length of START string and returns all that exists from there until first index of END.
There are lots of ways to split strings in all programming languages.
Since I don't know what you are especially asking for I provide a sample way to get the output
you want from your sample.
package main
import "strings"
import "fmt"
func main() {
initial := "<h1>Hello World!</h1>"
out := strings.TrimLeft(strings.TrimRight(initial,"</h1>"),"<h1>")
fmt.Println(out)
}
In the above code you trim <h1> from the left of the string and </h1> from the right.
As I said there are hundreds of ways to split specific strings and this is only a sample to get you started.
Hope it helps, Good luck with Golang :)
DB
I improved the Jan Kardaš`s answer.
now you can find string with more than 1 character at the start and end.
func GetStringInBetweenTwoString(str string, startS string, endS string) (result string,found bool) {
s := strings.Index(str, startS)
if s == -1 {
return result,false
}
newS := str[s+len(startS):]
e := strings.Index(newS, endS)
if e == -1 {
return result,false
}
result = newS[:e]
return result,true
}
Here is my answer using regex. Not sure why no one suggested this safest approach
package main
import (
"fmt"
"regexp"
)
func main() {
content := "<h1>Hello World!</h1>"
re := regexp.MustCompile(`<h1>(.*)</h1>`)
match := re.FindStringSubmatch(content)
if len(match) > 1 {
fmt.Println("match found -", match[1])
} else {
fmt.Println("match not found")
}
}
Playground - https://play.golang.org/p/Yc61x1cbZOJ
In the strings pkg you can use the Replacer to great affect.
r := strings.NewReplacer("<h1>", "", "</h1>", "")
fmt.Println(r.Replace("<h1>Hello World!</h1>"))
Go play!
func findInString(str, start, end string) ([]byte, error) {
var match []byte
index := strings.Index(str, start)
if index == -1 {
return match, errors.New("Not found")
}
index += len(start)
for {
char := str[index]
if strings.HasPrefix(str[index:index+len(match)], end) {
break
}
match = append(match, char)
index++
}
return match, nil
}
Read up on the strings package. Have a look into the SplitAfter function which can do something like this:
var sample = "[this][is my][string]"
t := strings.SplitAfter(sample, "[")
That should produce a slice something like: "[", "this][", "is my][", "string]". Using further functions for Trimming you should get your solution. Best of luck.
func Split(str, before, after string) string {
a := strings.SplitAfterN(str, before, 2)
b := strings.SplitAfterN(a[len(a)-1], after, 2)
if 1 == len(b) {
return b[0]
}
return b[0][0:len(b[0])-len(after)]
}
the first call of SplitAfterN will split the original string into array of 2 parts divided by the first found after string, or it will produce array containing 1 part equal to the original string.
second call of SplitAfterN uses a[len(a)-1] as input, as it is "the last item of array a". so either string after after or the original string str. the input will be split into array of 2 parts divided by the first found before string, or it will produce array containing 1 part equal to the input.
if after was not found than we can simply return b[0] as it is equal to a[len(a)-1]
if after is found, it will be included at the end of b[0] string, therefore you have to trim it via b[0][0:len(b[0])-len(after)]
all strings are case sensitive
I'm trying to alter an existing string in Go but I keep getting this error "cannot assign to new_str[i]"
package main
import "fmt"
func ToUpper(str string) string {
new_str := str
for i:=0; i<len(str); i++{
if str[i]>='a' && str[i]<='z'{
chr:=uint8(rune(str[i])-'a'+'A')
new_str[i]=chr
}
}
return new_str
}
func main() {
fmt.Println(ToUpper("cdsrgGDH7865fxgh"))
}
This is my code, I wish to change lowercase to uppercase but I cant alter the string. Why? How can I alter it?
P.S I wish to use ONLY the fmt package!
Thanks in advance.
You can't... they are immutable. From the Golang Language Specification:
Strings are immutable: once created, it is impossible to change the contents of a string.
You can however, cast it to a []byte slice and alter that:
func ToUpper(str string) string {
new_str := []byte(str)
for i := 0; i < len(str); i++ {
if str[i] >= 'a' && str[i] <= 'z' {
chr := uint8(rune(str[i]) - 'a' + 'A')
new_str[i] = chr
}
}
return string(new_str)
}
Working sample: http://play.golang.org/p/uZ_Gui7cYl
Use range and avoid unnecessary conversions and allocations. Strings are immutable. For example,
package main
import "fmt"
func ToUpper(s string) string {
var b []byte
for i, c := range s {
if c >= 'a' && c <= 'z' {
if b == nil {
b = []byte(s)
}
b[i] = byte('A' + rune(c) - 'a')
}
}
if b == nil {
return s
}
return string(b)
}
func main() {
fmt.Println(ToUpper("cdsrgGDH7865fxgh"))
}
Output:
CDSRGGDH7865FXGH
In Go strings are immutable. Here is one very bad way of doing what you want (playground)
package main
import "fmt"
func ToUpper(str string) string {
new_str := ""
for i := 0; i < len(str); i++ {
chr := str[i]
if chr >= 'a' && chr <= 'z' {
chr = chr - 'a' + 'A'
}
new_str += string(chr)
}
return new_str
}
func main() {
fmt.Println(ToUpper("cdsrgGDH7865fxgh"))
}
This is bad because
you are treating your string as characters - what if it is UTF-8? Using range str is the way to go
appending to strings is slow - lots of allocations - a bytes.Buffer would be a good idea
there is a very good library routine to do this already strings.ToUpper
It is worth exploring the line new_str += string(chr) a bit more. Strings are immutable, so what this does is make a new string with the chr on the end, it doesn't extend the old string. This is wildly inefficient for long strings as the allocated memory will tend to the square of the string length.
Next time just use strings.ToUpper!