About the interview summary 4-python pen questions

Reprinted: https://www.cnblogs.com/yoyoketang/p/10082644.html

Foreword

Now test job interview, will normally require a familiar language (python / java), in order to test basic skills of job seekers, usually a two pen questions, these questions are generally not difficult, mainly on the basic skills.
Would you take a computer, write debugging side, not much difficulty in the editor inside edge. The main is to give you a pen and paper and let you write the scene, it is not so easy.
(Benpian code is based on python3.6)

1. Statistics

Statistics in a queue number, the number of positive, negative number, such as [1, 3, 5, 7, 0, -1, -9, -4, -5, 8]

method one

# coding:utf-8
a = [1, 3, 5, 7, 0, -1, -9, -4, -5, 8] # 用列表生成式,生成新的列表 b = [i for i in a if i > 0] print("大于0的个数:%s" % len(b)) c = [i for i in a if i < 0] print("小于0的个数:%s" % len(c))

Method Two

# coding:utf-8
a = [1, 3, 5, 7, 0, -1, -9, -4, -5, 8] # 用传统的判断思维,累加 m = 0 n = 0 for i in a: if i > 0: m += 1 elif i < 0: n += 1 else: pass print("大于0的个数:%s" % m) print("小于0的个数:%s" % n)

2. Slice the string

String "axbyczdj", If you get the result "abcd"

method one

# 字符串切片
a = "axbyczdj"
print(a[::2])

Method Two

# 传统思维
a = "axbyczdj"

c = []
for i in range(len(a)):
    if i % 2 == 0: c.append(a[i]) print("".join(c))

3. Cutting strings

Given a string "hello_world_yoyo", how to get a queue [ "hello", "world", "yoyo"]

a = "hello_world_yoyo"
b = a.split("_")
print(b)

4. Output Formatting

It is known as a number 1, how to output "0001"

a = 1

print("%04d" % a)

5. Queue

Given a queue, such as: [1, 3, 5, 7], how the first number into a third position, to give: [3, 5, 1, 7]

a = [1, 3, 5, 7]

# insert插入数据
a.insert(3, a[0])
print(a[1:])

6. exchange

Known that a = 9, b = 8, how to exchange the values ​​of a and b, to obtain a value of 8, 9, the value of b

method 1

a = 8
b = 9

a, b = b, a
print(a)
print(b)

Method 2

a = 8
b = 9

# 用中间变量c
c = a
a = b
b = c
print(a)
print(b)

7. daffodils

100-999 print out all the "number of Narcissus", the so-called "Narcissus number" refers to a three-digit number, which is equal to the digits of the cube and the number itself. For example: 153 is a "Narcissus number", because cubic cubic cubic +3 +5 1 = 153.

sxh = []
for i in range(100, 1000):
    s = 0 m = list(str(i)) for j in m: s += int(j)**len(m) if i == s: print(i) sxh.append(i) print("100-999的水仙花数:%s" % sxh)

8. perfect number

If a number is exactly equal to the sum of its factors, called the number "perfect number", also known as the number of complete or perfect number. For example: The first is a perfect number 6, it is about the number 1,2,3,6, 6 is removed outside itself, adding the number of the remaining 3,
1 + 2 + 3 = 6. The second number is the 28 completely, it is about several 1,2,4,7,14,28, 28 itself is removed, the remaining number of the sum 5, 1 + 2 + 4 + 7 + 14 = 28.
So the question is, find the perfect numbers less than 1000 what?

a = []

for i in range(1, 1000):
    s = 0 for j in range(1, i): if i % j == 0 and j < i: s += j if s == i: print(i) a.append(i) print("1000以内完全数:%s" % a)

9. Sort

Write a bubble sort in python

a = [1, 3, 10, 9, 21, 35, 4, 6] s = range(1, len(a))[::-1] print(list(s)) # 交换次数 for i in s: for j in range(i): if a[j] > a[j + 1]: a[j], a[j + 1] = a[j + 1], a[j] print("第 %s 轮交换后数据:%s" % (len(s)-i+1, a)) print(a)

Sort 10.sort

Given a queue [1, 3, 6, 9, 7, 3, 4, 6]

  • From small to large order
  • Ordered from the large size
  • Remove duplicate numbers
a = [1, 3, 6, 9, 7, 3, 4, 6] # 1.sort排序,正序 a.sort() print(a) # 2.sort倒叙 a.sort(reverse=True) print(a) # 3.去重 b = list(set(a)) print(b)

Guess you like

Origin www.cnblogs.com/yuany66/p/11389132.html