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 8622609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:05:57+00:00 2026-06-12T07:05:57+00:00

I am suppose to write a program that returns the shortest distance from point

  • 0

I am suppose to write a program that returns the shortest distance from point A to point E. I coded to get the length, but I cant figure out how to actually get the points.

enter image description here

d  = {("A","A"):0, ("A","B"):1, ("A","C"):3, ("A","D"):7 , ("A","E"):101,
           ("B","A"):101, ("B","B"):0, ("B","C"):42, ("B","D"):6, ("B","E"):27,
           ("C","A"):101, ("C","B"):101, ("C","C"):0, ("C","D"):2, ("C","E"):13,
           ("D","A"):101, ("D","B"):101, ("D","C"):101, ("D","D"):0, ("D","E"):5,
           ("E","A"):101, ("E","B"):101, ("E","C"):101, ("E","D"):101, ("E","E"):0
    }

def shortestPath(Cities,Distances):
'''Returns the length of the shortest path from the first city in the list to the last city in the list, using only cities that appear in that list.'''
    if len(Cities)==1: return 0
    else: return min( map( lambda n: (Distances[Cities[0],Cities[n]] + shortestPath(Cities[n:],Distances)), range(1,len(Cities))) )

The answer to the input: shortestPath([“A”,”B”, “C”, “D”, “E”],d) is 10. But the program should also out put the distances, so the answer should actually be [10, [“A”, “C”, “D”, “E”]]

  • 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-06-12T07:05:58+00:00Added an answer on June 12, 2026 at 7:05 am

    If you are determined to keep it in one meaty line, you can make a small change to your code:

    def track_past_city(x,y):
        return (x[0]+y[0],x[1:]+y[1:]) #0 is how far you've gone, #[1:] is where you've been
    
    def shortestPath(Cities,Distances):
        if len(Cities)==1: return 0, Cities[0]
        else: return min( map( lambda n: (track_past_city((Distances[Cities[0],Cities[n]],Cities[0]),shortestPath(Cities[n:],Distances))), range(1,len(Cities))) )
    
    
    shortestPath(["A","B", "C", "D", "E"],d)
    # (10, ('A', ('C', ('D', 'E'))))
    

    I’m not quite sure how to where the erroneous tuple addition occurs, but from this you should be able to tweak your own solution…

    NOTE: This comes with a health warning, you shouldn’t write all your code on one line, it’s hard to read, hard to debug and generally a bad idea.

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

Sidebar

Related Questions

I am supposed to write a program that gets some PNG images from the
Hi I suppose to write a program that show the run time complixity I
I need to write a program that gets a string from the user and
I am supposed to write a program that should read from input numbers in
I'm trying to write a program in Scala, that will accept SOAP-requests, get the
I am suppose to write a program using python's turtle that creates a tree
Suppose I want to write an app for Android OS that is not going
I'm dealing with a problem in a kernel module that get data from userspace
Background: my assignment was to write a program that uses linked list to keep
I have to write a program that will essentially sort nodes in a linked

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.