Thirteen Python Exercises

Posted by uptime on Sun, 04 Aug 2019 10:45:32 +0200

I. Perfect Cube

Write a program to find all quaternions (a,b,c,d) for any given positive integer N (N < 100), so that a^3= b^3 + c^3 + d^3, where a,b,c,d is greater than 1, less than or equal to N.

Input a positive integer N (N < 100) and output a perfect cube per line. The output formats are: Cube = a, Triple = b, c, d, where a,b,c,d are substituted with the actual quaternion values.
Please output from small to large according to the value of A. When the value of a is the same in two perfect cube equations, the preferential output with small b value is the same, while the preferential output with small c value is the same, and then the first output with small d value is the same.

n = int(input())
cube = [0]
for i in range(2, n+1):
  cube.append(i**3)
for a in range(3,n):
  for b in range(0,a-2):
    if cube[a] < (cube[b] + cube[b+1] + cube[b+2]):
      break
  for c in range(b+1,a-1):
    if cube[a] < (cube[b] + cube[c] + cube[c+1]):
      break
  for d in range(c+1,a):
    if cube[a] == (cube[b] + cube[c] + cube[d]):
      print("Cube = %d,Tripe = (%d,%d,%d)" %(a+1,b+1,c+1,d+1))

Conversion of RMB and US Dollar

The exchange rate is: 1 US dollar = 6.78 RMB.

The input format is USD10 and the output format is RMB67.8 (note that there is no space in the middle).

str1=input()
import re
num=int(''.join(re.findall('\d+',str1)))
if 'RMB' in str1:
    print('USD','%.2f' %(num/6.78),sep='')
elif 'USD' in str1:
    print('RMB','%.2f' %(num*6.78),sep='')

III. Caesar Code

Enter a string of letters and add them all to 3. If they exceed 122 (the ASCII value of z), subtract 26.

For example,'xyz'will become'abc'.

original=input()
cipher=''
for i in original:
    a=ord(i)
    if 97<=a<=119:
        cipher+=chr(a+3)
    elif 120<=a<=122:
        cipher+=chr(a+3-26)
    else:
        cipher+=i
print(cipher) 

Fourth, the power of daily upward C

365 days a year, based on the ability value of the first day, is recorded as 1.0. When learning well, the ability value increased by N compared with the previous day; when not learning, the ability value decreased by N due to forgetting and other reasons. Every day hard or laissez-faire, what is the difference in ability value over the past year? Among them, N ranges from 1 to 10, and N can be decimal.

Get user input N, calculate the ability value and the ability ratio after 365 days of daily effort and laissez-faire. Among them, the ability value retains two decimal places, the ability ratio outputs integers, and the output results are separated by English commas.

  

The code is as follows:

1 percent=eval(input())
2 hard=sunning=1
3 for i in range(2,366):
4   hard*=(1+percent/1000)
5   sunning*=(1-percent/1000)
6 print('%.2f,%.2f,%d' %(hard,sunning,hard/sunning))

5. Happy Numbers

Write an algorithm to determine whether a number is "happy". Happy numbers are determined by starting with a positive integer, replacing it with the sum of squares of each digit, and repeating the process until the final number either converges to 1 and remains equal to 1, or it will cycle endlessly and eventually not converge to 1. The number that eventually converges to 1 is the number of happy people.

For example, 19 is a happy number. The calculation process is as follows:

  • 12 + 92 = 82
  • 82 + 22 = 68
  • 62 + 82 = 100
  • 12 + 02 + 02 = 1

When entering a happy number, output True or False.

The code is as follows:

 1 num=eval(input())
 2 list1=[]
 3 while (num not in list1) and (num!=1):
 4   list1.append(num)
 5   a=num%10
 6   b=num//10%10
 7   c=num//100
 8   num=a**2+b**2+c**2 9 if num==1:
10   print('True')
11 else:
12   print('False')

6. Step-jumping

A frog can jump up a step or two at a time. How many jumping methods does the frog jump onto an n-step?

How many kinds of jumping methods are there in the number of input steps and output?

Note: If the operation is timed out, think about ways to reduce the time complexity.

The code is as follows: (recursive, may exceed the time limit)

 1 def wawa(step):
 2   if step==1:
 3     way=1
 4   elif step==2:
 5     way=2
 6   else:
 7     way=wawa(step-1)+wawa(step-2)
 8   return way
 9 
10 step=eval(input())
11 print(wawa(step))

(Cycle, simple and fast)

 1 def wawa(step):
 2   if step==1 or step==2:
 3     return step
 4   a=1
 5   b=2
 6   c=0
 7   for i in range(3,step+1):
 8     c=a+b
 9     a=b
