Python Forum
hello, i was trying a testdome python test
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
hello, i was trying a testdome python test
#1
hello, i was trying a testdome python test but failed the 1st question despite getting the required answers. Any idea or advice why and how to improve my statements?
def unique_names(names1, names2):
    return None

if __name__ == "__main__":
    names1 = ["Ava", "Emma", "Olivia"]
    names2 = ["Olivia", "Sophia", "Emma"]
    joinednames = names1 + names2
#    print(joinednames)
    
    output = set()
    for x in joinednames:
     output.add(x)
    print(output)
  #  unique_names = set()
  #  for x in joinednames:
  #   joinednames.add(x)
 #   print(unique_names(names1, names2)) # should print Ava, Emma, Olivia, Sophia
my results were {'Ava', 'Sophia', 'Emma', 'Olivia'}
test is random and not a very big samples - available here Testdome URL for Python
Reply
#2
What this code should accomplish?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
As we don't know what is the expected output we cannot tell what's wrong or how to fix it. Post the full assignment/test.
if the expected output is Ava, Emma, Olivia, Sophia , then (i) order is preserved (i.e. set will not work for you) and (ii) it does not print a container, but comma-and-space-separated list of names
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
the question is:
Implement the unique_names method. When passed two lists of names, it will return a list containing the names that appear in either or both lists. The returned list should have no duplicates.

for eg.,calling unique_names(['Ava','Emma','Olivia'],['Olivia','Sophia','Emma'] should return a list containing Ava, Emma, Olivia, and Sophia in any order.
Reply
#5
so, number of issues
it expect list (most probably preserving the order - "in any order" is your assumption)
it expect you to implement the unique_names function, which will return that list

what you do:
your unique_names function just returns None (you didn't change anything)
you produce set
you just print that set
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
i tried another set of data, i think the problem is i am not calling unique_names to be printed despite the values are distinct.
def unique_names(names1, names2):
    return None

if __name__ == "__main__":
    names1 = ["Ava", "Emma", "Olivia", "Mary"]
    names2 = ["Olivia", "Sophia", "Emma", "Jane", "Mary", "Mary"]
     
    output = set()
    for x in names1 + names2:
     output.add(x)
    print(output)
 #   print(unique_names(names1, names2)) # should print Ava, Emma, Olivia, Sophia
i could get distinct values Ava, emma, olivia, mary, sophia, jane.
Reply
#7
don't write the test case. implement the function. i.e. your code that produce list of unique names should be part of unique_names function and the function should return the list
Also, your indentation does not match the template provided. i.e. they use 4 spaces per level. your code uses 2 spaces
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
(Aug-26-2020, 06:30 AM)buran Wrote: so, number of issues
it expect list (most probably preserving the order)
it expect you to implement the unique_names function, which will return that list

what you do:
your unique_names function just returns None (you didn't change anything)
you produce set
you just print that set


Thanks buran.. but so sorry, i do not understand how to proceed then. what should i do? I know i am not calling the function correctly 1 point now. do i return print joinednames (which is names1 + names2 joined list?) instead of None? the set need not be produced actually? they should appear in any order not defined.
thank you again.. very new self learning pythoner here.
Reply
#9
what they expect is
They expect just the function, e.g.

def unique_names(names1, names2):
    output = set()
    for x in names1 + names2:
        output.add(x)
    return output
Note - I just moved YOUR code to where they expect it to be. It will still fail, because they expect a list, not set. Also, in any case it's not how I would implement it.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
It seems to me that main question here is whether order should be preserved or not. If order is not important then very simple set-list conversion would do:

>>> def unique_names(first, second):
...     return list(set(first + second))
...
If order is important then you should have keep track whether element is already seen or not. Something like (pseudocode, not real one):

create list where unique items will be kept

for every item in lists which items will be uniquefied:
     if item in unique items list: continue
     else: append item to unique items list
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to test the high-score in my Python-game Pluviometer 2 547 Oct-02-2023, 06:55 PM
Last Post: deanhystad
  How to test and import a model form computer to test accuracy using Sklearn library Anldra12 6 3,068 Jul-03-2021, 10:07 AM
Last Post: Anldra12
  cannot build python 3.8.2 (make test fails on test_imprtlib) borabora 1 2,706 May-08-2020, 09:10 PM
Last Post: Larz60+
  How to write test cases for a init function by Unit test in python? binhduonggttn 2 3,062 Feb-24-2020, 12:06 PM
Last Post: Larz60+
  How to write test cases by Unit test for database configuration file? binhduonggttn 0 2,512 Feb-18-2020, 08:03 AM
Last Post: binhduonggttn
  new python package test ramkrishna 0 1,834 Feb-25-2019, 12:18 PM
Last Post: ramkrishna
  Python test failed CodingMaster111 1 3,809 May-09-2018, 04:24 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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