这学期的 Java、Python 作业,因为没地方放就丢到这里了,持续更新。

不保证绝对的正确性,如要复制粘贴请谨慎操作。

第一次作业【0301】

  1. 编写程序,从键盘上输入一个浮点数,然后将该浮点数的整数部分输出。
import java.io.*;

public class test{
    public static void main(final String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        while(true){
            int num=Float.valueOf(br.readLine()).intValue();
            System.out.println(num);
        }
    }
}
  1. 写出下列表达式的值,设:x=3,y=17,yn=true.

(1) x+y*x– 54
(2) -x*y+y -34
(3) x<y && yn true
(4) x>y || !yn false
(5) y!=++x?x:y 4
(6) y++/–x 8

第二次作业【0308】

  1. 从键盘输入 n 个数,求这 n 个数中的最大数与最小数并输出。
import java.io.*;

public class test{
    public static void main(final String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        while(true){
            int n=Integer.valueOf(br.readLine());
            int[] num=new int[n];
            for (int i=0;i<n;i++){
                num[i]=Integer.valueOf(br.readLine());
            }
            int min=num[0],max=num[0];
            for (int i=1;i<n;i++){
                if (num[i]<min){
                    min=num[i];
                }
                if (num[i]>max){
                    max=num[i];
                }
            }
            System.out.println(max);
            System.out.println(min);
        }
    }
}
  1. 从键盘上输入一个字符串和一个字符,从该字符串中删除给定的字符。
import java.io.*;

public class test{
    public static void main(final String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        while(true){
            String str=br.readLine();
            String godie=br.readLine();
            str=str.replace(godie,"");
            System.out.println(str);
        }
    }
}

第三次作业【0315】

  1. 定义一个类时所使用的修饰符有哪几个?每个修饰符的作用是什么?是否可以混用?
public 将一个类声明为公共类,它可以被任何对象访问,一个程序的主类必须是公共类
abstract 将一个类声明为抽象类,没有实现的方法,需要子类提供方法的实现
final 将一个类声明为最终类即非继承类,表示它不能被其他类所继承
friendly 友元类型,默认的修饰符,只有在相同包中的对象才能使用这样的类
  1. 在方法调用中,使用对象作为参数进行传递时,是“传值”还是“传址”?对象作参数起到什么作用?

    当参数是基本数据类型时,则是传值方式调用,而当参数是引用类型的变量时,则是传址方式调用。

  2. 定义一个 student 类,包含如下内容:

成员变量:学号,姓名,性别,班干部否,数学,语文,外语

成员方法:输入,总分,平均分

编程实现这个类,并调用相应的方法输入数据,计算总分和平均分。

class Student{
    long id;
    String name,sex;
    boolean is_offical;
    int math,chinese,english;

    public void input(){
        Scanner sc = new Scanner(System.in);
        id=sc.nextLong();
        name=sc.next();
        sex=sc.next();
        is_offical=sc.nextBoolean();
        math=sc.nextInt();
        chinese=sc.nextInt();
        english=sc.nextInt();
    }

    public int sum(){
        return chinese+english+english;
    }
    public int average(){
        return sum()/3;
    }
}

第一次试验报告【0317】

第一题:教材 P15 页,例 2.1

public class App2_1{
    public static void main( String[ ] args) {
        System.out.println(Hello java!);
    }
}

第二题:教材 P17 页,例 2.2

import java.awt.*;
import javax.swing. JApplet;

public class App2_2 extends Japplet{
    public void paint( Graphics g){
        g.drawString( "Hello Java! " ,50,50);
    }
}

第三题:编写程序,从键盘输入圆柱体的底半径 r 和高 h,然后计算体积并输出。要用 Java 的类编程实现。

import java.util.Scanner;

