-3

I need some help with my homework for school. I am supposed to write a program in python that ask the person for a list of numbers, in my case:

[53,16,22,81,43,16,88,55,43,5]

and print out the numbers that appear twice in it in the same order they appeared. I am also supposed so use the eval function in my program.

All I have currently is this:

list= eval(input("Input your list: "))

The end result is supposed to look like this in python shell: Input a list: [53,16,22,81,43,16,88,55,43,5] [16, 43]

Please help!

Mike
  • 13
  • 5
  • Suppose I give you a list of 1000 numbers on paper and ask you to find the number that appears twice. Ignoring the problem of how long it would take you and how boring it would be, how would you do it? Imagine it and describe it to me in simple terms. – Alex Hall Nov 11 '16 at 21:52
  • 2
    Don't use `eval`! It's a very dangerous function combined with parsing user input. use `ast.literal_eval` instead. Why? http://stackoverflow.com/questions/15197673/using-pythons-eval-vs-ast-literal-eval – woockashek Nov 11 '16 at 21:53
  • Alex Hall I think that I would go through all the numbers and than write down the ones that appear twice. The problem is that I don't knwo what to say to python to do this. – Mike Nov 11 '16 at 21:57
  • Thank you for your suggestion woockashek, but I am supposed to use this function because the homework is tested by a program that checks explicitly which functions did I use and I have to use this one. – Mike Nov 11 '16 at 22:00
  • Don't name your variable `list` as it is a function in Python. – trincot Nov 11 '16 at 22:08
  • Explain your teacher that this requirement is wrong and harmful. Maybe you will get additional + for the knowledge :) – woockashek Nov 11 '16 at 22:08
  • Haha, the problem is that my teacher is an older lady, past 60 and she doesn't really understand the program herself. – Mike Nov 11 '16 at 22:11
  • Choose a new school, quickly. If your instructor has difficulties with this... – takendarkk Nov 11 '16 at 22:15
  • Oh I am studying chemistry, but I have a class called The basics of programming which is causing me problems. – Mike Nov 11 '16 at 22:22

1 Answers1

0

Edit: from the comments I got to understand the numbers in the output should be listed in order of their second occurrence in the input, as the description apparently says:

The numbers should be in [the] order [of] when the students went to get their extra piece of cake.

You could use list comprehension with inp[:i].count(inp[i]) == 1 as filter condition, meaning the current number should appear once before the current index:

inp = eval(input('Input your list:'))
print([j for i, j in enumerate(inp) if inp[:i].count(j) == 1])

Don't use list as a variable name, as it is an existing function in Python.

As stated by others: using eval is frowned upon, certainly when the argument is provided by the user.

Basic alternative (not efficient)

This implements the same algorithm, but just uses the most basic constructs:

result = []
n = len(inp)
for i in range(n):
    count = 0
    # only look at number preceding the one at i
    for j in range(i): 
        if inp[i] == inp[j]:
            count += 1
            # we look for cases where there is exactly one match
            # so we know that at i it is the 2nd occurrence
            if count == 2:
                break
    if count == 1:
        result.append(inp[i])
print(result)
Community
  • 1
  • 1
