2020-04
15

反正这是我的答案

By xrspook @ 19:44:55 归类于: 扮IT

题目摆在这里,没有确切的答案,下面是我的解答,对不对不知道。words.txt资源在这里。

There are solutions to these exercises in the next section. You should at least attempt each one before you read the solutions.

Exercise 1: Write a program that reads words.txt and prints only the words with more than 20 characters (not counting whitespace).

Exercise 2: In 1939 Ernest Vincent Wright published a 50,000 word novel called Gadsby that does not contain the letter “e”. Since “e” is the most common letter in English, that’s not easy to do. In fact, it is difficult to construct a solitary thought without using that most common symbol. It is slow going at first, but with caution and hours of training you can gradually gain facility. All right, I’ll stop now. Write a function called has_no_e that returns True if the given word doesn’t have the letter “e” in it. Write a program that reads words.txt and prints only the words that have no “e”. Compute the percentage of words in the list that have no “e”.

Exercise 3: Write a function named avoids that takes a word and a string of forbidden letters, and that returns True if the word doesn’t use any of the forbidden letters. Write a program that prompts the user to enter a string of forbidden letters and then prints the number of words that don’t contain any of them. Can you find a combination of 5 forbidden letters that excludes the smallest number of words?

Exercise 4: Write a function named uses_only that takes a word and a string of letters, and that returns True if the word contains only letters in the list. Can you make a sentence using only the letters acefhlo? Other than “Hoe alfalfa”?

Exercise 5: Write a function named uses_all that takes a word and a string of required letters, and that returns True if the word uses all the required letters at least once. How many words are there that use all the vowels aeiou? How about aeiouy?

Exercise 6: Write a function called is_abecedarian that returns True if the letters in a word appear in alphabetical order (double letters are ok). How many abecedarian words are there?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
fin = open('words.txt') # 第1小问
for line in fin:
    if len(line) >= (20+2):
        word = line.strip()
        print(word)
# counterdemonstrations
# hyperaggressivenesses
# microminiaturizations
 
def has_no_e(word): # 第2小问
    for letter in word:
        if letter == 'e':
            return False
    return True
fin = open('words.txt')
all = 0
count = 0
for line in fin:
    word = line.strip()
    all = all + 1
    if has_no_e(word):
        print(word)
        count = count + 1
print(count, 'words without e')
print('{:.0%}'.format(count/all), 'words without e')
# ...
# zymosis
# zymotic
# zymurgy
# 37641 words without e
# 33% words without e
 
def avoids(word, x): # 第3小问,最后一个问题举手投降
    for letterw in word:
        for letterx in x:
            if letterw == letterx:
                return False
    return True
fin = open('words.txt')
x = input('withtout: ')
num = 0
# word = 'jwrojgre' # input('word is ')
# print(avoids(word, x))
for line in fin:
    word = line.strip()
    if avoids(word, x):
        num = num + 1
print(num, 'words without', x)
# withtout: aeiou
# 107 words without aeiou
# count = 0
# import itertools
# for i in itertools.combinations('abcdefghijklmnopqrstuvwxyz', 5):
#     print(''.join(i))
#     count = count + 1
# print(count) # 65780个排列组合的可能性啊啊啊啊啊啊
 
 
def uses_only(word, x): # 第4小问
    for letter in word:
        if letter not in x:
            return False
    return True
word = input('word is ')
x = input('uses is ')
print(uses_only(word, x))
# word is abc
# uses is efg
# False
 
def uses_all(word, x): # 第5小问
    for letter in x:
        if letter not in word:
            return False
    return True
fin = open('words.txt')
x = input('must use: ' )
num = 0
for line in fin:
    word = line.strip()
    if uses_all(word, x):
        num = num + 1
print(num, 'words with', x)
# must use: aeiou
# 598 words with aeiou
# must use: aeiouy
# 42 words with aeiouy
 
def is_abecedarian(word): # 第6小问
    index = 1
    while index < len(word) - 1:
        if ord(word[index-1]) > ord(word[index]):
            return False
        index = index + 1
    return True
fin = open('words.txt')
num = 0
for line in fin:
    word = line.strip()
    if is_abecedarian(word):
        num = num + 1
print(num, 'words is abecedarian')
# 1573 words is abecedarian
2020-04
15

字符偏移加密

By xrspook @ 13:28:09 归类于: 扮IT