public class Cylinder{
    private double radius=0,height=0;
    public Cylinder(double r,double h){
        radius=r;
        height=h;
    }
    public double volume(){
        return 3.14*radius*radius*height;
    }
    public static void main(final String[] args) {
        Scanner sc = new Scanner(System.in);
        while(true){
            double r=sc.nextDouble();
            double h=sc.nextDouble();
            System.out.println(new Cylinder(r,h).volume());
        }
    }
}

第四次作业【0322】

  1. 一个类的构造方法的作用是什么?若一个类没有声明构造方法,该程序能正确执行吗?为什么?

使用构造方法,创建一个新的对象,并可以给对象中的实例进行赋值。

可以执行。因为当没有指定构造方法时,系统会自动添加无参的构造方法。

  1. 静态变量与实例变量有哪些不同?
  • 静态变量在类中,不属于实例对象,属于类所有,只要程序加载了字节码,不用创建实例对象静态变量就会被分配空间,已经可以使用。
    实例变量是某个对象的属性,只有实例化对象后,才会被分配空间,才能使用。
  • 静态变量是所有对象共有的,某一个对象将它的值改变了,其他对象再去获取它的值,得到的是改变后的值;
    实例变量则是每一个对象私有的,某一个对象将它的值改变了,不影响其他对象取值的结果,其他对象仍会得到实例变量一开始就被赋予的值。
  1. 在一个静态方法内调用一个非静态成员为什么是非法的?

因为类的静态方法存在的时候,类的非静态成员可能不存在,自然无法访问一个内存中不存在的东西。

第五次作业【0329】

  1. 什么是多态机制?Java 语言中是如何实现多态的?

    1. “多态机制”是指一个程序中同名的多个不同方法共存的情况,即一个对外接口,多个内在的实现方法。

    2. Java 语言通过子类对父类方法的覆盖实现多态,也可以利用重载在同一个类中定义多个同名的不同方法来实现多态。

  2. this 和 super 分别有什么特殊的含义?

super() 与 this 的功能相似,但 super() 是从子类的构造方法调用父类的构造方法,而 this 则是在同一个类内调用其他的构造方法。当构造方法有重载时,super() 与 this() 均会根据所给出的参数类型与个数,正确的执行相对应的构造方法。

  1. 如何定义接口?接口与抽象类有哪些异同?
interface Test{
    public abstract void print();
}

接口只定义方法但没有实现,只有在实现接口的类中去实现,但是类中的所有方法都是实现的 (抽象类除外)。

接口可以多重继承而抽象类不行。

第二次试验报告【0331】

第一题:求 200 以内的素数,并输出。

public class test{
    public static void main(final String[] args) {
        for (int i = 2; i < 200 ; i ++) {
            boolean is_prime = true;
            for (int j = 2; j <= Math.sqrt(i); j ++) {
                if (i % j == 0) {
                    is_prime = false;
                    break;
                }
            }
            if (is_prime) {
                System.out.print(i + " ");
            }
        }
    }
}

第二题:编程统计从键盘输入的字符串中字母,数字和其他字符的个数。

import java.util.Scanner;

public class test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        while (true) {
            int letterNum = 0, numberNum = 0, otherNum = 0;

            String str = sc.nextLine();
            char[] ch = str.toCharArray();

            for (int i = 0; i < ch.length; i++) {
                if (Character.isLetter(ch[i])) {
                    letterNum ++;
                } else if (Character.isDigit(ch[i])) {
                    numberNum ++;
                } else {
                    otherNum++;
                }
            }
            System.out.println("letter: " + letterNum
                            + ", number: " + numberNum 
                            + ", other: " + otherNum);
        }
    }
}

第三题:编写程序,在圆柱体 Cylinder 中,用一个构造方法调用另一个构造方法。

import java.util.Scanner;

public class Cylinder{
    private double radius=0, height=0;
    public Cylinder(double r, double h){
        radius=r;
        height=h;
    }
    public Cylinder(double h ){
        this(1, h);
    }
    public double volume(){
        return 3.14*radius*radius*height;
    }
    public static void main(final String[] args) {
        Scanner sc = new Scanner(System.in);
        while(true){
            double r=sc.nextDouble();
            double h=sc.nextDouble();
            System.out.println(new Cylinder(r,h).volume());
        }
    }
}

