• 几个算法题解


    import math
    import time
    import random
    import string
    from scipy.stats import t
    
    #喊七,按照题目要求,实质就是对输入数据进行排序即可,由大到小、
    #喊七,逢七过,未知人数,未知什么时候结尾,在只知道每个人喊了多少次过的情况下,需要根据每个人喊过的次数来重新排列出他们分别是在几号位,即第几个开始喊的
    
    def seven_counts(player_num,ed_num):
        temp_list = ['过' if i % 7 == 0 or '7' in str(i) else i for i in range(1,ed_num)]
        temp_dict = {}
        all_value = []
        for i in range(math.ceil(len(temp_list)/player_num)):
            all_value.append(temp_list[player_num*i:(i+1)*player_num])
        for i in range(len(all_value)):
            for j in range(len(all_value[i])):
                if all_value[i][j] == '过' and j not in temp_dict.keys():
                    temp_dict[j] = 1
                elif all_value[i][j] == '过':
                    temp_dict[j] += 1
                else:
                    pass
        return temp_dict
    
    def seven_function():
        input_value = input('please input the num:\t')
        origin_list = input_value.split(' ')
        player_num = len(origin_list)
        counts = sum([int(i) for i in origin_list])
        s = 1
        while True:
            result = seven_counts(player_num,s)
            s += 1
            if sum(list(result.values())) == counts:
                for i in range(len(result.keys())):
                    print(result[i],end=' ')
                break
    
    #最长子字符串长度,首尾相连成环形、
    #统计以o结尾且为偶数位的长度,如果字符串中含o的个数为奇数,则取到前一个即可
    def length_str(strs):
        temp_list = []
        for i in range(len(strs)):
            if strs[i] == 'o':
                temp_list.append(i)
        if len(temp_list) % 2 == 0:
            print(temp_list[len(temp_list)-1] + 1)
        #     print(max(len(temp_list)))
        else:
            print(temp_list[len(temp_list) - 2] + 1)
    # 字符串统计及排序
    #统计一个字符串中每个字母出现的次数并排序,按照字母自然顺序排列
    def sort_str(strs):
        temp_dict = {}
        for word in strs:
            if word not in temp_dict.keys():
                temp_dict[word] = 1
            else:
                temp_dict[word] += 1
        for keys_word in string.ascii_letters:
            for j in temp_dict.keys():
                if j == keys_word:
                    print('%s:%s'%(j,temp_dict[j]),end=';')
    
    #字符串反转
    #将每个字符串做为一个基本元素对象进行翻转
    def reverse_words(strs):
        args_word = strs.split('\n')
        st_num = int(args_word[1])
        ed_num = int(args_word[2])
        words = args_word[0].split(' ')
        words[st_num:ed_num+1] = words[st_num:ed_num+1][::-1]
        print(' '.join(words))

    说明:如果有人看了这个文档之后,题目建议自己去找一下,我这边只是为了方便自己看一下逻辑,所以题目没有写的很清楚,只写了一些关键字。

  • 相关阅读:
    FastAdmin开发七牛云上传插件
    Vue3中插槽(slot)用法汇总
    react 实现监听逻辑
    【云原生】SpringCloud-Spring Boot Starter使用测试
    frp内网穿透并实现开机自启动
    Vue-CLI 项目搭建
    如何使用react-router v6快速搭建路由?
    SOFAJRaft与BRaft:打造稳定高效的分布式一致性架构
    社区便利店销售微信APP的设计与实现(源码+论文)_kaic
    js 统计一个字符串中某个字符出现的次数
  • 原文地址:https://blog.csdn.net/qq_44862918/article/details/126932811