Python Forum

Full Version: hello, i was trying a testdome python test
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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
What this code should accomplish?
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
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.
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
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.
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
(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.
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.
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
Pages: 1 2