第四题:书上 P115,例题 7.14

class Person
{
    private String name;
    private int age;
    public Person(String name, int age)
    {
        this.name = name;

        this.age = age;
    }
    public static int minAge(Person[ ] p)
    {
        int min = Integer.MAX_VALUE;
        for(int i = 0; i < p.length; i ++)
            if(p[i].age < min)
                min = p[i].age;
        return min;
    }
}
    
public class App7_14
{
    public static void main( String[ ] args)
    {
        Person[ ] per = new Person[3];
        per[0] = new Person("张三", 20);
        per[1] = new Person("李四", 21);
        per[2] = new Person("王二", 19) ;
        System.out.println("最小的年龄为:" + Person.minAge(per));
    }
}

第六次作业【0412】

  1. Throwable 类的两个直接子类 Error 和 Exception 的功能各是什么?用户可以捕获的异常是哪个类的异常?
  • Error 子类由系统保留,因为该类定义了那些应用程序通常无法捕捉到的异常。即 Error 类对象是由 Java 虚拟机生成并抛出给系统,这种错误有内存溢出错、栈溢出错、动态链接错等。通常 Java 程序不对这种错误进行直接处理,必须交由操作系统处理。
  • Exception 子类是供应用程序使用的,它是用户程序能够捕捉到的异常情况,通常产生它的子类来创建自己的异常。即 Exception 类对象是 Java 程序抛出和处理的对象,它有各种不同的子类分别对应于各种不同类型的异常。
  1. 在捕获异常时,为什么要在 catch() 括号内设有一个变量 e?

e 的作用是如果捕获到异常,则 Java 会利用异常类创建一个类变量 e,利用此变量便能进一步提取有关异常的信息。

  1. 若 try 语句结构中有多个 catch() 子句,这些子句的排列顺序与程序执行效果是否有关?为什么?

有关。由于异常对象与 catch 块的匹配是按照 catch 块的先后排列顺序进行的,所以在处理多异常时应注意认真设计各 catch 块的排列顺序。一般地,将处理较具体的较常见的异常的 catch 块应放在前面,而可以与多种异常相匹配的 catch 块应放在较后的位置。若将子类异常的 catch 语句块放在父类的后面,则编译不能通过。

  1. 产生 15 个 20~9999 之间的随机整数﹐然后利用 BufferedWriter 类将其写入文件 file2.txt 中;之后再读取该文件中的数据并将它们以升序排序。
import java.io.*;
import java.util.Arrays;
import java.util.Random;
public class test {
    public static void main(String[] args) throws IOException {
        write();
        read();
    }
    public static void  write() throws IOException {
        try{
            Random random = new Random();
            BufferedWriter writer = new BufferedWriter(new FileWriter("./file2.txt"));
            for(int i = 0; i < 15; i ++) {
                writer.write(Integer.toString(random.nextInt(9980) + 20));
                writer.newLine();
            }
            writer.flush();
            writer.close(); 
        }
        catch(IOException e) {
            System.out.println(e);
        }
    }
    public static void read() throws IOException {
        try{
            BufferedReader reader = new BufferedReader(new FileReader("./file2.txt"));
            int num[] = new int [15];
            for(int i = 0; i < 15; i ++) {
                num[i] = Integer.valueOf(reader.readLine());
            }
            Arrays.sort(num);
            reader.close();
            for(int i = 0; i < 15; i ++) {
                System.out.println(num[i]);
            }
        }
        catch(IOException e) {
            System.out.println(e);
        }
    }
}

第三次试验报告【0414】

第一题:封装:在网上,我们经常登录注册,在里面有一个用户 User,User 有用户名 username,password,年龄 age,性别 sex,请使用封装将用户封装为一个完整的个体,运行结果如下:

