URL to challenge:
This puzzle isn't too difficult. Having said that though, my solution isn't too elegant. I generally do all of these puzzles/challenges in PHP, which is a language I know through and through, however I've decided to start using Python instead. There's no doubt features of the language and functions I'm not properly utilising, but in time I hope to remedy this.Code:http://www.facebook.com/careers/puzzles.php?puzzle_id=20
If you have a solution, please post it, as I'd love to read through it.
Spoiler:Code:#!/usr/bin/python import sys import re # Ensure we have the proper amount of arguments if len(sys.argv) < 2: print "Please pass through a file to open" sys.exit(0) # Let's open the file filename = sys.argv[1] try: file = open(filename,"r").read() except: print "An error has occured when opening the file" # Let's parse the data in to a dictionary lines = file.split("\n") people = {} pointer = 1 # Where we currently are while len(people) < int(lines[0].strip()): (person, amount) = re.sub(" \s+", " ", lines[pointer].strip()).split(" ") amount = int(amount) people[person] = [] for x in range(1, amount + 1): pointer += 1 people[person].append(lines[pointer].strip()) pointer += 1 # Sort out the liars from the ones telling truth (truth, liars) = ([],[]) # Now let's go through and see if someone is telling the truth def tellingTruth(name, trail=[]): # If we've already determined whether or not they're liars/telling truth if name in truth: return len(trail) % 2 != 0 elif name in liars: return len(trail) % 2 == 0 # Loop through if name not in trail: trail.append(name) for i in range(0, len(people[name])): if people[name][i] in trail: #we've already checked if len(trail) <= 2: return None else: return ( len(trail) - 1 ) % 2 == 0 else: trail.append(people[name][i]) return tellingTruth(people[name][i], trail) # Let's go through and determine who's lying and who isnt for i in range(0,len(people)): isTruth = tellingTruth(people.keys()[i], []) if isTruth == True: truth.append(people.keys()[i]) elif isTruth == False: liars.append(people.keys()[i]) # Have a preliminary list, let's see if that solves for person in truth: subjects = people[person] for subject in subjects: if subject not in liars: liars.append(subject) for person in liars: subjects = people[person] for subject in subjects: if subject not in truth: truth.append(subject) # Let's now output if len(truth) > len(liars): print str(len(truth)) + " " + str(len(liars)) else: print str(len(liars)) + " " + str(len(truth))
Results 1 to 13 of 13
Threaded View
- 04 Dec. 2011 10:02am #1
Facebook Puzzle Challenge - Liar Liar - Difficulty Easy
Last edited by Artificial; 09 Dec. 2011 at 11:59pm.