Python tip challenge (16-25)

Keywords: encoding

16. Printing of RMB amount

When the bank prints the bill, it often needs to convert the RMB amount in Arabic numerals into capital letters. Now please complete such a procedure.
In Chinese capitalization, 0 to 10 and 100, 1000 and 10000 are successively expressed as: one hundred twenty-three, four fifty-six, seventy-nine million
The following example demonstrates the conversion rules from Arabic numerals to RMB capitalization:
1 circles
11 eleven yuan
111 one hundred and eleven yuan
101 one hundred and one yuan
1000 - 1000 yuan
1234567 one million two hundred and thirty four thousand five hundred and sixty seven yuan
Now I will give you an integer a (|a| < 100000000), please print out the capital expression of RMB.
For example: a = 90901001
Output: ninety nine hundred and eleven yuan

a = 90901001

digit = [u'Zero',u'One',u'Two',u'Three',u'Four',u'Five',u'Six',u'Seven',u'Eight',u'Nine']
weight = [u'circular',u'Ten',u'Bai',u'Thousand',u'ten thousand',u'Ten',u'Bai',u'Thousand']
Z = [(u'Zero Qian',u'Zero',u'Zero pick up',u'000',u'00',u'Zero',u'Zero circle'),(u'Zero',u'Zero',u'Zero',u'Zero',u'Zero',u'ten thousand',u'circular')]
num = str(abs(a)) #abs() absolute value str() integer converted to string
s = u'negative' if a<0 else ''
for i,x in enumerate(num): #enumerate()
    s += digit[int(x)] + weight[len(num)-i-1]
for z,v in zip(Z[0],Z[1]): #zip()
    s = s.replace(z,v) #str.replace(old,new)
print(u"One circle") if a==0 else print(s)

17. Number of common divisors

Give you two positive integers a and B, and output the number of their common divisors.
For example: a = 24, b = 36
Output: 6

a = 24
b = 36

print(len([x for x in range(1,min(a,b)+1) if a%x==0 and b%x==0]))

18. Maximum common divisor and minimum common multiple of inverse solution

The problem we often encounter is to give you two numbers and ask you to find the maximum common divisor and the minimum common multiple. Today we go the other way.
Give you two numbers a and b, and work out which two numbers are the greatest common divisor and the smallest common multiple respectively. Output these two numbers. The small ones are in the front and the large ones are in the back, separated by spaces. If there are multiple groups of solutions, output the group with the smallest sum. Note: all the data given have solutions, so it is not necessary to consider the situation without solutions.
For example: a=3, b = 60
Then output: 12 15

a = 3
b = 60

x = [x for x in range(1,max(a,b)+1) if max(a,b) % x == 0 ]
y = [int(a*b/i) for i in x]
z = [(x0,y0) for x0,y0 in zip(x,y)] #zip() returns an object composed of tuples
ans = sorted(z,key=sum)[0] #sorted(iterable,key=None,reverse=False)
print('%s %s' % (min(ans),max(ans)))  #The case of no solution and a=1 is not considered.

19. Single love song

Give you a string a, if it contains "LOVE" (LOVE is not case sensitive), then output "LOVE", otherwise output "SINGLE"
For example: a = "OurWorldIsFullOfLOVE"
Then output: LOVE

a = "OurWorldIsFullOfLOVE"

print("LOVE" if "love" in a.lower() else "SINGLE")

20. Information encryption

Give you a lowercase English string a and a non negative number b (0 < = b < 26). Replace each lowercase character in a with a letter larger than b in the alphabet. Here we connect z and a of the alphabet. If we go beyond z, we go back to a.
For example: a = "cagy", b=3,
Then output: fdjb

a = "cagy"
b = 3

print(''.join([chr(ord('a')+(ord(x)-ord('a')+b)%26) for x in a]))

a = "cagy"
b = 3

t = ""
for c in a:
    if 'a' <=c <= 'z':
        t += chr(ord('a') + (ord(c)-ord('a') + b) % 26)  #Ciphertext character = (original character + b)% 26
    else:
        t += c
print(t)
#chr(u) u is a Unicode code, which returns the corresponding characters
#ord(x) x is a character and returns its corresponding Unicode encoding

21. Palindrome substring

Give you a string a and a positive integer n to determine whether there is a palindrome substring of length N in a. If it exists, YES is output; otherwise, NO is output.
Definition of palindrome string: note that the string after str reverse is str1. If str=str1, then str is palindrome string, such as "abcba".

a = "adeda"  #String a
n = 5  #Palindrome substring of length n

result = "NO"
for i in range(len(a)):
    if (i+n<=len(a)):
        str = a[i:i+n]
        str1 = str[::-1]
        if (str==str1):
            result = "YES"
            break
print(result)

22. Time is money

Give you two time intervals st and ET (00:00:00 < = st < = et < = 23:59:59), please give the number of seconds between the two time intervals.
For example: st = "00:00:00", et = "00:00:10", output 10.

st="00:56:00"
et="01:00:10"

a = st.split(":")
b = et.split(":")
c = []
for i in range(3):
    c.append(int(b[i])-int(a[i]))
d=c[0]*3600+c[1]*60+c[2]
print(d)

st="00:56:00"
et="01:00:10"

import datetime
a = datetime.datetime.strptime(et,"%H:%M:%S")
b = datetime.datetime.strptime(st,"%H:%M:%S")
print((a-b).seconds)

23.365 Or 366?

How many days a year, this is a big problem, it is worth thinking about. Now I will give you a year(year is a four digit string, such as "2008", "0012"), and you will output the days of the year. If year = "2013", output 365.

year = "2019"

year1 = int(year)
if year1 % 4 == 0 and year1 % 100 != 0 or year1 %400 == 0:
    print(366)
else:
    print(365)

print(366 if int(year)%4==0 and int(year)%100!=0 or int(year)%400==0 else 365)

import calendar
print(366 if calendar.isleap(int(year)) else 365)
#Calendar.islap (year) leap year returns True, otherwise False

24. Take the lead

People who have played chess know that a horse can only walk in the shape of 'Day' (including the day when it rotates 90 °). Now imagine giving you a n-line m-column grid chessboard.
There is a horse in the lower left corner of the chessboard. Please count at least a few steps to move it to the upper right corner of the chessboard. If you can't reach it, the output is - 1.
If n=1, m=2, at least one step is required; if n=1, m=3, output - 1.

25. Format time

Posted by andrewdunstall on Thu, 24 Oct 2019 01:39:16 -0700