用python做oj上的简单题

用python做oj上的简单题(持续更新中.......)_生死看淡,不服就干!-CSDN博客

py配置

# 单行注释:ctrl + /

# 多行注释1:CTRL + K + C

# 取消注释 :CTRL+K+U

# 多行注释2 :ALT + SHIFT + A

如何在VSCode中使用模板,插入作者等信息?_iKaChu的博客-CSDN博客_vscode添加作者信息

 9*9 乘法口诀表

def table():
    #在这里写下您的乘法口诀表代码吧!
    for i in range(1,10):
        for j in range(1,i+1):
            print('%d*%d=%d'%(i, j, i*j),end='\t')
        print()

if __name__ == '__main__':
    table()

遍历”Day1-homework”目录下文件

找到文件名包含“2020”的文件;

将文件名保存到数组result中;

按照序号、文件名分行打印输出。

注意:提交作业时要有代码执行输出结果。

#导入OS模块
import os
#待搜索的目录路径
path = "Day1-homework"
#待搜索的名称
filename = "2020"
#定义保存结果的数组
result = []

def findfiles():
    #在这里写下您的查找文件代码吧!
    number = 1
    for root, dirs, files in os.walk(path):
        print("当前正在遍历的文件夹:%s"%str(root))
        print("当前文件夹下所有目录的名字:%s"%str(dirs))
        print("当前文件夹中所有文件的名字:%s"%str(files))
        print("***************")
        for name in files:
            if filename in name:
                result.append(os.path.join(root, name))
    for aresult in result:
        print(aresult)
if __name__ == '__main__':
    findfiles()

linux常见命令

cd [目录名]:切换当前目录

pwd:查看当前目录的路径

ls:查看当前目录下的所有文件

cp [a.txt] [目标目录]:复制文件或目录

mv:移动文件

mkdir:创建文件夹

touch命令用于修改文件或者目录的时间属性,包括存取时间和更改时间。若文件不存在,系统会建立一个新的文件。

rm:删除一个目录中的一个或多个文件或目录,

gzip:压缩文件

tar:tar 本身不具有压缩功能,只具有打包功能,

vi和i    esc

1--100

# 1
def main(n):
    sum  = 1
    for i in range(1, n+1):
        sum = sum * i
    return sum

# 2
def main(a , b, c):
    return a+b+c

# 3
def main(lst, item):
    flag = 1
    for index,a in enumerate(lst):
        if(item == a):
            return index
            flag = 0
            break
    if(flag == 1):
        return '不存在'

# 4
def main(lst):
    sum = 0
    for i in lst:
        sum  = sum + i
    sum = sum / len(lst)
    lst1 = []
    for i in lst :
        if(i >= sum):
            lst1.append(i)
    return lst1

