Giter Club home page Giter Club logo

practice-go's Issues

lastlettergame - tests only 1 possible solution

lastlettergame_test.go - checks only 1 possible solution:
But solutions can be more than 1.

Here is possible examples:

  1. [machamp pinsir registeel landorus snivy yamask kricketune emboar remoraid darmanitan nosepass simisear rufflet trapinch heatmor relicanth haxorus seaking girafarig gabite exeggcute emolga audino]
  2. [machamp petilil landorus snivy yamask kricketune emboar registeel loudred darmanitan nosepass simisear rufflet trapinch heatmor relicanth haxorus seaking girafarig gabite exeggcute emolga audino]
  3. [machamp petilil loudred darmanitan nosepass snivy yamask kricketune exeggcute emboar rufflet trapinch haxorus simisear relicanth heatmor registeel landorus seaking girafarig gabite emolga audino]
  4. And more...

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]
}

Not all permutations are presented for the short-hash

{"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]

Support multiple solutions

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.

nasacollage: nasa api is unreliable

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

nodedegree: minor simplifications

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.