Random String, Longest Word, GCD, and System Info

  • Share this:

Code introduction


This function generates a random string, finds the longest word in a text, calculates the greatest common divisor of two numbers, gets the current working directory, and returns a dictionary containing all this information.


Technology Stack : random, string, re, math, os, sys

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random
import string
import re
import math
import os
import sys

def generate_random_string(length, chars=string.ascii_letters + string.digits):
    return ''.join(random.choice(chars) for _ in range(length))

def find_longest_word(text):
    words = re.findall(r'\b\w+\b', text)
    longest_word = max(words, key=len)
    return longest_word

def xxx(arg1, arg2):
    # 生成一个指定长度的随机字符串
    random_string = generate_random_string(10)
    # 查找文本中最长的单词
    longest_word = find_longest_word(arg1)
    # 计算两个数字的最大公约数
    gcd = math.gcd(arg2, 18)
    # 获取当前工作目录
    current_directory = os.getcwd()
    # 打印系统版本信息
    sys_info = sys.version
    # 返回一个包含所有信息的字典
    return {
        "random_string": random_string,
        "longest_word": longest_word,
        "gcd": gcd,
        "current_directory": current_directory,
        "sys_info": sys_info
    }