Python Challenge: Bagels

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 5464 Views - Last Post: 13 January 2013 - 12:26 PM Rate Topic: -----

#1 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 317
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Python Challenge: Bagels

Post icon  Posted 02 January 2013 - 01:29 PM

This week's Python Challenge is aimed for those of you nursing sore heads from New Year's Eve Festivities. That is, it's not extremely challenging. However, it's got a few little hurdles to get over, so it should be a good way to occupy your fuzzy brain while you knock back a few glasses of the hair of the dog what bit you.

The challenge is to implement the game "Bagels" as described in David Ahl's collection of "Basic Games". A link to the original article, with BASIC source code is provided, in case that might be helpful. :)

The game is simplicity itself:

The computer picks a three-digit number. The player's task is to guess the number by submitting three-digit guesses. The computer provides feedback thusly:

- If the guess is entirely correct, the player wins.
- If the guess includes at least one digit that is in the correct place, the computer reports "Fermi".
- If the guess includes at least one digit contained in the target number, but it's in the wrong place, the computer reports "Pico".
- If the guess is entirely wrong, the computer reports "Bagels".

Only one response will be provided for a given guess, so if the target is 123 and the guess is 132, the response will be "Fermi" and not "Fermi, Pico".

You get twenty guesses.

That's it.

Your first task is to implement the game for a human solver. The computer will pick a number, and the human player will determine what it was.

For extra sport, write a computer that can zero in on a number you provide. How many guesses does your computer player require to find the correct number?

Hint
Be careful of how you handle leading zeros in the input. If you convert the input to integer form, be careful not to lose any information, or at least to ensure you recover the information you lose.

This post has been edited by Simown: 05 January 2013 - 05:25 PM


Is This A Good Question/Topic? 3
  • +

Replies To: Python Challenge: Bagels

#2 fashionnugget  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 163
  • Joined: 25-November 08

Re: Python Challenge: Bagels

Posted 02 January 2013 - 05:42 PM

Why not? I haven't posted in like a year.

Spoiler

Was This Post Helpful? 2
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10183
  • View blog
  • Posts: 37,596
  • Joined: 27-December 08

Re: Python Challenge: Bagels

Posted 02 January 2013 - 06:01 PM

Here is my solution for the basic stage (in Java). Now to get to work on the AI portion.

Spoiler

Was This Post Helpful? 2
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1275
  • View blog
  • Posts: 2,837
  • Joined: 05-April 11

Re: Python Challenge: Bagels

Posted 02 January 2013 - 07:53 PM

View Postmacosxnerd101, on 03 January 2013 - 01:01 AM, said:

Here is my solution for the basic stage (in Java). Now to get to work on the AI portion.

Spoiler


If you make an interface for the AI's to implement, then I will happily use your example and make an AI to it ^^
Was This Post Helpful? 1
  • +
  • -

#5 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 317
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Python Challenge: Bagels

Posted 05 January 2013 - 02:03 PM

Oh no, what have I done? First section done in Python with some nice little tricks for a bit of fun. Works fine but could do with some better error-checking really.

Spoiler

This post has been edited by Simown: 05 January 2013 - 03:51 PM

Was This Post Helpful? 0
  • +
  • -

#6 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,672
  • Joined: 13-March 10

Re: Python Challenge: Bagels

Posted 05 January 2013 - 02:13 PM

Is it common in Python to return different data types? Like here, you return 0 under one condition, otherwise you return a string.

This post has been edited by darek9576: 05 January 2013 - 02:15 PM

Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: Python Challenge: Bagels

Posted 05 January 2013 - 02:26 PM

Python is Dynamic language, you can return whatever you want.
Was This Post Helpful? 0
  • +
  • -

#8 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 317
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Python Challenge: Bagels

Posted 05 January 2013 - 03:38 PM

View Postdarek9576, on 05 January 2013 - 09:13 PM, said:

Is it common in Python to return different data types? Like here, you return 0 under one condition, otherwise you return a string.


You can, but I don't think it's common - I'd say it certainly wasn't good practice. In Python all non-empty strings are going to evaluate to True (like most languages?) and the 0 to False, which means that the "You win!" will print only if 0 is returned. I could have substituted the empty string in there (which probably would make more sense to maintain the same data type)

Honestly, it's not the kind of code you'd want to maintain or actually use, just showing off some Pythonisms :)

This post has been edited by Simown: 05 January 2013 - 04:55 PM

Was This Post Helpful? 1
  • +
  • -

#9 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,672
  • Joined: 13-March 10

Re: Python Challenge: Bagels

Posted 05 January 2013 - 03:51 PM

I know Python allows to return different types but my question was whether it's a good practice. Thanks for the answer.
Was This Post Helpful? 0
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: Python Challenge: Bagels

Posted 05 January 2013 - 05:54 PM

A VB.net version for comparison, using Event to decouple to output.
Uses the iterator feature for input.
Spoiler

This post has been edited by AdamSpeight2008: 05 January 2013 - 05:54 PM

Was This Post Helpful? 1
  • +
  • -

#11 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1131
  • View blog
  • Posts: 2,484
  • Joined: 05-May 05

Re: Python Challenge: Bagels

Posted 05 January 2013 - 10:06 PM

A Scala version.

Spoiler


If you know Java and you want to try to implement the auto solver, do the following:

Spoiler

This post has been edited by blackcompe: 07 January 2013 - 09:17 PM

Was This Post Helpful? 1
  • +
  • -

#12 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1131
  • View blog
  • Posts: 2,484
  • Joined: 05-May 05

Re: Python Challenge: Bagels

Posted 07 January 2013 - 09:16 PM

I can't find any information on a standard algorithm to solve this problem. I believe that anything I'd implement would solve it in about 15 steps on average, and that's just coding the logic I'd use to solve it. Any thoughts?
Was This Post Helpful? 0
  • +
  • -

#13 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 317
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Python Challenge: Bagels

Posted 08 January 2013 - 09:06 AM

I wouldn't think there was a standard algorithm either. I've been playing with this and I can't make a correct guess *every* time yet, is the one you are thinking of more reliable? That's probably the best algorithm you could get for this problem? I wouldn't know either.
Was This Post Helpful? 0
  • +
  • -

#14 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1131
  • View blog
  • Posts: 2,484
  • Joined: 05-May 05

Re: Python Challenge: Bagels

Posted 08 January 2013 - 04:40 PM

Quote

I wouldn't think there was a standard algorithm either. I've been playing with this and I can't make a correct guess *every* time yet, is the one you are thinking of more reliable?


No, my algorithm wouldn't solve it every time.
Was This Post Helpful? 0
  • +
  • -

#15 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 317
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Python Challenge: Bagels

Posted 09 January 2013 - 07:51 AM

I think basically, there is too many possibilities before you can hone in on a number, this challenge is actually slightly different from the BASIC one, as Jon made the restriction only one answer can be returned i.e. Bagels, Pico or Fermi not multiple clues - if you could return more than one "hint", you could make a better guess I assume.

However, I don't think this can be algorithmically solved anyway, it's just mimicking human logic as you say. The BASIC challenge says, assuming you can return Pico or Fermi for each digit or Bagels you should be able to solve every one in 8 moves or less. I'm going to try it that way and see where I get :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2