本用户的用户名为: admin,密码为: 123456,年龄: 23,性别为:男

提示:
(1)定义类,并对其进行封装,加入两个构造方法,添加加一个 info 的方法,返回 void,输出用户信息;
(2)再定义一个类 UserTest,测试输出。
注:注意修饰符,构造方法,注释等各个规范

public class test {
    public static void main(String[] args){
        new User("admin", "123456", 23, "男").info();
    }
}
class User{
    private String username, password, sex;
    private int age;
    User(String username, String password, int age, String sex){
        this.username = username;
        this.password = password;
        this.age = age;
        this.sex = sex;
    }
    public String getUsername(){
        return this.username;
    }
    public void setUserName(String username){
        this.username = username;
    }
    public String getPassword(){
        return this.password;
    }
    public String setPassword(String password){
        this.password = password;
    }
    public int getAge(){
        return this.age;
    }
    public String setAge(int age){
        this.age = age;
    }
    public String getSex(){
        return this.sex;
    }
    public String setSex(String sex){
        this.sex = sex;
    }
    public void info(){
        System.out.println("本用户的用户名为:" + this.username 
                         + ",密码为:" + this.password 
                         + ",年龄:" + this.age 
                         + ",性别为:" + this.sex);
    }
}

第二题:继承:每个人的名字都由名和字组成,中国人的名字是“名“+”字”,英国人的是”字” + “.”+”名”:
有一个类英国人 Englishman,里面有姓 fristName,名 lastName,有输出姓名的方法 display(),还有一个类中国人 Chinese,里面有姓 fristName,名 lastName,也有输出姓名的方法 display(),还有一个普通类人类 Person,有输出姓名的方法 display()
请使用面向对象 + 继承的思想完成结果图:

欧阳明日
MingRi.OuYnag

提示:
(1)定义一个父类 Person,抽取子类共有的属性和方法
(2)定义一个 Englishman 类,继承父类,并重写方法
(3)定义一个 Chinese 类,继承父类,并重写方法
(4)定义一个 PersonTest 类,用来做测试 display 输出姓名
注:注意修饰符,构造方法,使用多态,注释的各个规范

public class test {
    public static void main(String[] args){
        new Chinese("明日", "欧阳").display();
        new Englishman("MingRi", "OuYnag").display();
    }
}
class Person{
    String firstName,lastName;
    Person(String firstName, String lastName){
        this.firstName = firstName;
        this.lastName = lastName;
    }
}
class Englishman extends Person{
    Englishman(String firstName, String lastName){
        super(firstName,lastName);
    }
    void display(){
        System.out.println(this.firstName + "." + this.lastName);
    }
}
class Chinese extends Person{
    Chinese(String firstName, String lastName){
        super(firstName,lastName);
    }
    void display(){
        System.out.println(this.lastName + this.firstName);
    }
}

第三题:继承抽象类:定义一个抽象类机动车 Motovercal,里面有车牌号 no,类型 type,价格 price 属性,里面有一个show()方法是抽象方法,定义一个轿车 Car 类,他有特有的属性颜色 color,有一个公共汽车 Bus,他有特有属性座位数 seatCount,实现如图功能:

本车车牌号为: A00001,类型为: jeep,价格为: 200000,颜色为:绿色
本车车牌号为: B00002,类型为:金龙,价格为: 250000,拥有 30 个座位

提示:
(1)定义一个抽象类 Motovercal,里面有属性车牌号 no,类型 type,价格 price,里面有抽象方法 show()
(2)定义一个轿车 Car 类,继承 Motovercal,他有特有的属性颜色 color
(3)定义一个轿车 Bus 类,继承 Motovercal,他有特有的属性座位数 seatCount
(4)编写测试类 MotovercalTest
注:注意修饰符,构造方法,使用多态,注释的各个规范