本来我根本没考虑字母以外的那些怎么办,测试过参考答案以后,发现原来字母以外的东西原始输出,于是我也这般弄了,等于再加一个是否字母的判断,折腾。不告诉人家怎么把字符合并成字符串,我就只好准备两个对象二人转连接。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def rotate_word(something, n): # a-z: 97-122, A-Z: 65-90
    newletter1 = ''
    for letter in something:
        if ord(letter) < ord('A') or ord('Z') < ord(letter) < ord('a') or ord('z') < ord(letter):
            newletter2 = newletter1 + letter
        else:
            if ord(letter) + n > ord('z'):
                newletter2 = newletter1 + chr(ord(letter) + n - 26)
            elif ord('a') > ord(letter) + n > ord('Z'):
                newletter2 = newletter1 + chr(ord(letter) + n - 26)
            else:
                newletter2 = newletter1 + chr(ord(letter) + n)
        newletter1 = newletter2
    return newletter2
something = input('please write something: ')
n = int(input('how many shifts do you want: '))
print('before:', something)
print('after :', rotate_word(something, n))
# please write something: IBM
# how many shifts do you want: -1
# before: IBM
# after : HAL
# please write something: g858^h{O
# how many shifts do you want: 6
# before: g858^h{O
# after : m858^n{U
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
12

被微信的暗黑模式亮瞎了

By xrspook @ 12:26:01 归类于: 烂日记

我不知道微信是怎么想的,更新了新版本之后,微信里出现了暗黑模式。之前我一直期待着能有暗黑模式,但是当我有了以后我会发现这也太刺眼了吧!微信的暗黑模式不能直接在微信里面取消掉,我要在手机的系统里选择不用暗黑模式,微信的普通模式才会回来。在系统里用暗黑模式很正常,毕竟系统绝大多数的东西都是可控制的,所以其实当系统推出安全模式的时候,绝大多数的东西都是经过调整的,所以不会让你觉得非常刺眼。我的红米Note7还没有暗黑模式之前,语记就已经有了暗黑模式。语记的暗黑模式看上去很舒服,一直以来我都觉得这很不错,相比于传统的浅色来说,暗黑模式语记爽歪歪。之所以可以这样,是因为语记录入界面的东西并不多,里面就只出现少数几个颜色,一切都很搭。准确来说,语记的录入界面就只有大概5个颜色。一个是深灰色,一个是更深一点的灰色,一个是白色,一个是浅紫色,还有一个是蓝色。蓝色只在语音输入的部分出现。

但是,微信里的暗黑模式,不是这样!语记的录入界面没有花花绿绿的头像,但微信有!纯白色里看到花花绿绿,和深灰色里看到花花绿绿完全是不一样的感觉!有些用户或公众号的头像PS得不好,还有白色底纹你知道吗?暗黑之下就恶心了!比如说广州日报公众号!最要命的是进入对话框的时候,微信居然还一直沿用着他们的荧光绿色,虽然这个荧光绿色已经没有普通模式下的荧光了,但还是非常刺眼你知道吗!这样很要命,白色里面用荧光绿色,感觉还行,但是黑色里面还用略微暗淡的荧光绿色,这不是亮瞎别人的狗眼吗?这样的对话框还叫人家怎么聊下去。那个经典荧光绿的对话框,你起码得把颜色在调暗一点才行啊!码农你码字的时候有在你的界面搞一坨这样的暗淡荧光绿吗!

同样让人觉得非常要命的还有公众号里的每篇文章背景也统一用上了深色。这样的设计是非常恐怖的!!!为什么会这么说?如果所有文字都是默认形式显示,那还好一点,但是,为了一些警醒的作用,某些字体是设置了颜色的,比如红色、蓝色,以及各种荧光色。设计者在撰写的时候,默认的背景是浅色的,为了卖萌装嫩所以他们用了粉嫩的浅色,但在暗黑模式之下,那根本是亮瞎人的节奏你们有想过吗?要避免这种恐怖状况发生,微信必须有套算法自动转换公众号文章里的自定义颜色,使之适配暗黑,但没有,现在根本没有!同样,让人觉得很突兀的还有不少表情包的背景颜色是白色的,因为正常来说,不经过特殊处理,文章的背景是白色的,但是,在暗黑模式之下,想一想某篇文章里有无数多个白色方框,那是一个什么感觉!

其实微信暗黑模式这个东西他们已经测试了好长时间了,我感觉从第一批小白鼠算起,已经过了起码半年,但是这一趟微信更新,全部人都默认用上以后,效果简直让人骂街。用上MIUI 11以后,我的系统也换上暗黑模式,虽然我的手机不是amoled屏幕,所以其实暗黑模式不能为我省电。系统设置界面,我早就已经是暗黑模式了,那个东西感觉还不错。微信自己的设置页面,暗黑的情况其实还行的,但是他们根本无法控制公众号里面花花绿绿的各种自定义颜色。所以,是不是要每个公众号自己其实也有一个设置选项,选择默认浅色还是深色为他的背景呢?他们往后的创作也要基于那个设定。在微信想出一个万全之策之前,起码他们要在设置里给我们留一个手动选择要不要暗黑模式的选项,而不是让我们一律跟随系统。

不作死就不会死啊微信!!!!!

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