面试测试岗位常常要求手写的代码系列【一】

要求当场写一个算法,倒序一个字符串,并且字符串中到空格有3个的,2个的,1个的;

Step1: 常见的算法就是倒序排一下,不考虑空格的。比如下面的将 i am a boy 倒序成boy a am i,其实有准备就简单得很

public class test {

      public static void main(String[] args){

            String str2 = "I am a boy";
            String[] words = str2.trim().split(" ");
            for(int i = words.length-1;i>=0;i--){
                System.out.print(words[i]+" ");
            }
        }
    } 

Step2: 加大一丢丢难度,需要考虑空格的数量,比如 i ⭕️⭕️⭕️am ⭕️⭕️a⭕️boy,倒序成boy⭕️⭕️⭕️a⭕️⭕️am⭕️i

import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Sort {

    public static void main(String[] args) {


        String str = "i-am-a-boy";
        String[] words = str.trim().split("-");

        for (int i = words.length - 1; i >= 0; i--) {
            System.out.print(words[i] + "-");
        }
        System.out.print("!!!注意多了一个-,是空格往往看不出来" + "\n");


        String strb = "i-am-a-boy";
        String[] wordb = strb.trim().split("-");

        for (int i = wordb.length - 1; i >= 0; i--) {
            if (i == 0) {
                System.out.print(wordb[i]);
            } else
                System.out.print(wordb[i] + "-");
        }
        System.out.print("修复了一下,将最后一个不输出-了" + "\n");

        //下面是多个空格的处理方式
        String str2 = "i---am--a-boy";
        String[] words2 = str2.trim().split("-");//会有空字符串的出现
        System.out.println("----字符串数组的长度是-----:" + words2.length);

        for (int i = words2.length - 1; i >= 0; i--) {
            if (i == 0) {
                System.out.println(words2[i]);
            } else
                System.out.print(words2[i] + "-");
        }
        //思路是变成两个字符串数组,
        // 一个是有字符串(不为空)的列表list
        // 一个是记录---的数组;这边又考虑了两种方式,一种正则表达式截取字符串(how to),一种是数-的个数,怎么数,用上述数组中到空格
        // 然后拼起来
        //String newstr = str2.replace("-", " ");//使用库函数
        String newstr2 = str2.replaceAll("[a-zA-Z]", "*");//
        System.out.println(newstr2);
        String newstr3 = newstr2.replace("-", "R");//使用
        System.out.println(newstr3);
        //1
        List<String> strlist = Lists.newArrayList();
        for (int i = words2.length - 1; i >= 0; i--) {
            if (words2[i].length() != 0) {//
                strlist.add(words2[i]);
            }
        }
        System.out.println(strlist);


        String[] words3 = newstr3.trim().split("\\*");//会有空字符串的出现
        List<String> sinlist = Lists.newArrayList();
        for (int i = 0; i <=words3.length-1; i++) {
            if (words3[i].length() != 0) {//
                sinlist.add(words3[i]);
            }
        }
        System.out.println(sinlist);
        //Collections.replaceAll(sinlist, "a", "-");


        if(strlist.size()-1==sinlist.size())
        {for(int i=0;i<=sinlist.size()-1;i++)
        {System.out.print(strlist.get(i)+sinlist.get(i));}
        System.out.print(strlist.get(strlist.size()-1));
        }
        else System.out.println("那就尴尬了");

        String aaa="";
        if(strlist.size()-1==sinlist.size())
        {for(int i=0;i<=sinlist.size()-1;i++)
        {aaa=aaa+(strlist.get(i)+sinlist.get(i));}
            aaa=aaa+strlist.get(strlist.size()-1);
        }

        else System.out.println("那就尴尬了");

        aaa = aaa.replace("R", "-");
        System.out.println("\n"+aaa);
    }
}

  

现在的测试大多是自动化测试才能生存,去网上查看了一些api自动化测试框架,各式各样的;

一般对测试的要求是熟悉Java/Python等一种编程语言,

熟悉是怎样的要求? 要求会写(开发怂了的时候你上啊),还是能看懂(比如一些代码review)?

 