# 5
def main(p, q):
    return (p//q, p%q)

# 6
def main(num):
    sum = 0
    while(num // 10):
        sum = sum + num %10
        num = num // 10
    return sum + num

# 7
def main(lst):
    lst1 = []
    for i in lst:
        flag = 1
        for j in lst1:
            if(i == j):
                flag = 0
        if(flag == 1):
            lst1.append(i)
    return lst1

# 8
def main(lst):
    lst1 = []
    for item in lst:
        lst1.append(item.lower())
    return lst1

# 9
def main(lst):
    return sorted(lst , key = len,reverse= True)

# 10
from functools import reduce
from operator import mul

def main(n):
    return reduce(mul, range(1, n+1))

print(2432902008176640000)
print(265252859812191058636308480000000)
print(815915283247897734345611269596115894272000000000)

# 11
from operator import mul

def main(vector1, vector2):
    sum = 0
    for i, ii in enumerate(vector1):
        for j, jj in enumerate(vector2):
            if( i == j):
                sum = sum + mul(ii,jj)
    return sum

# 12
def main(lst):
    return list(sorted(lst, key=len,reverse =True))[0]

# 13
def main(lst):
    return list(filter(aa, lst))
def aa(n):
    return n

# 14
def main(lst):
    max = 0
    for i in lst:
        if(abs(i)>abs(max)):
            max = i
    return max

# 15
def main(lst):
    lst1 = []
    for i in lst:
        if(i%2==1):
            lst1.append(i)
    return lst1

# 16
def main(s):
    if(len(s)>=20):
        return s
    num = 20 -len(s)
    for i in range(0, num//2):
        s=s +'#'
        s = '#' +s
    if(num % 2):
        s = s +'#'
    return s

# 17
def main(s):
    str = list(s)
    for i in range(0, len(s)):
        if(s[i] == '0'):
            str[i] = '零'
        if(s[i] == '1'):
            	str[i] = '一'
        if(s[i] == '2'):
            str[i] = '二'
        if(s[i] == '3'):
            str[i] = '三'
        if(s[i] == '4'):
            str[i] = '四'
        if(s[i] == '5'):
            str[i] = '五'
        if(s[i] == '6'):
            str[i] = '六'
        if(s[i] == '7'):
            str[i] = '七'
        if(s[i] == '8'):
            str[i] = '八'
        if(s[i] == '9'):
            str[i] = '九'
    return ''.join(str)

# 18
def main(lst):
    lst.sort()
    if(lst[0] == lst[len(lst)-1]):
        return 0
    for i in range(0, len(lst)-1):
        if(lst[i]==lst[i+1]):
            return 2
    return 1

# 19
import re

def main(s):
    lst = []
    for i in range(0, len(s)):
        if(s[i].isalpha()):
            lst.append(s[i])
    for j in range(0, len(lst)):
        if(lst[j] != lst[len(lst)-j-1]):
            return False
    return True

#20
from collections import Counter

def main(s):
    dict = {}
    for i in s:
        if i not in dict:
            dict[i] = 1
        else:
            dict[i] += 1
    after = sorted(dict.items(),key=lambda e:e[1],reverse=True)
    num = 3
    list = []
    for key in after:
        list.append(key[0])
        num = num -1
        if(num==0):
            break
    return list

# 21
def isPrime(num):
    for i in range(2, num):
        if(num % i == 0):
            return False
    return True

def main(n):
    if isPrime(n) == True:
        return n
    while(n):
        n = n - 1
        if isPrime(n) == True:
            return n

# 22
import math
def main(n, i):
    return math.factorial(n)//(math.factorial(n-i)*math.factorial(i))

# 23
def main(n, a):
    sum  = map(lambda x, a:c(x, a), list(range(1, n+1, 1)), [a]*n)
    #print(sum)
    sum = list(sum)
    aaa = 0
    while(n):
        n = n - 1
        aaa = aaa + sum[n]
    return aaa
def c(x, a):
    if x == 1:
        return a
    sum = a
    while(x > 1):
        sum = sum + a *10
        a = a * 10
        x = x - 1
    return sum

# 24
def main():
    f = open('data24.txt', 'r')
    lines = f.read()
    data = lines.split(',')
    for i in range(0, len(data)):
        data[i] = int(data[i] )* 10
    return data
print(main())

# 25
def main(lst):
    s= lst[0]
    for i in lst:
        s = i|s
    return s

# 26
from math import sin, radians

def main(lst):
    for i in range(0, len(lst)):
        lst[i] = sin(radians(lst[i]))
    return lst

# 27
from datetime import date

def main(year1, month1, day1, year2, month2, day2):
    d1 = date(year1, month1, day1)
    d2 = date(year2, month2, day2)
    return abs(d1-d2).days

# 28
def main(year):
    if year % 4 == 0 and year % 100 != 0:
        return 'yes'
    elif year % 400 == 0:
        return 'yes'
    else:
        return 'no'

# 29
def main(func, lst):
    max = -999999
    for i in lst:
        if(max < func(i)):
            max = func(i)
    return max

# 30
def main(tup):
    
    Sum = 0
    Max = tup[0]
    Min = tup[0]
    for i in tup:
        Sum = Sum + i
        if Max < i:
            Max = i
        if Min > i:
            Min = i
    Sum = Sum - Max - Min
    Sum = Sum/(len(tup)-2)
    Sum =('%.1f' % Sum)
    return Sum

# 31
def main(n):
    a = 1
    b = 1
    sum = 0
    for i in range(0, n):
        sum = sum + a
        c = a
        a =b
        b = b + c
    return sum

# 32
def main(s, n = 3):
    return s * n

# 33
def main(s, n):
    return s[n:]+s[:n]

# 34
class Number:
    def __init__(self, value):
        self.value = value
    def __add__(self, another):
        return Number(self.value + another.value)
    def __str__(self):
        return ('{value}'.format(value=self.value))
    
def main(x, y):
    return x+y

# 35
class Number:
    def __init__(self, value):
        if isinstance(value,int):
            self.__value = value
        else:
            self.__value = 0
    def __set(self, value):
        if isinstance(value,int):
            self.__value = value
    def __get(self):
        return self.__value
    value = property(__get, __set)
    
def main(x, y):
    obj = Number(x)
    obj.value = y
    return (type(obj), obj.value)

# 36
def main(*para):
    sum = 0
    for i in para:
        sum = sum + 1 / i
    return round(1/sum, 1)

# 37
def main(n):
    num = n
    lst = [] 
    while(n):
        lst = lst + [n%10]
        n = n // 10
    lst.sort()
    Max = 0
    Min = 0
    for i in lst:
        Min = Min * 10 + i
    lst.sort(reverse=True)
    for i in lst:
        Max = Max * 10 + i
    if(Max - Min == num):
        return True
    return False

# 38
def main(n):
    sum = 0
    num = list(map(eval, str(n)))
    for i in num:
        sum = sum + i**len(num)
    if sum == n:
        return True
    return False

# 39
def main(lst):
    sum = 0
    for i in lst:
        sum = sum + abs(i)
    return sum

# 40
def main(s):
    if isinstance(s, list):
        return '参数必须为字符串'
    return (s.encode('utf-8'),s.encode('GBK'))

# 41
def main(s1, s2, *s3):
    if not s2 in s1:
        return False
    for i in s3:
        if not i in s1:
            return False
    return True

# 42
from math import pi as PI

def main(r):
    if not isinstance(r, int) or r <= 0:
        return '参数必须是大于0的整数或实数'
    return round(PI*r*r, 2)

# 43
from itertools import permutations

def main(lst):
    lt2 = map(str, lst)
    it = permutations(lt2,len(lst))
    m = map(lambda  x:''.join(x), it)
    return max(m)

# 44
def main(n):
    sum = 0
    num = 1
    for i in range(0, n*n):
        sum = sum + num
        num = num * 2
    return sum

# 45
def main(lst):
    str = ''
    max = 'a'
    if isinstance(lst, int):
        return '数据格式不正确'
    for i in lst:
        if isinstance(i, int):
            return '数据格式不正确'
        if i.lower() > max:
            max = i.lower()
            str = i
    return str

# 46
def main(lst):
    lst.sort(reverse = True)
    num = -6687485
    Max = 0
    sum = 0
    a = -5787585
    for i in lst:
        if a == i:
            sum = sum + 1
        else:
            sum = 1
            a = i
        if Max < sum:
            num = i
            Max = sum
    return num

# 47
from math import cos, radians

def main(a, b, theta):
    if not isinstance(a, (int,float,bool,complex)) or not isinstance(b, (int,float,bool,complex)) or not isinstance(theta, (int,float,bool,complex)):
        return '数据不对'
    if a <= 0 or b <= 0 or theta <=0 or theta >= 180:
        return '数据不对'
    c = round(pow(a*a + b*b -2*a*b*cos(radians(theta)),0.5),1)
    return c


# 48
def main(vector1, vector2):
    if not isinstance(vector1, list):
        return '数据不对'
    if not isinstance(vector2, list):
        return '数据不对'
    if len(vector1) != len(vector2):
        return '数据不对'
    sum = 0
    for i in range(0, len(vector1)):
        if not isinstance(vector1[i], (int, float, bool, complex)):
            return '数据不对'
        if not isinstance(vector2[i], (int, float, bool, complex)):
            return '数据不对'
        sum = sum + abs(vector1[i] - vector2[i])
    return sum

#49
def main(score):
    if not isinstance(score, (int, float,bool, complex)):
        return '数据不对'
    if score > 100 or score <0:
        return '数据不对'
    if score >= 90 and score <= 100:
        return 'A'
    if score >= 80 and score <= 89:
        return 'B'
    if score >= 70 and score <= 79:
        return 'C'
    if score >= 60 and score <= 69:
        return 'D'
    if score >= 0 and score <= 59:
        return 'F'

#50
def main(n):
    a = 1
    b = 1
    sum = 0
    Max = 0
    while True:
        sum = sum + a
        c = a
        a = b
        b = c + a
        if sum <= n:
            Max = sum
        if sum >= n:
            return Max + 1

#51
def main(n, m):
    for i in range(0, n+1):
        j = n - i
        if 2 * i + 4 * j == m:
            return (i, j)
    return '数据不对'

#52
def main(lst):
    Max = max(lst)
    list = []
    for index, item in enumerate(lst):
        if Max == item:
            list.append(index)
    return list

#53
from os.path import splitext

def main(s):
    sum = 0
    for i in range(0, len(s)):
        if s[i] == '.':
            sum = i
            break
    return s[:sum]+'_new'+s[sum:] 

#54
from itertools import combinations

def main(lst):
    lst1 = []
    for i in combinations(lst, 3):
        j = list(i)
        if j[0] + j[1] + j[2] == 10:
            lst1.append(i)
    return lst1

#55
def main(s):
    str =''
    for i in range(0, len(s)):
        flag = 1
        for j in range(0, len(s)):
            if s[i] == s[j] and i != j:
                flag = 0
                break
        if flag == 1 :
            str = str + s[i]
    return str

#56
def main(lst):
    lst.sort()
    if lst[0] == lst[len(lst)-1]:
        return 0
    for i in range(0, len(lst)-1):
        if lst[i] == lst[i+1]:
            return 2
    return 1

#57
def main(s):
    #return s
    s= s.strip()
    s = list(s)
    str = ''
    for i in range(0, len(s)-1):
        if i == len(s):
            break
        if s[i] == ' 'and s[i+1] == ' ':
           a = 1 
        else:
            str = str + s[i]
    return str + s[len(s)-1]

#58
from re import findall

def main(s):
    
    lst1 = []
    lst2 = []
    for i in s:
        if i >='0' and i<= '9':
            lst1.append(i)
        else:
            sum = 0
            if lst1 != []:
                for j in lst1:
                    jj = int(j)
                    sum = sum*10 + jj
                    lst2.append(sum)
            lst1 = []
    sum = 0
    if lst1 != []:
        for j in lst1:
            jj = int(j)
            sum = sum*10 + jj
            lst2.append(sum)
            lst1 = []
    if lst2 != []:
        return max(lst2)
    return '没有数字'

#59
import math
from functools import reduce

def main(lst):
    return reduce(math.gcd, lst)

#60
from docx import Document

def main():
    file = Document(r'data60.docx')
    for i in range(len(file.paragraphs)):
        if '山东' in file.paragraphs[i].text and '烟台' in file.paragraphs[i].text:
            return file.paragraphs[i].text
            
print(main())

#61
from docx import Document

def main():
    sum = 0
    document = Document(r'data60.docx')
    tables = document.tables
    table = tables[0]
    for i in range(0, len(table.rows)):
        for j in range(0, len(table.columns)):
            result = table.cell(i,j).text
            sum = sum + int(result)
    return sum
print(main())

#62
def main(s):
    sum = 0
    lst = []
    for i in range(len(s)-1, -1, -1):
        if s[i]!= ',' and (s[i] <'0' or s[i]>'9' ):
            return '数据错误'
        if s[i] == ',':
            if sum == 3:
                sum = 0
            else:
                return '数据错误'
        else:
            sum = sum + 1
            lst.insert(0, s[i])
        if(sum > 3):
            return '数据错误'
    return ''.join(lst)

#63
def main(s):
    if s[0] != '0':
        lst = [s[0],]
    else:
        lst = []
    for i in range(1,len(s),1):
        if (s[i-1] == '-' or s[i-1] == ':' or s[i-1] == ' ' )and s[i] == '0':()
        else:
            lst.append(str(s[i]))
    return ''.join(lst)

#64
def main(s):
    str = s.replace('-', ':').replace(' ', ':').split(':')
    for i in range(1, len(str)):
        num  = int(str[i])
        if num // 10 == 0 and len(str[i]) == 1:
            str[i] = '0' + str[i]
    return str[0]+ '-' + str[1] + '-' + str[2] + ' ' + str[3] + ':' + str[4] + ':' + str[5]

#65
def main(num):
    lst  = []
    sum = 0
    while(num):
        if sum == 3:
            lst.append(',')
            sum = 0
        lst.append(str(num%10))
        num = num // 10
        sum = sum + 1
        
    return ''.join(lst[::-1])

#66
def main(num):
    if not isinstance (num,int):
        return '数据错误'
    list = []
    while(num):
        list.append(',')
        list.append(str(num%10))
        
        num = num // 10
    return ''.join(list[len(list):0:-1])

#67
def isPrime(k):
    if k in (2,3):
        return True
    if k%2 == 0:
        return False
    for i in range(3, int(k**0.5)+2, 2):
        if k%i == 0:
            return False
    return True

def main(start, num):
    while(start):
        if isPrime(start):
            num = num - 1
        if num == 0:
            return start
        start = start + 1

#68
def main(num):
    while num % 2 ==0:
        num = num / 2
    while num % 3 == 0:
        num = num / 3
    while num % 5 == 0:
        num = num / 5
    if num == 1:
        return True
    else :
        return False

#69
def main(lst):
    return sorted(lst, key=lambda x : c(x),)
def c(x):
    sum = 0
    while x:
        sum = x + sum % 10
        x = x // 10
    return sum

#70
def main(lst):
    sum1 = 0
    sum2 = 0
    for i in range(len(lst)):
        if i % 2 == 1:
            sum1  = sum1 + lst[i]
        else:
            sum2 =  sum2 + lst[i]
    return (sum1, sum2)

#71
def main(lst):
    try:
        list(map(lambda x: x%10, lst))
    except:
        return False
    else:
        return True

#72
def main(lst):
    if not isinstance(lst, list):
        return '数据不符合要求'
    lst1 = []
    for i in lst:
        if not isinstance(i, int):
            return '数据不符合要求'
        if i > 8 and i % 2 ==0:
            lst1.append(i)
    return lst1    

#73
def main(s1, s2):
    sum = 0
    length = min(len(s1), len(s2))
    for i in range(length):
        if s1[i] == s2[i]:
            sum = sum + 1
    return sum

#74
def main(s1, s2):
    for i in range(len(s1)):
        ii = i
        for j in range(len(s2)):
            if s1[ii] != s2[j]:
              break
            else:
              
              ii = ii +1
              if ii == len(s1):
                
                return s1 + s2[j+1::]
    return s1 + s2

#75
from math import gcd
from functools import reduce

def main(*integers):
    if len(integers) == 0:
        return '必须提供至少一个整数'
    aa = integers[0]
    for i in integers:
        if not isinstance(i, int):
            return '必须都是整数'
        aa = gcd(aa, i)
    return aa

#76
def main(n):
    str = bin(n)
    sum = 0
    for i in str[::-1]:
        if i != '0':
            break
        sum  = sum + 1
    return sum 

#77
def main(n):
    sum = 1
    for i in range(n-1):
        sum = (sum + 1) * 2
    return sum

#78
from operator import itemgetter
from docx import Document
from docx.shared import RGBColor

def main():
    word = Document(r'data78.docx')
    colors = {}
    for p in word.paragraphs:
        for r in p.runs:
            color = r.font.color.rgb
            if color not in (RGBColor(0,0,0),None):
                colors[color] = colors.get(color, 0) + 1
    colors = sorted(colors.items(), key=itemgetter(1), reverse = True)
    return tuple(map(str, map(itemgetter(0), colors[:3])))

print(main())

#79
from operator import mul
from functools import reduce

def main(lst):
    num = 1
    for i in lst:
        num = mul(num, i)
    return num

#80
from operator import sub
from functools import reduce
def main(a, b):
    return sum(list(map(lambda x, y: abs(sub(x, y)), a, b)))

#81
def main(a, b):
    return (a//b, a%b)

#82
def main(s1, s2):
    dict = {}
    for i in s2:
        if dict.get(i, 0) == 0:
            for j in s1:
                if i == j:
                    dict[i] = dict.get(i,0) + 1
    sum = 0    
    for key in dict:
        sum = sum + dict[key]
    return sum

#83
def main(start, end):
    return sum(range(start, end+1))

#84
def main(data):
    return max(data, key=abs)

#85
def main(data):
    data_local = data[:]
    data_local = sorted(data_local)
    #return data_local
    return data_local[len(data_local)//2]

#86
def main(num):
    for n in range(2, num+1)[::-1]:
        flag = 1
        for i in range(2, int(n**0.5)+1):
            if n%i == 0:
                flag = 0
                break
        if flag == 1:
            result = n
            break
    return result

#87
x = 3

def main(num):
    return num
    x = num
    return globals()['x']

#88
from math import pi as PI

def main(r):
    if isinstance(r, (int,float)) and r >= 0:
        return round(PI*r*r, 3)
    else:
        return '半径必须为大于0的整数或实数'

#89
def main(origin, userInput):  
    return sum(map(lambda oc, uc:oc == uc, origin, userInput))

#90
from itertools import cycle

def main(lst, k):
    n = len(lst)
    if n == 1:
        return lst[0]
    result = 0
    for i in range(2, n+1):
        result = (result+ k)  % i
    return lst[result]

#91
def main(s):
    lst = list(s)
    for i in range(len(lst)):
        if lst[i] == 'a':
            lst[i] = 'A'
        if lst[i] == 'e':
            lst[i] = 'E'
        if lst[i] == 'o':
            lst[i] = 'O'
        if lst[i] == 'i':
            lst[i] = 'I'
        if lst[i] == 'u':
            lst[i] = 'U'
    return ''.join(lst)

#92
def main(s):
    sum = 0
    for i in s:
        if i == ' ':
            sum = sum + 1
        else:
            break
    return sum

#93
import datetime
def main(year, month, day):
    t1 = datetime.datetime(year, month, day)
    t2 = datetime.datetime(year, 1 , 1)
    return (t1 - t2).days + 1

#94
def main(data):
    for i in range(len(data) -1):
        if data[i] > data[i+1]:
            return False
    return True

#95
from pypinyin import pinyin

def main(s):
    lst = list(s)
    lst.sort(key = lambda char:pinyin(char)[0][0])
    return ''.join(lst)

#96
def main(pwd):
    #return pwd
    if len(pwd) < 6:
        #return len(pwd)
        return 'weak'
    num = 0
    astr = 0
    bstr = 0
    cstr = 0
    for i in pwd:
        if i<= '9' and i >= '0':
            num = 1
        if i >= 'a' and i <= 'z':
            astr = 1
        if i >= 'A' and i <= 'Z':
            bstr = 1
        if i == ',' or i == '.':
            cstr = 1
    if (num + astr + bstr + cstr) == 1:
        return 'weak'
    if num + astr + bstr + cstr == 2:
        return 'below_middle'
    if num + astr + bstr + cstr == 3:
        return 'above_middle'
    if num + astr + bstr + cstr == 4:
        return 'strong'
    return weak

#97
def main(s):
    sum1 = 0
    sum2 = 0
    for i in s:
        if i >= 'a' and i <= 'z':
            sum1  =  sum1 + 1
        if i >= 'A' and i <= 'Z':
            sum2 = sum2 + 1
    return (sum2, sum1)

#98
from functools import reduce

def main(factors, x):
    return reduce(lambda c1, c2: c1*x+c2, factors)

#99
def main(n):
    #return n
    lis = [0] * 100
    lis[1] = 1
    lis[2] = 1
    lis[3] = 1
    for i in range(1 ,n+1):
        lis[i + 1] = lis[i+1] + lis[i] 
        lis[i + 2] = lis[i+2] + lis[i] 
        lis[i + 3] = lis[i+3] + lis[i] 
    return lis[n]

#100
def main():
    Max = 0
    with open('data100.txt', 'r') as f:
        for line in f.readlines():
            line = line.strip('\n')
            if len(line) > Max:
                Max = len(line)
    return Max+1

print(main())

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

z6q6k6

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值