Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6811295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:18:35+00:00 2026-05-26T20:18:35+00:00

I have certain algorithm need to implement. Basically the rules are: The first whitespace-separated

  • 0

I have certain algorithm need to implement. Basically the rules are:

  1. The first whitespace-separated token on a line will be the word being defined.
  2. The later tokens will be the definition. If the definition is “.”, that word is a primitive, i.e., a word with no definition.
  3. The output is a single comma-separated line of text containing each word in the dictionary exactly once. Each word is only to be printed after all of the words in its definition. Note that for certain input sets, there may be multiple valid outputs.

For example the input:

Civic        Honda Car
Honda        Manufacturer
VW           Manufacturer
Manufacturer .
Car          .
Beetle       VW Car

Some Possible Outputs:

Car, Manufactor, Honda, VW, Beetle, Civic
Manufacturer, VW, Car, Beetle, Honda, Civic
Manufacturer, Honda, VW, Car, Beetle, Civic

My implementation:

def defIt(pre, cur):
    # If previous and current strings are the same, no action take
    if pre == cur:
        return pre

    # Split two strings to list
    l_pre = pre.split()
    l_cur = cur.split()

    # If previous string length is shorter than the current string     length, then swap two lists
    if len(l_pre) < len(l_cur):
        l_pre, l_cur = l_cur, l_pre

    found = False


    for j, w_cur in enumerate(l_cur):
        for i, w_pre in enumerate(l_pre):
            if w_pre == w_cur:
                found = True
                return ' '.join(l_cur[j:] + l_cur[:j] + l_pre[:i] + l_pre[(i + 1):])


    if not found:
        return ' '.join(l_cur[1:] + [l_cur[0]] + l_pre)

Just cannot get it right. What do I missing? Thanks a lot.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:18:36+00:00Added an answer on May 26, 2026 at 8:18 pm
    def read_graph(lines):
        g = {}
        for line in lines:
            words = line.split()
            if words[1] == '.':
                words = words[:1]
            g[words[0]] = words[1:]
        return g
    
    def dump_graph(g):
        out = []
        def dump(key):
            for k in g[key]:
                dump(k)
            if key not in out:
                out.append(key)
        for k in g:
            dump(k)
        return out
    

    Usage:

    >>> data = """Civic        Honda Car
    ... Honda        Manufacturer
    ... VW           Manufacturer
    ... Manufacturer .
    ... Car          .
    ... Beetle       VW Car
    ... """
    >>> g = read_graph(data.splitlines())
    >>> g
    {'VW': ['Manufacturer'], 'Civic': ['Honda', 'Car'], 'Car': [],
    'Honda': ['Manufacturer'], 'Beetle': ['VW', 'Car'], 'Manufacturer': []}
    >>> dump_graph(g)
    ['Manufacturer', 'VW', 'Honda', 'Car', 'Civic', 'Beetle']
    >>>
    

    Every word in the resulting list comes after all its “definition” words.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a need to write code that will prorate a value across a
I need help selecting or creating a clustering algorithm according to certain criteria. Imagine
I'm looking for a way to have to write and maintain a certain algorithm
I have certain strings which contain special characters so they can not be shared
I have certain PHP class methods that access external variables. These variables are not
I have certain panels on my page that are hidden under certain circumstances. For
We have certain configuration files which we want to be in version control as
I have certain critical bash scripts that are invoked by code I don't control,
I have certain initializing functions that I use to set up audit logging on
Can I have certain settings that are universal for all my users?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.