plutov / practice-go Goto Github PK
View Code? Open in Web Editor NEWPractice Go: a collection of Go programming challenges
Practice Go: a collection of Go programming challenges
lastlettergame_test.go
- checks only 1 possible solution:
But solutions can be more than 1.
Here is possible examples:
[machamp pinsir registeel landorus snivy yamask kricketune emboar remoraid darmanitan nosepass simisear rufflet trapinch heatmor relicanth haxorus seaking girafarig gabite exeggcute emolga audino]
[machamp petilil landorus snivy yamask kricketune emboar registeel loudred darmanitan nosepass simisear rufflet trapinch heatmor relicanth haxorus seaking girafarig gabite exeggcute emolga audino]
[machamp petilil loudred darmanitan nosepass snivy yamask kricketune exeggcute emboar rufflet trapinch haxorus simisear relicanth heatmor registeel landorus seaking girafarig gabite emolga audino]
But test expected only this result:
[machamp petilil landorus scrafty yamask kricketune emboar registeel loudred darmanitan nosepass simisear relicanth heatmor rufflet trapinch haxorus seaking girafarig gabite exeggcute emolga audino]
Here is the code that can produce the correct random result. Here is the first solution that came to the heads, sorry I didn't optimize the code, the main task was to solve.
package lastlettergame
func Sequence(dic []string) []string {
var deepest *Word
dict := SliceToMap(dic)
cnt := 0
for _, word := range dic {
delete(dict, word)
t := GetDeepest(nil, word, dict)
cnt++
if t != nil && (deepest == nil || deepest.Lvl < t.Lvl) {
deepest = t
}
dict[word] = true
}
if deepest == nil {
return []string{}
}
result := make([]string, deepest.Lvl)
for node := deepest; node != nil; node = node.Prev {
result[node.Lvl-1] = node.Value
}
return result
}
type Word struct {
Value string
Lvl int
Prev *Word
}
func SliceToMap(source []string) map[string]bool {
result := make(map[string]bool, len(source))
for _, word := range source {
result[word] = true
}
return result
}
func GetDeepest(prev *Word, main string, dic map[string]bool) *Word {
lvl := 1
if prev != nil {
lvl = prev.Lvl + 1
}
current := &Word{Value: main, Lvl: lvl, Prev: prev}
var deepest *Word
for word := range dic {
if !dic[word] {
continue
}
if CanBeNext([]rune(main), []rune(word)) {
dic[word] = false
t := GetDeepest(current, word, dic)
if t != nil && (deepest == nil || deepest.Lvl < t.Lvl) {
deepest = t
}
dic[word] = true
}
}
if deepest == nil {
deepest = current
}
return deepest
}
func CanBeNext(word, next []rune) bool {
if len(word) < 2 || len(next) < 2 {
return false
}
return word[len(word)-1] == next[0]
}
Issue to solve this challenge - https://github.com/plutov/practice-go/tree/master/node_degree
{"123", 3, []string{"1", "2", "3", "11", "12", "13", "22", "23", "33", "111", "112", "113", "122", "123", "133", "222", "223", "233", "333"}},
this expected value misses a lot of values, like 121
The actual result generated by my implementation:
[1 11 111 112 113 12 121 122 123 13 131 132 133 2 21 211 212 213 22 221 222 223 23 231 232 233 3 31 311 312 313 32 321 322 323 33 331 332 333]
Often we have >1 good solutions, I want to keep them all in the repo and don't lose. So we need a way to submit all possible working solutions.
It's very slow and generates HTTP 500 and other errors. You'll lose some pictures even when obeying the quota of 1000 requests/h. After a while it stopped working at all for me. (While writing this I tested again: works again but errors remain)
API is unreliable but downloading the linked pictures in the API data works like a charm. I already used https://apod.nasa.gov/apod/archivepix.html to extract the dates needed to query the API. I think it could be easier to scrape the needed URLs out of the linked pages and not use the API at all. No quota involved. Just download +8500 files in one batch.
PS: I used this to slowly hammer the API:
cat dates04.txt | xargs -I{} curl https://api.nasa.gov/planetary/apod?api_key=<mykey>\&date={}
I have 11 dates??.txt files with ~800 lines each.
dates04.txt: https://drive.google.com/file/d/1UstQi3wGO5bzDvAd7AnrwmijIZw4zd6H/view?usp=sharing
Hi plutov,
I'm glad you're back with a new challenge.
I want to suggest a minor change:
change:
var smallGraph = [][]int{ []int{1, 2}, []int{1, 3}, }
to:
var smallGraph = [][2]int{ {1, 2}, {1, 3}, }
This makes smallGraph a slice of values (slice elements are pointers in disguise). It assures the values to be fixed arrays with exactly two elements. And the whole data structure is densely packed.
I didn't start the challenge yet but I presume it will be more efficient to juggle with two element arrays instead of arbitrary size slices.
Create multiple gocode.txt files with different content.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.