• 面试题:打印课程


    时间:2022年10月份面试题
    在这里插入图片描述
    在这里插入图片描述
    亮点:
    java7:Path、Files
    java8:分组排序,拼接字符串
    java+:切割字符串,拼接字符串,比较日期的大小,如何生成标准化模板

    初步分析:

    这道题看起来很简单,实际上做起来并不简单。首先,你要考虑到分组这个问题。因为按照要求文件里的内容是
    周一 ~ 周五这几天的内容,所以可以先按照星期x进行分组。然后对每一天都的课程进行分组,最后打印。
    
    • 1
    • 2

    预期思考:如何解决某个时间段没课的情况,如何精确将没有课的节数打印出来?
    解决方案:模板填充法(大概的意思就是我按照要求创建一个动态且有规则的模板,后期在解析数据时只需要往模板里插入数据就行了,不满足条件的模板格子自然打印空白)

      上午的上课时间,可能是8:00,也可能是8:15等等,无所谓只要给我一个上午上课时间就行了。所以上课时间
      必须要确定,且这个时间由用户自定义指定。午休时间必须要确定,用于推断下午第一节课的上课时间。每节
      课实际上课的时间以及每两节课之间的休息时间都要明确,用于在创建上课时间模板时推断每节课与下一节课
      的时间点。
    
    • 1
    • 2
    • 3
    • 4

    /**操作步骤
    * (1) 第一步:调用打印课程的方法
    * (2) 第二步:校验此文件在磁盘中是否存在
    * (3)第三步:对数据进行分组,按照数字进行分组(例如:数字1代表星期一)
    * (4)第四步:对星期x进行排序
    * (5)第五步: 对每一天的课程时间进行排序
    * (6) 第六步:获取每天标准化上课时间模版
    * (7) 第七步:填充模版
    * (8) 第八步:打印课程
    * */

    源码

    package com.boot.test.算法.打印课程2;
    import org.junit.Test;
    import java.io.FileNotFoundException;
    import java.nio.file.Files;
    import java.nio.file.LinkOption;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.time.Duration;
    import java.time.LocalTime;
    import java.time.format.DateTimeFormatter;
    import java.util.*;
    import java.util.concurrent.atomic.AtomicLong;
    import java.util.stream.Collectors;
    
    public class PrintCourse {
        public void printCourse(String pathStr) throws FileNotFoundException {
            Path path = Paths.get(pathStr);
                boolean exists = Files.exists(path, LinkOption.NOFOLLOW_LINKS);
    //            第二步:校验此文件在磁盘中是否存在
                if(exists){
                    String[] arr=null;
                    try {
    //                    第三步:对数据进行分组,按照数字进行分组
                        arr = Files.lines(path).collect(Collectors.joining(";")).split(";");
                        Map> collect = Arrays.stream(arr).collect(Collectors.groupingBy(x -> {
                            String[] s = x.split("\t");  //空格
                            switch (s[1]){
                                case "星期一" : return "1";
                                case "星期二" : return "2";
                                case "星期三" : return "3";
                                case "星期四" : return "4";
                                case "星期五" : return "5";
                            }
                            return s[1];
                        }, LinkedHashMap::new,Collectors.toList()));
    //                    System.out.println("collect..."+collect);
                        //第四步:对星期x进行排序: 根据分组的key值对结果进行排序、放进另一个map中并输出(主要对Map根据数字1,2,3,4,5对星期x进行排序,使得每一天的课程有序)
                        Map result = collect.entrySet().stream()
                                .sorted(Map.Entry.comparingByKey())
                                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                                        (oldValue, newValue) -> oldValue, LinkedHashMap::new));
    
                        Collection> values = result.values();
    //                    System.out.println("values.."+values);
    //                    第五步: 对每一天的课程时间进行排序
                        for(List list:values){
                            list.sort((o1, o2) -> {
                                String[] s1 = o1.split("\t");
                                String[] s2 = o2.split("\t");
                                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("H:mm");
                                LocalTime time1 = LocalTime.parse(s1[2], formatter);
                                LocalTime time2 = LocalTime.parse(s2[2], formatter);
                                Duration duration = Duration.between(time1,  time2 );
    //                            对每一天的课程时间进行排序
                                if (duration.toMillis()>0){
                                    return -1;
                                }else if(duration.toMillis()==0){
                                    return 0;
                                }else{
                                    return 1;
                                }
                            });
                            int i=0;
                            Map map=new LinkedHashMap();
    //                        第六步:获取标准化时间模版
                            Map,String> generalMap = general("8:00", 90, 45, 15, 8);
                            String day=null;
    //                       第七步:填充模版: 往标准化模版里面填充课程:采用的原理是对Map集合进行put的时候,key相同时value会覆盖原来的旧值,不断的将模版中的内容更换,达到按照模版精确输出的目的
                            for(String str:list){
                                String[] split = str.split("\t");
                                if(i==0){
                                    day=split[1]+"\t";
                                }
                                generalMap.put(split[2],split[4]);
                                i++;
                            }
    //                        第八步:打印课程
                            System.out.print(day);
                            for(Map.Entry me:generalMap.entrySet()){
                                System.out.print("  "+me.getValue());
                            }
                            System.out.println();
                        }
    
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
    
                }else{
                    throw new FileNotFoundException("没有发现此文件...");
                }
    
        }
        /**
         * @param amStartTime: 上午的上课时间,可能是8:00,也可能是8:15等等,无所谓只要给我一个上午上课时间就行了
         *     lunchTime:午休时间,用于推断下午第一节课的上课时间
         *     courseTime:每节课实际上课的时间
         *      restTime: 每两节课之间的休息时间
         *     courseNumber:每天实际上课的节数(这是一个扩展字段暂时没有用到)
        * */
        public Map general(String amStartTime,long lunchTime,long courseTime,long restTime,int courseNumber){
            Map map=new LinkedHashMap();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("H:mm");
            LocalTime time = LocalTime.parse(amStartTime, formatter);
            for(int i=1;i<=courseNumber;i++){
                if(time.toString().startsWith("0")){
                    map.put(time.toString().substring(1),"   ");
                }else{
                    map.put(time.toString(),"   ");
                }
                time = time.plusMinutes(courseTime + restTime);
                if(i==4){
                    time = time.plusMinutes(lunchTime);
                }
            }
            return map;
        }
    //测试方法----------------------------------------------------------------------------------------------
        @Test
        public void test(){
            try {
    //            第一步:调用打印课程的方法
                System.out.println("                 "+"上午"+"         "+"下午");
                printCourse("D:\\ruanjian\\houtai\\ideas\\ideacode\\spring-quick\\src\\main\\java\\com\\boot\\test\\打印课程\\面试题输入文件.txt");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    
    //    测试标准化模版
        @Test
        public void test2(){
            general("8:00",90,45,15,8);
        }
    
    //    生成标准格式化模版
    //    map...{8:00=   , 9:00=   , 10:00=   , 11:00=   , 13:30=   , 14:30=   , 15:30=   , 16:30=   }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139

    附录:面试题输入文件.txt 的文件内容如下(选择拉取直接将内容复制后,在本地新建一个面试题输入文件.txt文件,然后将内容粘贴到文件里即可)
    张三 星期一 8:00 8:45 语文
    张三 星期二 10:00 10:45 生物
    张三 星期二 11:00 11:45 化学
    张三 星期五 11:00 11:45 化学
    张三 星期一 9:00 9:45 数学
    张三 星期一 10:00 10:45 英语
    张三 星期一 11:00 11:45 化学
    张三 星期三 13:30 14:15 语文
    张三 星期四 10:00 10:45 物理
    张三 星期四 11:00 11:45 化学
    张三 星期四 13:30 14:15 英语
    张三 星期一 13:30 14:15 物理
    张三 星期一 14:30 15:15 生物
    张三 星期一 15:30 16:15 体育
    张三 星期一 16:30 17:15 美术
    张三 星期三 8:00 8:45 物理
    张三 星期三 9:00 9:45 生物
    张三 星期三 10:00 10:45 英语
    张三 星期三 11:00 11:45 体育
    张三 星期四 14:30 15:15 生物
    张三 星期四 15:30 16:15 体育
    张三 星期三 14:30 15:15 数学
    张三 星期三 15:30 16:15 化学
    张三 星期二 9:00 9:45 体育
    张三 星期五 13:30 14:15 英语
    张三 星期五 14:30 15:15 生物
    张三 星期五 15:30 16:15 美术
    张三 星期五 16:30 17:15 体育
    张三 星期二 13:30 14:15 物理
    张三 星期二 14:30 15:15 英语
    张三 星期二 16:30 17:15 语文
    张三 星期四 8:00 8:45 生物
    张三 星期四 9:00 9:45 数学
    张三 星期四 16:30 17:15 数学
    张三 星期五 8:00 8:45 物理
    张三 星期五 9:00 9:45 数学
    张三 星期五 10:00 10:45 语文

  • 相关阅读:
    20220915使用python3下载ts格式的视频切片文件
    [数据结构]~二叉树
    2023年优化算法之之霸王龙优化算法(TROA),原理公式详解,附matlab代码
    LeetCode //C - 172. Factorial Trailing Zeroes
    ES6 Class(类) 总结(九)
    模拟实现memcpy memmove,字符类库函数的介绍,strerror,strtok的使用讲解。
    英语单词(二)
    Helm简易安装使用
    《猎杀:对决》是适合什么样的人玩 Mac电脑怎么玩《猎杀:对决》
    Learn Prompt-ChatGPT 精选案例:内容总结
  • 原文地址:https://blog.csdn.net/CNCDXX_88/article/details/127698671