{"id":18468,"date":"2021-08-26T10:39:29","date_gmt":"2021-08-26T05:09:29","guid":{"rendered":"https:\/\/python-programs.com\/?p=18468"},"modified":"2021-11-22T18:37:16","modified_gmt":"2021-11-22T13:07:16","slug":"python-program-to-print-number-in-ascending-order-which-contains-1-2-and-3-in-their-digits","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-number-in-ascending-order-which-contains-1-2-and-3-in-their-digits\/","title":{"rendered":"Python Program to Print Number in Ascending Order which contains 1, 2 and 3 in their Digits"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Check if Array can be Sorted with One Swap<\/a> 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 Below are the ways to print the numbers with the digits 1, 2, and 3 in ascending order, which is separated by commas.<\/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 Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n In the previous article, we have discussed Python Program to Check if Array can be Sorted with One Swap Given a list and the task is to print the numbers with the digits 1, 2, and 3 in ascending order, which are separated by commas. Examples: Example1: Input: Given List = [67123, 1234, 985, 126, …<\/p>\n
\nGiven a list and the task is to print the numbers with the digits 1, 2, and 3 in ascending order, which are separated by commas.<\/p>\nGiven List = [67123, 1234, 985, 126, 1011]<\/pre>\n
The numbers with the digits 1, 2, and 3 in ascending order, which is separated by commas= [1234, 67123]<\/pre>\n
Given List = [75, 4123, 87123, 5312, 9098]<\/pre>\n
The numbers with the digits 1, 2, and 3 in ascending order, which is separated by commas= [4123, 5312, 87123]<\/pre>\n
Program to Print Number in Ascending Order which contains 1, 2, and 3 in their Digits in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the list as static input and store it in a variable.\r\ngven_lst = [67123, 1234, 985, 126, 1011]\r\n# Take a new empty list say \"numb\" and store it in another variable.\r\nnumb = []\r\n# Loop in the given list using the for loop.\r\nfor itr in gven_lst:\r\n # Inside the loop, convert the iterator value to the string using the str() function\r\n # and store\u00a0it in another variable.\r\n strng_number = str(itr)\r\n# Check if 1 and 2 and 3 are present in the string number using the if conditional\r\n# statement and 'and' keyword.\r\n if '1' in strng_number and '2' in strng_number and '3' in strng_number:\r\n # If the statement is true, then append the iterator value to the above initialized\r\n # new empty list \"numb\".\r\n numb.append(itr)\r\n # Sort the above list \"numb\" using the sort() function.\r\nnumb.sort()\r\n# print the list \"numb\" to print the numbers with the digits 1, 2, and 3 in ascending order,\r\n# which is separated by commas.\r\nprint(\"The numbers with the digits 1, 2, and 3 in ascending order, which is separated by commas= \", numb)\r\n<\/pre>\n
The numbers with the digits 1, 2, and 3 in ascending order, which is separated by commas= [1234, 67123]<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngven_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Take a new empty list say \"numb\" and store it in another variable.\r\nnumb = []\r\n# Loop in the given list using the for loop.\r\nfor itr in gven_lst:\r\n # Inside the loop, convert the iterator value to the string using the str() function\r\n # and store\u00a0it in another variable.\r\n strng_number = str(itr)\r\n# Check if 1 and 2 and 3 are present in the string number using the if conditional\r\n# statement and 'and' keyword.\r\n if '1' in strng_number and '2' in strng_number and '3' in strng_number:\r\n # If the statement is true, then append the iterator value to the above initialized\r\n # new empty list \"numb\".\r\n numb.append(itr)\r\n # Sort the above list \"numb\" using the sort() function.\r\nnumb.sort()\r\n# print the list \"numb\" to print the numbers with the digits 1, 2, and 3 in ascending order,\r\n# which is separated by commas.\r\nprint(\"The numbers with the digits 1, 2, and 3 in ascending order, which is separated by commas= \", numb)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 75 4123 87123 5312 9098\r\nThe numbers with the digits 1, 2, and 3 in ascending order, which is separated by commas= [4123, 5312, 87123]<\/pre>\n
\n