Python Forum
Correct number wrong position func.
Thread Rating:
  • 4 Vote(s) - 2.25 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Correct number wrong position func.
#6
Quote:Albry wrote:

Im Sorry, this is a little advanced for me. I understand that what you have included is running through and matching what I needed but I'm still not sure on how to make it work. Would I be setting list1 as my 'guess' variable and list2 as 'question' ?

I misunderstood the original question.

I thought you were trying to show number of correct items in (guess history) list, as well as those in correct positions.
the example I showed (although not relevant now that I understand the question) was if you had a random list and a guess history list
as in a mastermind type of game, so example was:

Output:
random_list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] guesses: [4, 8, 2, 0, 5, ...] |____________ Correct Number, exact position

so all guesses are correct, but only one (5) is in the correct position, so

def show_correct_guess(random_list, guesses, exact=True):
    if exact:
        return [b for a, b in zip(random_list, guesses) if b == a]
    else:
        return [b for a, b in zip(random_list, guesses) if b != a]
would return [4, 8, 2, 0] if exact is False
and [5] if exact is True
Reply


Messages In This Thread
Correct number wrong position func. - by albry - Jan-10-2019, 10:01 AM
RE: Correct number wrong position func. - by albry - Jan-11-2019, 10:58 AM
RE: Correct number wrong position func. - by Larz60+ - Jan-11-2019, 04:01 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to output one value per request of the CSV and print it in another func? Student44 3 1,319 Nov-11-2022, 10:45 PM
Last Post: snippsat
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,526 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,624 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  wrong forum, please delete. reposted in correct forum. woodmister 0 1,581 Jan-04-2021, 11:17 PM
Last Post: woodmister
  Func Animation not displaying my live graph jotalo 0 1,551 Nov-13-2020, 10:56 PM
Last Post: jotalo
  Trying to write func("abcd") -> "abbcccdddd" omm 8 4,080 Oct-24-2020, 03:41 AM
Last Post: bowlofred
  How can i judge 1st string position is correct number christing 3 2,403 Oct-30-2019, 03:32 AM
Last Post: newbieAuggie2019
  python gives wrong string length and wrong character thienson30 2 2,983 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  Number bug, calculation is wrong d3fi 2 2,442 Aug-27-2019, 05:29 AM
Last Post: perfringo
  call func from dict mcmxl22 3 2,852 Jun-21-2019, 05:20 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020