trincot
  • 317,000
  • 35
  • 244
  • 286
  • Thanks for you help! Your program works, but do you by any chance know a way to do this task with simpler functions, such as `for` and `if` because we haven't used the `inp.count` function yet. – Mike Nov 11 '16 at 22:20
  • So what else can you use? Dictionaries? `index()`? `find()`? – trincot Nov 11 '16 at 22:29
  • We haven't learned any of those. In our last few classes we were just using `for` and `if` functions, however I have to admit I don't really know what `for` function means... :/ – Mike Nov 11 '16 at 22:32
  • They are not functions, but statements. I added a non-efficient alternative, that uses `for` and `if`, but needs one method to add to the result: `append`. I hope you know that one... – trincot Nov 11 '16 at 22:36
  • Just to be sure: do you need to list the numbers that occur twice, or all those that occur *at least* twice? – trincot Nov 11 '16 at 22:38
  • Oh in that case I think we haven't even used any functions yet, only statements. And I am not familiar with the ˙append˙ function or statement. Yes I need the list of numbers that occur at least twice. – Mike Nov 11 '16 at 22:43
  • This is supposed to be seen in SHELL: `Input a list: [53,16,22,81,43,16,88,55,43,5]` `[16, 43]` – Mike Nov 11 '16 at 22:45
  • I removed that method as well, and just print the numbers as they are identified: not really the way it is done. I now see the desired output: but how can you build an array if you are not allowed to use `append` or similar stuff? And now I also see you want to print the `16` only once, not twice? Seems important to mention that in the question. – trincot Nov 11 '16 at 22:46
  • Yes just wanted to ask you, how do I do that? Also ou helped me tremendously by the way thank you very much! – Mike Nov 11 '16 at 22:50
  • Well, you tell me: what have you learned so far about building lists? How have you been told to create one and add elements to it? – trincot Nov 11 '16 at 22:54
  • We haven't added any elements to existing lists. We would sometimes change a parts of lists like this : `nike = randomlist[5]` I tried to print every second element of `i` – Mike Nov 11 '16 at 23:03
  • That is not changing part of a list; that is copying one of a list's elements in another variable ;-) – trincot Nov 11 '16 at 23:08
  • oh i did not mean it that way, i meant it like this: `randomlist[5] = "nike"` – Mike Nov 11 '16 at 23:12
  • we also learned how to print every second, third etc. element of a list using this `[::2]` – Mike Nov 11 '16 at 23:13
  • I see, but that wont help to build the array that you need here. I put back a version with `append`, as I think you must use it (explain it to your teacher). And I adapted both solutions so they only include these values once in the output. – trincot Nov 11 '16 at 23:24
  • However the is a program that we use to test if our program is done alright, and this program says it's not alright. If you want you can check it out. It's basically a python programe. I just dont know how to send it to you because the code is too long to post it in this comment. – Mike Nov 11 '16 at 23:45
  • You can use a website, like for instance repl.it: choose Python, copy/paste the code, press share, and post the link here. If the code is not too long/complicated I might have a look at it. – trincot Nov 12 '16 at 08:08
  • [link](https://repl.it/EWCL/0) This is the code. Also to test our program with this we had to name our program ˙doplacilo` – Mike Nov 12 '16 at 09:48
  • Do you have the file `config.json` that this code refers to? – trincot Nov 12 '16 at 09:56
  • yes, where should i upload it? and there is also a folder called public, should i add it too? – Mike Nov 12 '16 at 11:46
  • [link](https://ufile.io/34e18) here is the file – Mike Nov 12 '16 at 11:49
  • Sorry, but there is too much missing information. There are test files involved as well. – trincot Nov 12 '16 at 12:00
  • Better tell me what it says when you say *"it says it's not alright*" – trincot Nov 12 '16 at 12:01
  • `Public tests: Test 1: OK Test 2: OK Test 3: OK Test 4: OK Test 5: expected "Vnesite seznam: [88, 43] ", found "Vnesite seznam: [43, 88]" Test 6: OK Test 7: OK Tests passed: 6/7` – Mike Nov 12 '16 at 13:10
  • Are you sure about the requirement in which order the numbers must be output. Is it really the first occurrence of the multiple occurrences that must be kept, and kept in order, or is there a requirement to order the output by value, or still something else? Can you check? – trincot Nov 12 '16 at 13:14
  • Here are the instructions for the task: `The faculty decided that on the Day of the school it will give every student a free piece of cake. In order to get the cake the student had to show his student ID. If someone wanted another piece of cake he could get another one, but he had to pay for it. We want to know which students already ate their free cake and which ones will have to pay before leaving the school. Write a program in python that will get a list of student ID's, where the order of the numbers on the IDs is the same as the order in which the students got their cake.` – Mike Nov 12 '16 at 15:56
  • `If a student went to get an extra piece of cake he stood in line for the cakes twice or more times. The program should write out the list of student ID numbers that will have to pay. The numbers should be in order when the students went to get their extra piece of cake. If a student went to get a piece of cake more than twice he should only appear in the list once. If no one took an extra piece of cake the list should be empty. ` These are the instructions that we got. Sorry if you don't understand everything. The original inst. are in Slovene and I had to translate them. – Mike Nov 12 '16 at 16:00
  • Ask if you need to understand things better. – Mike Nov 12 '16 at 16:03
  • OK, this is different than I understood from your original question. The order should be given along the *second* occurrence of the numbers, not the *first*. Did I get that right? It would also be interesting to get access to the file `test_5.in`, which is the one that failed. – trincot Nov 12 '16 at 16:16
  • I updated my answer (both the short and long code) assuming I understood that correctly, and the order should be by the *second* occurrence of duplicates. – trincot Nov 12 '16 at 16:33
  • Now it passed all the tests! Thank you sooo much! You helped me out a lot!!!!!! I hope that you will get infinite luck until your last days! :D – Mike Nov 12 '16 at 16:38