GithubHelp home page GithubHelp logo

ypapax / quiz Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jameskennethrobinson/quiz

0.0 2.0 0.0 865 KB

find the longest compound-word in the list - golang job interview task

License: The Unlicense

SaltStack 96.08% Go 3.92%

quiz's Introduction

quiz

Q: Given a list of words like https://github.com/NodePrime/quiz/blob/master/word.list find the longest compound-word in the list, which is also a concatenation of other sub-words that exist in the list. The program should allow the user to input different data. The finished solution shouldn't take more than one hour. Any programming language can be used, but Go is preferred.

Fork this repo, add your solution and documentation on how to compile and run your solution, and then issue a Pull Request.

Obviously, we are looking for a fresh solution, not based on others' code.

Go Solution

Build

go test && go build

Run

$ ./quiz /path/to/word.list
inputted words count 263534
The longest compound word is
antidisestablishmentarianisms
It has 29 characters
It consists of the following words:
[anti disestablishmentarian isms]

Algorithm

  • Since we want the biggest compound word, sort all the words by length - biggest goes first
  • For each word (compoundWordCandidate) starting from the first (biggest), find other words - its substrings and put them to subWords slice.
  for _, compoundWordCandidate := range sortedWords {
    var subWords []string
    for _, partWordCandidate := range sortedWords {
      if compoundWordCandidate == partWordCandidate {
        continue
      }
      if !strings.Contains(compoundWordCandidate, partWordCandidate) {
        continue
      }
      subWords = append(subWords, partWordCandidate)
      validParts := getCompoundParts(compoundWordCandidate, subWords)
      if len(validParts) == 0 {
        continue
      }
      compound = compoundWordCandidate
      parts = validParts
      return
    }
  }
  • After every append to subWords, check if the slice has length more than 1 and try to build the current compoundWordCandidate from subWords using method getCompoundParts. If it returns positive length slice, that's it. Current compoundWordCandidate is the result of the program - the longest compound word. It consists of validParts.

  • Function func getCompoundParts(wholeWord string, parts []string) (validParts []string) cuts off beginning and end of wholeWord reducing number of possible parts by 2 and calls itself with shorter wholeWord and lower length parts until has trivial cases when parts length is one or two.

Solution author

Maxim Yefremov upwork profile

quiz's People

Contributors

bobbae avatar ypapax avatar

Watchers

James Cloos avatar  avatar

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.