MAC Learning Series Python Challenge 1-10

Source: Internet
Author: User
Tags greatest common divisor

    1. Give you both Var A and B, print the value of a+b, just do it!!
      Answer:
      print a+b

    2. Give you a list L, such as l=[2,8,3,50], sort L in ascending order and output,
      The result of example L is [2,3,8,50]
      Answer:
      L=[2,8,3,50]
      L.sort() or L.sorted()
      print L

    3. Give you a string a, such as a= ' 12345 ', to reverse the output a.
Answer:a = ‘12345’b = a[::-1]print b
    1. Give you a dictionary a, such as A={1:1,2:2,3:3}, output the key of dictionary A to ', ' link, such as ' a '.
Answer:print(‘,‘.join(map(str, a.keys())))
Map Function Example
    >>> def add100(x):...     return x+100... >>> List = [11,22,33]>>> map(add100, List)[111122133]
    1. Give you a string a, the output word Fu Chi number position of the string. such as A= ' 12345 ', then output 135.
Answer:print a[::2]
    1. All prime numbers within the output 100, separated by a space between primes
answer:primes  =[]  for  i  in range ( 2 , 100 ): flag = False For  j  in range (2 , int (i /2 ) +1 ): if  i  % j = = 0:  flag =trueif  not Flag:primes.  Append (i ) print (. Join (Map (str, primes ))  
    1. Known rectangle length A, width b, output its area and perimeter, area and perimeter separated by a space
str(a*b)+‘ ‘+str(2*(a+b))
    1. Give you a list L, such as l=[0,1,2,3,4], the median of the output l (if the result is a decimal, one decimal place is reserved).
Answer:L = sorted(L)iflen(L)%2 == 1:    x =  L[(len(L)-1)/2]else:    x = L[len(L)/2-1]/2.0+L[len(L)/2]/2.0print  x
    1. Give you two positive integers a and B, and output their greatest common divisor.
Answer:printmaxforin range(1if (a%i+b%i)==0])
    1. Give you two positive integers a and B, and output their least common multiple.
a*b/maxforin range(1,aif (a%i+b%i)==0])

Not finished, to be continued

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

MAC Learning Series Python Challenge 1-10

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.