public class test {
    public static void main(String[] args){
        new Car("A00001", "jeep", 200000, "绿色").show();
        new Bus("B00002", "金龙", 250000, 30).show();
    }
}
abstract class Motovercal{
    String No,type;
    int price;
    Motovercal(String No, String type, int price){
        this.No = No;
        this.type = type;
        this.price = price;
    }

    abstract void show();
}
class Car extends Motovercal{
    String color;
    Car(String No, String type, int price, String color){
        super(No, type, price);
        this.color = color;
    }
    void show(){
        System.out.println("本车车牌号为:" + this.No 
                         + ",类型为:" + this.type 
                         + ",价格为:" + this.price 
                         + ",颜色为:" + this.color);
    }
}
class Bus extends Motovercal{
    int seatCount;
    Bus(String No, String type, int price, int seatCount){
        super(No, type, price);
        this.seatCount = seatCount;
    }
    void show(){
        System.out.println("本车车牌号为:" + this.No 
                         + ",类型为:" + this.type 
                         + ",价格为:" + this.price 
                         + ",拥有" + this.seatCount + "个座位");
    }
}

第四题:继承类并实现接口:马继承 Animal,还能实现能飞的能力,简称飞马,运行结果如下:

小马嘟嘟,是一只会飞的飞马!

提示:
(1)定义一个抽象类 Animal,里面有 name 属性
(2)定一个 Flyable 的接口,表示飞的能力,里面有飞 fly() 的方法,返回 void
(3)定义一个 Horse 类,让他继承抽象类 Animal,并实现接口 Flyable
注:注意修饰符,构造方法,使用多态,注释的各个规范

public class test {
    public static void main(String[] args){
        new Horse("嘟嘟").fly();
    }
}
abstract class Animal{
    String name;
    Animal(String name){
        this.name = name;
    }
}
interface Flyable {
    public void fly();
}
class Horse extends Animal implements Flyable{
    String color;
    Horse(String name){
        super(name);
    }
    public void fly(){
        System.out.println("小马" + this.name + ",是一只会飞的飞马!");
    }
}

第七次作业【0419】

这次的作业我是从网上抄的

  1. 设计一个窗口,内含一个文本框、三个复选框、二个单选按钮、一个标签和一个按钮。各组件的位置、大小和其上的文字由用户自己设定。
import javax.swing.*;
import java.awt.*;

