Você está na página 1de 7

''' Facebook hiring sample test There are K pegs.

Each peg can hold discs in decreasing order of radius when looked from bottom to top of the peg. There are N discs which have radius 1 to N; Given the initial configuration of the pegs and the final configuration of the pegs, output the moves required to transform from the initial to final configuration. You are required to do the transformations in minimal number of moves. A move consists of picking the topmost disc of any one of the pegs and placing it on top of anyother peg. At anypoint of time, the decreasing radius property of all the pegs must be maintained. Constraints: 1<= N<=8 3<= K<=5 Input Format: NK 2nd line contains N integers. Each integer in the second line is in the range 1 to K where the i-th integer denotes the peg to which disc of radius i is present in the initial configuration. 3rd line denotes the final configuration in a format similar to the initial configuration. Output Format: The first line contains M - The minimal number of moves required to complete the transformation. The following M lines describe a move, by a peg number to pick from and a peg number to place on. If there are more than one solutions, it's sufficient to output any one of them. You can assume, there is always a solution with less than 7 moves and the initial confirguration will not be same as the final one. Sample Input #00: 23 11 22 Sample Output #00: 3 13 12 32 Sample Input #01:

64 424311 111111 Sample Output #01: 5 3 4 4 2 3 1 3 1 1 1

NOTE: You need to write the full code taking all inputs are from stdin and outputs to stdout If you are using "Java", the classname is "Solution" ''' import sys import itertools from collections import deque class Node(object): def __init__(self, parent, last_move, config_str): self.parent = parent self.last_move = last_move self.config_str = config_str def next_configs(self, k_pegs, permutations): pegs = config_to_pegs(self.config_str, k_pegs) nexts = [] for frm, to in permutations: if pegs[frm] != [] and (pegs[to] == [] or pegs[frm][-1] < pegs[to][-1]): moved = pegs[frm][-1] next = self.config_str.split(' ') next[moved - 1] = str(to + 1) nexts.append(Node(self, (frm + 1, to + 1), " ".join(next))) return nexts def __hash__(self): return hash(self.config_str) def __eq__(self, other): return self.config_str == other.config_str def config_to_pegs(config_str, n_pegs): config = [int(x) for x in config_str.split(' ')]

pegs = [[] for _ in range(n_pegs)] ints = range(1, len(config) + 1) for i in reversed(range(len(config))): pegs[config[i] - 1].append(ints[i]) return pegs def pegs_to_config(pegs): config = [None] * len(pegs) for i in range(len(pegs)): peg = pegs[i] for x in peg: config[x - 1] = i + 1 return " ".join(map(str, config)) def bfs(s_config, e_config, k_pegs): permutations = list(itertools.permutations(range(k_pegs), 2)) visited_nodes = set() queue = deque([Node(None, None, s_config)]) while queue: node = queue.popleft() if node not in visited_nodes: visited_nodes.add(node) if node.config_str == e_config: break nexts = node.next_configs(k_pegs, permutations) queue.extend(nexts) moves = [] while node: if node.last_move: moves.append(node.last_move) node = node.parent moves.reverse() return moves while True: try: line = raw_input() n, k = [int(x) for x in line.split(' ')] s_config = raw_input() e_config = raw_input() moves = bfs(s_config, e_config, k) print len(moves) for x, y in moves: print x, y except EOFError:

break

Breathalyzer To safeguard against the dreaded phenomenon of wall posting while drunk, Facebook is implementing a feature that detects when post content is too garbled to have been done while sober and informs the user that they need to take an online breathalyzer test before being allowed to post. Unfortunately, there is far too much content for a given set of persons to evaluate by hand. Fortunately, you are a programmer of some repute and can write a program that processes and evaluates wall posts. You realize that such a program would be of great use to society and intend to resolve the problem once and for all. The program you write must compute a score for a body of text, returning this score upon completion. Your program will be given a list of accepted words and run on one wall post at a time. For each word W in the post, you must find word W' from the list of accepted words such that the number of changes from W to W' is minimized. It is possible that W is already W' and thus the number of changes necessary is zero. A change is defined as replacing a single letter with another letter, adding a letter in any position, or removing a letter from any position. The total score for the wall post is the minimum number of changes necessary to make all words in the post acceptable. Input Specification Your program must take a single string argument, representing the file name containing the wall post to analyze. In addition, your program must open up and read the accepted word list from the following static path location: /var/tmp/twl06.txt For testing purposes, you may download and examine the accepted word list here. When submitting your code, you do not need to include this file, as it is already present on the machine. The input file consists entirely of lower case letters and space characters. You are guaranteed that the input file will start with a lower case letter, and that all words are separated by at least one space character. The file may or may not end with a new line character. Example input file: tihs sententcnes iss nout varrry goud You are guaranteed that your program will run against well formed input files and that the accepted word list is identical to the one provided for testing.