上述用了一个R是为了看了更清楚,下面更新了一版

import java.util.List;
import java.util.ArrayList;

public class TestMain {

    public static void main(String[] args) {

        //下面是多个空格的处理方式
        String str2 = "i---am--a-boy";
        String[] words2 = str2.trim().split("-");//会有空字符串的出现
        System.out.println("----字符串数组的长度是-----:" + words2.length);

        for (int i = words2.length - 1; i >= 0; i--) {
            if (i == 0) {
                System.out.println(words2[i]);
            } else
                System.out.print(words2[i] + "-");
        }
        
        
        //思路是变成两个字符串数组,
        // 一个是有字符串(不为空)的列表list
        // 一个是记录---的数组;这边又考虑了两种方式,一种正则表达式替换掉字母字符(how to),一种是数-的个数,然后拼起来
        
        
        String newstr2 = str2.replaceAll("[a-zA-Z]", "*");//将所有的字母字符替换成为*号
        System.out.println(newstr2);

        //1

        List<String> strlist = new ArrayList();
        for (int i = words2.length - 1; i >= 0; i--) {
            if (words2[i].length() != 0) {//排除空字符串
                strlist.add(words2[i]);
            }
        }
        System.out.println("第一种方式的结果:"+strlist);

        //2
        String[] words2a = newstr2.trim().split("\\*");//会有空字符串的出现
        List<String> sinlist = new ArrayList();
        for (int i = 0; i <=words2a.length-1; i++) {
            if (words2a[i].length() != 0) {//
                sinlist.add(words2a[i]);
            }
        }
        System.out.println("第二种方式的结果:"+sinlist);


        String final_str="";
        if(strlist.size()-1==sinlist.size())//连接符必须少一位啊
        {
            for(int i=0;i<=sinlist.size()-1;i++)
                {
                    final_str = final_str +(strlist.get(i)+sinlist.get(i));
                }
            final_str = final_str +strlist.get(strlist.size()-1);
        } else

            System.out.println("那就尴尬了");

        System.out.println("\n"+ final_str );
    }
}

  

import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.List;

public class Sort {

    public static void main(String[] args) {

        //下面是多个空格的处理方式
        String str2 = "i---am--a-boy";
        String[] words2 = str2.trim().split("-");//会有空字符串的出现
        System.out.println("----字符串数组的长度是-----:" + words2.length);

        for (int i = words2.length - 1; i >= 0; i--) {
            if (i == 0) {
                System.out.println(words2[i]);
            } else
                System.out.print(words2[i] + "-");
        }
        //思路是变成两个字符串数组,
        // 一个是有字符串(不为空)的列表list
        // 一个是记录---的数组;这边又考虑了两种方式,一种正则表达式替换掉字母字符(how to),一种是数-的个数,
        // 然后拼起来
        String newstr2 = str2.replaceAll("[a-zA-Z]", "*");//将所有的字母字符替换成为*号
        System.out.println(newstr2);

        //1
        List<String> strlist = Lists.newArrayList();
        for (int i = words2.length - 1; i >= 0; i--) {
            if (words2[i].length() != 0) {//排除空字符串
                strlist.add(words2[i]);
            }
        }
        System.out.println(strlist);

        //2
        String[] words2a = newstr2.trim().split("\\*");//会有空字符串的出现
        List<String> sinlist = Lists.newArrayList();
        for (int i = 0; i <=words2a.length-1; i++) {
            if (words2a[i].length() != 0) {//
                sinlist.add(words2a[i]);
            }
        }
        System.out.println(sinlist);


        String aaa="";
        if(strlist.size()-1==sinlist.size())//连接符必须少一位啊
        {for(int i=0;i<=sinlist.size()-1;i++)
        {aaa=aaa+(strlist.get(i)+sinlist.get(i));}
            aaa=aaa+strlist.get(strlist.size()-1);
        } else System.out.println("那就尴尬了");

        System.out.println("\n"+aaa);
    }
}

  

posted @ 2018-11-22 16:46  巴黎爱工作  阅读(1095)  评论(0编辑  收藏  举报