Python outputs a three-digit number consisting of 1, 2, 3, and 4, which are inconsistent and have no duplicates.

Source: Internet
Author: User

Python outputs a three-digit number consisting of 1, 2, 3, and 4, which are inconsistent and have no duplicates.

Question: There are four numbers: 1, 2, 3, and 4. How many three numbers can be composed of different and non-repeated numbers? What is each?

Program analysis: the numbers that can be filled in hundreds, ten, and one digit are 1, 2, 3, and 4. Make up all the sorting before dropping the sorting that does not meet the conditions.

Program source code

Method 1:

#!/usr/bin/python# -*- coding: UTF-8 -*- for i in range(1,5): for j in range(1,5):  for k in range(1,5):   if( i != k ) and (i != j) and (j != k):    print i,j,k

The above instance output is as follows:

1 2 3
1 2 4
1 3 2
1 3 4
1 4 2
1 4 3
2 1 3
2 1 4
2 3 1
2 3 4
2 4 1
2 4 3
3 1 2
3 1 4
3 2 1
3 2 4
3 4 1
3 4 2
4 1 2
4 1 3
4 2 1
4 2 3
4 3 1
4 3 2

Method 2:

Remove duplicate elements with a set

#! /Usr/bin/env python #-*-coding: UTF-8-*-import pprintlist_num = ['1', '2', '3 ', '4'] list_result = [] for I in list_num: for j in list_num: for k in list_num: if len (set (I + j + k) = 3: list_result + = [int (I + j + k)] print ("three digits that can make up % d different and have no repeated numbers:" % len (list_result) pprint. pprint (list_result)

The output result is the same as the above, but one thing is output in the form of a set.

Summary

The above section describes all the three-digit numbers output by Python consisting of 1, 2, 3, and 4, which are different from each other and have no duplicates. I hope this will help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.