{"id":25727,"date":"2021-12-04T09:19:40","date_gmt":"2021-12-04T03:49:40","guid":{"rendered":"https:\/\/python-programs.com\/?p=25727"},"modified":"2021-12-04T09:19:40","modified_gmt":"2021-12-04T03:49:40","slug":"python-program-for-friends-travel-problem","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-friends-travel-problem\/","title":{"rendered":"Python Program for Friends-Travel Problem"},"content":{"rendered":"
In this article, we’ll look at The Friends-Travel Problem, which is highly interesting.<\/p>\n
Friends-Travel Problem:<\/strong><\/p>\n Assume n friends want to go to a party; they can travel alone or with another friend as a couple. We presume that n motorbikes are offered for n buddies.<\/p>\n We need to figure out how many different ways the n friends can get to the party, either individually or in pairs of two as a couple.<\/p>\n Answer:<\/strong><\/p>\n One can either manually implement the naive technique using loops and if-else statements or use the quicker Recursion approach.<\/p>\n Before going into the problem just have a glance over the recursion.<\/p>\n To solve a larger problem, it is necessary to divide it into smaller difficulties.<\/p>\n Let us consider the lower values of n like ( 1, 2, and 3).<\/p>\n There are only one and two viable options for n = 1 and n = 2, respectively. And for n = 3, there are four alternative outcomes. How?<\/p>\n For each value of n, a friend has two options: either travel alone or search for n-1 friends.<\/p>\n Alternatively, the friend might choose a friend from the n-1 friends to travel with, and we will then look for n-2 friends.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n In this article, we’ll look at The Friends-Travel Problem, which is highly interesting. Friends-Travel Problem: Assume n friends want to go to a party; they can travel alone or with another friend as a couple. We presume that n motorbikes are offered for n buddies. We need to figure out how many different ways the …<\/p>\nGiven number of friends = 3<\/pre>\n
Different ways the 3 friends can get to the party = 4<\/pre>\n
Given number of friends = 5<\/pre>\n
Different ways the 5 friends can get to the party = 26<\/pre>\n
Program for Friends-Travel Problem in Python<\/h2>\n
\n
Method #1: Using Recursion (Static Input)<\/h3>\n
\n
# Create a recursive function say numberof_ways() which accepts the given number\r\n# as an argument and returns the total number of ways friends can get to the party.\r\n\r\n\r\ndef numberof_ways(no_frnds):\r\n # Inside the function, check if the given number is less than 3 using the if\r\n # conditional statement.\r\n if(no_frnds < 3):\r\n # If it is true, then return the given number.\r\n return no_frnds\r\n # Return the value of count_no_ways(no_frnds-1)) + ((no_frnds-1) *\r\n # count_no_ways(no_frnds-2) (Recursive Logic).\r\n return (numberof_ways(no_frnds-1)) + ((no_frnds-1) * numberof_ways(no_frnds-2))\r\n\r\n\r\n# Give the number(no of friends) as static input and store it in a variable.\r\nno_frnds = 3\r\n# Pass the given number as an argument to the numberof_ways() function and\r\n# store it in a variable.\r\nrslt = numberof_ways(no_frnds)\r\n# Print the above result.\r\nprint(\"Different ways the\", no_frnds, \"friends can get to the party = \", rslt)\r\n<\/pre>\n
Different ways the 3 friends can get to the party = 4<\/pre>\n
Method #2: Using Recursion (User Input)<\/h3>\n
\n
# Create a recursive function say numberof_ways() which accepts the given number\r\n# as an argument and returns the total number of ways friends can get to the party.\r\n\r\n\r\ndef numberof_ways(no_frnds):\r\n # Inside the function, check if the given number is less than 3 using the if\r\n # conditional statement.\r\n if(no_frnds < 3):\r\n # If it is true, then return the given number.\r\n return no_frnds\r\n # Return the value of count_no_ways(no_frnds-1)) + ((no_frnds-1) *\r\n # count_no_ways(no_frnds-2) (Recursive Logic).\r\n return (numberof_ways(no_frnds-1)) + ((no_frnds-1) * numberof_ways(no_frnds-2))\r\n\r\n\r\n# Give the number(no of friends) as user input using the int(input()) function and store it in a variable.\r\nno_frnds = int(input(\"Enter some random number = \"))\r\n# Pass the given number as an argument to the numberof_ways() function and\r\n# store it in a variable.\r\nrslt = numberof_ways(no_frnds)\r\n# Print the above result.\r\nprint(\"Different ways the\", no_frnds, \"friends can get to the party = \", rslt)\r\n<\/pre>\n
Enter some random number = 5\r\nDifferent ways the 5 friends can get to the party = 26<\/pre>\n","protected":false},"excerpt":{"rendered":"