Python exercises to print three-bit non-repeating numbers

Source: Internet
Author: User
Tags builtin repetition

title: There are four numbers: 1, 2, 3, 4, how many different and no repetition of the number of three digits? What's the number?

Program Analysis: can be filled in hundreds, 10 digits, single digit numbers are 1, 2, 3, 4. Make all the permutations and then remove the permutations that do not meet the criteria.

Program Source code:

Source:
#!/usr/bin/python
#-*-Coding:utf-8-*-
For I Inch Range(1,5):
For J Inch Range(1,5):
For K Inch Range(1,5):
If( I! = k) and (i! = j) and (j! = k ) :
print i,J,K

My Source:

Num=5
#num =raw_input ("input number to get Three-digital number:")
List1=range (1,num)
Counter=0
For I in List1:
For J in List1:
if (I!=J):
For K in List1:
if (I!=j and J!=k and i!=k):
Print (I*100+J*10+K)
Counter+=1
Else
Continue
Else
Continue

Print ("Total:" +str (counter))


Other people's solutions refer to:
  1. Use the list form and calculate the summary:

    #!/usr/bin/python#-*-Coding:utf-8-*-# The original answer does not indicate the number of three digits, add the number of three digits without repetitionD=[]ForAInchRange(1,5): ForBInchRange(1,5): ForCInchRange(1,5): If (A!=B) and  (a!= c)  andc!=b D. ([a,bc) print  Len (dprint D             
  2. Hope

    [Email protected]

    Combine the For loop and if statement into one sentence and print the result directly

    #!/usr/bin/env python#-*-Coding:utf-8-*-List_num= [1,2,3,4]List= [I*100 +J*10 + k for I in List_num for J in List_num for K in     list_num 
                                                   
                                                    if
                                                     (j! = I and K!  = j and K!  = i)]print c32> (list)              
                                                   

  3. Habit Oolong Tea

    [Email protected]

    Reference method (set maximum, minimum value):

    #!/usr/bin/python#-*-Coding:utf-8-*-Line=[]ForIInchRange(123,433):A=I%10B=(I%100)10C=(I%1000)100IfA!=BandB!=CandA!=Cand 0<A<5 and 0<b<5  and 0< c<5 : print  (i line. (i) print ( "The total is: ' ,len (line           



Python exercises to print three-bit non-repeating numbers

Related Article

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.