10     b=c
11   return c
12 
13 step=eval(input())
14 print(wawa(step))

7. Conversion of Percentage Achievement to Five-point System (Cycle)

Write a student achievement conversion program. The output of the user input percentile system is "A". The output of students whose score is greater than or equal to 90 and less than or equal to 100 is "A". The output of students whose score is greater than or equal to 80 and less than 90 is "B". The output of students whose score is greater than or equal to 70 and less than 80 is "C". The output of students whose score is greater than or equal to 60 and less than 70 is "C". The output is "D" and the output with score less than 60 is "E". Output "data error" when input data is not valid. Users can input results repeatedly for conversion, output "end" when input negative number and end the program.

The code is as follows:

 1 flag=1
 2 while flag:
 3   grade=input()
 4   try:
 5       grade=eval(grade)
 6       if 100>=grade>=90:
 7         print('A')
 8       elif 90>grade>=80:
 9         print('B')
10       elif 80>grade>=70:
11         print('C')
12       elif 70>grade>=60:
13         print('D')
14       elif 60>grade>=0:
15         print('E')
16       elif grade<0:
17         print('end')
18         flag=0
19       else:
20         print('data error!')
21   except:
22     print('data error!')

8. Judgment of Prime Number

Prime Number is also called prime number. A natural number greater than 1, except for 1 and itself, which cannot be divided by other natural numbers, is called a prime number; otherwise, it is called a prime number. Composite number.

This topic requires the realization of a function to determine whether the parameter is a prime number, if yes, return True, otherwise return False.

The code is as follows:

 1 def isPrime(num):
 2   import math
 3   for i in range(2,int(math.sqrt(num))+1):
 4     if num%i==0:
 5       return 0
 6   return 1
 7 
 8 num=int(input())
 9 if isPrime(num):
10     print('yes')
11 else:
12     print('no')

Abbreviations for September and Month

Write a program, the user input a month's number, output the abbreviation of the month.

The code is as follows:

1 months={1: 'Jan.',2:'Feb.',3:'Mar.',4:'Apr.',5:'May.',6:'Jun.',7:'Jul.',8:'Aug.',9:'Sep.',10:'Oct.',11:'Nov.',12:'Dec.'}
2 str1=int(input())
3 print(months[str1])

DESCRIPTION OF SEGMENTAL FUNCTIONS

  

Input X and solve it according to the above piecewise function. If the input exceeds the range of x, the output "ERROR".

The code is as follows:

1 x=eval(input())
2 if -1<x<=0:
3   print('f(x)=-10')
4 elif 0<x<=1:
5   print('f(x)=5')
6 elif 1<x<=2:
7   print('f(x)=9.6')
8 else:
9   print('ERROR')

11. How many days are there this year?

366 leap years and 365 other years. A leap year is a year in which the average year (year not divisible by 100) can be divided by four. (For example, 2004 is a leap year, 1999 is not a leap year);

A leap year divides a century (a year divisible by 100) by 400. If 2000 is a leap year, 1900 is not a leap year.

The user enters a positive integer representing the year. How many days does the year have to be output?

The code is as follows:

'''
Nobody answered the question? Editor created a Python learning and communication QQ group: 857662006 
Look for like-minded friends, help each other, there are good video learning tutorials and PDF e-books in the group!
'''
 1 year=int(input())
 2 if year%100==0: Century Year
 3   if year%400==0:
 4     print('366')
 5   else:
 6     print('365')
 7 else:
 8   if year%4==0:
 9     print('366')
10   else:
11     print('365')

12. Verification Code Test

Users often need to enter validation codes when they log on to websites. The validation codes include upper and lower case letters and numbers, which appear randomly. When the user enters the authentication code, it is case-insensitive and can be verified as long as the characters appear in the correct order.
Write a program to complete the matching validation of the validation code, assuming that the currently displayed validation code is'Qs2X'.
If user input validation code correctly, output validation code correctly, and output validation code error when input error, please re-enter.

The code is as follows:

1 str1='Qs2X'
2 str2=input()
3 if str1.lower()==str2.lower():
4   print('Verification code is correct')
5 else:
6   print('Validation code error, please re-enter')

13. Sum of odd sequence

Seek 1+3+5+... + (2n-1) the sum of the first n terms.

The code is as follows: (This is really simple to send,,,,,,,,,,,,)

1 i=int(input())
2 s=0
3 for i in range(1,i*2,2):
4   s=s+i
5 print(s)

Topics: Python less ascii