【面试题】python代码

1.99乘法表

复制代码
for i in range(1, 10):
    for j in range(1, i+1):
        print(f'{i}*{j}={i*j}', end=' ')
    print()

2.求1-100内能被7整除和包含7的数

复制代码
l = []
for i in range(7,101):
    if i % 7 == 0:
        l.append(i)
    elif i % 10 == 7:
        l.append(i)
print(l)

3. 判断回文数的三种方法

双指针(时间复杂度和空间复杂度均为O(n)
复制代码
    def func(num):
        if not num:
            return False
        else:
            s = str(num)
            start_index = 0
            end_index = len(s)-1
            while start_index <= end_index:
                if s[start_index] != s[end_index]:
                    return False
                start_index += 1
                end_index -= 1
            return True


    print(func('0'))
逆序(时间复杂度和空间复杂度均为O(n))
复制代码
def func(s):
    if not s:
        return False
    s = str(s)
    return s[::] == s[::-1]


print(func('srtrs'))

4.给定字符串,不论大小写,统计文中出现的字符个数。

时间复杂度O(n).
  • 因为需要for循环遍历一次所有的字符,n为字符串长度
空间复杂度O(n)
  • 因为最坏的情况下,所有字符不同

    def func(s):
    char_dic = {}
    if not s:
    return None
    else:
    s = s.lower()
    for i in s:
    if i not in char_dic:
    char_dic[i] = 1
    else:
    char_dic[i] += 1
    return char_dic

    if name == 'main':
    t = "12w234WooOuU"
    print(f'字符个数分别是:{func(t)}')

5.1返回列表中的多数元素,多数元素是指元素重复出现的次数大于列表长度的一半

时间复杂度O(n)
  • 因为所有操作主要是线性操作的。for循环遍历可迭代对象中所有元素
空间复杂度O(n)
  • 针对字段list_dic和element,最坏的情况是存储所有元素。

    def func(obj):
    list_dic = {}
    if not obj:
    return None
    elif len(obj) == 1:
    return obj[0]
    else:
    for i in obj:
    if i not in list_dic:
    list_dic[i] = 1
    else:
    list_dic[i] += 1
    element = [key for key, value in list_dic.items() if value > len(obj)/2]
    return element

5.2返回列表中的多数元素,多数元素是指元素重复出现的次数大于列表长度的一半

时间复杂度O(n)
  • for循环遍历所有元素

空间复杂度O(1)
  • candidate变量只会存储一个值

复制代码
  def func(obj):
      candidate = None
      count = 0
      # 找出可能的多数元素.由于多数元素的数量超过列表长度的一半,所以即使其他元素试图"抵消"它的计数,它最终也会成为候选。
      for num in obj:
          if not count:
              candidate = num
              count += 1
          elif num == candidate:
              count += 1
          else:
              count -= 1
      # 判断是否为真的多数元素
      count = 0
      for num in obj:
          if num == candidate:
              count += 1
      if count > len(obj)//2:
          return candidate
      else:
          return None


  if __name__ == '__main__':
      t = [3, 3, 4, 2, 4, 4, 2, 4, 4]
      print(f'多数元素是:{func(t)}')
相关推荐
DanCheng-studio3 小时前
网安毕业设计简单的方向答疑
python·毕业设计·毕设
轻抚酸~3 小时前
KNN(K近邻算法)-python实现
python·算法·近邻算法
独行soc5 小时前
2025年渗透测试面试题总结-264(题目+回答)
网络·python·安全·web安全·网络安全·渗透测试·安全狮
汤姆yu5 小时前
基于python的外卖配送及数据分析系统
开发语言·python·外卖分析
如何原谅奋力过但无声6 小时前
TensorFlow 1.x常用函数总结(持续更新)
人工智能·python·tensorflow
翔云 OCR API6 小时前
人脸识别API开发者对接代码示例
开发语言·人工智能·python·计算机视觉·ocr
AndrewHZ7 小时前
【图像处理基石】如何在图像中提取出基本形状,比如圆形,椭圆,方形等等?
图像处理·python·算法·计算机视觉·cv·形状提取
温轻舟8 小时前
Python自动办公工具05-Word表中相同内容的单元格自动合并
开发语言·python·word·自动化办公·温轻舟
习习.y9 小时前
python笔记梳理以及一些题目整理
开发语言·笔记·python
撸码猿9 小时前
《Python AI入门》第10章 拥抱AIGC——OpenAI API调用与Prompt工程实战
人工智能·python·aigc