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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:23:03+00:00 2026-06-15T09:23:03+00:00

I am writing a solver for MasterMind in which I must take in a

  • 0

I am writing a solver for MasterMind in which I must take in a guess and an answer and return some representation of the number of black and white pegs where a black peg represents a correct color in the correct spot and a white peg represents a correct color in the incorrect spot. I have to run this code for about 2 million iterations and so it needs to be as fast as possible. Currently the biggest time sinks of this are the split and index calls, but I’m not sure how to remove them. Any ideas on how to make this code run faster while maintaining its functionality?

def returnPegs(guess, answer):
        guessList = guess.split(" ")
        answerList = answer.split(" ")
        response = ""
        iterator = [0,1,2,3]
        for i in iterator:
            if answerList[i] == guessList[i]:
                response = response + "B"
                guessList[i] = "alsoNotAColor"
                answerList[i] = "notAColor"
        for j in iterator:
            if guessList[j] in answerList:
                response = response + "W"
                answerList[answerList.index(guessList[j])] = "notAColor"
                guessList[j] = "alsoNotAColor"
        return response

To ensure clarity. My input is a string of four colors separated by spaces, and my output doesn’t have to have any particular form so long as it is unique for every combination of black and white pegs.

After some heavy optimization This is the current state of the code:

 def returnPegs(guess, answer):
    pegs = 0 
    for answerPeg, guessPeg in zip(answer, guess):
        if answerPeg == guessPeg:
            pegs += 5
        elif guessPeg in answer:
            pegs +=1
    return pegs

Optimizing further, kinda going back to some original code, this version is actually the fastest of them all. By about a factor of 4 over the first and a factor of 2 over the second.

def returnPegs(guess, answer):
    response = 0
    iterator = [0,1,2,3]
    for i in iterator:
        if guess[i] == answer[i]:
            response += 5
            guess[i] = "alsoNotAColor"
            answer[i] = "notAColor"
        elif guess[i] in answer:
            response += 1
            answer[answer.index(guess[i])] = "notAColor"
            guess[i] = "alsoNotAColor"
    return response
  • 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-15T09:23:04+00:00Added an answer on June 15, 2026 at 9:23 am

    If I’m reading your question correctly, this code should do what you need:

    def returnPegs(guess, answer):
        guesses = guess.split()
        answers = answer.split()
    
        pegs = []
    
        for answer, guess in zip(answers, guesses):
            if answer == guess:
                pegs.append('B')
            elif guess in answers:
                pegs.append('W')
    
        return ''.join(pegs)
    

    zip() zips together two sequences:

    >>> a = [1, 2, 3, 4]
    >>> b = [5, 6, 7, 8]
    >>> zip(a, b)
    [(1, 5), (2, 6), (3, 7), (4, 8)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a beginning programmer. I've been writing codes on a mastermind solver in
Hi and thanks in advance, I am writing a Sudoku Solver which uses OCR
I am writing a program in C to solve an optimisation problem, for which
Writing documentation in html requires some code examples. What to do with characters that
I am writing a solver for the N-Puzzle (see http://en.wikipedia.org/wiki/Fifteen_puzzle ) Right now I
I'm learning basics of C and writing a simple first order equation solver. I
As a programming exercise, I just finished writing a Sudoku solver that uses the
I'm writing a small sudoku game/solver in Linux using python with TUI (not GUI,
I am writing a program in C (a 2d poisson solver) and I am
I am writing a sudoku solver. It has been a long time since I

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.