public class test {
    public static void main(String[] args) {
        JFrame window = new JFrame("Test");
        window.setLayout(new FlowLayout());

        JTextField textField = new JTextField(20);
        window.add(textField);

        JCheckBox checkBox1 = new JCheckBox("CheckBox1");
        JCheckBox checkBox2 = new JCheckBox("CheckBox2");
        JCheckBox checkBox3 = new JCheckBox("CheckBox3");
        window.add(checkBox1);
        window.add(checkBox2);
        window.add(checkBox3);

        JRadioButton radioButton1 = new JRadioButton("RadioButton1");
        JRadioButton radioButton2 = new JRadioButton("RadioButton2");
        ButtonGroup bg = new ButtonGroup();
        bg.add(radioButton1);
        bg.add(radioButton2);

        window.add(radioButton1);
        window.add(radioButton2);

        JLabel label = new JLabel("Label");
        window.add(label);

        JButton button = new JButton("Button");
        window.add(button);

        window.pack();
        window.setVisible(true);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

第八次作业【0426】

  1. 编写程序,用户输入一个三位以上的整数,输出其百位以上的数字。例如用户输入 1234,则程序输出 12(提示:使用整除运算)。
print(int(input()) // 100)

期中机试【0428】

第一题:闰年的判断

(1)提示用户通过键盘输入年份判断是否为闰年,是输出 Y,不是输出 N。当用户输入年份小于 0 时退出程序。
(2)注意:必要的测试用例应包括:2000,2001,2004

import java.util.Scanner;

class Year{
    public static void main (String[] args){
        Scanner sc = new Scanner(System.in);
        while(true){
            int year = sc.nextInt();
            if (year < 0){
                break;
            }
            else if (year % 400 == 0 ||(year % 100 != 0 && year % 4 == 0)){
                System.out.println("Y");
            }
            else{
                System.out.println("N");
            }
        }
    }
}

第二题:类的单继承和应用

(1)设计一个类 MagicPotion,继承 Item,重写 effect 方法,能够输出”蓝瓶使用后,可以会魔法“。
(2)编写测试类,对 MagicPotion 的方法进行测试,测试的主要代码为:

Item item1 = new MagicPotion();
item1.effect();

(3)类 Item 的声明如下:

class Item{
    String name;
    int price;
    public void buy(){System.out.print("购买");}
    public void effect(){System.out.print("物品使用后,可以有效果");}}
class Item{
    String name;
    int price;
    public void buy(){
        System.out.print("购买");
    }
    public void effect(){
        System.out.print("物品使用后,可以有效果");
    }
}
class MagicPotion extends Item{
    public void effect(){
        System.out.println("蓝瓶使用后,可以回魔法");
    }
}
class Test{
    public static void main (String[] args){
        Item item1 = new MagicPotion();
        item1.effect();
    }
}

第三题:类的多接口与应用

(1)有一个基类 Animal,内部包含 Sleep 和 Eat 两个抽象方法,用来打印一个动物的睡觉习惯,以及喜欢吃的食物。
(2)有一个接口 Hobby,内部包含 Voice 方法,用来打印叫声。
(3)请实现一个猫的类 Cat,继承 Animal 类并实现 Hobby 接口
(3)请实现一个狗的类 Cat,继承 Animal 类并实现 Hobby 接口
(4)对应的测试代码为:

public static void main (String[] args){
    Dog d1 = new Dog();
    d1.Eat();
    d1.Sleep();
    d1.Voice();
    Cat c1 = new Cat();
    c1.Eat();
    c1.Sleep();
    c1.Voice();
}

测试结果为:

小狗吃骨头
小狗晚上睡觉
小狗汪汪叫
小猫吃鱼
小猫白天睡觉
小猫喵喵叫
abstract class Animal{
    public abstract void Sleep();
    public abstract void Eat();
}
interface Hobby {
    public abstract void Voice();
}
class Cat extends Animal implements Hobby{
    public void Sleep(){
        System.out.println("小猫白天睡觉");
    }
    public void Eat(){
        System.out.println("小猫吃鱼");
    }
    public void Voice(){
        System.out.println("小猫喵喵叫");
    }
}
class Dog extends Animal implements Hobby{
    public void Sleep(){
        System.out.println("小狗晚上睡觉");
    }
    public void Eat(){
        System.out.println("小狗吃骨头");
    }
    public void Voice(){
        System.out.println("小狗汪汪叫");
    }
}
class Test{
    public static void main (String[] args){
        Dog d1 = new Dog();
        d1.Eat();
        d1.Sleep();
        d1.Voice();
        Cat c1 = new Cat();
        c1.Eat();
        c1.Sleep();
        c1.Voice();
    }
}

第九次作业【0510】

  1. 编写程序,生成包含 1000 个 0~100 之间的随机整数,并统计每个元素的出现次数。
import random

d = {}

for i in random.choices(range(100), k=1000):
    d[i] = d.get(i, 0) + 1

for i in d:
    print(f"{i}: {d[i]}")
  1. 编写程序,用户输入一个列表和 2 个整数作为下标,然后输出列表中介于 2 个下标之间的元素组成的子列表。例如用户输入 [1, 2, 3, 4, 5, 6]2, 5 ,程序输出 [3, 4, 5, 6]
l = eval(input())
s, e = eval(input())
print(l[s:e + 1])
  1. 编写程序,生成包含 20 个随机数的列表,然后将前 10 个元素升序排列,后 10 个元素降序排列,并输出结果。
import random

l = random.sample(range(100), 20)

lh = l[:10]
lh.sort()

ll = l[-10:]
ll.sort(reverse=True)

l = lh + ll

print(l)

第四次实验报告【0512】

第一题:编写程序,生成包含 20 个各不相同的随机数的列表,然后将前 10 个元素按降序排列,后 10 个元素按升序排列,输出结果。

import random

l = random.sample(range(100), 20)

lh = l[:10]
lh.sort(reverse=True)

ll = l[-10:]
ll.sort()

l = lh + ll

print(l)

第二题:编写程序,至少用两种方法计算 100 以内所有偶数的和。

sum = 0
for i in range(100):
    if not i & 1:
        sum += i

# sum = sum(range(0, 100, 2))

print(sum)

第三题:用 pip 安装一个第三方库。

pip install nonebot-plugin-manager

第?次作业【0517】

上周的作业,老师自己都不知道是第几次。

  1. 编写程序,用户从键盘输入小于 1000 的整数,对其进行因式分解。例如,10 = 2 * 5,60 = 2 * 2 * 3 * 5。
def factor(num: int):
    result = []
    for i in range(2, int(num ** 0.5 + 1)):
        if num % i == 0:
            result.append(str(i))
            result.extend(factor(int(num / i)))
            break
    if not result:
        result.append(str(num))
    return result

num = int(input())
print(f"{num} = {' * '.join(factor(num))}")
  1. 编写程序,输出所有由 1、2、3、4 这四个数字组成的素数,并且在每个素数中每个数字只使用一次
import itertools

def is_prime(num: int):
    is_prime = False
    for i in range(2, int(num ** 0.5 + 1)):
        if num % i == 0:
            is_prime = True
            break
    return is_prime

for a, b, c, d in itertools.permutations(range(1, 5)):
    num = a * 1000 + b * 100 + c * 10 + d
    if not is_prime(num):
        print(num)

第十次作业【0524】

  1. 假设有一段英文,其中有单词中间的字母 i 误写为 I,请编写程序进行纠正。
s = input()
s = s.split(" ")
s = [w.replace("I","i") if "I" in w and w not in ["I", "I'm"] else w for w in s]
print(" ".join(s))
  1. 有一段英文文本,其中有单词连续重复了 2 次,编写程序检查重复的单词并只保留一个。例如,文本内容为“This is is a desk.”,程序输出为”This is a desk.”。
import itertools

s = input()
s = s.split(" ")
s = [w for w, g in itertools.groupby(s)]
print(" ".join(s))
  1. 编写程序,用户输入一段英文,然后输出这段英文中所有长度为 3 个字母的单词。
s = input()
s = s.split(" ")
for w in s:
    if len(w) == 3:
        print(w)

第五次实验报告【0526】

第一题:编写程序,用户输入一段英文,然后输出这段英文中长度小于 4 个字母的所有单词。

s = input()
s = s.split(" ")
for w in s:
    if len(w) < 4:
        print(w)

第二题:用户输入若干个分数,求所有分数的平均分。每输入一个分数后询问是否继续输入下一个分数,回答“yes”就继续输入下一个分数,回答“no”就停止输入分数。

is_ask = False
point = []

while True:
    point.append(int(input()))
    is_ask = True
    if is_ask:
        if input("is continue? ")[0] in ["n", "N"]:
            break
        
print(int(sum(point) / len(point)))

第十一次作业【0531】

这次作业我都懒得调试了。累了,毁灭吧,赶紧的。

  1. 编写函数,接收一个字符串,分别统计大写字母、小写字母、数字、其他字符的个数,并以元组的形式返回结果。
def count(s: str):
    a, b, c, d = 0, 0, 0, 0
    for i in s:
        if i >= 65 and i <=90:
            a = a + 1
        elif i >=97 and i<=122:
            b = b + 1
        elif i >=48 and i<=57:
            c = c + 1
        else:
            d = d + 1
    return a, b, c, d
  1. 编写函数,可以接受任意多个整数并输出其中的最大值和所有整数之和。
def sum(*args):
    return max(args),sum(args)