2020-04
14

一句秒杀一段话

By xrspook @ 19:52:09 归类于: 扮IT

还记得初中的时候数学老师跟我说初等数学比高等数学难多了,幸好,我这辈子暂时只学过高等数学,而且几乎都还给大学老师了……

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# def first(word):
#     return word[0]
# def last(word):
#     return word[-1]
# def middle(word):
#     return word[1:-1]
# def is_palindrome(word):
#     if len(word) <= 1:
#         return True
#     elif first(word) != last(word):
#         return False
#     else:
#         return is_palindrome(middle(word))
def is_palindrome(word):
    return word[::-1] == word
word = input('word is ')
print(is_palindrome(word))
# word is qwerreq
# False
# word is poiuuiop
# True
2020-04
13

制表符

By xrspook @ 19:14:09 归类于: 扮IT

题目本身很简单,对我这种新手难就难在完全靠自己去摸索格式。这本书之前根本没说过要怎么输出制表符,一个制表符不能解决问题的时候要连续用2个,幸好这里两个就够了,如果超过16个字符,还得3个或以上制表符。于是明明很简单的print输出里面除了套个必须有的while以外还得来一对if-else分开整除了和有尾数的情况。为什么出题的人就这么喜欢超纲呢?????

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import math
def mysqrt(a):
    x  = 10
    while True:
        y = (x + a/x) / 2
        if abs(y - x) < 1e-11:
            break
        x = y
    return y
def test_squre_root():
    print('a\tmysqrt(a)\tmath.sqrt(a)\tdiff')
    print('-\t---------\t------------\t----')
    a = 1
    while a < 10:
        if mysqrt(a)%1 == 0:
            print(str(float(a))+'\t'+str(float('%.11f'% mysqrt(a)))+'\t\t'+str(float('%.11f'% math.sqrt(a)))+'\t\t'+str(float('%.11e'% abs(mysqrt(a)-math.sqrt(a)))))
        else:
            print(str(float(a))+'\t'+str(float('%.11f'% mysqrt(a)))+'\t'+str(float('%.11f'% math.sqrt(a)))+'\t'+str(float('%.11e'% abs(mysqrt(a)-math.sqrt(a)))))
        a = a + 1
test_squre_root()
2020-04
11

辗转相除法

By xrspook @ 20:51:51 归类于: 扮IT

题目是这样的

Exercise 5: The greatest common divisor (GCD) of a and b is the largest number that divides both of them with no remainder. One way to find the GCD of two numbers is based on the observation that if r is the remainder when a is divided by b, then gcd(a, b) = gcd(b, r). As a base case, we can use gcd(a, 0) = a. Write a function called gcd that takes parameters a and b and returns their greatest common divisor.

为什么从前我学最大公约数的时候就不是用这个方法呢?如果数字很大,这个应该很快吧,以前我们用的是质因数分解法,我一直没用过其它方法。这里用的是辗转相除法,也叫欧几里德算法,估计在外国这是基础算法,否则Excel也不会有GCD(A, B)这个求最大公约数的公式……

深感自己的无知……

1
2
3
4
5
6
7
8
9
10
11
12
def gcd(a, b): # 可以在Excel里用GCD(a, b)函数测试结果
    if b == 0:
        return a 
    else:
        return gcd(b, a%b) # b不断取代a,b不断被a%b取代
a = int(input('a is '))
b = int(input('b is '))
print(gcd(a, b))
# result
# a is 1024
# b is 480
# 32
2020-04
10

判断a是不是b的幂

By xrspook @ 23:59:43 归类于: 扮IT

Think Python 2第六章的某道原题是这样的:

Exercise 4: A number, a, is a power of b if it is divisible by b and a/b is a power of b. Write a function called is_power that takes parameters a and b and returns True if a is a power of b. Note: you will have to think about the base case.

版本A的中文翻译

练习4:一个数字a为b的权(power),如果a能够被b整除,并且a/b是b的权。写一个叫做is_power 的函数接收a和b作为形式参数,如果a是b的权就返回真。注意:要考虑好基准条件。

版本B的中文翻译

习题6-4:当数字a能被b整除,并且a/b是b的幂时,它就是b的幂。编写一个叫is_power的函数,接受两个参数a和b,并且当a是b的幂时返回True。注意:你必须要想好基础情形。

研究这道题到底在说什么,我纠结了起码1个小时。什么叫做权?幂来幂去,还有“它”,你真的知道那指代的是什么?我相信翻译这本书的人都一定是编程老手,但你们真的有琢磨过中文表述是否恰当吗?幸好我没买人民邮电出版社出版的Think Python 2中文版,貌似从某些页面看来,翻译也会让我非常吐槽。

还是直接看英文原文比较好懂,那段话我会这么翻译:

如果a能被b整除,且a/b是b的幂,那么a是b的幂(例如:2**3=8,即2的3次幂等于8,a=8, b=2)。编写一个名叫is_power的函数,以a和b为形式参数接收数据,如果a是b的幂,返回True。注意:要考虑好基准条件。

研究这道题在说什么研究了好长时间,随便输入几个显而易见的测试数据也挺顺利, 但要把情况想周全貌似很不简单。a == 0的情况有人想到了,但万一作死的写了b == 0呢!测试过好几个网友的编程,b == 0几乎全部挂了…… 我承认,这样测试过分了。

1
2
3
4
5
6
7
8
9
10
11
12
def is_power(a, b):
    if a == 0 or b == 0 or a%b != 0: # a和b的特殊情况先杀死
        return False
    elif a == 1 or a == b: 
        return True
    else:        
        return is_power(a/b, b) # 被除数a为0会死循环,除数b不能为0
a = 4 # int(input('a is '))
b = 2 # int(input('b is '))
print('a =', a)
print('b =', b)
print(is_power(a, b))
2020-04
9

转换时间戳

By xrspook @ 15:18:47 归类于: 扮IT

脑袋实在转不过来,不知道怎么把1W多天转化为月份,长短月怎么处理?????所以直接暴力地使用本来就内置的time.localtime()以及time.strftime()格式化当前时间。我完全可以把逝去的时间也这么整,但逝去时间我是有认真人肉计算过的,所以同一个时间戳,用了两种方式转化。

经过这个以后,我完全明白点点导出数据里那一串标记着的数字是什么鬼,也不能说他们这样不好,因为转化为实在人肉可读的时间格式以后万一要用其它表达方式呢?一开始就用最原始的东西,前端表达用内置的函数格式化也就可以了。所以,大概看懂的人会会心微笑,看不懂的人会骂街。作为理论上应该可读的XML文件,他们这般“原始输出”实在够姜!

1
2
3
4
5
6
7
8
9
10
11
12
13
import time
def now(num):
    mytime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(num))
    return(str(mytime))
num = int(time.time())
sec = num % 60
min = (num // 60) % 60
hour = (num // (60 * 60)) % 24 + 8 # 万恶的东八区!!!!!!!!!!
day = num // (60 * 60 * 24)
print('Time is', now(num))
print('since the epoch, ' + str(day) + ' days ' + str(hour) + ' hours ' + str(min) + ' minutes ' + str(sec) + ' seconds has gone')
# Time is 2020-04-09 15:03:41
# since the epoch, 18361 days 15 hours 3 minutes 41 seconds has gone

© 2004 - 2024 我的天 | Theme by xrspook | Power by WordPress