python每日一题总结7

20180618 qzd
总结:全

每日一题23


  1. 读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。

my code:

for i in range(7):
    n=int(input('input a int in 1-50 :'))
    print(n*'*')

code:

if __name__ == '__main__':
    n = 1
    while n <= 7:
        a = int(input('input a number:\n'))
        while a < 1 or a > 50:
            a = int(input('input a number:\n'))
        print (a * '*')
        n += 1
  1. 计算字符串中子串出现的次数。

code:

if __name__ == '__main__':
    str1 = input('请输入一个字符串:\n')
    str2 = input('请输入一个子字符串:\n')
    ncount = str1.count(str2)
    print(ncount)
  1. 从键盘输入一些字符,逐个把它们写到磁盘文件上,直到输入一个 # 为止。

code:

if __name__ == '__main__':
    from sys import stdout
    filename = input('输入文件名:\n')
    fp = open(filename,"w")
    ch = input('输入字符串:\n')
    while ch != '#':
        fp.write(ch)
        stdout.write(ch)
        ch = input('')
    fp.close()
  1. 从键盘输入一个字符串,将小写字母全部转换成大写字母,然后输出到一个磁盘文件"test"中保存。

code:

if __name__ == '__main__':
    fp = open('test.txt','w')
    string = input('please input a string:\n')
    string = string.upper()
    fp.write(string)
    fp = open('test.txt','r')
    print (fp.read())
    fp.close()
  1. 有两个磁盘文件A和B,各存放一行字母,要求把这两个文件中的信息合并(按字母顺序排列), 输出到一个新文件C中。

code:

if __name__ == '__main__':
    import string
    fp = open('test1.txt')
    a = fp.read()
    fp.close()
 
    fp = open('test2.txt')
    b = fp.read()
    fp.close()
 
    fp = open('test3.txt','w')
    l = list(a + b)
    l.sort()
    s = ''
    s = s.join(l)
    fp.write(s)
    fp.close()
  1. 列表转换为字典。

code:

i = ['a', 'b']
l = [1, 2]
print (dict([i,l]))

每日一题24 —— Similar RGB Color


(字符串处理) In the following, every capital letter represents some hexadecimal digit from 0 to f.
The red-green-blue color "#AABBCC" can be written as "#ABC" in shorthand. For example, "#15c" is shorthand for the color "#1155cc".
Now, say the similarity between two colors "#ABCDEF" and "#UVWXYZ" is -(AB - UV)^2 - (CD - WX)^2 - (EF - YZ)^2.
Given the color "#ABCDEF", return a 7 character color that is most similar to #ABCDEF, and has a shorthand (that is, it can be represented as some "#XYZ")

注意事项:

color is a string of length 7.
color is a valid RGB color: for i > 0, color[i] is a hexadecimal digit from 0 to f
Any answer which has the same (highest) similarity as the best answer will be accepted.
All inputs and outputs should use lowercase letters, and the output is 7 characters.

样例:

Input: color = "#09f166"
Output: "#11ee66"
Explanation:
The similarity is -(0x09 - 0x11)^2 -(0xf1 - 0xee)^2 - (0x66 - 0x66)^2 = -64 -9 -0 = -73.
This is the highest among any shorthand color.

在线练习

my code:

color=input("color = ")
while len(color)!=7:
    color=input("color = ")
#print('color = %s',color)
colorlist=list(color)
colorlist[:]=colorlist[1:]
#print(colorlist)
a=[]
b=[[0,0]]*3
for i in range(3):
    a.append(colorlist[2*i:2*i+2])
    if a[i][0] == a[i][1]:
        b[i]=a[i]
   
    else:
        c=int(a[i][0])*16+int(a[i][1])
        d=int(a[i][0])*16+int(a[i][0])
        e=int(a[i][1])*16+int(a[i][1])
        t= (e if abs(d-c)>abs(e-c) else d)
        b[i][0]=str(int(t/16))
        b[i][1]=str(t%16)
#print(b)
    b[i]=''.join(b[i])
output=''.join(b)
print("input: %s" %color)
print("output: #%s"%output)

每日一题25 — 翻转游戏

(字符串处理) 翻转游戏:给定一个只包含两种字符的字符串:+和-,你和你的小伙伴轮流翻转"++"变成"--"。当一个人无法采取行动时游戏结束,另一个人将是赢家。
编写一个函数,计算字符串在一次有效移动后的所有可能状态。
样例
给定 s = "++++", 在一次有效移动后,它会变成下列状态之一:

[
"--++",
"+--+",
"++--"
]

如果无法移动,则返回一个空列表[].
在线练习

my code:

class Solution:
    """
    @param s: the given string
    @return: all the possible states of the string after one valid move
    """
    def generatePossibleNextMoves(self, s):
        # write your code here
        a=[]
        for i in range(len(s)-1):
            b=s
            if (s[i]=='+') and (s[i+1]=='+'):
                b=list(b)
                b[i]='-'
                b[i+1]='-'
                c=''
                c=c.join(b)
                a.append(c)
        return a

停三天~i am sorry。

上一篇:每日一题6
下一篇:每日一题8

你可能感兴趣的:(python每日一题总结7)