Output Specification Your program must print out the minimum number of changes necessary to turn all words in the input wall post into accepted words as defined by the word list file. Words may not be joined together, or separated into multiple words. A change in a word is defined as one of the following: Replacing any single letter with another letter. Adding a single letter in any position. Removing any single letter. This score must be printed out as an integer and followed by a single new line. Example Output (newline after number): 8

Hoppity Hop! To help test whether your puzzle submissions fit the guidelines, try this simple test puzzle. Your solution must follow the guidelines like any other puzzle. Write a program that takes as input a single argument on the command line. This argument must be a file name, which contains a single positive integer. The program should read this file and obtain the integer within, and then output a sequence of strings based upon the number (details below). Input specifications The input file will contain a single positive integer (in base 10) expressed as a string using standard ASCII text (e.g. for example, the number "15" but without the double quotes). This number may or may not be padded on either side with white space. There will be no commas, periods, or any other non-numeric characters present within the number. The file may or may not terminate in a single new line character ("\n"). An example input file is below: 15 Output specifications The program should iterate over all integers (inclusive) from 1 to the number expressed by the input file. For example, if the file contained the number 10, the submission should iterate over 1 through 10. At each integer value in this range, the program may possibly (based upon the following rules) output a single string terminating with a newline. For integers that are evenly divisible by three, output the exact string Hoppity, followed by a newline. For integers that are evenly divisible by five, output the exact string Hophop, followed by a newline. For integers that are evenly divisible by both three and five, do not do any of the above, but instead output the exact string Hop, followed by a newline. Example output (newline at end of every line): Hoppity

Hophop Hoppity Hoppity Hophop Hoppity Hop

=head1 NAME liar-liar.pl =head1 SYNOPSIS liar-liar.pl INPUT_FILE =head1 DESCRIPTION As a newbie on a particular internet discussion board, you notice a distinct trend among its veteran members; everyone seems to be either unfailingly honest or compulsively deceptive. You decide to try to identify the members of the two groups, starting with the assumption that every senior member either never lies or never tells the truth. You compile as much data as possible, asking each person for a list of which people are liars. Since the people you are asking have been around on the board for a long time, you may assume that they have perfect knowledge of who is trustworthy and who is not. Each person will respond with a list of people that they accuse of being liars. Everyone on the board can see that you are a tremendous n00b, so they will grudgingly give you only partial lists of who the liars are. Of course these lists are not to be taken at face value because of all the lying going on. You must write a program to determine, given all the information you've collected from the discussion board members, which members have the same attitude toward telling the truth. It's a pretty popular discussion board, so your program will need to be able to process a large amount of data quickly and efficiently. =head2 Input Specifications Your program must take a single command line argument; the name of a file. It must then open the file and read out the input data. The data begins with the number of veteran members n followed by a newline. It continues with n chunks of information, each defining the accusations made by a single member. Each chunk is formatted as follows: <accuser name> <m> followed by m lines each containing the name of one member that the accuser

says is a liar. accuser name and m are separated by some number of tabs and spaces. m will always be in [0, n]. All member names contain only alphabetic characters and are unique and case-sensitive. =head3 Example input file: =over 4 5 Stephen 1 Tommaso Tommaso 1 Galileo Isaac 1 Tommaso Galileo 1 Tommaso George 2 Isaac Stephen =back =head2 Output Specifications Your output must consist of two numbers separated by a single space and followed by a newline, printed to standard out. The first number is the size of the larger group between the liars and the non-liars. The second number is the size of the smaller group. You are guaranteed that exactly one correct solution exists for all test data. =head3 Example output: =over 4 32 =back =cut

Você também pode gostar