1.已知一个数字列表,求列表中心元素。
if len(nums) & 1 == 0:
print(nums[len(nums)/2], nums[len(nums)/2 -1])
else:
print(nums[(len(nums)+1)/2-1])
2.已知一个数字列表,求所有元素和。
sum = 0
for num in nums:
sum += num
print(sum)
3.已知一个数字列表,输出所有奇数下标元素。
index = []
for i in range(len(nums)):
if nums[i] & 1 == 1:
index.append(i)
print(i)
4.已知一个数字列表,输出所有元素中,值为奇数的元素。
index = []
for num in nums:
if num & 1 == 1
print(num,end=" ")
5.已知一个数字列表,将所有元素乘二。
例如:nums = [1, 2, 3, 4] —> nums = [2, 4, 6, 8]
for i in range(len(nums)):
nums[i] = 2*nums[i]
print(nums)
6.有一个长度是10的列表,数组内有10个人名,要求去掉重复的
例如:names = ['张三', '李四', '大黄', '张三'] -> names = ['张三', '李四', '大黄']
name_new = []
for i in range(10):
if names[i] in name_new:
continue
else:
name_new.append(names[i])
print(name_new)
7.已经一个数字列表(数字大小在0~6535之间), 将列表转换成数字对应的字符列表
例如: list1 = [97, 98, 99] -> list1 = ['a', 'b', 'c']
for i in range(len(nums)):
nums[i] = chr(nums[i])
print(nums)
8.用一个列表来保存一个节目的所有分数,求平均分数(去掉一个最高分,去掉一个最低分,求最后得分)
sum = 0
for j in range(len(scores)-1)
for i in range(len(scores)-1-j):
if scores[i] > scores[i+1]:
a = scores[i]
scores[i] = scores[i+1]
scores[i+1] = a
else:
pass
for i in range(1, len(scores)-1):
sum += scores[i]
avg = sum/(len(scores)-2)
print(avg)
9.有两个列表A和B,使用列表C来获取两个列表中公共的元素
例如: A = [1, 'a', 4, 90] B = ['a', 8, 'j', 1] --> C = [1, 'a']
A = ['路飞','山治','索隆','乌索普','鸣人','一护','露琪亚','恋次','五河','纱雾','486','董香','两仪式','琦玉']
B = ['剑八','友哈','一方','董香','五河','井上','486','路飞','琦玉','一护','雨龙','玉藻前','saber']
C = []
for a in A:
for b in B:
if a == b:
C.append(a)
print(C)
10.有一个数字列表,获取这个列表中的最大值.(注意: 不能使用max函数)
例如: nums = [19, 89, 90, 600, 1] —> 600
for i in range(len(nums)-1):
if nums[i] > nums[i+1]:
a = nums[i]
nums[i] = nums[i+1]
nums[i+1] = a
else:
pass
print(score[i+1])
11.获取列表中出现次数最多的元素
例如:nums = [1, 2, 3,1,4,2,1,3,7,3,3] —> 打印:3
list1 = ['周杰伦','林俊杰','吴亦凡','蔡徐坤','邓紫棋','蔡徐坤','庞麦郎','蔡徐坤','庞麦郎','蔡徐坤','伍佰','林忆莲','周杰伦','面筋哥','蔡徐坤','吴亦凡','邓紫棋','吴亦凡','邓紫棋','蔡徐坤']
list_new = []
list_count = []
for i in range(len(list1)):
list_new.append('_')
while len(list1) > 0:
list_new[0] = list1[0]
count = 0
for a in list1[:]:
if a == list_new[0]:
list_new[count] = a
count += 1
list1. remove(a)
list_count.append(str(count)+'个')
list_count.append(list_new[count-1])
print(list_count)
1.一张纸的厚度大约是0.08mm,对折多少次之后能达到珠穆朗玛峰的高度(8848.13米)?
height = 0.08
times = 0
while height < 8848.13:
height *= 2
times += 1
print(times)
2. 古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
month1 = 1
month2 = 2
n = int(input('Please input the month: '))
if n < 3:
print(1)
else:
for i in range(n-2):
monthn = month2+month1
month1 = month2
month2 = monthn
print(monthn)
3. 将一个正整数分解质因数。例如:输入90,打印出90=2x3x3x5。
num = int(input('Please input a integer: '))
i = 2
while num != 1:
if num/i == num//i:
num /= i
print(i, ' *', end=' ')
else:
i += 1
print(1)
4. 输入两个正整数m和n,求其最大公约数和最小公倍数。 程序分析:利用辗除法。
m, n = int(input('m: ')).split(",")
if n > m:
temp = n
n = m
m = temp
plus = m*n
while True:
mod = m%n
m = n
n = mod
if m%n == 0:
break
lcm = plus/mod
print(mod, lcm)
5. 一个数如果恰好等于它的因子之和,这个数就称为 "完数 "。例如6=1+2+3. 编程 找出1000以内的所有完数
for num in range(2, 1001):
factors = []
sum = num
for i in range(1, num):
if num%i == 0:
factors.append(i)
sum -= i
if sum == 0 and i >= num/2:
print(num, factors)
break
6.输入某年某月某日,判断这一天是这一年的第几天? 程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。
sum = 0
year = int(input('Please input the year: '))
month = int(input('Please input the month: '))
day = int(input('Please input the day: '))
while not (1 <= month <=12 and 1 <= day <= 31):
print('Please input the date: ')
month_day_nums = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
sum = month_day_nums[month - 1]+day
if year%400 == 0 or (year%4 == 0 and year%100 != 0):
if month > 2:
sum += 1
print(sum)
7. 某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。求输入的四位整数加密后的值
num = input("Please input the integer: ")
new = []
for i in num:
new.append((int(i)+5)%10)
new[0], new[1] = new[3], new[2]
for i in new:
print(i, end='')
8. 获取第n个丑数。 什么是丑数: 因子只包含2,3,5的数
6 =1* 2*3 -> 丑数
2 = 1*2 -> 丑数
7 = 1*7 -> 不是丑数
1, 2, 3, 4, 5, 6, 8,9,10, 12 ….
n = int(input("Please input a integer: "))
factors = []
count = 0
i = 2
while count == n:
num_ = 2
num_ += 1
num = num_
factors = []
flag = 0
i = 2
while num != 1:
if num/i == num//i:
num /= i
factors.append(i)
else:
i += 1
for x in factor:
if not(x == 2 or x == 3 or x == 5):
flag = 1
if flag == 0:
count += 1
print('wo bu hui a!-!')