佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 3621|回复: 124

高手进来,看看一个interview的问题。做对有赏~

[复制链接]
发表于 22-10-2018 10:25 PM | 显示全部楼层 |阅读模式
本帖最后由 martinng 于 22-10-2018 10:33 PM 编辑

         public class Student
        {
            public string name;
            public char sex;
        }

        public static Student[] studentList = new Student[] {
            new Student() { name = "Martin", sex = 'M' },
            new Student() { name = "Jenny", sex = 'F' },
            new Student() { name = "Wendy", sex = 'F' },
            new Student() { name = "Siti", sex = 'F' },
            new Student() { name = "Thomas", sex = 'M' },
            new Student() { name = "Siva", sex = 'M' },
            new Student() { name = "Mun Hui", sex = 'F' },
            new Student() { name = "Richard", sex = 'M' },
            new Student() { name = "Kumar", sex = 'M' },
            new Student() { name = "Isah", sex = 'F' },
            new Student() { name = "Samson", sex = 'M' },
            new Student() { name = "JiaJia", sex = 'F' },
        };


Question: Write code to print studentList in format ({name} - {sex}), boys go first followed by girls. Sorting among boys / girls is not neccessary.

Rules:   
- Any languages even pseudocode can be used.
- Language specific features are NOT allowed, such as C/C like languages ternary operator: (x==y)?true:false.
- ONLY one for/while loop can be used, but cannot be nested.
- ONLY integer and boolean variables can be used.
- ONLY standard output functions such as printf/cout/echo/console.Write and others similar can be used.
- The studentList is NOT allowed to be extended or duplicated.


回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 22-10-2018 10:44 PM | 显示全部楼层
这是朋友去新加坡软件公司面试时的题目。他抄给我看,我以为很容易就随手写了大概5~6行(包括for loop)。看了答案后才发觉原来可以更少行!

if (xxx) print yyy <--- 算1行

if (xxx) {         
  print yyy               <----也算1行
}

if (xxx) {         
  print yyy               <----算2行
  print zzz
}

if (xxx) yyy, zzz  <--- 也算2行

回复

使用道具 举报

 楼主| 发表于 22-10-2018 10:50 PM | 显示全部楼层
目前有1个人做到一行for loop,一行if print(应该是最简化了):

  1. for (int i = 0; i < 2 * n; i++)
  2. {
  3.     if (studentList[i % n].sex == (i < n ? 'M' : 'F'))
  4.    {
  5.         Console.WriteLine(string.Format("{0} - {1}", studentList[i % n].name, studentList[i % n].sex));
  6.    }
  7. }
复制代码


但却犯规了,因为用到C 的ternary operator,所以DQ了。
回复

使用道具 举报

 楼主| 发表于 22-10-2018 10:53 PM | 显示全部楼层
另外一个也是写到很简:
  1. foreach(var student in studentList.OrderByDescending(x => x.sex)){ ... string.format("{0}-{1}",student.name,student.sex) }
复制代码


但也是DQ了。因为call到standard output以外的functions/methods/calls。
回复

使用道具 举报

 楼主| 发表于 22-10-2018 11:03 PM | 显示全部楼层
我看了官方答案,只有4行而已。而且是pseudo code的写法,变成任何language都是同样的写法。我以为没有更好了,却有一个中国programmer把这个官方答案化简到两行而已一行for loop + 一行if print!

在此玩个游戏,我悬赏RM50,如果有人可以满足所有条件而且只有一行for loop + 一行if print而已~
回复

使用道具 举报

发表于 23-10-2018 08:50 AM | 显示全部楼层
本帖最后由 yan13 于 23-10-2018 10:23 AM 编辑

不懂符合條件嗎
  1.             int n = studentList.Length;
  2.             int i = 0;
  3.             int t = System.Convert.ToInt32('M');
  4.             
  5.             do
  6.             {
  7.                 if (System.Convert.ToInt32(studentList[i % n].sex) == t)
  8.                 {
  9. Console.WriteLine(string.Format("{0} - {1}", studentList[i % n].name, studentList[i % n].sex));
  10.                 }
  11.                 if (i == n-1)
  12.                 {
  13.                     t = System.Convert.ToInt32('F');
  14.                 }
  15.                 i++;
  16.             } while (i < n*2);
复制代码


回复

使用道具 举报

Follow Us
发表于 23-10-2018 08:54 AM | 显示全部楼层
本帖最后由 yan13 于 23-10-2018 10:33 AM 编辑

想用2個string,1個收"M",1個收"F",after loop, concat both string then print.應該好過上面的,不過不能用string發現到和第一個的答案一樣思路,想刪除帖子但不會.就算了
回复

使用道具 举报

 楼主| 发表于 23-10-2018 11:47 AM | 显示全部楼层
本帖最后由 martinng 于 23-10-2018 11:48 AM 编辑

yan13, 是犯了一条规:

- ONLY standard output functions such as printf/cout/echo/console.Write and others similar can be used.

之前就是有人用Collection.sort(),然后print studentList,不懂是否真的可以run到,但因为违反只能用standard output function/method/call 而已,所以DQ了。

System.Convert.ToInt32()<--- 这个是standard output之外的method了。
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 23-10-2018 11:50 AM | 显示全部楼层
我觉得这家公司要出这种鬼问题,是因为要programmer写出portable to every system的代码,尽量减少依赖。
回复

使用道具 举报

 楼主| 发表于 23-10-2018 11:54 AM | 显示全部楼层
