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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:32:43+00:00 2026-06-17T11:32:43+00:00

I have converted the following block of codes from a java program. How can

  • 0

I have converted the following block of codes from a java program. How can I write the name of the countries in Map using their names, instead of their IDs?

from collections import defaultdict
colors = ['Red', 'Yellow', 'Green', 'Blue']
mapColors = defaultdict(str)

def okToColor(Map ,country, color):
    for c in Map[country]:
        if mapColors[c] == color: return False
    return True

def explore(Map, country, color):
    if country >= len(Map): return True
    if okToColor(Map, country, color):
        mapColors[country] = color
        for color in colors:
            if explore(Map, country + 1, color): return True
    return False

def printMap():
    for c in mapColors:
        print c, mapColors[c]

Map = [[1, 4, 2, 5], [0, 4, 6, 5], [0, 4, 3, 6, 5], [2, 4, 6],
        [0, 1, 6, 3, 2], [2, 6, 1, 0], [2, 3, 4, 1, 5]]
result = explore(Map, 0, 'Red')
print result
printMap()

I want’t the Map to be a graph like this:

Map = { 'A':['B', 'C'], 'B':['A','D'], ...}

where A, B, C, D are the name of the countries.

  • 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-17T11:32:44+00:00Added an answer on June 17, 2026 at 11:32 am

    The main idea is to define a mapping between countries and numerical indices:

    countries = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
    cindex = dict(zip(countries, range(len(countries))))
    # {'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5, 'G': 6}
    

    Then, with only little changes, you can use your original code. Where before country was a numerical index, now you put cindex[country] when you need the numerical index.

    And when you need to reverse the mapping, countries[index] gives you the string name of the country.


    from collections import defaultdict
    
    colors = ['Red', 'Yellow', 'Green', 'Blue']
    mapColors = defaultdict(str)
    
    
    def okToColor(Map, country, color):
        for c in Map[country]:
            if mapColors[c] == color:
                return False
        return True
    
    
    def explore(Map, country, color):
        if cindex[country] >= len(Map):
            return True
        if okToColor(Map, country, color):
            mapColors[country] = color
            for color in colors:
                try:
                    next_country = countries[cindex[country] + 1]
                except IndexError:
                    return True
                if explore(Map, next_country, color):
                    return True
        return False
    
    
    def printMap():
        for c in mapColors:
            print c, mapColors[c]
    
    countries = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
    cindex = dict(zip(countries, range(len(countries))))
    Map = [[1, 4, 2, 5], [0, 4, 6, 5], [0, 4, 3, 6, 5], [2, 4, 6],
          [0, 1, 6, 3, 2], [2, 6, 1, 0], [2, 3, 4, 1, 5]]
    Map = {countries[idx]: [countries[n] for n in item] for idx,
           item in enumerate(Map)}
    result = explore(Map, 'A', 'Red')
    print(result)
    printMap()
    

    yields

    True
    A Red
    C Yellow
    B Yellow
    E Green
    D Red
    G Blue
    F Green
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have JDBC connection code similiar to the following from the Java JDBC tutorial:
I have converted my Datatable to json string use the following method... public string
I have the following lines showing up in files that have been converted to
I have never used Foxpro before. Can you convert the following Foxpro code into
I have the following code to convert the type of the matrix using cv::Mat.convertTo()
I have the following basic server/client java code, that works when server and client
I have a document that was converted from PDF to HTML for use on
This may well have come up before but the following code is taken from
I have the following Java class: package web; import javax.servlet.*; import javax.servlet.http.*; import java.io.*;
I have the following block of code: let rec Sieve p nums = let

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.