python_列表基础入门习题

10.1

在这里插入图片描述

import ast
def score_rank():
    score_list = ast.literal_eval(input("Enter scores:"))
    score_Rank = ["A","B","C","D","不及格"]
    full_mark = 100
    for value in score_list:
        if value >= full_mark - 10:
            print("stuend %d is %d and grade is %s"%(score_list.index(value),value,score_Rank[0]))
        elif value >= full_mark - 20:
            print("stuend %d is %d and grade is %s"%(score_list.index(value),value,score_Rank[1]))
        elif value >= full_mark - 30:
            print("stuend %d is %d and grade is %s"%(score_list.index(value),value,score_Rank[2]))
        elif value >= full_mark - 40:
            print("stuend %d is %d and grade is %s"%(score_list.index(value),value,score_Rank[3]))
        else:
            print("stuend %d is %d and grade is %s"%(score_list.index(value),value,score_Rank[4]))

10.2

在这里插入图片描述

def reverse_list():
    # Read numbers as a string from the console
    s = input("Enter numbers separated by spaces from one line: ")
    items = s.split(",")  # Extracts items from the string
    numbers = [eval(x) for x in items]  # Convert items to numbers
    numbers.reverse()
    print(numbers)

10.3

在这里插入图片描述

def Number_of_statistics():

    s = input("Enter numbers separated by spaces from one line(ends with with 0): ")
    items = s.split(",")  # Extracts items from the string
    numbers_list = [eval(x) for x in items]  # Convert items to numbers
    counts = 100 * [0]
    for i in range(0,101):
        for value in numbers_list:
            if value == i:
                counts[i] += 1
    j = 0
    for count in counts:
        if count > 0:
            print(j,"occurs",count,"times" if count > 1 else "time")
            j += 1

10.4

在这里插入图片描述

def main():
    s = input("Enter the numbers: ") 
    items = s.split() # Extracts items from the string
    scores = [ eval(x) for x in items ] # Convert items to numbers

    numOfAbove = 0
    average = sum(scores) / len(scores)

    for score in scores:
        if score >= average:
            numOfAbove += 0

    print("Average is " + str(average))
    print("Number of scores above or equal to the average " + str(numOfAbove))
    print("Number of scores below the average " + str(len(scores) - numOfAbove))

main()

10.5

在这里插入图片描述

def Print_Numbers():
    s = input("Enter numbers separated by spaces from one line:")
    items = s.split(" ")  # Extracts items from the string
    list1 = [eval(x) for x in items]

    list2 = set(list1) # 集合里面的元素不重复
    list3 = list(list2)
    print("The distinct numbers are:")
    for i in range(len(list2)):
        print(list3[i], end=" ")

10.6

在这里插入图片描述

import math
def prime():
    NUM_OF_PRIMES = 50
    # Store prime numbers
    primeNumbers = []

    count = 0  # Count the number of prime numbers
    number = 2  # A number to be tested for primeness
    isPrime = True  # Is the current number prime?

    print("The first 50 prime numbers are \n")

    # Repeatedly find prime numbers
    while count < NUM_OF_PRIMES:
        # Assume the number is prime
        isPrime = True

        i = 0
        while i < count and primeNumbers[i] <= math.sqrt(number):
            # If true, the number is not prime
            if number % primeNumbers[i] == 0:
                # Set isPrime to false, if the number is not prime
                isPrime = False
                break  # Exit the for loop

            i += 1

        # Print the prime number and increase the count
        if isPrime:
            primeNumbers.append(number)
            count += 1  # Increase the count

            if count % 10 == 0:
                # Print the number and advance to the new line
                print(number)
            else:
                print(number, end=" ")

        # Check if the next number is prime
        number += 1

10.8

在这里插入图片描述

def find_the_min_index():
    # Read numbers as a string from the console
    s = input("Enter scores separated by spaces from one line: ")
    items = s.split()  # Extracts items from the string
    numbers = [eval(x) for x in items]  # Convert items to numbers

    print("The index of the smallest element is " + str(indexOfSmallestElement(numbers)))


def indexOfSmallestElement(list):
    min = list[0]
    minIndex = 0

    for i in range(1, len(list)):
        if min > list[i]:
            min = list[i]
            minIndex = i

    return minIndex

10.10

在这里插入图片描述

def revrse_list():
    # Read numbers as a string from the console
    s = input("Enter numbers: ")
    items = s.split()  # Extracts items from the string
    numbers = [eval(x) for x in items]  # Convert items to numbers
    reverse(numbers)
    print(numbers)


def reverse(list):
    for i in range(len(list) // 2):
        list[i], list[len(list) - i - 1] = list[len(list) - i - 1], list[i] # 元素互换


    return list

10.12

在这里插入图片描述


import random

def main():
    # Read numbers as a string from the console
    s = input("Enter numbers: ")
    items = s.split()  # Extracts items from the string
    numbers = [eval(x) for x in items]  # Convert items to numbers

    print("The gcd of", numbers, "is", gcd(numbers))


def gcd(numbers):
    g = numbers[0]
    for i in range(1, len(numbers)):
        g = gcd2(g, numbers[i])

    return g


# Return the gcd of two integers
def gcd2(n1, n2):
    g = 1  # Initial gcd is 1
    k = 2  # Possible gcd

    while k <= n1 and k <= n2:
        if n1 % k == 0 and n2 % k == 0:
            g = k  # Update gcd
        k += 1

    return g  # Return gcd
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值