0

Hey guys I was learning python and I was stuck on this one problem. I have attempted it but my output does not meet the correct answer. Any reasons why and how to fix it?

Problem: The game of Assassin is a simple game played on university campuses where each player is assigned a target to assassinate by simply saying "you’re dead" to them. Of course with thousands of people on campus and only a few dozen in the game you never know who is looking to assassinate you. Once you assassinate someone you take on the target they were looking for. If this happens to be you then you are the winner. To ensure that this works properly the targets must form a continuous "chain." Write a program that allows the user to enter their target assignments and output whether it is valid or not based on if there is a continuous "chain." Each person in the list is represented by position in the list. The value at the position is their target.

My code:

total=0
list_of_players=[]
player=raw_input("Enter a player (end to stop): ")
while player !="end":
 list_of_players.append(int(player))
 player=raw_input("Enter a player (end to stop): ")
for x in range(len(list_of_players)):
 total+=1
 if list_of_players[x]==0 and len(list_of_players)==total:
 print "The game is valid."
if list_of_players[x]!=0 or len(list_of_players)!=total:
 print "The game is not valid. "

The problem states that if I enter players 4,3,0,5,6,2,1: It should be valid. My program displays this as invalid. Any ideas how I can improve my program? I got this practice question from programmersheaven.com if anyone is curious! Thanks!

asked Apr 21, 2013 at 20:10
3
  • what is continuous chain ? Commented Apr 21, 2013 at 20:25
  • @ThanakronTandavas directed graph consisting of exactly 1 cycle 0>4>6>1>3>5>2>0 Commented Apr 21, 2013 at 20:28
  • list_of_players[x]==0 should hold for the last item only if you follow the links between players, not for the last item of input list Commented Apr 21, 2013 at 20:36

3 Answers 3

1

IMO, this problem is a good fit for starting with graph traversal, so have a look at:

https://www.python.org/doc/essays/graphs/

  • follow the edges from 1st item
  • if you end up back at the 1st item and all nodes were visited, tadaaa :)
twasbrillig
19.2k9 gold badges47 silver badges71 bronze badges
answered Apr 21, 2013 at 20:42
Sign up to request clarification or add additional context in comments.

Comments

0

Hint: Your code will always reject any sequence that does NOT end with 0.

Try to implement your validation logic as a function that returns True or False based on whether the sequence is valid or invalid and print the verification statement based on the result of that function.

EDIT

After some further inspection, I will try to give you some better solution-directed hints.

As mentioned in @a-rodas answer, if 0 in list_of_players will help you to check whether 0 is part of the sequence. For the other constraint, you may read up on the max() built-in function and think about how you may use it for your total/len(list_of_players).

With those hints from the edit, you should be able to validate the sequence with a simple if/else statement (no function required). For the sake of practising, I did not include code examples. But feel free to ask questions so we can further help you finding a solution of your own.

answered Apr 21, 2013 at 20:22

4 Comments

The exercise states I can only use what I have learned so far, and that does not include max () or set (). Is there another way I can do this problem?
Also, may you provide a code sample of how "if 0 in list_of_players" would look in this program?
Could you post a link to where exactly the exercise can be found (maybe by adding it to your question)? This would help to see what you should have learned so far.
The link is a direct download from programmersheaven.com. I have learned if statements, loops, and lists.
0
len(list_of_players)

won't ever be equal to total because

range(len(list_of_players))

will create a range from 0 to len(list_of_players) exclusive of the stop paramater so the last element will be equal to len(list_of_players) - 1. Since you add 1 to total on each iteration of the loop, total will only ever reach len(list_of_players) - 1 and your len(list_of_players)==total condition will never be satisfied.

However you are also not checking if it is a chain, in the example 4 -> 6 -> 1 -> 3 -> 5 -> 2 -> 0 -> 4 which is a valid chain, you need to check for this. Pick any index to start at. Keep a set of items you have seen, if you encounter an item you have already seen before getting to your starting element, the chain is not continuous.

total=0
list_of_players=[]
player=raw_input("Enter a player (end to stop): ")
while player !="end":
 list_of_players.append(int(player))
 player=raw_input("Enter a player (end to stop): ")
seen = set() # this could also be seen = [] but sets are faster
i = 0 # index to start at
# we should be able to go through len(list_of_players)
# before reaching the start player
for x in range(len(list_of_players)): 
 if i in seen:
 print "the game is not valid. "
 break
 seen.add(i)
 i = list_of_players[i]
else: # successful completion, no breaks
 print "The game is valid. "
answered Apr 22, 2013 at 0:04

2 Comments

ì = 0 will do. The above code is not executable per se, as the initial value for i will always be end instead of a number.
@cyroxx I had it as that before and then i changed it when I though it only had to consider the player, I forgot to change it back thanks I will fix that

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.