yan13 发表于 23-10-2018 08:54 AM
想用2個string,1個收"M",1個收"F",after loop, concat both string then print.應該好過上面的,不過不能用string發現到和第一個的答案一樣思路,想刪除帖子但不會.就算了

有人试过了,但DQ了,哈哈哈。因为额外String variable是不允许的。
回复

使用道具 举报

发表于 23-10-2018 02:03 PM | 显示全部楼层
martinng 发表于 23-10-2018 11:54 AM
有人试过了,但DQ了,哈哈哈。因为额外String variable是不允许的。

其實看到第一個回答的思路我就不太想寫了.覺得一樣,像是在抄他的.然後又不會刪貼,所以算了.不過既然都開始了,做完它吧
  1.             int n = studentList.Length;
  2.             int i = 0;
  3.             
  4.             do
  5.             {
  6.                 if (((studentList[i % n].sex) == 'M' && i < n) || ((studentList[i % n].sex) == 'F' && i > n))
  7.                 {
  8. Console.WriteLine(string.Format("{0} - {1}", studentList[i % n].name, studentList[i % n].sex));
  9.                 }
  10.                 i++;
  11.             } while (i < n*2);
复制代码


回复

使用道具 举报

 楼主| 发表于 23-10-2018 02:37 PM | 显示全部楼层
本帖最后由 martinng 于 23-10-2018 03:21 PM 编辑

n*2 和 mod 看来是必须的。

你这个code可以再简化到:

  1. for (int i = 0; i < studentList.Length * 2; i ++) {
  2. if (((studentList[i % studentList.Length].sex) == 'M' && i < studentList.Length) || ((studentList[i % studentList.Length].sex) == 'F' && i >= studentList.Length))
  3.                 {
  4. Console.WriteLine(string.Format("{0} - {1}", studentList[i % studentList.Length].name, studentList[i % studentList.Length].sex));
  5.                 }

  6. }
复制代码

回复

使用道具 举报

 楼主| 发表于 23-10-2018 03:24 PM | 显示全部楼层
其实我也觉得你是做到了一行loop+一行if print了。但还有更elegant的写法,试试看~
回复

使用道具 举报

发表于 23-10-2018 10:48 PM | 显示全部楼层
martinng 发表于 23-10-2018 03:24 PM
其实我也觉得你是做到了一行loop+一行if print了。但还有更elegant的写法,试试看~

算了吧.我一向不太嚮往以最短的code來完成同樣的東西.

回复

使用道具 举报

发表于 24-10-2018 04:05 AM | 显示全部楼层
martinng 发表于 23-10-2018 03:24 PM
其实我也觉得你是做到了一行loop+一行if print了。但还有更elegant的写法,试试看~

不知道这个PSEUDOCODE如何?我有用到ABS() 即:

ABS() returns the absolute value of a variable. The result of the function has the same type as its argument, which can be any numerical type.


看起来yan13已经是最好的了。


  1. FOR COUNT=-11 TO 11
  2.       IF (STUDENTLIST(ABS(COUNT)).SEX='M' AND COUNT<=0) OR (STUDENTLIST(ABS(COUNT)).SEX='F' AND COUNT=>0) THEN
  3.               PRINT STUDENTLIST(ABS(COUNT)).NAME & " - " & STUDENTLIST(ABS(COUNT)).SEX
  4. NEXT
复制代码


回复

使用道具 举报

 楼主| 发表于 24-10-2018 04:16 AM | 显示全部楼层
褐眼睛,good try。可惜ABS()是standard output call之外了。

yan13的code真的还可以再简化(IF statement)
回复

使用道具 举报


ADVERTISEMENT

发表于 24-10-2018 06:14 AM | 显示全部楼层
martinng 发表于 24-10-2018 04:16 AM
褐眼睛,good try。可惜ABS()是standard output call之外了。

yan13的code真的还可以再简化(IF statement)

尝试改良了yan13的部分,不过还是一样长。
  1. FOR COUNT=0 TO (STUDENTLIST.LENGTH X 2) - 1
  2.       IF  (STUDENTLIST[COUNT MOD 12].SEX='M' AND COUNT=COUNT MOD 12) OR (STUDENTLIST[COUNT MOD 12].SEX='F' AND COUNT>COUNT MOD 12) THEN
  3.              PRINT STUDENTLIST[COUNT MOD 12].NAME & " - " & STUDENTLIST[COUNT MOD 12].SEX
  4.       ENDIF
  5. NEXT   
复制代码


回复

使用道具 举报

发表于 24-10-2018 07:41 AM 来自手机 | 显示全部楼层
本帖最后由 aquamax 于 24-10-2018 07:45 AM 编辑
褐眼睛 发表于 24-10-2018 06:14 AM
尝试改良了yan13的部分,不过还是一样长。


把count > 12先测。。
if count < 12 and M
...Print student list[count mod 12]
Else F
...
回复

使用道具 举报

 楼主| 发表于 24-10-2018 01:57 PM | 显示全部楼层
yan13的code是用最少行,这个是肯定了。只是他的if (。。。)还可以再更简化。给点贴士:需要用到一点数学。。。
回复

使用道具 举报

发表于 24-10-2018 07:55 PM | 显示全部楼层
aquamax 发表于 24-10-2018 07:41 AM
把count > 12先测。。
if count < 12 and M
...Print student list[count mod 12]
Else F
...

楼主说有中国高手只需一行loop和一行if print就搞定了。

你列出的这个方法超过一行了。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 28-3-2024 07:14 PM , Processed in 0.070291 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表