Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Python: Find Character Exist, but not in correct position

Posted on 2008-10-24
6
Medium Priority
?
688 Views
Last Modified: 2012-06-22
Hello all, I am trying to achieve something like this:

Say the magic word is "aabb"
The user suppose to guess it, however if the user for example typed "abab", it should tell them that one of the "a" is in the right position, while the other is not. How should I go about doing that?

Thanks!

*This is quite urgent, so if someone can reply really fast, that would be great! Thanks!
0
Comment
Question by:Kira-Kun
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
6 Comments
 
LVL 29

Expert Comment

by:pepr
ID: 22794328
How can you proof that it is not a homework? It is not allowed here to give the solutions. However, you can have series of related questions and get the answers that will help you to get "your own" solution (i.e. the one that you really understand well and that you can solve again alone).

Is it a kind of the implementation of the Master Mind game?
0
 
LVL 5

Expert Comment

by:zmo
ID: 22794932
well, first learn python : http://docs.python.org/tutorial/
then for your project, choose the right tools on strings : http://docs.python.org/library/strings.html
and input output (which you'll have learned in the tutorial)
and  on random's usage : http://docs.python.org/library/random.html
so you can generate the aabb or abab or whatever...

now you have all the RTFM you need to start, and we're here to help you if you have *real* questions about python. We're not here to do your homework, but to help you get more knowledge, so I'm following pepr's comment.
0
 

Expert Comment

by:GDR-LDE
ID: 22943341
Would this be enough?
word = "aabb"
guess = "abab"
 
if not len(word) == len(guess):
    print "error"
 
for i in range(len(word)):
    if word[i] == guess[i]:
        print "The letter %s on position %d is correct" % (guess[i], i)
    else:
        print "The letter %s on position %d is not correct" % (guess[i], i)

Open in new window

0
 
LVL 29

Accepted Solution

by:
pepr earned 2000 total points
ID: 22943725
OK. Let's start wit thinking and suggestions. Your solution is too talkative. The messages do not make it more comfortable for the user. Also, we are developing the core using the test case (fixed word and guess). Let's remove the unneccessary things to make it apparent.

Try the snippet below. It does the same using different form of displaying the result.

Now you can see that you are able only to indicate or the correct char on the correct position. However, you can say nothing about the other characters. Only that they are not correct chars on the correct positions. You need third state to indicate that it is the correct character on the bad position. (Not solved here.)

Think how would you do it by hand with paper and the pencil.
word = 'aabb'
guess = 'abab'
result = []
 
for i in range(len(word)):
    if word[i] == guess[i]:
        result.append('x')    # correct letter on the position
    else:
        result.append('-')    # wrong letter on the position
 
# Now print the word and the result formed into one string.
print word
print ''.join(result)

Open in new window

0
 
LVL 29

Assisted Solution

by:pepr
pepr earned 2000 total points
ID: 22943782
Now, more "pythonic" way is to think differently about the for loop than in other languages. Think about for as about iteration through all elements in the container (often the sequence, here the word string). If you need also the counter for the iteration, use the enumerate() function. It returns the tuple (counter, element). The tuple can be assigned into the related number of variables. This way you can write (the same functionality as above)...
word = 'aabb'
guess = 'abab'
result = []
 
for i, c in enumerate(word):
    if c == guess[i]:
        result.append('x')    # correct letter on the position
    else:
        result.append('-')    # wrong letter on the position
 
# Now print the word and the result formed into one string.
print word
print ''.join(result)

Open in new window

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question