Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Python written test common basic algorithm questions


May 30, 2021 Article blog


Table of contents


1. Find the sum of all even numbers within 100 (including 100).

Range (start, end, step) this sequence generator, and that slice of the syntax, with no tail, step is the step, here is no need to judge j, for these simple odd and, even and, on the n multiple and so on can do so.

for j in range(0,101,2):

s = s + j

print(s)

2. Ask for the nth Fibonacci number

This is to find the nth fibonacci number, we can try a variety of methods to do, I do not do more demonstration, if you ask n (including n) bit before all Fibonacci number, create a list to save it, I hope you can do it, deepen the use of the list method

# 波 那 数: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 .....

# From the third position: The last number is equal to the front two plus

n = int (INPUT ('Please enter you ask the first few Fiboacci):'))

a = 0

b = 1

for i in range(n):

# c = a

# a = b

# b = c + b

# 上 三 式 简 简 简:

a,b = b,a+b

print(a)

3. Ask for the number of daffodils

I define a function to find n digits of daffodils, but the number of daffodils is 100-1000 bits of ten hundred 3 times the sum of their own number, I just analogy, of course, can also not define the function, directly use input input a n or directly given n value

def get_Narcissistic(n):

for i in range(pow(10,n-1),pow(10,n)):

s = 0

for j in range(n):

s = s+pow(i//10**j%10,n)

if i==s:

print(i)

get_Narcissistic(3)

# 153

# 370

# 371

# 407

4. Print the 99 multiplication table

Used to understand the use of loops

# a=0

# while a<9:

#     a+=1

#     b=0

#     while b<a:

#         b+=1

# Print (b, '*', a, '=', a * b, SEP = ', end =' \ t ') #SEP is the separator default is space, \ t is a tab

#     print()

for i in range(1,10):

for j in range(1,i+1):

print(f'{j}*{i}={i*j}',end='\t')

print()

"""

1*1=1

1*2=2 2*2=4

1*3=3 2*3=6 3*3=9

1*4=4 2*4=8 3*4=12 4*4=16

1*5=5 2*5=10 3*5=15 4*5=20 5*5=25

1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36

1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49

1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64

1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

"""

5. A hundred horses

A big horse can carry 3 loads, a medium horse can carry 2 loads, two ponies can carry 1 load, if you use a hundred horses to carry a hundred loads, what are the different backing methods

For i in range (34): # i represents the number of matches

For J in Range (51): # j represents the number of midmat

if i*3+j*2+(100-i-j)/2 == 100:

Print (f 'Malaysian = {i}, middle horse = {j}, pony = {100-i-j}')

# 大马 = 2, Zhongma = 30, Pony = 68

# 大马 = 5, Zhongma = 25, Pony = 70

# 大马 = 8, Zhongma = 20, Pony = 72

# 大马 = 11, Zhongma = 15, Pony = 74

# 大马 = 14, Zhongma = 10, Pony = 76

# 大马 = 17, Zhongma = 5, Pony = 78

# 大马 = 20, Zhongma = 0, Pony = 80

6. Determine whether the year entered by the user is a leap year

Enter a number, first to determine if it is a multiple of 400, then satisfied, if not a multiple of 400, and then to judge that if the number can be divided by 4, but not by 100, then satisfied.

n = int (INPUT ('Please enter your year:')))

if n % 400 == 0 or n % 4 == 0 and n % 100 != 0:

Print ('{0} is a leap year' .format (n))

else:

Print ('{0} is not a leap year' .format (n))

Efficient approach:

A method isleap() has been encapsulated in python's calendar library to implement this judgment as a leap year:

import calendar

n = int (INPUT ("Please enter the year:"))

year = calendar.isleap(n)

if year == True:

Print ("{0} is a leap year". format (n))

else:

Print ("{0} is not a leap year". format (n))


Recommended lessons: Python3 Getting Started, Python3 Advanced