Logical title python base (3)

Python basis of logical problems (3)

  1. Write a function that converts IP addresses to achieve an integer

  2. seek results: --- lambda

  3. The result of seeking a

  4. The following requirements nums output

  The following fragments required output

  6. The results of written procedures: ------ my_dict

  7. function result: --- add_end

  8. Write Function: data

  9. The two queues implemented by a stack

  10. Write about the results of the code and explain: parent

1. Write a function that converts IP addresses to achieve an integer

10.3.9.12 The conversion rule is:
     10 00001010 
    . 3 00000011 
    . 9 00001001 
    12 is 00001100 
or more spliced together and then calculates a decimal-binary results: 0,000,101,000,000,011 0,000,100,100,001,100 = ?

 ------------------ -------------------------------------------------- ------- DEF FUNC (IP): 
    A = ip.split ( ' . ' ) 
    S = '' 
    L = []
     for I in A: 
        I = bin (int (I)) [2:]     # bin (10) = 0b1010 
        I = i.rjust (. 8, '

0 ' )     # 0000 the rjust 1010 (): returns a string right-aligned atoms, and filled with a new empty string 0, if the specified string length is less than the length of the original string is returned. 
        l.append (I) 
    S = s.join (L)
     return S 
RET = FUNC ( ' 10.3.9.12 ' )
 Print (int (RET, 2 )) 

str.rjust (width [, FillChar])
     -width - Specifies the total length of the specified character string after filling.
     -fillchar - padding characters, blank space by default. 

         10 octal binary Hexadecimal 
binary         - bin (X, 8) bin (X, 10) bin (X, 16 ) 
8-ary oct (x,2) - OCT (X, 10) OCT (X, 16 ) 
10 hexadecimal int (X, 2) int (X,. 8) - int (X, 16 ) 
16 hex hex (X, 2) hex (X, 8) hex (int (x, 10))

2. seek results: lambda

def num():
    return [lambda x: i * x for i in range(4)]  # 返回值是一个列表
print(num())
#[<function num.<locals>.<listcomp>.<lambda> at 0x00000000029726A8>, <function num.<locals>.<listcomp>.<lambda> at 0x0000000002972598>, <function num.<locals>.<listcomp>.<lambda> at 0x0000000002972730>, <function num.<locals>.<listcomp>.<lambda> at 0x00000000029727B8>]
print([m(2) for m in num()])
#[6,6,6,6]

def num():
    return (lambda x: i * x for i in Range (. 4)) # return value is a generator 
Print (NUM ())
 # <Generator Object NUM. <about locals>. <genexpr> AT 0x0000000002944B48> 
Print ([m (2) for m in NUM ()])
 # [0, 2, 4, 6]

3. The result of seeking a

a =. 1
  Print (ID (a))
  DEF FUNC (a): 
     a = 2
      Print (ID (a))          # local variables and global variables addresses are not equal 
FUNC (a)
  Print (a)                  # . 1 for a variable function defines a = 2 after executing the function at this time to destroy the old printing is a global variable a 
 a = []
  DEF FUNC (a): 
     a.append ( . 1 ) 
 FUNC (a) 
 Print (a )                     # [1] for a list passed to a function of the increase of a value a is a constant address 
# variable data types and data type immutable

4. The following evaluation codes nums

num = range(2,20)
 for i in num:
     num =list(filter(lambda x:x==i or x%i ,num))
 print(num)
    
  # 答案:[2, 3, 5, 7, 11, 13, 17, 19]


 num = range(2,20)
 for i in num:
     num =filter(lambda x:x==i or x%i ,num)
 print(num)
 print(list(num))

#<filter object at 0x000000000239C4E0>
#[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

5. Give the following output fragments

DEF say_hi (FUNC):
      DEF warpper (* args, ** kwargs):
          Print ( ' the HI ' ) --------------------------- --1    performed first 
         RET = FUNC (* args, ** kwargs)
          Print ( ' the BYE ' ) ---------------------------- -4     executed after
          return RET
      return warpper 

 DEF say_yo (FUNC):
      DEF warpper (* args, ** kwargs):
          Print ( ' YO2 ' ) ------------------- ---------- 2 calls, so before 4
         return FUNC (* args, ** kwargs)
      return warpper 

 @say_hi 
 @say_yo 
 DEF FUNC ():
      Print ( ' Rock & roll ' ) ----------------------- --------. 3    decorator, etc. 2 before been performed. 4 
 FUNC () 
 results of 
 the HI 
 YO2 
 Rock & roll 
 the BYE

6. Please write the output of the program

= {my_dict ' a ' : 0, ' B ' :. 1 }
 DEF FUNC (D): 
     D [ ' a ' ] = ---------. 1 the value of a becomes. 1
      return D 
 FUNC ( my_dict) 
 my_dict [ ' C ' ] = 2 ----- added a C
  Print (my_dict) 
 { ' a ' :. 1, ' B ' :. 1, ' C ' : 2} dictionary variable data type

7. Functions

DEF add_end (L = []): 
    l.append ( " End " )
     return L 

add_end () # nothing output 
add_end () # nothing output

8. Write a function

'''
data:{"time":"2016-08-05T13:13:05",
"some_id":"ID1234",
"grp1":{"fld1":1,"fld2":2},
"xxx2":{"fld3":0,
    "test":{‘fld5’:0.4}},"fld6":11,"fld7":7,"fld46":8 } 
    
fields:由"|"连接的以"fld"开头的字符串,如:fld2|fld3|fld7|fld19   
def select(data,fields):                
    return result
'''
data = {
      "time":"1989-05-06",
        "some_id":123456,
    "grp1":{"fld1":1, "fld2":2},
    "xxx2":{"fld3":0, "fld4":0.4},
    "fld6":11,
    "fld7":7,
    "fld45":8
}

def select(data, field): # "fld2|fld7|fld29"
    result = {}
    lst = field.split("|")

    for item in lst:
        result[item] = None

    for item in lst: # "fld2"
        if item in data: #
            result[item] = data[item]

    for value in data.values():
        if type(value) == dict:
            d = select(value, field)
            for k, v in d.items():
                if v:
                    result[k] = v
    return result
print(select(data, "fld2|fld7|fld29"))

9. The two queues implemented with a stack

Ideas: 
     1 . Any time there is always a two queues are empty.
    2 . Additive element always to add an element to a non-empty queue.
    3 . Remove the element when the element other team is always an outer end of the last element, introducing another empty queue, the last element of the team. 
Pseudocode: 
    class StackWithTwoQueues (Object):
     DEF  the __init__ (Self): 
        self.q1 = [] 
        self.q2 = []
     DEF Push (Self, Item): 
        self.q1.append (Item) 
    DEF POP (Self):
         IF len (self.q1) == 0:
             return None
         the while len (self.q1) =. 1! : 
            self.q2.append (self.q1.pop (0)) 
        self.q1, self.q2 = self.q2,self.q1
        return self.q2.pop(0)

10 Please give the correct answer

class the Parent (Object): 
    X =. 1
 class Child1 (the Parent):
     Pass 
class Child2 (the Parent):
     Pass 

Print (Parent.x, Child1.x, Child2.x) # 1,1,1 

Child1.x = 2
 Print ( Parent.x, Child1.x, Child2.x) # 1,2,1      

Parent.x =. 3
 Print (Parent.x, Child1.x, Child2.x) # 3,2,3 # value when the subclasses when not inherit the parent class, would like to inherit a super ()

 

Guess you like

Origin www.cnblogs.com/tianshuai1/p/10955394.html