Python Forum
Help with Recursive solution,list items
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Recursive solution,list items
#1
Hi. This exercise was at the first exam of my university but I cant solve it. It needs to make a function using recursion. We have for example a list like [1,1,1,1] and we need to check if the items in list appear only one time. Example: [1,1,1,1] function has to return True. [1,2,3] has to return False . [1,1,2,2] True . [1,1,2,3] false.
Reply
#2
we are glad to help but we are not going to do it for you. Post your code in python tags, any error traceback - in error tags and in full. Also post the assignment verbatim (your current example is contradicting - how passing [1, 1, 1, 1] to function will return True (i.e. items in list appear only one time) when the item 1 appears 4 times)... why [1, 1, 2, 2] will return True and [1, 1, 2, 3] will return False?
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
#3
I am sorry. I mean if every item in list appears more than one time then it is true. I am glad to try solve this and I really tried. I can solve the problem but not with recursive function because I cant understand exactly what to do with list. I will be very happy if you give me some tips to do it my own
Reply
#4
So far you haven't show any effort. This is not going to work like this...
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
#5
def allSames(l):
Value= True
for i in l:
if l.count(i) < 2:
Value = False
return Value

This is my programme but with no recursion. Excuse me but I write this from my phone and I cant give the cod in python box. If it is not enough give me some time and I definatly will provide the code in python box as fast as I find a pc
Reply
#6
Do you know how recursion works?
Reply
#7
First try to write the program without recursion. Show us your code (and error message). (You must use BBcode) Then we will help you to make a recursive function out of it.
Reply
#8
Took your phone code and properly formatted, and you are right, it works. Now the challenge is to do it with recursion. Here is your code formatted so all can more easily visualize (with an example at the end)
def allSames(l):
    Value= True
    for i in l:
        if l.count(i) < 2:
            Value = False
    return Value
print(allSames('1112233344'))
Output:
True
Now for recursion - your solution is more elegant than the recursive one will be. Forget that l.count(i) is available. In its place write code that includes a call back to the original function (that call back makes it recursive).
Or, another idea - do a count on the first digit. If 2+, remove all copies of that digit and call the function on the resulting string (this is the recursion part). Return if either out of digits or if any digit has a count of 1.
Reply
#9
Thanks for the advice. In some hours I will finally be in front of a pc and I will try it more.and i will upload my result
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Collisions for items in a list Idents 3 2,298 Apr-06-2021, 03:48 PM
Last Post: deanhystad
  Removing items in a list cap510 3 2,347 Nov-01-2020, 09:53 PM
Last Post: cap510
  Removing items from list slackerman73 8 4,440 Dec-13-2019, 05:39 PM
Last Post: Clunk_Head
  Sum of elements on the list recursive sev 3 2,566 Jun-20-2019, 02:14 PM
Last Post: sev
  Find 'greater than' items in list johneven 2 4,470 Apr-05-2019, 07:22 AM
Last Post: perfringo
  How to add items within a list Mrocks22 2 2,679 Nov-01-2018, 08:46 PM
Last Post: Mrocks22
  Storing Minimum List of values from a recursive function sigsegv22 1 2,545 Sep-10-2018, 01:25 PM
Last Post: ichabod801
  How to keep duplicates and remove all other items in list? student8 1 4,946 Oct-28-2017, 05:52 AM
Last Post: heiner55
  Help printing any items that contains a keyword from a list Liquid_Ocelot 13 74,356 May-06-2017, 10:41 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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