7 Replies - 1286 Views - Last Post: 07 April 2014 - 10:57 AM

#1 abcd8989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-April 14

Recursion

Posted 07 April 2014 - 06:37 AM

Recently I learnt the basics of recursion and I find it extremely difficult for me. I have been asked to create a recursive function generating a list of intersection of two sorted list using pseudo code. Say, if the input lists, list1 and list2 are [1,2,4,5,9] and [2,3,6,9] , the function Intersect(list1, list2)would yield [2,9]. I have spent hours thinking about it and still, I am not sure what I should actually do. My brain gets lost in the recursive thinking style. Much obliged more any help!

Is This A Good Question/Topic? 0
  • +

Replies To: Recursion

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3652
  • View blog
  • Posts: 11,421
  • Joined: 05-May 12

Re: Recursion

Posted 07 April 2014 - 06:52 AM

Show us the pseudo code that you have tried.
Was This Post Helpful? 0
  • +
  • -

#3 abcd8989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-April 14

Re: Recursion

Posted 07 April 2014 - 07:00 AM

if (length(list1) = 0 or length(list2) =0 ) then return (I don't know...)

if listHead(list1) = listHead(list2) then 
	return 	createList(listHead(list1),ListConcat(ListTail(list1, list2)

if listHead(list1) < listHead(list2) then 
	return 	ListIntersection(listTail(list1),list2))
else 
	return 	ListIntersection(list1,listTail(list2))


This post has been edited by macosxnerd101: 07 April 2014 - 07:07 AM
Reason for edit:: Please use code tags

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10785
  • View blog
  • Posts: 40,160
  • Joined: 27-December 08

Re: Recursion

Posted 07 April 2014 - 07:08 AM

Is this a C/C++ question? If not, I will move it to a more appropriate forum.
Was This Post Helpful? 0
  • +
  • -

#5 abcd8989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-April 14

Re: Recursion

Posted 07 April 2014 - 07:09 AM

it is not, but i couldn't find a pseduo code forum so i put here , sorry about that><
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10785
  • View blog
  • Posts: 40,160
  • Joined: 27-December 08

Re: Recursion

Posted 07 April 2014 - 07:15 AM

I'll move this to Software Development.
Was This Post Helpful? 0
  • +
  • -

#7 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 356
  • View blog
  • Posts: 783
  • Joined: 27-June 09

Re: Recursion

Posted 07 April 2014 - 09:45 AM

Quote

if (length(list1) = 0 or length(list2) =0 ) then return (I don't know...)

if one of the lists is empty, then there is no intersection, right? Just return an empty list.

Quote

if listHead(list1) = listHead(list2) then
return createList(listHead(list1),ListConcat(ListTail(list1, list2)

You have determined that the head is part of your solution. So, shouldn't you concatenate that to the intersection of the tails of the two lists?
Was This Post Helpful? 0
  • +
  • -

#8 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 586
  • View blog
  • Posts: 1,306
  • Joined: 24-April 12

Re: Recursion

Posted 07 April 2014 - 10:57 AM

If you're having trouble with recursion, it might be worth your while to go through my tutorial. It's written for C#, but recursion is recursion ... recursively.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1